Archived

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

drdarkon

glPushMatrix(); glPopMatrix(); moving a triangle

Recommended Posts

Hello all, I have recently gotten into OpenGL programming. I have understood most stuff up until now and I kinda understand how these work. I think I understand that they will take the current matrix and put it down to the #2 matrix on the stack and so you can manipulate the matrix you are currently working with, and then pop back to your previous matrix. I am not sure if I got this right. Anyway, this is giving me a lot of trouble because I am not sure how exactly it is working. I think my problem may be related to the matrix I am using. I tried making a little demo that would allow you to rotate a triangle and the move in the direction that the triangle was facing. I assumed all I would have to do was rotate the triangle and then move in the triangle''s z direction as it would already be turned. What is happenin: Instead of the triangle rotating around its own axis it is rotating around what appears to be the origins axis. When I rotate it goes all they way around and off the screen and then rotates all the way back. I am not sure what I am doing wrong. How do I set this up so that my triangle rotates on its axis and goes foward when you want it to. Do I need to use Push and Pop matrix here? - Thanks in advance

Share this post


Link to post
Share on other sites
It's all to do with the order in which you perform your matrix manipulations:

Rotating your triangle then translating it will have a different final position/orientation for the triangle to translating it then rotating it.

You're performing the rotation and translation in the wrong order, swap them over and your problem should be solved.

[edited by - poontardis on July 16, 2002 7:25:40 PM]

Share this post


Link to post
Share on other sites
Okay, I swapped them so now I translate and then rotate. But now my problem is with the translation. Now the triangle rotates on its own axis but it now translates along the origins z axis instead of its own axis. It seems like it is one or the other. Oh geez... any suggestions on how I can get the triangle to both translate and rotate on its own axis?

- thanks

Share this post


Link to post
Share on other sites
When you create your triangle, let's presume you use local co-ordinates for it (e.g. (0,1,0), (-1, -1, 0), (1, 1, 0) rather than world co-ordinates (10, 23, -100), (9, 21, -100), (11, 21, -100)).

Next translate it so that it's in the correct position in the world. (e.g. glTranslatef(0.0f, 0.0f, -100.0f).

Next rotate it to the correct angle on its/the world axis (they're both the same just now as we haven't applied any rotations) (e.g. glRotatef(0.1f, 0.0f, 1.0f, 0.0f).

Now translate it along its own axis (this is now different to the world axis as we've applied a rotation to the current matrix) using glTranslatef again with the appropriate translation.


[edited by - poontardis on July 17, 2002 8:28:52 AM]

Share this post


Link to post
Share on other sites
hmmm...

well I tried that also. But it still isn''t working. This is my code. Maybe this will help. (I am now trying to move a colored cube.)



// do rendering here
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear screen and depth buffer
glLoadIdentity();

if(KEYDOWN(VK_RIGHT))
{
angle -= 1.0f;
}

if(KEYDOWN(VK_LEFT))
{
angle+=1.0f;
}
if(KEYDOWN(VK_UP))
{
z_coord -=0.1f;
}
if(KEYDOWN(VK_DOWN))
{
z_coord +=0.1f;
}

if (angle >= 360.0f) // if we''ve gone in a circle, reset counter
angle = 0.0f;


glTranslatef(x_coord,y_coord,z_coord);
glRotatef(angle, 1.0f,1.0f,1.0f); // rotate along y-axis




glColor3f(0.0f,1.0f,0.0f);
glBegin(GL_POLYGON);
glVertex3f(0.0f, 0.0f, 0.0f); // top face
glVertex3f(0.0f, 0.0f, -1.0f);
glVertex3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.0f, 0.0f, 0.0f);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(0.0f, 0.0f, 0.0f); // front face
glVertex3f(-1.0f, 0.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glVertex3f(0.0f, -1.0f, 0.0f);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(0.0f, 0.0f, 0.0f); // right face
glVertex3f(0.0f, -1.0f, 0.0f);
glVertex3f(0.0f, -1.0f, -1.0f);
glVertex3f(0.0f, 0.0f, -1.0f);
glColor3f(0.0f,1.0f,1.0f);
glVertex3f(-1.0f, 0.0f, 0.0f); // left face
glVertex3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glColor3f(1.0f,1.0f,0.0f);
glVertex3f(0.0f, 0.0f, 0.0f); // bottom face
glVertex3f(0.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glColor3f(1.0f,1.0f,1.0f);
glVertex3f(0.0f, 0.0f, 0.0f); // back face
glVertex3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(0.0f, -1.0f, -1.0f);
glEnd();



SwapBuffers(g_HDC);


I have tried doing translate, rotate, translate... it doesn''t work. It rotates around an axis x units out from its own, where x = the amount of the second translation. Oh man... Well, any help is greatly appreciated

- Jesse Barksdale

Share this post


Link to post
Share on other sites
quote:
Original post by drdarkon



glTranslatef(x_coord,y_coord,z_coord);
glRotatef(angle, 1.0f,1.0f,1.0f); // rotate along y-axis




for rotating around the y-axis you need to call
glRotatef(angle,0.0,1.0,0.0);
quote:



glColor3f(0.0f,1.0f,0.0f);
glBegin(GL_POLYGON);
glVertex3f(0.0f, 0.0f, 0.0f); // top face
glVertex3f(0.0f, 0.0f, -1.0f);
glVertex3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.0f, 0.0f, 0.0f);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(0.0f, 0.0f, 0.0f); // front face
glVertex3f(-1.0f, 0.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glVertex3f(0.0f, -1.0f, 0.0f);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(0.0f, 0.0f, 0.0f); // right face
glVertex3f(0.0f, -1.0f, 0.0f);
glVertex3f(0.0f, -1.0f, -1.0f);
glVertex3f(0.0f, 0.0f, -1.0f);
glColor3f(0.0f,1.0f,1.0f);
glVertex3f(-1.0f, 0.0f, 0.0f); // left face
glVertex3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glColor3f(1.0f,1.0f,0.0f);
glVertex3f(0.0f, 0.0f, 0.0f); // bottom face
glVertex3f(0.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glColor3f(1.0f,1.0f,1.0f);
glVertex3f(0.0f, 0.0f, 0.0f); // back face
glVertex3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(0.0f, -1.0f, -1.0f);
glEnd();



SwapBuffers(g_HDC);


I have tried doing translate, rotate, translate... it doesn't work. It rotates around an axis x units out from its own, where x = the amount of the second translation. Oh man... Well, any help is greatly appreciated

- Jesse Barksdale

Use GL_TRIANGLES in stead of GL_POLYGON. What you are drawing here is one polygon with lot's of vertices. Which probably isn't working because it's unlikeley all those vertices lie on the same plane. If you use GL_TRIANGLES a triangle will be drawn with every 3 vertices. Note the S at the end of GL_TRIANGLES, and the lack of an S at the end of GL_POLYGON.

Shit just realized you're drawing quads, so you need to use GL_QUADS (you could of course split your quad's up in triangles)

Also there's a forum named OpenGL, I suggest you go there next time (or go to NeHe's forum).


[edited by - George2 on July 17, 2002 4:49:55 PM]

Share this post


Link to post
Share on other sites