1

I'm pretty new to GLSL and am attempting to get the two cubes I'm loading in to have different coloration based upon the normal of each of their faces. Instead, the entirety of both cubes is exactly the same color. As I rotate the camera, the coloration changes from blue to green and red, but all sides of both cubes always simultaneously remain the same color. I want something more along the lines of the top of the cubes being blue, one side being green, one side being red, etc. I don't particularly care which side is which color, just so long as the different sides aren't all the same color.

Vertex Shader

uniform mat4 gl_ModelViewMatrix; 
varying vec3 viewVert;
varying vec3 normal;

void main()
{
    viewVert = gl_ModelViewMatrix * gl_Vertex;
    normal = gl_NormalMatrix * gl_Normal;

    gl_Position = ftransform();
}

Fragment Shader

uniform mat4 gl_ModelViewMatrix;
varying vec3 viewVert;
varying vec3 normal;

void main()
{
    vec3 nor = normalize(normal);
    gl_FragColor = vec4(nor.x,nor.y,nor.z, 1.0);
}

I assume I'm doing something wrong with transforming between spaces and causing all the normals to be the same, but I'm not sure what. Perhaps I'm accidentally using the camera's normal instead of the normal of the faces?

8
  • If your normal matrix multiplies points from model space through to view space then the result you are getting is expected. The normal of the cube face that is facing directly towards you will likely always be (0,0,1) which would produce a blue color. If that's what you see it is likely you have normals in camera space. Feb 18, 2015 at 2:57
  • It is blue whenever I start the scene, so I suspect you are correct. How do I go about fixing that though? I don't want the normal of the cube itself if that's a thing. I want the normals of the individual sides/vertices. Feb 18, 2015 at 3:01
  • To clarify, it is blue whenever I start the scene, but not just for the sides facing me. All visible sides, not just those that are aimed toward the camera are blue. As I move/rotate the camera around, the color changes to green or red, including on the faces that were originally facing the camera and were at the time blue. Feb 18, 2015 at 3:09
  • 1
    It sounds like there is more incorrect than just expecting your normals to be in a different space. If all normals on every face are appearing the same color then I can't really guess on what is wrong. gl_NormalMatrix takes a normal from model to view space. So if that is correct then I would inspect the actual normals you are passing. Feb 18, 2015 at 3:44
  • 1
    Also I would recommend not using gl_ModelViewMatrix, gl_NormalMatrix, gl_Vertex, and gl_Normal. This tutorial can help you understand how to pass your vertex data to the shader. Link Feb 18, 2015 at 3:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.