Hi, I'm writing a small app at the moment to demo some shader techniques, I was originally using the matrix stack and using the built-in uniforms in the shader code, however I want to switch to sending the model and view matrices separately to the shader. I'm using the glut teapot as one of the models, and looking at the glut source code, the following transformations are applied to the matrix stack before drawing:
So the matrix stack has the following matrices multiplied together:
camera transform
rotate around y-axis
translate along z-axis
glutteapot base transforms (as above)
How can I replicate this without using the stack? I have a matrix/vector library (SVL) that can generate rotation/translation/scale matrices.
I've just finished a rewrite of my engine to address this. I created my own Matrix code, you'll find it very helpful to learn this because its one of the most important parts of any 3D app. Another option is to read the OpenGL Super Bible and look at his code to see how it can be done. I personally don’t use a stack system but prefer the scene hierarchy itself to store Matrix data. My code needs optimizing still :-)
Hi, maybe I should clarify a bit, I know how to generate and multiply transformation matrices together, the problem I am having is that I want to use matrices computed in the app to position an object in exactly the same way as I have done using glScale/glRotate/glTranslate. My problem is that I can't figure out the correct order. Using SVL, the matrix code I have tried looks like this:
//base transform as defined in glut_teapot.c
teapotBaseTransform = HTrans4(Vec3(0, 0, -1.5)) * HScale4(Vec3(0.5, 0.5, 0.5)) * HRot4(Vec3(1, 0, 0), 1.5 * M_PI);
//this is updated every frame to rotate the object
worldTransform = HTrans4(Vec3(-2, 0, 0)) * HRot4(Vec3(0, 1, 0), rotAngle / (180.0f / M_PI));
//multiply both together to get the total matrix transform for the teapot
teapotTransform = teapotBaseTransform * worldTransform;
Make sure that your matrices elements are in the same order as that produced by GL. You can compute the matrices with GL and then download them with glGetFloatv. That's what I did for my lib.
Sig: http://glhlib.sourceforge.net an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
Yes, I did the same.. Use the old GL functions just to view whats happening then match it to the LIB your using. You may just have the Matrix Multiply in the wrong order. But I really Really recommend you make your own Matrix stack. You say you already know how, but your still using a lib to do it?
Cheers
Lee
Code is Life
C/C++, ObjC Programmer, 3D Artist, Media Production
means that the new matrix M[sub]i+1[/sub] is computed as M[sub]i+1[/sub] := M[sub]i[/sub] * R * S * T
where M[sub]i[/sub] was the matrix previously on the stack (probably the VIEW matrix) and R, S, and T mean the rotation, scaling, and translation, resp. Notice please that the order of commands from top to bottom corresponds to the order of matrices from left to right in the formula.
glPushMatrix / glPopMatrix does nothing more than to store / restore the current matrix for further use. Because this is implemented as a stack, the stack-typical operations "push" and "pop" are used. Push simply creates a copy of the current top matrix as new top matrix, while pop removes the current top matrix, making the new lower matrix the new top one. The other routines (like glLoadIdentity, glMultMatrix, glRotated, ...) just replace the top matrix with the new one.