Jump to content
  • Advertisement
Sign in to follow this  
GosuDrew

Rotations

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

Hi, I've been reading the "Red Book" in hopes of learning OpenGL and do not seem to understand how transformations work. Say I wanted to rotate a sphere with radius of 10 units whose center lies at (20,0,0) about any axis that goes through its center. Would I always have to perform a glRotate() first before glTranslate()? If so, this seems like I would be making a lot of unnecessary calls to glTranslate() when I would like to just call glTranslate() once and then keep calling glRotate(). However, this seems only possible if the axis of rotation is (1,0,0). So, my question is: is it possible to have the sphere rotate about any axis through the center except (1,0,0) when it has already been translated? Any help would be appreciated. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Don't worry about multiple calls to glTranslate() and glRotate(). Typically, models are defined in local space (more or less centered at the origin) and then positioned in the world 'from scratch' each frame. Usually this involves rotating the model to the desired orientation, and then translating it to the desired position. As you probably know, OpenGL transforms occur in 'reverse order' relative to the code, so for your sphere, you might do something like this each frame:

glLoadIdentity();
glTranslate(x, y, z);
glRotate(angle, axis.x, axis.y, axis.z);

The reason only (20,0,0) and (1,0,0) is working for you is that with that particular series of transformations, order doesn't matter. However, once you change to some other axis of rotation, you don't get the behavior you expect. Try the code snippet above, and see if that doesn't fix the problem.

Share this post


Link to post
Share on other sites
Thanks, I got a sphere drawn by glut to rotate. But, I'm having trouble to get my own cube to rotate. I can't seem to set-up the right projection and viewing transformations in my reshape function. And, I don't understand why some of the sides disappear as it rotates with glOrtho(). Any ideas or suggestions?


#include <GL/glut.h>
#include <stdlib.h>

GLfloat angle = 0.0;

static GLfloat vertices[] = {1.0, 1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0,
1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0,
1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0,
-1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0,
1.0, -1.0, -1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0,
1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0};

static GLfloat colors[] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0,
1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0,
0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0,
1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0};


void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glColorPointer(3, GL_FLOAT, 0, colors);

glTranslatef(0.0, 0.0, -100.0);
glRotatef(angle, 1.0, 1.0, 1.0);
glScalef(25.0, 25.0, 25.0);

//draw cube
for (int i = 0; i < 24; i = i + 4)
{
glBegin(GL_QUADS);
glArrayElement(i);
glArrayElement(i+1);
glArrayElement(i+2);
glArrayElement(i+3);
glEnd();
}

glFlush();
glutSwapBuffers();
}

void spin_display()
{
angle = angle + .5;
if (angle > 360.0)
angle = angle - 360;
glutPostRedisplay();
}

void init (void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
}

void reshape (int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50, 50, -50, 50, 5, 100);
// glFrustum(-20.0, 20.0, -20.0, 20.0, 5.0, 100.0);
// glFrustum(-25.0, 25.0, -25.0, 25.0, 5.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// gluLookAt(10.0, 0.0, 0.0, 0, 0, 0 , 0.0, 0.0, 1.0);

}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(spin_display);
glutMainLoop();
return 0;
}





Share this post


Link to post
Share on other sites
The reason you're seeing parts of the cube being clipped is because they are going outside your far plane. You're translating 100 units into the screen, then drawing the cube there, so parts will pass over the far plane -- just increase the far plane value in glOrtho() to fix this.

Share this post


Link to post
Share on other sites
Thanks, that fixed the cube from being blacked-out. However, colors from other sides are mixing together and some sides contain triangles. Any ideas as to why it is doing this?
Thanks.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!