Jump to content
  • Advertisement
Sign in to follow this  

source code for glRotate

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

hey, Im am currently using glRotate to rotate my cube, does ne know the code that glRotate uses to rotate an object. They way i see it is i have to rotate all 8 of my vectors for my cube. (or if ne one know how i can roate about the x or y axis using the vectors i have for my cube) thanks

Share this post


Link to post
Share on other sites
Advertisement
The glRotate function just multiplies the current matrix (which may be the projection, modelview or texture matrix) by a matrix that does the rotation. One way to create this matrix would be to use a quaterion to represent the rotation and then converting this into a matrix.

Share this post


Link to post
Share on other sites
Look up some stuff of matrix algebra and geometry. It'll explain how rotations are performed then give you matrices which will do them. Remember you need the 3-D versions of the matrices, not the 2-D versions.

Then you'd create the matrix (or matrices) that would apply your desired rotation, then lastly, multiply your points by these matrices to yield the final location.

-Auron

Share this post


Link to post
Share on other sites
This will create a rotation matrix that rotates a degrees around axis (x,y,z).


void build_4x4_rotation_matrix(float x, float y, float z, float a, float *matrix) {

a = a * PI / 180.0; // convert to radians
float s = sin(a);
float c = cos(a);
float t = 1.0 - c;

float tx = t * x;
float ty = t * y;
float tz = t * z;

float sz = s * z;
float sy = s * y;
float sx = s * x;

matrix[0] = tx * x + c;
matrix[1] = tx * y + sz;
matrix[2] = tx * z - sy;
matrix[3] = 0;

matrix[4] = tx * y - sz;
matrix[5] = ty * y + c;
matrix[6] = ty * z + sx;
matrix[7] = 0;

matrix[8] = tx * z + sy;
matrix[9] = ty * z - sx;
matrix[10] = tz * z + c;
matrix[11] = 0;

matrix[12] = 0;
matrix[13] = 0;
matrix[14] = 0;
matrix[15] = 1;

}


this will transform a point by a matrix


void transform_3d_point_by_4x4_matrix(float *point, float *matrix, float *newPoint) {

for(int c = 0; c < 3; c++) {

float sum = 0;

for(int r = 0; r < 3; r++) sum += (point[r] * matrix[r * 4 + c]);

sum += matrix[r * 4 + c];

newPoint[c] = sum;

}

}


For your specific case this probably isn't the fastest way to rotate your cube (you don't actually need a 4x4 matrix for that). It's just a cut and past of a few functions from my math header.

I tend to only use quats for animation and other things where their properties are most useful. For general transformations matricies are nice as they allow you to use a single representation for any transform.

I don't want to start a quat/matrix debate though, both forms definetly have their uses.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!