Rotate a point around a user-defined axis

Started by
6 comments, last by Gumgo 16 years, 7 months ago
In OpenGL: glRotatef( angle, xvec, yvec, zvec); applies that transformation to what is rendered. But how do I find the X, Y, and Z position of a point rotated around a user defined axis manually? I need to sort some triangles but in order to do that I need their position after they have been transformed. Thanks.
Advertisement
get modelview with glGetFloat*v.

multiply this matrix (sub3x3) with your vertex and add the translation vector (last row)
Create a quaternion from the axis and the angle you want to rotate by. Then multiply the point by the quaternion to get your transformed position.
Dang... now if only I knew what that meant...

Currently trying to learn quaternions and matrix math in the next 10 minutes [oh]

EDIT: Hmm well I know very basic matrix math, I just don't know its uses. Also I'm guessing that to apply transformations you multiply the transformation matrix with a (3x1) matrix that is the vertex position? But if the transformation matrix is (4x4) then how would you do that?

I'm also not sure how to use matrices in C++ or OpenGL. DirectX I believe has D3DMATRIX or something but... is there a predefined matrix type in OpenGL or do I have to create one?

EDIT: I think I've found something I can finally understand:
	  glRotate produces a rotation of angle	degrees	around the	  vector (x,y,z).  The current matrix (see glMatrixMode) is	  multiplied by	a rotation matrix with the product replacing	  the current matrix, as if glMultMatrix were called with the	  following matrix as its argument:		  ( xx(1-c)+c	xy(1-c)-zs  xz(1-c)+ys	 0  )		  |					    |		  | yx(1-c)+zs	yy(1-c)+c   yz(1-c)-xs	 0  |		  | xz(1-c)-ys	yz(1-c)+xs  zz(1-c)+c	 0  |		  |					    |		  (	 0	     0		 0	 1  )	  Where	c = cos(angle),	s = sine(angle), and ||( x,y,z )|| = 1	  (if not, the GL will normalize this vector).

When they say "xx", "yx", "xy", what do they mean by that, multiply them? Or something else.

[Edited by - Gumgo on September 4, 2007 2:24:51 PM]
Quote:But if the transformation matrix is (4x4) then how would you do that?
Treat the 3-d vector as a 4-d vector with a w component of '1'.
Quote:I'm also not sure how to use matrices in C++ or OpenGL. DirectX I believe has D3DMATRIX or something but... is there a predefined matrix type in OpenGL or do I have to create one?
OpenGL does not have anything like the DirectX math library (not per se, at least), but there are plenty of free math libraries available online that you could use.
Quote:EDIT: I think I've found something I can finally understand:
	  glRotate produces a rotation of angle	degrees	around the	  vector (x,y,z).  The current matrix (see glMatrixMode) is	  multiplied by	a rotation matrix with the product replacing	  the current matrix, as if glMultMatrix were called with the	  following matrix as its argument:		  ( xx(1-c)+c	xy(1-c)-zs  xz(1-c)+ys	 0  )		  |					    |		  | yx(1-c)+zs	yy(1-c)+c   yz(1-c)-xs	 0  |		  | xz(1-c)-ys	yz(1-c)+xs  zz(1-c)+c	 0  |		  |					    |		  (	 0	     0		 0	 1  )	  Where	c = cos(angle),	s = sine(angle), and ||( x,y,z )|| = 1	  (if not, the GL will normalize this vector).

When they say "xx", "yx", "xy", what do they mean by that, multiply them? Or something else.
Yup, multiply. However, you can avoid constructing matrices like the above yourself by a) querying the OpenGL matrix stack (probably not advisable for production code, but fine for demo code), or b) using a third-party math library as mentioned previously.
Oh one more thing is the normals. Do I use this same transformation matrix to rotate the normals correctly?
Quote:Original post by Gumgo
Oh one more thing is the normals. Do I use this same transformation matrix to rotate the normals correctly?
For normals, set the w component to 0. (This is assuming the matrix represents a rigid-body transform. If the matrix incorporates other transforms - such as scaling - you'll need to transform your normals - again, with w set to 0 - by the inverse transpose of the matrix and then normalize the results.)
What does the "w" mean anyways?

This topic is closed to new replies.

Advertisement