#### Archived

This topic is now archived and is closed to further replies.

# How to rotate without glRotate() ?

## Recommended Posts

Thats it! How to rotate a shape/poly without using glRotate() given the X, Y and Z coords for all the shape/poly''s vertices? I need to know the resulting vertices'' coordinates instead of drawing them... need to store them. How do I do this? Thank you guys! See you!

##### Share on other sites
That''s like saying..
"I want to drink water without water!"
OR
"I want to breath, but without air!"

UNLESS.. U use GLULookat

Don''t forget to include "glu.h"

void gluLookAt(GLdouble eyex,GLdouble eyey,GLdouble eyez,
GLdouble centerx,GLdouble centery,GLdouble centerz,
GLdouble upx,GLdouble upy,GLdouble upz);

eye = Where the camera is
center = what ya lookin'' at
up = rotation axis... typical values = 0,1,0

##### Share on other sites
quote:
Original post by TerraX
That's like saying..
"I want to drink water without water!"
OR
"I want to breath, but without air!"

UNLESS.. U use GLULookat

Don't forget to include "glu.h"

void gluLookAt(GLdouble eyex,GLdouble eyey,GLdouble eyez,
GLdouble centerx,GLdouble centery,GLdouble centerz,
GLdouble upx,GLdouble upy,GLdouble upz);

eye = Where the camera is
center = what ya lookin' at
up = rotation axis... typical values = 0,1,0

well.. why do i need opengl to rotate?

he asks for the math, because he has to calculate it for himself..

my suggestion (as i don't have the code around currently):
type glRotatef source code MESA in google.. and.. well.. take a look around hope you find it..

"take a look around" - limp bizkit

[edited by - davepermen on May 1, 2002 7:26:15 AM]

##### Share on other sites
I do a bit of software transform in my LOTR demo. I''m afraid the code''s horrible, but here it is:

  if (vertices[initialVertex + 2] != 0)	radialAngle = atan(vertices[initialVertex] / vertices[initialVertex + 2]);else if (vertices[initialVertex] < 0)	radialAngle = (3 * PI) / 2;else	radialAngle = (PI / 2);if (vertices[initialVertex + 2] < 0)	radialAngle += PI;radialAngle += yRotation;pointDistance = sqrt((vertices[initialVertex] * vertices[initialVertex]) + (vertices[initialVertex + 2] * vertices[initialVertex + 2]));xCoordinate = pointDistance * sin(radialAngle);zCoordinate = pointDistance * cos(radialAngle);if (vertices[initialVertex + 1] != 0)	elevationAngle = atan(zCoordinate / vertices[initialVertex + 1]);else if (zCoordinate < 0)	elevationAngle = (3 * PI) / 2;else	elevationAngle = (PI / 2);if (vertices[initialVertex + 1] < 0)	elevationAngle += PI;elevationAngle += xRotation;pointDistance = sqrt((vertices[initialVertex + 1] * vertices[initialVertex + 1]) + (zCoordinate * zCoordinate));yCoordinate = pointDistance * cos(elevationAngle);zCoordinate = pointDistance * sin(elevationAngle);

Note - this does a rotate about the y-axis, then a rotate about the x-axis - order IS important.

Sorry that the code isn''t commented - if you can''t understand it & are willing to wait a few days (''til I''ve finished the latest batch of coursework!) then i''ll try and explain it better.

Enigma
------
... We do much, much more harm than good. ...

##### Share on other sites
Whoops, err, is it better to post a reply, look stoopid and hope that the person asking the question is a newbie (like me)
Or better to just say nuffin?
*blush*

##### Share on other sites
That depends on how bad you feel about making an a out of yourself

- An eye for an eye will make the world go blind -

##### Share on other sites
Eugh - you''re right that code is hideous!

And cmon guys, you can''t send this programmer on a wild goose chase though the mesa source code - that library is huge!! Besides, what this guy wants to know is common knowledge...

Ah - errr... Ok go look at the maths library I posted on my website (Link at end of post), it''s in the downloads section (Sorry for not posting the code). If you have any questions, please post them on my forum so that the information is documented.

The basic idea is this - change to modelview matrix (if you''re currently in worldview), create your own matrix, perform rotate function on matrix, upload the matrix to opengl, draw verts as usual. Et voila

BTW: " That''s like saying..
"I want to drink water without water!"
OR
"I want to breath, but without air!"

UNLESS.. U use GLULookat" - Please refrain from misinforming other programmers as that golden nugget of knowledge is complete rubbish If you dont know the answer to the question posed then please keep shtum because it just confuses the newbies.

A couple of interesting notes:
1) gluLookAt is a very useful function but I have heard word that it is quite a slow function and should be avoided if possible. Feel free to investigate this if you have overlooked it in the past.
2) Writing your own version of glRotate is all well and good, but you will find that the boys at SGI (Microsoft?) are quite smart (Microsoft is excluded from that statement) and actually optimised this function quite well and it is not necessary to write your own glRotate function because in one way or another it is working in exactly the same way.
3) Programmers most often write their own maths routines when it becomes a necessity - why reinvent the wheel without good reason? For example, DX8 and OpenGL work differently, so a maths library which can do it independantly of the API is therefore useful.

Hope that helps!

-------- E y e .Scream Software --------
----------------------------------------
/-\
http://www.eyescream.cjb.net | * |
\-/
----------------------------------------

##### Share on other sites
Ok guys,

First off, let me thank you for the replies!

I''ll try to explain myself. What i''m trying to do is calculate the vertices of all objects in my world. My program is loading the objects'' information from a file, but it don''t know the vertices coordinates, the file just tells the object''s center coordinates, the type of object and it''s size.

But, to do collision, I need the objects'' vertices... I know how to cauculate the vertices for objects that are orthogonal to the X, Y ans Z axis... but, when the world information file tells to the program that the object is to be rotated in a given angle, the program need to rotate the collision area''s vertices too... thats why I need to cauculate the rotation by myself... or I need some kind of glRotate() function that return the result vertices to me... I just need to store the result vertices after rotation into an array to pass them to the collision function.

Any idea?

Thank you guys!... And sorry about my English errors!

See you!

##### Share on other sites
I read in the OpenGL docs that you can actually return transformed vertexed. I''m not sure how it works though (and I dont have the docs here in my office so I can''t look it up for you).

- An eye for an eye will make the world go blind -

##### Share on other sites
Up!

Please... someone who could point me to a site, text or something?

Thanks!

##### Share on other sites
Can''t you just use binding boxes to detect colision. like in quake.

##### Share on other sites
After checking the bounding boxes I still have to check that specific object''s vertices, otherwise, I''ll collide with spinning shapes... middle of doors and so.

Any idea?

##### Share on other sites
The use bounding spheres. They''re always the same size no matter what rotation. Just do this:

R1 = Radius bounding sphere object1
R2 = same for object2
V1 = vector to center position object1
V2 = same for object2

bool Collide(R1, R2, V1, V2)
{
If distance(V1, V2)>(R1+R2)
return TRUE;

return FALSE;
}

- An eye for an eye will make the world go blind -

##### Share on other sites
I have thought of a much easier way of doing your rotations within the collision function:

Rotate the colliding vector, not the object. To rotate the object meand to rotate all the vertexes. To rotate the vector that should be checked means only rotating 2 vertexes. Just rotate it opposite to the objects rotation in the world. Pass the objects vertex list to the collision function and check it against the rotated collision vector. Voila!

- An eye for an eye will make the world go blind -

##### Share on other sites
I NEED to know the resulting values of a rotate vertice.

Thanks!

##### Share on other sites
I NEED to know the resulting values of a rotate vertice.

Thanks!

##### Share on other sites
In the appendix of the red book (although I''m not sure about the online version) it tells you the matrix created by glRotate.
Create that matrix yourself, then simply multibly tour vertex position by it.

##### Share on other sites
try this:
http://www.makegames.com/3drotation/

this one is good:
http://www.siggraph.org/education/materials/HyperGraph/modeling/mod_tran/3drota.htm

##### Share on other sites
Alternatively you can let OpenGL generate a matrix for you and then transform a few vertices using it.

glGetFloatv(GL_MODELVIEW_MATRIX, matrix) will get the curent MODELVIEW matrix and copy it to matrix (declared
GLfloat matrix[16]).

Then you can use something similar to the vertex_trans function in this sample code to transorm a vertex acording to the matrix (it's a matrix multiplication).

    /* To compile this you need GLUT. Use the cursor keys, Z, z and a * to move the cube. The important stuff is in vertex_trans * and Display */#include <stdio.h>#include <stdlib.h>#include <math.h>#include <GL/glut.h>static GLfloat Xrot = 0, Yrot = 0, Zrot = 0;static GLboolean Anim = GL_FALSE;static GLint ViewportWidth, ViewportHeight;static void Idle( void ){   Xrot += 3.0;   Yrot += 4.0;   Zrot += 2.0;   glutPostRedisplay();}static void vertex_trans(GLfloat r[3], const GLfloat v[3], const GLfloat m[16]){   r[0] = m[0]*v[0] + m[4]*v[1] + m[8]*v[2]  + m[12];   r[1] = m[1]*v[0] + m[5]*v[1] + m[9]*v[2]  + m[13];   r[2] = m[2]*v[0] + m[6]*v[1] + m[10]*v[2] + m[14];   r[3] = m[3]*v[0] + m[7]*v[1] + m[11]*v[2] + m[15];}static void Display( void ){   /* A cube vertices (and a lot of typing) */   static GLfloat cube[24][3] = {   {1.0f, 1.0f,-1.0f},                                    {-1.0f, 1.0f,-1.0f},                                    {-1.0f, 1.0f, 1.0f},                                    {1.0f, 1.0f, 1.0f},                                    {1.0f,-1.0f, 1.0f},                                    {-1.0f,-1.0f, 1.0f},                                    {-1.0f,-1.0f,-1.0f},                                    {1.0f,-1.0f,-1.0f},                                    {1.0f, 1.0f, 1.0f},                                    {-1.0f, 1.0f, 1.0f},                                    {-1.0f,-1.0f, 1.0f},                                    {1.0f,-1.0f, 1.0f},                                    {1.0f,-1.0f,-1.0f},                                    {-1.0f,-1.0f,-1.0f},                                    {-1.0f, 1.0f,-1.0f},                                    {1.0f, 1.0f,-1.0f},                                    {-1.0f, 1.0f, 1.0f},                                    {-1.0f, 1.0f,-1.0f},                                    {-1.0f,-1.0f,-1.0f},                                    {-1.0f,-1.0f, 1.0f},                                    {1.0f, 1.0f,-1.0f},                                    {1.0f, 1.0f, 1.0f},                                    {1.0f,-1.0f, 1.0f},                                    {1.0f,-1.0f,-1.0f}   };   GLfloat matrix[16];   GLfloat v[4];   GLint vertex;   glClear( GL_COLOR_BUFFER_BIT );   /* Use GL to set a new MODELVIEW matrix */   glLoadIdentity();   glTranslatef( 0.0, 0.0, -15.0 );   glRotatef(Xrot, 1, 0, 0);   glRotatef(Yrot, 0, 1, 0);   glRotatef(Zrot, 0, 0, 1);   /* Get the new MODELVIEW matrix */   glGetFloatv(GL_MODELVIEW_MATRIX, matrix);   /* Restore the matrix to the identity matrix */   glLoadIdentity();   /* Transform vertices using the previous matrix and draw them */   glBegin(GL_QUADS);      for (vertex=0; vertex<24; vertex++)         {            vertex_trans(v, cube[vertex], matrix);            glVertex3f(v[0], v[1], v[2]);         }   glEnd();   glutSwapBuffers();}static void Reshape( int width, int height ){   ViewportWidth = width;   ViewportHeight = height;      glViewport( 0, 0, width, height );   glMatrixMode( GL_PROJECTION );   glLoadIdentity();   glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );   glMatrixMode( GL_MODELVIEW );}static void Key( unsigned char key, int x, int y ){   const GLfloat step = 3.0;   (void) x;   (void) y;   switch (key) {      case 'a':         Anim = !Anim;         if (Anim)            glutIdleFunc(Idle);         else            glutIdleFunc(NULL);         break;      case 'z':         Zrot -= step;         break;      case 'Z':         Zrot += step;         break;      case 27:         exit(0);         break;   }   glutPostRedisplay();}static void SpecialKey( int key, int x, int y ){   const GLfloat step = 3.0;   (void) x;   (void) y;   switch (key) {      case GLUT_KEY_UP:         Xrot -= step;         break;      case GLUT_KEY_DOWN:         Xrot += step;         break;      case GLUT_KEY_LEFT:         Yrot -= step;         break;      case GLUT_KEY_RIGHT:         Yrot += step;         break;   }   glutPostRedisplay();}static void Init( void ){   /* setup lighting, etc */   glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);   glShadeModel(GL_FLAT);   glDisable(GL_LIGHTING);   glDisable(GL_DEPTH_TEST);}int main( int argc, char *argv[] ){   glutInit( &argc, argv );   glutInitWindowPosition( 0, 0 );   glutInitWindowSize( 400, 400 );   glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );   glutCreateWindow(argv[0]);   glutReshapeFunc( Reshape );   glutKeyboardFunc( Key );   glutSpecialFunc( SpecialKey );   glutDisplayFunc( Display );   Init();   glutMainLoop();   return 0;}

I know, half of the code is grey. This is a
problem with the forum.

[edited by - __ALex_J_ on May 8, 2002 4:34:06 PM]

##### Share on other sites
Couldn''t you just take the poly''s verticies and crank them thru the rotation matricies by hand?

##### Share on other sites
What do you mean?

Is it not what I just did?

By hand? Do you mean with a peice of paper and your head?

[edited by - __ALex_J_ on May 8, 2002 4:42:33 PM]

##### Share on other sites
you need to create a rotation matrix. and multiply the vector through the rotation matrix. if you want to know how OpenGL does it....

go here.... OpenGL Programmer''s Guide click on the Appendix F
Homogeneous Coordinates and Transformation Matrices
chapter and go down to glRotate .....

if you need to know how to multiply a vector through a matrix, search the web or gamedev....

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

##### Share on other sites
The following transformation rotates the point (x, y, z) around the x-axis, producing a new point (x'', y'', z'').

[x''y''z''1] = [x y z 1] * 1 0 0 0
0 cos(a) sin(a) 0
0 -sin(a) cos(a) 0
0 0 0 1

In this example matrix, the letter a stands for the angle of rotation, in radians. Angles are measured clockwise when looking along the rotation axis toward the origin.

##### Share on other sites
sorry in that last reply but I couldnt get the 4*4 matrix to appear correctly

##### Share on other sites
quote:
Original post by Anonymous Poster
sorry in that last reply but I couldnt get the 4*4 matrix to appear correctly

[code] and [/code] help to preserve spacing.

• ### Forum Statistics

• Total Topics
628368
• Total Posts
2982293

• 10
• 9
• 13
• 24
• 11