Sign in to follow this  
dawberj3

Is my matrix stuff correct?

Recommended Posts

Im storing a coordinate frame in a matrix so I can rotate and translate objects around, do I have the correct frame in the correct places in the matrix?
// Create matrix
	float m[16];

	m[0]  = this->object[0].side.x;
	m[1]  = this->object[0].side.y;
	m[2]  = this->object[0].side.z;
	m[3]  = 0.0f;
	m[4]  = this->object[0].up.x;
	m[5]  = this->object[0].up.y;
	m[6]  = this->object[0].up.z;
	m[7]  = 0.0f;
	m[8]  = this->object[0].look.x;
	m[9]  = this->object[0].look.y;
	m[10] = this->object[0].look.z;
	m[11] = 0.0f;
	m[12] = this->object[0].position.x;
	m[13] = this->object[0].position.y;
	m[14] = this->object[0].position.z;
	m[15] = 1.0f;




Once ive loaded this matrix, I need to rotate and translate like so and then update the matrix, (In other words, I need to rotate and translate the above matrix but I aint so good with matrix maths so im trying to do it this way):
glPushMatrix();
	
		// Load above Matrix
		glMultMatrixf(m);
		
		glPushMatrix();
			
			// Translate to new position
			glTranslatef(newPosition.x, newPosition.y, newPosition.z); 
		
			// Rotate
			glRotatef(angleOfRotation, rotation.x, rotation.y, rotation.z);
			
			// Draw the object
this->object[0].draw();
			
			
			// Load this new matrix into the frame
			glGetFloatv(GL_MODELVIEW_MATRIX, m);
			
			this->object[0].side.x = m[0];
			this->object[0].side.y = m[1];
			this->object[0].side.z = m[2];
			
			this->object[0].up.x = m[4];
			this->object[0].up.y = m[5];
			this->object[0].up.z = m[6];
			
			this->object[0].look.x = m[8];
			this->object[0].look.y = m[9];
			this->object[0].look.z = m[10];
			
			this->object[0].position.x = m[12];
			this->object[0].position.y = m[13];
			this->object[0].position.z = m[14];
	
		glPopMatrix();
		
	glPopMatrix();




Does this seem correct because the object isnt appearing in the correct position and seems to be 90 degrees rotated in the wrong direction. If anyone can explain who to do it uses matrix math then id be grateful. Thx

Share this post


Link to post
Share on other sites
it looks like instead of loading your matrix m you are multiplying it by m. You should be using glLoadMatrixf to load your own matrix onto the stack. Make sure that also that glMatrixMode is set to GL_MODELVIEW so you do not end up modifying some other stack.

Share this post


Link to post
Share on other sites
(1) BornToCode is right:
glPushMatrix creates a copy of the topmost matrix. So you need to do a
glPushMatrix();
glLoadIdentity(); // <-- this
glMultMatrixf(m);
or even better
glPushMatrix();
glLoadMatrixf(m);
to load!

(2) Notice that the MODELVIEW matrix, as the name says, holds a combination of the VIEW and the MODEL matrix. But you intention is to retrieve the MODEL matrix only!

(3) You have named the translation parameter "newPosition". Notice please that newPosition must not denote a (absolute) position but a (relative) offset, since glTranslate does a matrix multiplication. Similarly you may misinterpret the orientation. (Perhaps you have the right understanding and simply the variable named unprecise, but I don't know, so I hint you at this possibility.)

(4) The inner pair of glPushMatrix/glPopMatrix seems me a waste of time if your snippet shows all stuff, but that isn't the current problem ;)


I strongly suggest you to apply your own matrix stuff instead of misusing GL's stack. Many problems reported here at GDnet arised from such kind of use of GL's matrix stacks. Most often the VIEW/MODEL concatenation makes the problem.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this