Advertisement

Reflection Using cube mapping does not work. What am I doing wrong?

Started by January 23, 2019 03:24 PM
1 comment, last by babaliaris 5 years, 7 months ago

Hello! I'm trying to understand why my reflection code does not work and I can't think why is this happening. What I want from you is to give me hints of what might go wrong in order to check it and give you more feedback and maybe we find the problem. This is the first time I'm doing reflection.

 

These are the steps and order of rendering my scene:

1) First I'm creating a Frame buffer with an RGB texture as it's color buffer and a Renderer Buffer as it's depth and stencil buffer with 24_8 precision accordingly (Even though I don't use the stencil).

2) I'm rendering the skybox cube map into the custom Frame Buffer with the depth test disabled. After that I'm enabling depth test again.

3) I'm rendering the cube into the custom Frame Buffer and sampling from the cube map using the reflection from the viewer and the cube's fragment.

4) I'm rendering a quad into the default frame buffer by sampling the color buffer texture from the custom frame buffer.

Ps: I have blend enable too, but I tried to disabled it and got the same results.

Below you can see the results:

 

So what should I check?

Below you can see the vertex and fragment shaders for the cube (I'm doing calculations in World space):

Vertex Shader:


#version 330 core

layout(location = 0) in vec3 aPos;
layout(location = 1) in vec3 aNormal;

uniform mat4 model;
uniform mat4 view;
uniform mat4 proj;

out vec3 normal;
out vec3 fragPos;

void main()
{
	gl_Position = proj * view * model * vec4(aPos, 1.0f);
	
	normal   = mat3(transpose(inverse(model))) * aNormal;

	fragPos = vec3(model * vec4(aPos, 1.0f));
}

Fragment Shader:


#version 330 core

out vec4 aPixelColor;

in vec3 normal;
out vec3 fragPos;

uniform samplerCube cube_map;

uniform vec3 camPos;

void main()
{
	vec3 view = normalize(fragPos - camPos);
	vec3 refl = reflect(view, normalize(normal));

	aPixelColor = vec4(texture(cube_map, refl).rgb, 1.0f);
}

Cube Vertices:


		float l_vertices[] = {
			
          	//Positions           Normals.
			-0.5f, -0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
			 0.5f, -0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
			 0.5f,  0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
			 0.5f,  0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
			-0.5f,  0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
			-0.5f, -0.5f, -0.5f,  0.0f,  0.0f, -1.0f,

			-0.5f, -0.5f,  0.5f,  0.0f,  0.0f, 1.0f,
			 0.5f, -0.5f,  0.5f,  0.0f,  0.0f, 1.0f,
			 0.5f,  0.5f,  0.5f,  0.0f,  0.0f, 1.0f,
			 0.5f,  0.5f,  0.5f,  0.0f,  0.0f, 1.0f,
			-0.5f,  0.5f,  0.5f,  0.0f,  0.0f, 1.0f,
			-0.5f, -0.5f,  0.5f,  0.0f,  0.0f, 1.0f,

			-0.5f,  0.5f,  0.5f, -1.0f,  0.0f,  0.0f,
			-0.5f,  0.5f, -0.5f, -1.0f,  0.0f,  0.0f,
			-0.5f, -0.5f, -0.5f, -1.0f,  0.0f,  0.0f,
			-0.5f, -0.5f, -0.5f, -1.0f,  0.0f,  0.0f,
			-0.5f, -0.5f,  0.5f, -1.0f,  0.0f,  0.0f,
			-0.5f,  0.5f,  0.5f, -1.0f,  0.0f,  0.0f,

			 0.5f,  0.5f,  0.5f,  1.0f,  0.0f,  0.0f,
			 0.5f,  0.5f, -0.5f,  1.0f,  0.0f,  0.0f,
			 0.5f, -0.5f, -0.5f,  1.0f,  0.0f,  0.0f,
			 0.5f, -0.5f, -0.5f,  1.0f,  0.0f,  0.0f,
			 0.5f, -0.5f,  0.5f,  1.0f,  0.0f,  0.0f,
			 0.5f,  0.5f,  0.5f,  1.0f,  0.0f,  0.0f,

			-0.5f, -0.5f, -0.5f,  0.0f, -1.0f,  0.0f,
			 0.5f, -0.5f, -0.5f,  0.0f, -1.0f,  0.0f,
			 0.5f, -0.5f,  0.5f,  0.0f, -1.0f,  0.0f,
			 0.5f, -0.5f,  0.5f,  0.0f, -1.0f,  0.0f,
			-0.5f, -0.5f,  0.5f,  0.0f, -1.0f,  0.0f,
			-0.5f, -0.5f, -0.5f,  0.0f, -1.0f,  0.0f,

			-0.5f,  0.5f, -0.5f,  0.0f,  1.0f,  0.0f,
			 0.5f,  0.5f, -0.5f,  0.0f,  1.0f,  0.0f,
			 0.5f,  0.5f,  0.5f,  0.0f,  1.0f,  0.0f,
			 0.5f,  0.5f,  0.5f,  0.0f,  1.0f,  0.0f,
			-0.5f,  0.5f,  0.5f,  0.0f,  1.0f,  0.0f,
			-0.5f,  0.5f, -0.5f,  0.0f,  1.0f,  0.0f
	};

 


void life()
{
  while (!succeed())
    try_again();

  die_happily();
}

 

Problem solved.

That was embarrassing...

Because I copy-paste the vertex shader code to make the fragment''s shader code I forgot to change the fragPos from out to in ?

 


#version 330 core

out vec4 aPixelColor;

in vec3 normal;
out vec3 fragPos;

uniform samplerCube cube_map;

uniform vec3 camPos;

void main()
{
	vec3 view = normalize(fragPos - camPos);
	vec3 refl = reflect(view, normalize(normal));

	aPixelColor = vec4(texture(cube_map, refl).rgb, 1.0f);
}

 

I was literally searching 4 hours to find that...


void life()
{
  while (!succeed())
    try_again();

  die_happily();
}

 

This topic is closed to new replies.

Advertisement