OpenGL camera control

Started by
2 comments, last by Paddon 17 years, 11 months ago
I am new to graphics programming and have been using OpenGL and the GLUT library to fufil my needs. I have run into a problem however when tryin to orient my camera such that a crsytal lattice can be seen properly. What I am trying to do is read in points and plot them as spheres (using glut) in 3-D space for a visualization of particles for my physics research. Reading in points and displaying the spheres was no problem (mind you I am new to openGL programming) but I can't seem to set my camera up properly no matter how much I fiddle with it. Heres the case, I am plotting an fcc crystal which is contained in a 6x6x18 box, the bottom left corner of said box being located at the origin. I want to be able to get a good view of this box. I have read various tutorials on camera control in OpenGL and in GLUT and have tried both gluLookAt(); and just translating and rotating; so I am not sure if its just my 3-D math or my understanding of the functions that is off but for some reason I am always zoomed in too far, or can't see anything, the camera never seems to be oriented where I expect. Here is my most recent display function, I was under the assumption that this would locate my camera at (10,10,0) and point it towards the origin. Now I know for a fact everything gets drawn, and drawn correctly because from the various angles I have experimented with (none which give me close to what I want) the crystal structure looks correctly ordered. I was hoping someone could maybe provide a solution or even a good tutorial on camera control which could help me understand how to orient things in openGL. (I have tried nehe.gamedev, along with many others)

void display(void)
{    
     
//****** SET THE SYSTEM UP TO BE A 6X6X18 BOX ********************************//     
     glMatrixMode(GL_PROJECTION);
     glOrtho(0.0,6.0,0.0,6.0,0.0,25.0);

//****** CLEAR BUFFERS **************************************************    *****//     
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
//****** SETUP THE CAMERA ANGLE*********************************************    **// 
     
     glMatrixMode(GL_PROJECTION);

     glLoadIdentity();
     gluLookAt(0.0,0.0,0.0,10.0,10.0,0.0,0.0,1.0,0.0);

                                           
     glMatrixMode(GL_MODELVIEW); 
                                 
//****** DRAW THE PARTICLES*****************************************    **********//     
    for (int i=0;i<NUM_PARTICLES;i++){DrawParticle(XYZ.x,XYZ.y,XYZ.z);}

    
    glFlush(); 
}


Thanks - J. Paddon [Edited by - Paddon on May 25, 2006 12:24:21 PM]
Jeff PaddonUndergraudate Research AssistantPhysics DepartmentSaint Francis Xavier University
Advertisement
1)You have the gluLookAt function all wrong: The first 3 parameters give the position of the "camera", the next 3 give the reference point(where the "camera" looks at).

2)Camera operations must be done in the modelview matrix, not the projection.

So it should be:

//****** SET THE SYSTEM UP TO BE A 6X6X18 BOX ********************************//          glMatrixMode(GL_PROJECTION);     glLoadIdentity();     glOrtho(0.0,6.0,0.0,6.0,0.0,25.0);//****** CLEAR BUFFERS **************************************************    *****//         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    //****** SETUP THE CAMERA ANGLE*********************************************    **//           glMatrixMode(GL_MODELVIEW);     glLoadIdentity();     gluLookAt(10.0,10.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0);                                                                        //****** DRAW THE PARTICLES*****************************************    **********//         for (int i=0;i<NUM_PARTICLES;i++){DrawParticle(XYZ.x,XYZ.y,XYZ.z);}


Note that with glOrtho() you'll get an orthographic projection. If you want perspective, you must use glFrustum() or more preferably gluPerspective().
So, what exactly is the difference between Ortho and Perspective. When I run it with your code, all I see is a grid of spheres on the screen. It almost looks as though its 2D, maybe this is still due to how I have the camera oriented but I am assuming if the camera is located at 10, 10 and looking at the origin it would give me almost an isometric view, at the very least I figure I should be able to see the top edge of the box but this is not the case.
Also if I comment out the ortho part and run the program it is almost as though the camera nor model has moved at all, and the camera is still centered at the origin looking down the -z axis.
Jeff PaddonUndergraudate Research AssistantPhysics DepartmentSaint Francis Xavier University
Looks as though I got it too work, I was reloading the Identity when I shouldn't have been, oh well......I have posted a screen shot of the output and it is oriented exactly as I wanted it to be however the lighting could use some work.....any suggestions on how to light this up a bit better? It would be nice just ot have the whole seen as bright as possible.

http://www.stfx.ca/people/x2005/x2005fhc/fcc.jpg

This is my current lighting set-up.

void myInit(void){//****** Initalize Lighting and Turn on Lights for the scene *****************//    glEnable(GL_LIGHTING);                                                    //                          glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER,1.0);                           //                              GLfloat lightIntensity[]={1.0,1.0,1.0,1.0};                           //                       GLfloat lightSpecular[]={1.00,1.0,1.0, 1.0};                           //                       GLfloat lightPosition[]={-10.0,5.0,10.0,1.00};                           //                        GLfloat lightAmbient[]={0.50,0.50,0.50,1.0};                           //                                                                              //       glLightfv(GL_LIGHT0, GL_DIFFUSE, lightIntensity);                      //       glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpecular);                      //       glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);                      //       glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);                        //    glEnable(GL_LIGHT0);                                                      //    glDepthFunc(GL_LEQUAL);                                                   //      glEnable(GL_DEPTH_TEST);                                                  //    }                                                                             ////****************************************************************************//


[Edited by - Paddon on May 25, 2006 12:08:17 PM]
Jeff PaddonUndergraudate Research AssistantPhysics DepartmentSaint Francis Xavier University

This topic is closed to new replies.

Advertisement