🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

glRotate() and glTranslate()

Started by
5 comments, last by Hector 24 years, 7 months ago
Your problem is you aren't rotating and translating the camera at all, you are doing it to the modelview matrix.

I don't know glut well, but I'd image that reshape would be called as part of the initialization, and it leaves the modelview matrix as the last one (Im not sure if gluLookAt switches to projection, or is working with the modelview one as well)

To fix it, push the basic projection matrix onto the stack (it isnt really needed, but whatever), and rotate and translate that. Then if you pushed it, pop it as normal, so basically you need a

glMatrixMode(GL_PROJEECTION);

where the push is in display()

Advertisement
You need to use the glTranslate() before the glRotate(). Alot of people around here say gl does this "backwords" compared to other systems but it makes alot of sense if you think about it. While you're sitting there at your computer ROTATE yourself 90 deg. to the right. Now TRANSLATE yourself(while pointing the same direction) forward. You're actually moving to the right of your computer instead of towards it.

To keep track of the absolute coord. you're at you'll also need to use a vector or trig when you move with the keyboard function. Just switching the glTranslate() and glRotate() in the above code will work out fine until you move and then turn and then try to move forward again.

Since you're moving the camera, you want to go in the opposite direction you're actually moving. If you step forward one unit, a coordinate that was one unit from the origin should now be on it, so it needs to be translated -1 unit back so that the translated coordinate is on the origin.

Alternately, You might want to use gluLookAt() to set up the camera. the glu functions just call a string of gl functions, but it makes it a little more obvious in your code that you're moving the camera.

-the logistical one-http://members.bellatlantic.net/~olsongt
I had this same problem before and solved it by using gluLookAt() only, no glTransate*() or glRotate*(). I also used sin() and cos() functions in math.h. Do a search on my call name JD on this forum and you'll see some solutions to your problem. Good luck
Thanks JD. That was exactly what I was looking for. I got it to work in 3 min. Do you know where VirtualNext got those trig formulas from? I was looking every where for something like that.
Glad to hear from you and more importantly congratulation on solving the problem. I don't know where he got those formulas, but I personally was banging my head against my keyboard for 2 days trying to figure out the gluLookAt() routine. The way I think about it is that my feet are the eye_x, eye_y, eye_z coords, and my head is lookat_x, lookat_y, and lookat_z, which form a vector or direction I am looking at. The eye_* coords are the base or starting points i.e. position of the lookat* vector and the lookat* coords are decomposed x, y, and z coords of the vector. So I made a routine that first moves you for example to the right along the positive x-axis then up along negative z-axis during one strike of the up arrow key, then the unit vector moves the same amount to the right and up since if it didn't you would walk over your old lookat* coords, you want those coords to move the same x, y, and z direction as your eye* coords but one unit farther. Using this, then it was easy to configure my program to sidestep, move backwards, turn around myself, and move head up and down. Well, good luck in your programming
I am trying to use the mouse to look left and right and the keyboard to move forward, backwards, left and right. I used glTranslate() to move with the keyboard but I am having trouble looking left and right with the mouse. Whenever I use the mouse to look left and right, the object rotates about the origin instead of my view. This is not what I want. I want my view to rotate left and right as if I am turning my head. Is there a way to temporarily move the origin to the camera position so that I can use glRotate() to achieve the head turning affect?

#include
#include

GLfloat objectx = 0.0;
GLfloat objecty = 0.0;
GLfloat objectz = -5.0;
GLint rotation = 0;
GLint motionx = 250;
GLint motiony = 250;

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

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);

glPushMatrix();
glRotatef((GLfloat) rotation, 0.0, 1.0, 0.0);
glTranslatef(objectx, objecty, objectz);

glutWireCube(1.0);
glPopMatrix();
glutSwapBuffers();
}

void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat) w/ (GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 0.0, 0.0, 0.0, -5.0, 0.0, 1.0, 0.0);
}

void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 'i':
objectz += 0.1;
glutPostRedisplay();
break;
case 'k':
objectz -= 0.1;
glutPostRedisplay();
break;
case 'j':
objectx += 0.1;
glutPostRedisplay();
break;
case 'o':
objectx -= 0.1;
glutPostRedisplay();
break;
default:
break;
}
}

void motion(int x, int y)
{
if (motionx > x)
rotation = (rotation - 1) % 360;
if (motionx < x)
rotation = (rotation + 1) % 360;
glutPostRedisplay();

motionx = x;
motiony = y;
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMotionFunc(motion);
glutMainLoop();
return 0;
}

Hello Hector

I bought the book Opengl Superbible : The Complete Guide to Opengl Programming for Windows Nt and Windows 95. The examples on the CD are very good and the book is really worth to get bought.

VirtualNext

This topic is closed to new replies.

Advertisement