Jump to content
  • Advertisement
Sign in to follow this  
Toji

OpenGL OpenGL Matrix vs. Custom Matrix - different results

This topic is 3790 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So here's an odd one, and I'm surprised that I've never run into this before but in retrospect I guess I never used anything but your good old glRotate/Translate/Scale. I've got a simple OpenGL app that I'm using to test some render abstraction code, and at the moment all I'm rendering is a single small triangle (about 2 units high and 2 across). Well, in the last few days I've started replacing some of the "built in" variables with custom shader constants, the modelview matrix being one of them. I immediately noticed something weird: If I translated the modelview matrix by -3 and rendered using the built in OpenGL matrix the whole triangle fits nicely on the screen. However, doing the same translation and then passing the matrix as a constant (along with the appropriate math in the shader) gives me a much larger on-screen triangle! I have to translate back to about -10 to get the same results! On top of that, rotations also start acting strange, as if it's rotating around a different axis. It's all ALMOST right, but not quite. So, my question is: what in the world is OpenGL doing behind my back? Some pertinent code:
//C++
//Matrix setup
//This is the same code for both cases

//Set up the transform
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, zoom);
glRotatef(yRot, 1.0f, 0.0f, 0.0f);
glRotatef(xRot, 0.0f, 1.0f, 0.0f);

//Pull the transform for use as a shader constant
float myMat[16];
glGetFloatv(GL_MODELVIEW_MATRIX, myMat);

//Set as a shader constant (yes, the location is already good)
glUniformMatrix4fv(matLocation, 1, false, myMat);

//GLSL
//Vertex Shader
attribute vec3 Position;
uniform mat4 ModelView; // This is the matrix being set above

void main() {
        //This version works great
	//mat4 modelViewProj = (gl_ModelViewProjectionMatrix);

        //This version gives me the strange results
	mat4 modelViewProj = (ModelView * gl_ProjectionMatrix);

	gl_Position = modelViewProj * vec4(Position, 1.0);
}

Any clues?

Share this post


Link to post
Share on other sites
Advertisement
Okay, so I'm being stupid. Figured it out by doing a couple of things:

First, I tried using their modelview matrix separately in the shader:


mat4 modelViewProj = (gl_ModelViewMatrix * gl_ProjectionMatrix);



That gave me no rendering whatsoever, but flipping the operation gave me the right result (identical to gl_ModelViewProjectionMatrix):


mat4 modelViewProj = (gl_ProjectionMatrix * gl_ModelViewMatrix);



Okay, that made sense, I just forgot the order. So I tried it on my custom matrix to.


mat4 modelViewProj = (gl_ProjectionMatrix * ModelView);



And that works fine as long as the matrix is transposed correctly! (Which was a typo in my original post). Just thought you'd all like to know!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!