Jump to content
  • Advertisement
Sign in to follow this  
Aken H Bosch

OpenGL Obtain Matrix from positions and rotations

This topic is 4218 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

I am making a game in OpenGL in C++, and I have constructed a variety of methods to transform matrices and to use matrices to transform points. The problem is, I am used to working with matrices and vectors separately, i.e. with 3x3 rotation matrices and 3-component vectors. What I used to do is this: Rotate the point on the object with a 3x3 matrix, translate that by the difference between the object's location and the camera's location, and then rotate that point by the inverse of the camera's rotation matrix to get the point relative to the camera. Unfortunately, this does not work for OpenGL with its 4x4 matrices; I know how to perform the operations and have created functions to do them, but the order of operations is different when the thing I am trying to obtain is a 4x4 transformation matrix with translation components included in it. If anyone knows how to build an object-to-camera matrix from the following methods, please tell me. Following is a list of the methods I came up with and what they do:
#ifndef MATRIX4X4_
#define MATRIX4X4_

class Matrix4x4		//class which stores a 4x4 matrix (obviously)
	static const float PI = 3.1415926535897932384626433832795f;		//pi (obviously)
	Matrix4x4();				//constructor, does not create a working transformation
	float vals[16];				//the data for the matrix
	void InitI();				//sets the matrix up as the identity matrix
	void InitT(float x, float y, float z);		//sets the matrix up as a translation by <x, y, z>, with no rotation
	void InitR(float ax, float ay, float az, float rot);		//sets the matrix up as a rotation about the unit vector <ax, ay, az> by angle rot, with no translation
	void InitRT(float ax, float ay, float az, float rot, float tx, float ty, float tz);		//combines the features of InitR and InitT
	void Translate(float tx, float ty, float tz);		//translates the matrix's <x, y, z> offset from its previous position
	void SetTranslation(float tx, float ty, float tz);	//translates the matrix so that its <x, y, z> offset is <tx, ty, tz>
	void SetInvTranslation(float tx, float ty, float tz);	//translates the matrix as if it were translated before an inversion/transposition
	void SetRotation(float ax, float ay, float az, float rot);		//sets the rotation component of this matrix in the same manner as InitR, but does not affect translation
	void RotateLocally(float ax, float ay, float az, float rot);	//rotates relative to the current matrix without affecting the position offset
	void RotateGlobally(float ax, float ay, float az, float rot);	//rotates the entire matrix, including the position offset, about the origin
	void InitVals(float *nvals);					//copies values from another matrix, given as an argument using Matrix4x4::Values()
	void InitRotVals(float *nvals);					//copies values from another matrix, but doesn't change translation data
	void Multiply(Matrix4x4 *mat);					//mulitplies this matrix by the argument matrix
	void TransformPoint(float ix, float iy, float iz, float *dx, float *dy, float *dz);		//transforms the input point by this matrix and returns the transformed vector to <dx, dy, dz>
	void Invert();				//doesn't work right
	float Determinant();		//returns the determinant for use above
	float *Values();			//returns the values of the elements of this matrix
	void Transpose();			//transposes the matrix
	void InvertForRT();			//transposes the matrix, which, for R/T only matrices, is the same as inverting
	void InvertForR();			//transposes the rotation part of this matrix only
	void Display();				//outputs the values of the elements of this matrix in a neat-looking fashion
	void FlipZComponent();		//flips the z component of the ROTATION only
	void Negate();				//takes the negative of this matrix, but does not affect the element that is always supposed to be 1 (the bottom right element)
	float Magnitude(float px, float py, float pz);		//returns the magnitude of a vector
	static void DisplayVector(float a, float b, float c);	//displays a vector
	~Matrix4x4();				//destroys this matrix

#endif /*MATRIX4X4_*/

I believe that I have sufficient code to do any operations necessary. If there is one change to the matrix code I might make, it would be adding other methods similar to FlipZComponent() that change position only/as well. My objects have the following relevant methods: Matrix4x4 *RotMat(); float X(); float Y(); float Z(); So, how can I do this? What is the correct sequence? The result I need is something so that I can do this: glLoadMatrix(MatrixName->Values()); or if it is not a pointer glLoadMatrix(MatrixName.Values()); My matrices are already organized in the format used by OpenGL (Column major, I think it's called).

Share this post

Link to post
Share on other sites
Here's how to compute the matrix you describe (using OpenGL column-vector convention):
M = McameraR-1*McameraT-1*MobjectT*MobjectR
Where T refers to the translation matrix for the object or camera, and similarly R represents the rotation matrix. The '-1' superscript here refers to matrix inversion.

Assuming that your functions do what they say they do, then there are several ways you can use them to compute the matrix M. (Note that each of the inversions in the equation is trivial and can be special-cased).

Looking over the comments in your code, I notice what look like some potential problems (for example, the inverse of an 'RT-only' matrix is not the transpose, as your comments state). If you're uncertain about any particular functions, feel free to post them so that we can take a look at them.

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!