jProg2015
jProg2015

Reputation: 1128

OpenGL Projection/Matrix confusion

I'm trying to project the normal of a point and colour it based on if it is (from the perspective of the camera/viewing plane) leaning left of vertical, or right of vertical.

To do this I'm taking my point normal and multiplying it by the gl_ModelViewProjectionMatrix, and checking if it has a positive or negative x value. However all my points go red (indicating left of vertical, which definitely isnt the case). If I change ModelViewProjectionMatrix to gl_ProjectionMatrix I get red and green points, however they obviously aren't coloured from the camera's perspective without the ModelViewMatrix.

Am I misunderstanding something here?

Shader example:

void main(){
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

    vec4 temp = vec4(vNormal.x, vNormal.y, vNormal.z, 1);
    vMajorTransformed = gl_ModelViewProjectionMatrix * temp;

    if (vMajorTransformed.x < 0) {
        gl_FrontColor = vec4(1, 0, 0, 1);
    } else {
        gl_FrontColor = vec4(0, 1, 0, 1);
    }

    gl_PointSize = 3;
}

Upvotes: 1

Views: 135

Answers (1)

A normal is a direction, not a point. This means its w component should be 0. Otherwise, you can end up applying a translation to it, which will definitely mess up the x, y, z coordinates (I guess that's happening in your case).

Also note that if your modelview matrix contains anything other than a rotation and a translation, normals will get messed up again. They have to be transformed by the inverse transpose instead. Inverse and transpose cancel out on a rotation-only matrix, but for other transformations, you have to use them.

And you probably don't want to apply projection to them - the test you're after (x coordinate) can be evaluated in camera coordinates (which is generally easier to reason about than projected space).

Upvotes: 3

Related Questions