Advertisement

translate problem

Started by July 13, 2003 10:23 AM
8 comments, last by kalok 21 years, 7 months ago
I want to translate an object to an absolute coordinate(referring with 0,0,0) without affecting other objects, I use glLoadIdentity() before glTranslate it, then I apply gluLookAt() for viewing, but unfortunately, it does not work. The following simple program demostrate the problem, a cube is located at absolute coordinate (0,0,-2) permanently, a teapot is located dynamicly based on user input, the teapot should be a certain distance in front of camera, and the cube should move forward/backward based on user input, but it does not work what I expect, can anyone tell me why? #include <stdio.h> #include <string.h> #include <stdlib.h> #include <GL/glut.h> float deltaz=-1; float distance=-1.3; static void init(void) { glClearColor(0.5, 0.5, 0.5, 1.0); glColor3f(1.0, 1.0, 1.0); } static void reshape(int w, int h) { if(h == 0) h = 1; glViewport(0, 0, (GLint)w, (GLint)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective( 65.0, (GLfloat) w / (GLfloat) h, 0.1, 300.0 ); glMatrixMode(GL_MODELVIEW); //glLoadIdentity(); gluLookAt( 0.0f, 0.0, 3.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f ); } static void key(unsigned char key, int x, int y) { switch (key) { case 27: exit(0); } } static void specialkey(int key, int x, int y) { switch (key) { case GLUT_KEY_UP: distance += 0.3; //glutPostRedisplay(); break; case GLUT_KEY_DOWN: distance -= 0.3; //glutPostRedisplay(); break; case GLUT_KEY_LEFT: //glutPostRedisplay(); break; case GLUT_KEY_RIGHT: //glutPostRedisplay(); break; } } static void display(void) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glColor3f(1.0, 0.0, 0.0); glLoadIdentity(); glTranslatef(0.0, 0.0, distance); glutSolidTeapot(0.1); glLoadIdentity(); glColor3f(0.0, 0.0, 1.0); glTranslatef(0.0, 0.0, -2.0); glutSolidCube(0.2); glLoadIdentity(); printf("distance: %f\n", distance); printf("distance+deltaz: %f\n", distance+deltaz); gluLookAt( 0.0f, 0.0, distance, 0.0f, 0.0f, distance+deltaz, 0.0f, 1.0f, 0.0f ); glutPostRedisplay(); glutSwapBuffers(); } void main(int argc, char **argv) { glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowSize(500, 500); glutCreateWindow("Ka Lok"); init(); glutReshapeFunc(reshape); glutKeyboardFunc(key); glutSpecialFunc(specialkey); glutDisplayFunc(display); glutMainLoop(); }
Try using glPushMatrix() and glPopMatrix().

i.e. something like this (note: I didn''t test this):

static void display(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glLoadIdentity();
glPushMatrix();
printf("distance: %f\n", distance);
printf("distance+deltaz: %f\n", distance+deltaz);
gluLookAt( 0.0f, 0.0, distance, 0.0f, 0.0f, distance+deltaz, 0.0f, 1.0f, 0.0f );
glPopMatrix();

glPushMatrix();
glColor3f(1.0, 0.0, 0.0);
glTranslatef(0.0, 0.0, distance);
glutSolidTeapot(0.1);
glPopMatrix();

glPushMatrix();
glColor3f(0.0, 0.0, 1.0);
glTranslatef(0.0, 0.0, -2.0);
glutSolidCube(0.2);
glPopMatrix();

glutPostRedisplay();
glutSwapBuffers();
}
Advertisement
Thanks for telling me.

I tried, but it did not work.
Actually, I shouldn''t have included the GLPushMatrix/GLPopMatrix around the GLULookAt line. Try this (notice the distance was changed to a hard-coded number on the teapot):

static void display(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glLoadIdentity();
printf("distance: %f\n", distance);
printf("distance+deltaz: %f\n", distance+deltaz);
gluLookAt( 0.0f, 0.0, distance, 0.0f, 0.0f, distance+deltaz, 0.0f, 1.0f, 0.0f );

glPushMatrix();
glLoadIdentity();
glColor3f(1.0, 0.0, 0.0);
glTranslatef(0.0, 0.0, -1.3);
glutSolidTeapot(0.1);
glPopMatrix();

glPushMatrix();
glColor3f(0.0, 0.0, 1.0);
glTranslatef(0.0, 0.0, -2.0);
glutSolidCube(0.2);
glPopMatrix();

glutPostRedisplay();
glutSwapBuffers();
}
Thank you very much.
When I change the teapot to a dynamic position as below, it does not work, how can I solve the problem.

static void display(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glLoadIdentity();
printf("distance: %f\n", distance);
printf("distance+deltaz: %f\n", distance+deltaz);
gluLookAt( 0.0f, 0.0, distance, 0.0f, 0.0f, distance+deltaz, 0.0f, 1.0f, 0.0f );

glPushMatrix();
glLoadIdentity();
glColor3f(1.0, 0.0, 0.0);
glTranslatef(0.0, 0.0, distance);
glutSolidTeapot(0.1);
glPopMatrix();

glPushMatrix();
glColor3f(0.0, 0.0, 1.0);
glTranslatef(0.0, 0.0, -2.0);
glutSolidCube(0.2);
glPopMatrix();

glutPostRedisplay();
glutSwapBuffers();
}
Advertisement
Maybe I misunderstood your problem. You are dynamically moving the camere and, along with it, moving the teapot (i.e. the camera is following the teapot as you move).

If you also want to be able to change the distance between the camera and teapot, I think you need another variable (probably controlled by seperate keys on the keyboard) that defines how far the teapot is from the camera.
What I am going to do is to render a racing car on the scene. The position of the car is calculated dynamicly based on the movement, and the camera should be behind the car at a certain distance. I want to translate the car to the new position using glLoadIdentity() and glTranslatef() with gluLookAT() for camera, but I can not make it.
I have a demo that might help you - it''s the bottom one on my page.

What I do, is I use glTranslate and glRotate only to move the camera, and then never again. After moving the world''s coordinate system to emulate the camera, I just put everything at a certain place, i.e. 10,10 then 10,11 then 10,12 etc. to simulate the motion. The CRobot class is an exception to that but I think I''m going to rewrite it, I don''t like it the way it is.

I would recommend that you keep one set of varibles that''s the camera''s coordinates and one set of variables that''s the car''s coordinates. Translate to the camera''s coordinates then draw the car at its coordinates. Takes a little experimentation but helps you stay consistent.

Hope this helps
Joe

Love means nothing to a tennis player

My nothing-to-write-home-about OpenGL webpage. (please pardon the popups!)

there was an error as below when I compiled your program, where can I download the nafxcwd.lib?

Linking...
LINK : fatal error LNK1104: cannot open file "nafxcwd.lib"
Error executing link.exe.

This topic is closed to new replies.

Advertisement