PDA

View Full Version : Very odd problem with Reflection vector



TheFearlessHobbit
06-01-2017, 06:06 PM
Hey guys, I am experiencing a bizarre problem with my reflection and I hope someone here could tell me why this is happening. I have a cubemap and I am applying a reflection to several objects in my scene. I understand that shader programs are small programs that operates on the GPU, therefore it's worth noting that I am using a Macbook pro that uses AMD graphics. The reflection works beautifully on my Macbook. However, the reflection does not work at all on my sister's computer which uses NVIDIA graphics. I am not sure why this is happening, it could not be a problem with graphics cards, it could simply be a programming issue but it's also highly unlikely as the code itself works fine on my machine. Nevertheless, I will post some code below for you to see, maybe there is something I should enable to ensure that reflection would be applied everywhere on all machines?

Vertex Shader (.vs file)


#version 430 core

// Input
layout (location = 0) in vec3 Vertex_Position;
layout (location = 1) in vec2 Vertex_Texture;
layout (location = 2) in vec4 Vertex_Colour;
layout (location = 3) in vec3 Vertex_Normal;

// Output
out vec4 outputColors;
out vec2 TexCoord;
out vec3 normals;
out vec4 worldPosition;

// Uniforms
uniform mat4 TransformationMatrix;

void main()
{
worldPosition = TransformationMatrix * vec4(Vertex_Position, 1.0f);
gl_Position = TransformationMatrix * vec4(Vertex_Position, 1.0f);

TexCoord = Vertex_Texture;
outputColors = Vertex_Colour;
normals = (TransformationMatrix * vec4(Vertex_Normal, 0.0f)).xyz;
}

Fragment Shader (.fs file)


#version 430 core

// Inputs
in vec4 outputColors;
in vec2 TexCoord;
in vec3 normals;
in vec4 worldPosition;

// Output Color
out vec4 VertexColors;

// Uniforms
uniform sampler2D Texture1;
uniform samplerCube enviroMap;
uniform vec3 SpotlightPlayerPos;
uniform vec3 cameraPos;

// Variables
float ambient = 1.0f;

void main()
{
// lambertian lighting (Phong - Per Pixel Shading)
float brightness = max(dot(-vec3(0.0f, 0.0f, 1.0f), normals), 0.0) + ambient;
VertexColors = texture2D(Texture1, TexCoord) * clamp(dot(-vec3(0.0f, 0.0f, 1.0f), normals), 0.0, 1.0) * brightness;

// Reflection
vec3 viewVector = normalize(worldPosition.xyz - cameraPos);
vec3 ReflectionVector = reflect(viewVector, normalize(normals));

vec4 reflectedColor = texture(enviroMap, ReflectionVector);
VertexColors = mix(VertexColors, reflectedColor, 0.6);
}


During render call, just sending the texture ID of skybox to the GPU:


int enviroMapLoc = glGetUniformLocation(MainShader.GetProgram(), "enviroMap");
glUniform1i(enviroMapLoc, 0);


Again, the above code operates flawlessly on my Macbook but on my sister's computer, the objects appear invisible. But! If I bump up the mix value of the reflection from 0.6f to 1.0f, then I could see the reflection of the skybox on my objects, but not the skybox itself anymore. What kind of sorcery is this? :confused:

Thank you!

GClements
06-01-2017, 07:27 PM
Fragment Shader (.fs file)


#version 430 core
...
VertexColors = texture2D(Texture1, TexCoord) * clamp(dot(-vec3(0.0f, 0.0f, 1.0f), normals), 0.0, 1.0) * brightness;


Type-specific texture lookup functions such as texture2D() are deprecated; they shouldn't work in 4.3 core profile. Use texture() instead.

This suggests that you aren't checking for compilation or linking errors. Use glGetShaderiv() and glGetProgramiv() to query the status and the length of the info log; use glGetShaderInfoLog() and glGetProgramInfoLog() to obtain the log data, then display it.



But! If I bump up the mix value of the reflection from 0.6f to 1.0f, then I could see the reflection of the skybox on my objects, but not the skybox itself anymore. What kind of sorcery is this? :confused:

I suspect that the GLSL compiler optimises mix(VertexColors, reflectedColor, 1.0) to just reflectedColor, meaning that the value of VertexColors is unused. In turn, the code which assigns to VertexColors will be eliminated, removing the erroneous texture2D() call and allowing the shader to compile without error.

TheFearlessHobbit
06-01-2017, 07:48 PM
I have modified my texture2D() function with texture() and it now works on the other computer! Thank you very much! I had no idea that texture2D was deprecated, maybe because there are tons of deprecated sources out there :p

Anyway, thank you once again for your help.