martinez314
martinez314

Reputation: 12332

How to get untransformed position in vertex shader?

I am applying a transform to my model using:

glMatrixMode(GL_MODELVIEW);
glMultTransposeMatrixd(transform, 0);

Here is my vertex shader:

#version 110

varying vec4 pos;
varying vec3 N;
varying vec4 vertColor;

void main(void)  
{    
    gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;
    pos = gl_Vertex;
    N = normalize(gl_NormalMatrix * gl_Normal);
    vertColor = gl_Color;
}

How can I get the position without the model matrix applied to it? That is, as if I did not apply glMultTransposeMatrixd(transform, 0) in the first place.

I've tried gl_Vertex * gl_ModelViewMatrixInverse -- I'm not sure if this is a valid thing to do -- but it did not give anything like the right result.

Upvotes: 0

Views: 434

Answers (2)

fintelia
fintelia

Reputation: 1211

You can't undo a matrix multiplication without the inverse of that matrix. Since your transformation matrix has been combined with the camera matrix, gl_ModelViewMatrixInverse is not the inverse of transform. As others have mentioned, the best way to get the untransformed position is simply to not do the transformation at all.

Just pass transform to the shader as a uniform, and then apply it as needed. In fact, modern OpenGL does away with the built in matrices all together and requires you to pass your own matrices exclusively using uniforms.

Also, you might consider consolidating the camera and projection transforms into one matrix and using a second for model transforms. Then, applying just the model transform matrix gets you the world position and applying both gets you the screen position.

Upvotes: 2

derhass
derhass

Reputation: 45322

If you want the coordinates before/without applying a transformation to it, just don't apply a transformation to it. The untransformed vertex data is just gl_Vertex in your case.

Upvotes: 3

Related Questions