I was trying to move my camera along the red cube .. so i came across the gluLookAt function, but i am not able to get through this function as to what all it requires ... the content on the Documentation didn't quite help. it will be very helpful if anyone can help me out with the problem in my code and explain the gluLookAt .

#include <GL/gl.h>
#include <GL/glut.h>
#include<iostream>

using namespace std;
GLfloat angle = 0.0; //set the angle of rotation
float Cx=0.0,Cy=0.0,Cz=0.0;

typedef struct Car
{
	float x,y,z;
	float angle;
	float zvelocity,xvelocity;
}Car;

Car car;

void keyboard(unsigned char key,int x,int y)
{
	if(key =='w')
		Cz--;
	if( key == 's')
		Cz++;
	if(key =='a')
		Cx--;
	if(key == 'd')
		Cx++;
	if(key==' ')
	angle ++;
}

void cube (void) {
	glPushMatrix();
	glRotatef(angle,0,1,0);
	glTranslatef(Cx,Cy,Cz); 
	gluLookAt(0,0,Cz+5,0,0,0, 0.0, 1.0, 0.0);
	glColor3f(1.0, 0.0, 0.0);
	glutSolidCube(2);
	glPopMatrix();
}

void display (void) {
	glClearColor (1.0,1.0,1.0,1.0);
	glClear (GL_COLOR_BUFFER_BIT);
	glLoadIdentity();
	glPushMatrix();
	glTranslatef(2,0,-15);
	glColor3f(0,1,0);
	glutSolidCube(2);
	glPopMatrix();
	glPushMatrix();
	glTranslatef(2,0,5);
	glColor3f(0,1,0);
	glutSolidCube(2);
	glPopMatrix();
	cube();
	glutSwapBuffers();
}

void reshape (int w, int h) {
	glViewport (0, 0, (GLsizei)w, (GLsizei)h);
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
	gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0)
		;
	glMatrixMode (GL_MODELVIEW);
}

int main (int argc, char **argv) {
	glutInit (&argc, argv);
	glutInitDisplayMode (GLUT_DOUBLE); //set up the double 
	glutInitWindowSize (500, 500);
	glutInitWindowPosition (100, 100);
	glutCreateWindow ("A basic OpenGL Window");
	glutDisplayFunc (display);
	glutIdleFunc (display);
	glutKeyboardFunc(keyboard);
	glutReshapeFunc (reshape);
	glutMainLoop ();
	return 0;
}

Ok so you have 2 options here if you want to "move around the object".

1. Use gluLookAt() to change the camera position and move it around the object. If the ojbect is centered in the middle of your virtual window you can use something like this:

/* Spherical coordinates to cartesians.. Google for the formula 
   Note that you might have to switch some of the coordinates depending if your up vector is Y or Z
*/
eyeX = distance * cos(theta) * sin(phi);
eyeY = distance * sin(theta) * sin(phi);
eyeZ = distance * cos(phi);

objX = 0;
objY = 0;
objZ = 0;

gluLookAt(eyeX, eyeY, eyeZ, objX, objY, objZ, 0, 1, 0)

With this solution you need to change the angle theta and phi when you move the mouse and you can add keyboard shortcuts to change the distance value that will act as a zoom. Note that you don't need any glTranslate or glRotate if you use this technique.

2. Use only glTranslate and glRotate to rotate the object and move around it while not moving the camera. You won't need gluLookAt here. The equivalent of the code I posted above using glRotate and glTranslate would look something like this but note that it also depends on what you use up vector and on which plane you drew the object (most tutorials / people use XZ, I prefer XY and Z as up vector)

glTranslatef(0, 0, -distance);

glRotatef(phi, -1, 0, 0);
glRotatef(theta, 0, 0, -1);
			
glRotatef(90, 0 , 0 , -1);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.