Skip to main content
gamedev.net
🔒 Locked

Camera Moving Problem

Started by Asuralm Apr 12, 2006 at 12:29 PM 5 replies 450+ views
Original Post
Asuralm
Asuralm
Hi all: I drawed two objects in the scene. I want a rectangle to be ground which can't move. And another object can move and rotate. And I want the camera to be able to zoom in, zoom out and rotate etc. My code is like following:

float objectX = 0;
float objectY = 0;
float objectZ = 0;
float cameraX = 0;
float cameraY = 0;
float cameraZ = 100;
float lookAtX = 0;
float lookAtY = 0;
float lookAtZ = -1;

float xangle, yangle, zangle;

void init(void)
{
	glClearColor(0.0, 0.0, 0.0, 0.0); // Set display-window color to white.
	glShadeModel(GL_SMOOTH);
	glEnable(GL_DEPTH_TEST);		

	glMatrixMode(GL_MODELVIEW);	
	glClear(GL_COLOR_BUFFER_BIT);	
}
void display(void)
{	
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	
	glColor4f(1.0, 1.0, 0.0, 1.0);
	
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glTranslatef(objectX, objectY, objectZ);	
	glRotatef(xangle, 1, 0, 0);
	glRotatef(yangle, 0, 1, 0);
	glRotatef(zangle, 0, 0, 1);
	
	DrawObject1();

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	DrawGround();
	
	glFlush();	
	glutSwapBuffers();
	glutPostRedisplay();
}

void reshapeFcn(GLint newWidth, GLint newHeight)
{	
	glMatrixMode(GL_PROJECTION);	
	glLoadIdentity();

	glViewport(0, 0, newWidth, newHeight);

	if (newHeight == 0)
		newHeight = 1;
	gluPerspective(120.0, newWidth / newHeight, 1, 1000);

	gluLookAt(cameraX, cameraY, cameraZ, 
		lookAtX, lookAtY, lookAtZ, 
		0, 1, 0);
	
	glMatrixMode(GL_MODELVIEW);						
	glLoadIdentity();							

	winWidth = newWidth;
	winHeight = newHeight;		
}
void moveMeFlat(int i) {
	cameraX += i * lookAtX;	
	cameraZ += i * lookAtZ;
	glLoadIdentity();
	gluLookAt(cameraX, cameraY, cameraZ,
		      cameraX + lookAtX, cameraY + lookAtY, cameraZ + lookAtZ,
			  0.0f,1.0f,0.0f);
}
void SpecialKeyFunc(int key, int x, int y){
	switch (key) {
		case GLUT_KEY_UP : moveMeFlat(-1);break;
		case GLUT_KEY_DOWN : moveMeFlat(1);break;
	}
}

But when I press the up and down key, it doesn't work. It seems that the camera doesn't move at all. Could anybody tell me how to solve this please??? Thanks very much!!! [Edited by - Asuralm on April 12, 2006 2:12:06 PM]
Asura
nefthy
nefthy
First use [ source] [ /source] (without the space) tags around your code, it very hard to read otherwise.

Now, you are using glLoadIdentity before translating the object, which undoes all previous translations and rotations. You should do somthing like:

void draw() {  glClear(...);  glLoadIdentity();   glRotatef(-camera_rot_x, 1.0f, 0.0f, 0.0f);  glRotatef(-camera_rot_y, 0.0f, 1.0f, 0.0f);  glRotatef(-camera_rot_z, 0.0f, 0.0f, 1.0f);  glTranslate(-camera_x, -camera_y, -camera_z);  glRotatef(obj_rot_x, 1.0f, 0.0f, 0.0f);  glRotatef(obj_rot_y, 0.0f, 1.0f, 0.0f);  glRotatef(obj_rot_z, 0.0f, 0.0f, 1.0f);  glTranslate(obj_x, obj_y, obj_z);  //draw object}


also most of the glMatrixMode are unneeded. The matrix mode doesn't change until you call glMatrixMode
Asuralm
Asuralm
Thanks for your remind. In my scene, one object can be moved and the other rectangle simulates the ground. The camera can move along the ground to approaching the object or rotate with the object. I think from your code, when I move one object, the ground will be moved at the same time which isn't expected. Any better suggestions please???
Asura
nefthy
nefthy
use glPush() to store the Matrix before translating the moveable object and glPop() to restore it afterwards.

Or if you want to keep it simple draw the flor before the translation for the object.
Asuralm
Asuralm
I tried to draw the floor independently, but when I move the camera, only the object is moving but the floor remain static. It doesn't look like walking though.
Asura
nefthy
nefthy

void draw() {  glClear(...);  glLoadIdentity();   glRotatef(-camera_rot_x, 1.0f, 0.0f, 0.0f);  glRotatef(-camera_rot_y, 0.0f, 1.0f, 0.0f);  glRotatef(-camera_rot_z, 0.0f, 0.0f, 1.0f);  glTranslate(-camera_x, -camera_y, -camera_z);  //don't draw before doing the camera translation    glPushMatrix();  //translate the floor to its position   //draw floor  glPopMatrix();    glPushMatrix();  glRotatef(obj_rot_x, 1.0f, 0.0f, 0.0f);  glRotatef(obj_rot_y, 0.0f, 1.0f, 0.0f);  glRotatef(obj_rot_z, 0.0f, 0.0f, 1.0f);  glTranslate(obj_x, obj_y, obj_z);  //draw object  glPopMatrix();}
Asuralm
Asuralm
I have solved the problem. nefthy, thank you very much for your replies and patience!!! You are gentleman!!!
Asura

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.