PDA

View Full Version : Help with gluLookat!! I lost my spaceships!!! Help



ShaolinMist
11-15-2011, 06:31 PM
Im new to opengl and I'm trying to understand gluLookat function. In my program I have to move the camera to where it looks as though I am in the spaceship, but Im afraid I've lost my 2 space ships in the 3d world!! Please help me understand how the parameters in gluLookat correspond to the object you want it to look at.
Here's my screen shot and my code (please excuse the comments...i've tried everything!)
[void drawShipOne()
{

glTranslatef(0.0, 0.0, -5.0); // eveything 5 units back into the scene, otherwise we won't see the primitive

glPushMatrix();
glColor3fv(ship_color);
glTranslatef(100.0,10.0,10.0);
glRotatef(Rotx, 1.0, 0.0, 0.0);
glRotatef(Roty, 0.0, 1.0, 0.0);
glutWireTorus(INNER, OUTER, NO_SIDES, NO_RINGS); // Render the primitive[Torus]

glRotatef(90.0, 1.0, 0.0, 0.0);
glRotatef(Roty, 0.0, 1.0, 0.0);
glutWireTorus(INNER, OUTER, NO_SIDES, NO_RINGS); // Render the primitive[Torus]
glPopMatrix();
//glutSwapBuffers();;

}][/void drawShipTwo()
{
glTranslatef(0.0, 0.0, -5.0); // eveything 5 units back into the scene, otherwise we won't see the primitive

glPushMatrix();
glColor3fv(ship_color);
glTranslatef(105.0,0.0,15.0);
glRotatef(Rotx, 1.0, 0.0, 0.0);
glRotatef(Roty, 0.0, 1.0, 0.0);
glutWireTorus(INNER, OUTER, NO_SIDES, NO_RINGS); // Render the primitive[Torus]

glRotatef(90.0, 1.0, 0.0, 0.0);
glRotatef(Roty, 0.0, 1.0, 0.0);
glutWireTorus(INNER, OUTER, NO_SIDES, NO_RINGS); // Render the primitive[Torus]
glPopMatrix();
//glutSwapBuffers();
}

void drawClouds()
{
glTranslatef(0.0, 0.0, -5.0);

glPushMatrix();
glTranslatef(30.0, 30.0, 30.0);
glColor3fv(cloud_colors[0]);
glutWireSphere(SPHERERADIUS, NO_SLICES, NO_STACKS);

glTranslatef(30.0, 170.0, 15.0);
glColor3fv(cloud_colors[1]);
glutWireSphere(SPHERERADIUS, NO_SLICES, NO_STACKS);

glTranslatef(80.0, 110.0, 12.0);
glColor3fv(cloud_colors[2]);
glutWireSphere(SPHERERADIUS, NO_SLICES, NO_STACKS);

glTranslatef(70.0, 60.0, 25.0);
glColor3fv(cloud_colors[3]);
glutWireSphere(SPHERERADIUS, NO_SLICES, NO_STACKS);

glTranslatef(90.0, 150.0, 13.0);
glColor3fv(cloud_colors[4]);
glutWireSphere(SPHERERADIUS, NO_SLICES, NO_STACKS);

glTranslatef(120.0, 80.0, 170.0);
glColor3fv(cloud_colors[5]);
glutWireSphere(SPHERERADIUS, NO_SLICES, NO_STACKS);

glTranslatef(150.0, 40.0, 100.0);
glColor3fv(cloud_colors[6]);
glutWireSphere(SPHERERADIUS, NO_SLICES, NO_STACKS);

glTranslatef(160.0, 170.0, 220.0);
glColor3fv(cloud_colors[7]);
glutWireSphere(SPHERERADIUS, NO_SLICES, NO_STACKS);
glPopMatrix();

}]


[void mydisplay (void) {

//glClearColor(1.0f, 0.0f, 0.0f, 1.0f); // Clear the background to red
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Clear the colour buffer (more buffers later on)
glMatrixMode(GL_MODELVIEW); // Switch back to the model view matrix, so that we can start drawing shapes correctly
//glColor3fv(ground_color);
// glOrtho(0.0, 0.0, 200.0, 200.0, 0, 0);
glPushMatrix();
// Draw sum clouds
glTranslatef(35.0, 0.0, 10.0);
glColor3f(1.0, 0.0, 0.0);
glutWireSphere(SPHERERADIUS, NO_SLICES, NO_STACKS);
//glLoadIdentity();
//gluLookAt(90.0, 0.0, -height+2 , 90.0, 101.0, 20.0, 0.0, 10, 10.0);
//glTranslatef(0.0, 0.0, -5.0);

drawShipOne();
drawShipTwo();
drawClouds();
glPopMatrix();
//glFlush();

glutSwapBuffers();
}


void reshape (int w, int h)
{
ww = w;
wh = h;

glViewport(0, 0, (GLsizei)w, (GLsizei)h); // Set our viewport to the size of our window
glMatrixMode(GL_PROJECTION); // Switch to the projection matrix so that we can manipulate how our scene is viewed
glLoadIdentity(); // Reset the projection matrix to the identity matrix so that we don't get any artifacts (cleaning up)
gluPerspective(FOVY, (GLfloat)w / (GLfloat)h, 1.0, 10000.0); // Set the Field of view angle (in degrees), the aspect ratio of our window, and the new and far planes
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(90.0, 0.0, -height+2 , 90.0, 101.0, 20.0, 0.0, 10, 10.0);
// drawing area

} ][/int main (int argc, char **argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB |GLUT_DEPTH); // Set upbasic display buffer (only single buffered for now)
glutInitWindowSize (800, 800); // Set the width and height of the window
glutInitWindowPosition (100, 100); // Set the position of the window
glutCreateWindow ("SpaceShip"); // Set the title for the window
myInit();
glutDisplayFunc(mydisplay); // Tell GLUT to use the method "display" for rendering

glutReshapeFunc(reshape); // Tell GLUT to use the method "reshape" for reshaping

glutKeyboardFunc(mykeyboard); //
//glutKeyboardUpFunc(keyUp); // Tell GLUT to use the method "keyUp" for key up events

glutMainLoop(); // Enter GLUT's main loop
]