Advertisement

Rotation help [n00b]

Started by May 20, 2003 12:57 PM
6 comments, last by mAdMaLuDaWg 21 years, 9 months ago
void DrawCube(float xPos,float yPos,float zPos) { glPushMatrix(); glTranslatef(xPos,yPos,zPos); glBegin(GL_QUADS); glColor3f(1.0f,0.0f,0.0f); //left face glVertex3f(0.0f,0.0f,1.0f); glVertex3f(0.0f,0.0f,0.0f); glVertex3f(0.0f,1.0f,0.0f); glVertex3f(0.0f,1.0f,1.0f); glColor3f(0.0f,0.0f,0.0f); //bottom face glVertex3f(0.0f,0.0f,1.0f); glVertex3f(1.0f,0.0f,1.0f); glVertex3f(1.0f,0.0f,0.0f); glVertex3f(0.0f,0.0f,0.0f); glColor3f(0.0f,0.0f,0.0f); //right face glVertex3f(1.0f,0.0f,1.0f); glVertex3f(1.0f,0.0f,0.0f); glVertex3f(1.0f,1.0f,0.0f); glVertex3f(1.0f,1.0f,1.0f); //top face glColor3f(0.0f,1.0f,0.0f); glVertex3f(0.0f,1.0f,1.0f); glVertex3f(1.0f,1.0f,1.0f); glVertex3f(1.0f,1.0f,0.0f); glVertex3f(0.0f,1.0f,0.0f); //front face glColor3f(0.0f,0.0f,1.0f); glVertex3f(0.0f,0.0f,1.0f); glVertex3f(1.0f,0.0f,1.0f); glVertex3f(1.0f,1.0f,1.0f); glVertex3f(0.0f,1.0f,1.0f); //back face glVertex3f(0.0f,0.0f,0.0f); glVertex3f(1.0f,0.0f,0.0f); glVertex3f(1.0f,1.0f,0.0f); glVertex3f(0.0f,1.0f,0.0f); glEnd(); glPopMatrix(); } int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing { static float angle=0.0f; float i; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer glLoadIdentity(); // Reset The Current Modelview Matrix glTranslatef(-3.0f,1.0f,-15.0f); glRotatef(angle,0.0f,1.0f,0.0f); //rotation about the beginning of the first cube glColor3f(1.0f,0.3f,1.0f); for(i=0.0f;i<4.0f;i++) DrawCube(i,0.0f,0.0f); if(angle==360.0f)angle=0.0f; angle+=1.0f; return TRUE; // Everything Went OK } ----------------------- I'm trying to rotate the four cubes I drew about a point (1,1,-15) so that the four cubes rotate abouta central point as opposed to the beginning of the first cube. I don't know how to do this.. please help. Thanks. [edited by - mAdMaLuDaWg on May 20, 2003 2:01:48 PM]
-----------------------------"Q: How many Microsoft engineers does it take to change a light bulb?A: None. Bill Gates will just redefine Darkness(TM) as the new industry standard. "~wUn LoVe tO aLl ThE mAlUs OuT tHeRe!~
youve almoast got it right but you need to do this

void DrawCube(float xPos,float yPos,float zPos)
{
glTranslatef(xPos,yPos,zPos);
glBegin(GL_QUADS);

glColor3f(1.0f,0.0f,0.0f);
//left face
glVertex3f(0.0f,0.0f,1.0f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,1.0f,0.0f);
glVertex3f(0.0f,1.0f,1.0f);

glColor3f(0.0f,0.0f,0.0f);
//bottom face
glVertex3f(0.0f,0.0f,1.0f);
glVertex3f(1.0f,0.0f,1.0f);
glVertex3f(1.0f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,0.0f);

glColor3f(0.0f,0.0f,0.0f);
//right face
glVertex3f(1.0f,0.0f,1.0f);
glVertex3f(1.0f,0.0f,0.0f);
glVertex3f(1.0f,1.0f,0.0f);
glVertex3f(1.0f,1.0f,1.0f);

//top face
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(0.0f,1.0f,1.0f);
glVertex3f(1.0f,1.0f,1.0f);
glVertex3f(1.0f,1.0f,0.0f);
glVertex3f(0.0f,1.0f,0.0f);

//front face
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(0.0f,0.0f,1.0f);
glVertex3f(1.0f,0.0f,1.0f);
glVertex3f(1.0f,1.0f,1.0f);
glVertex3f(0.0f,1.0f,1.0f);

//back face
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(1.0f,0.0f,0.0f);
glVertex3f(1.0f,1.0f,0.0f);
glVertex3f(0.0f,1.0f,0.0f);
glEnd();



}


int DrawGLScene(GLvoid) // Here''s Where We Do All The Drawing
{
static float angle=0.0f;
float i;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Modelview Matrix
glPushMatrix();
glTranslatef(-3.0f,1.0f,-15.0f);



glRotatef(angle,0.0f,1.0f,0.0f); //rotation about the beginning of the first cube
glColor3f(1.0f,0.3f,1.0f);
for(i=0.0f;i<4.0f;i++)
DrawCube(i,0.0f,0.0f);
glPopMatrix();
if(angle==360.0f)angle=0.0f;

angle+=1.0f;

return TRUE; // Everything Went OK
}



i think im right although i stopped ogl and went to DX a while ago
Advertisement
Nopes, thanks for the try though... what I''m trying to do is make the whole system of cubes rotate about a point that is not at the beginning of the first cube. I understand I have to use glTranslate() but the problem is If I translate then the whole system of cubes gets drawn at the translated spot.
So I want to first draw the cubes, then translate to a point, then rotate the system about that point.
-----------------------------"Q: How many Microsoft engineers does it take to change a light bulb?A: None. Bill Gates will just redefine Darkness(TM) as the new industry standard. "~wUn LoVe tO aLl ThE mAlUs OuT tHeRe!~
I won't write out the code (I don't believe in giving answers when alternatives are available.) but I can lead you in the right direction. Look up glPushMatrix() and glPopMatrix(), these two give you the flexibility I think you are looking for.


[edited by - walshb on May 20, 2003 4:23:19 PM]
I need no signature... well, alright.http://www.derzwerg.net
Heheh... u sound like my Math teacher . Actually, Thats all I''m looking for walsh. I''m trying my best to understand this, but at this point, its beyond me .
Would appreciate some pointers on where I would place this code. If I place it before the DrawCube function and place it in between a push/pop matrix then when I draw the cubes, it won''t have an effect because the pop function popped the previous rotated co-ordinates outside the matrix queue. If I place it after then, it still won''t have an effect.
-----------------------------"Q: How many Microsoft engineers does it take to change a light bulb?A: None. Bill Gates will just redefine Darkness(TM) as the new industry standard. "~wUn LoVe tO aLl ThE mAlUs OuT tHeRe!~
Hello all,
I've figured it out, but I don't know why it works... the way I think about it, it should have given similar results, but it doesnt... anyway here it is:
void DrawCube(float xPos,float yPos,float zPos){glPushMatrix();glTranslatef(xPos,yPos,zPos);glBegin(GL_QUADS);glColor3f(1.0f,0.0f,0.0f);//left faceglVertex3f(0.0f,0.0f,1.0f);glVertex3f(0.0f,0.0f,0.0f);glVertex3f(0.0f,1.0f,0.0f);glVertex3f(0.0f,1.0f,1.0f);glColor3f(0.0f,0.0f,0.0f);//bottom faceglVertex3f(0.0f,0.0f,1.0f);glVertex3f(1.0f,0.0f,1.0f);glVertex3f(1.0f,0.0f,0.0f);glVertex3f(0.0f,0.0f,0.0f);glColor3f(0.0f,0.0f,0.0f);//right faceglVertex3f(1.0f,0.0f,1.0f);glVertex3f(1.0f,0.0f,0.0f);glVertex3f(1.0f,1.0f,0.0f);glVertex3f(1.0f,1.0f,1.0f);//top faceglColor3f(0.0f,1.0f,0.0f);glVertex3f(0.0f,1.0f,1.0f);glVertex3f(1.0f,1.0f,1.0f);glVertex3f(1.0f,1.0f,0.0f);glVertex3f(0.0f,1.0f,0.0f);//front faceglColor3f(0.0f,0.0f,1.0f);glVertex3f(0.0f,0.0f,1.0f);glVertex3f(1.0f,0.0f,1.0f);glVertex3f(1.0f,1.0f,1.0f);glVertex3f(0.0f,1.0f,1.0f);//back faceglVertex3f(0.0f,0.0f,0.0f);glVertex3f(1.0f,0.0f,0.0f);glVertex3f(1.0f,1.0f,0.0f);glVertex3f(0.0f,1.0f,0.0f);glEnd();glPopMatrix();}int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing{static float angle=0.0f;float i;glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth BufferglLoadIdentity(); // Reset The Current Modelview MatrixglTranslatef(-3.0f,1.0f,-15.0f);glRotatef(angle,0.0f,1.0f,0.0f); //rotation about the beginning of the first cubeglTranslatef(-2.0f,0.0f,1.0f); //New line addedglColor3f(1.0f,0.3f,1.0f);for(i=0.0f;i<4.0f;i++)DrawCube(i,0.0f,0.0f);if(angle==360.0f)angle=0.0f;angle+=1.0f;return TRUE; // Everything Went OK}   



Could someone please explain how this works... shouldn't it still rotate about the beginning of the first cube?
Appreciate the effort in advance.
-----------------------------
"Q: How many Microsoft engineers does it take to change a light bulb?
A: None. Bill Gates will just redefine Darkness(TM) as the new industry standard. "
~wUn LoVe tO aLl ThE mAlUs OuT tHeRe!~

[edited by - mAdMaLuDaWg on May 20, 2003 10:38:32 PM]

[edited by - mAdMaLuDaWg on May 20, 2003 10:39:06 PM]
-----------------------------"Q: How many Microsoft engineers does it take to change a light bulb?A: None. Bill Gates will just redefine Darkness(TM) as the new industry standard. "~wUn LoVe tO aLl ThE mAlUs OuT tHeRe!~
Advertisement
glPushMatrix() saves the current matrix, and glPopMatrix() loads it again, (pushing it on to the stack, and poping it back...).

So, if I:

glTranslatef(0.0f, 0.0f, -1.0f);
glPushMatrix();

then I have pushed this matrix off the stack, and from now on, I''m modifying the next matrix on the stack...

So let''s pretend that I:

glTranslatef(0.0f, 2.0f, 0.0f);
DrawCube();

Then this cube will be drawn at 0.0f, 2.0f, -1.0f...

And then afterwards:

glPopMatrix();
DrawCube();

I pop the matrix onto the stack again...
And draw another cube, then this one will be at 0.0f, 0.0f, -1.0f.

I hope you understood, and that I''m not just babbling nonsense ...

If not, go read the red book.
I understood what the glPushMatrix and glPopMatrix does. I'm going to try and step through my code here to understand the effects(pls correct me if I am wrong):
glTranslatef(-3.0f,1.0f,-15.0f);
glRotatef(angle,0.0f,1.0f,0.0f); //rotation about y axis first glTranslatef(-2.0f,0.0f,1.0f); //New line added

Now the current coordinates at this point is (-5.0f,1.0f,14.0f) rotated at an 'angle'.

for(i=0.0f;i<4.0f;i++)
DrawCube(i,0.0f,0.0f);

First cube is drawn at (-5.0f,1.0f,14.0f), the second one at (-4.0f,1.0f,14.0f), the third one at (-3.0f,1.0f,14.0f), and the fourth one at (-2.0f,1.0f,14.0f) all about the rotated co-ordinate system.
Now the way I picture this in my head is that the whole system should rotate about (-5.0f,1.0f,14.0f) which happends to be the bottom of the left face of the first cube. But this is not what is happening. I'm trying to figure out why this happend.

What I'm ultimately trying to do is rotate these cubes about the point (-5.0f,1.0f,12.0f), still don't know how to get there ..


[edited by - mAdMaLuDaWg on May 21, 2003 12:49:35 PM]
-----------------------------"Q: How many Microsoft engineers does it take to change a light bulb?A: None. Bill Gates will just redefine Darkness(TM) as the new industry standard. "~wUn LoVe tO aLl ThE mAlUs OuT tHeRe!~

This topic is closed to new replies.

Advertisement