PDA

View Full Version : Implementing rotation function like glRotate



SummertimeClothes
10-17-2012, 12:07 AM
I am trying to implement my own rotation function in OpenGL on C++, i.e. something like glRotate. My code for rotating around y looks like this:


void do_rotateY(GLdouble angle)
{
//rotate around y axis
GLdouble s = angle;
GLdouble c[4][4];
c[1][1] = c[3][3]= 1.0;
c[0][0] = c[2][2] = cos(s);
c[2][0] = sin(s);
c[0][2] = -c[2][0];
glMultMatrixd(*c);
}

When I'm doing a call like:



glPushMatrix();
do_rotateY(100);
...draw something...
glPopMatrix();
nothing appears on the screen. When I replace do_rotateY(...) with glRotatef(...) I can see everything correctly.

Thanks for your help!

uwi2k2
10-17-2012, 12:40 AM
Hi,

taken from a ES-Framework, this may help you:



void ESUTIL_API
esRotate(ESMatrix *result, GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
{
GLfloat sinAngle, cosAngle;
GLfloat mag = sqrtf(x * x + y * y + z * z);

sinAngle = sinf ( angle * PI / 180.0f );
cosAngle = cosf ( angle * PI / 180.0f );
if ( mag > 0.0f )
{
GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs;
GLfloat oneMinusCos;
ESMatrix rotMat;

x /= mag;
y /= mag;
z /= mag;

xx = x * x;
yy = y * y;
zz = z * z;
xy = x * y;
yz = y * z;
zx = z * x;
xs = x * sinAngle;
ys = y * sinAngle;
zs = z * sinAngle;
oneMinusCos = 1.0f - cosAngle;

rotMat.m[0][0] = (oneMinusCos * xx) + cosAngle;
rotMat.m[0][1] = (oneMinusCos * xy) - zs;
rotMat.m[0][2] = (oneMinusCos * zx) + ys;
rotMat.m[0][3] = 0.0F;

rotMat.m[1][0] = (oneMinusCos * xy) + zs;
rotMat.m[1][1] = (oneMinusCos * yy) + cosAngle;
rotMat.m[1][2] = (oneMinusCos * yz) - xs;
rotMat.m[1][3] = 0.0F;

rotMat.m[2][0] = (oneMinusCos * zx) - ys;
rotMat.m[2][1] = (oneMinusCos * yz) + xs;
rotMat.m[2][2] = (oneMinusCos * zz) + cosAngle;
rotMat.m[2][3] = 0.0F;

rotMat.m[3][0] = 0.0F;
rotMat.m[3][1] = 0.0F;
rotMat.m[3][2] = 0.0F;
rotMat.m[3][3] = 1.0F;

esMatrixMultiply( result, &rotMat, result );
}
}


cu
uwi

Dan Bartlett
10-17-2012, 05:36 AM
You need to zero out the terms of c that are meant to contain zero, because local variables aren't initialized so will contain garbage.