Camera movement not working

Hi All,

I am trying to move my camera and redisplay my scene. I am using a global variable called Camera2Origin for the Z location of the camera in gluLookAt.

In the glutSpecialKeys() function I am using the UP arrow to decrease Camera2Origin, the DOWN arrow increases Camera2Origin.

ex. gluLookAt(0,0,Camera2Origin,0,0,0,0,1,0);

The problem is that while function is being called, no change in the camera view is taking place.

Thanks for any replies,
jpummill

Source:
//---------------------------------------------
//-- Test to calculate the GL screen units –
//---------------------------------------------

#include <iostream.h>
#include <iomanip.h>
#include <gl/glut.h> // The GL Utility Toolkit (Glut) Header
#include <math.h>

//------------------------
//-- Global Variables –
//------------------------
const double PI = 3.14159265358979;
const double FOV = 40.0;
double Camera2Origin = 35.0;
double AspectRatio = 1.0;
double yMax = 0.0;
double yMin = 0.0;
double xMin = 0.0;
double xMax = 0.0;
double verIncr = 0.0;
double horIncr = 0.0;

//---------------------------
//-- Function Prototypes –
//---------------------------
double deg2rad ( double );
double getyMax (double, double);

//---------------------
//-- Init Function –
//---------------------
void init ( GLvoid )
{
glShadeModel(GL_FLAT); // Enable Smooth Shading
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glEnable ( GL_COLOR_MATERIAL );
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
}

//-----------------------------
//-- GLUT Display Function –
//-----------------------------
void display ( void )
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Modelview Matrix

gluLookAt( 0, 0, Camera2Origin, 0, 0, 0, 0, 1, 0 );

glColor3f(0.0f,0.0f,1.0f);

glRotatef(90, 1.0, 0.0, 0.0);
glutWireSphere(3, 24, 24);

glutSwapBuffers ( );
}

//------------------------------------
//-- GLUT Reshape Window Function –
//------------------------------------
void reshape ( int w, int h )
{
glViewport ( 0, 0, w, h );
glMatrixMode ( GL_PROJECTION ); // Select The Projection Matrix
glLoadIdentity ( ); // Reset The Projection Matrix
//------------------------------------------------
//-- Calculate the Aspect Ratio of the Window –
//------------------------------------------------
if ( h==0 )
AspectRatio = ( float ) w;
else
AspectRatio = ( float ) w / ( float ) h;
//------------------------------------------------------------------------
//-- Calculate the Max and Min GL units at the Camera Target location –
//------------------------------------------------------------------------
yMax = getyMax(FOV, Camera2Origin);
yMin = -yMax;
xMax = yMax * AspectRatio;
xMin = -xMax;

gluPerspective ( FOV, AspectRatio, 0.0, 5.0 );

glMatrixMode ( GL_MODELVIEW ); // Select The Model View Matrix
glLoadIdentity ( ); // Reset The Model View Matrix
}

//---------------------------------------
//-- GLUT Standard Keyboard Function –
//---------------------------------------
void keyboard ( unsigned char key, int x, int y ) // Create Keyboard Function
{
switch ( key ) {
case 27: // When Escape Is Pressed…
exit ( 0 ); // Exit The Program
break; // Ready For Next Case
default: // Now Wrap It Up
break;
}
}

//--------------------------------------
//-- GLUT Special Keyboard Function –
//--------------------------------------
void xkeyboard ( int xkeys, int x, int y ) // Create Special Function (required for arrow keys)
{
switch ( xkeys ) {
case GLUT_KEY_RIGHT: // When Right Arrow Is Pressed.
glutFullScreen ( ); // Go Into Full Screen Mode
break;
case GLUT_KEY_LEFT: // When Left Arrow Is Pressed.
glutReshapeWindow ( 640, 480 ); // Go Into A 640 By 480 Window
break;
case GLUT_KEY_UP: // When Up Arrow Is Pressed.
if ( Camera2Origin > 10 )
{
Camera2Origin -= 0.2;
cout << Camera2Origin << endl;
}
break;
case GLUT_KEY_DOWN: // When Down Arrow Is Pressed.
if ( Camera2Origin < 40 )
{
Camera2Origin += 0.2;
cout << Camera2Origin << endl;
}
break;
default:
break;
}
}

//---------------------
//-- Main Function –
//---------------------
void main ( int argc, char** argv ) // Create Main Function For Bringing It All Together
{
glutInit ( &argc, argv ); // Necessary GLUT init function.
glutInitDisplayMode ( GLUT_RGB | GLUT_DOUBLE ); // Display Mode
glutInitWindowSize ( 640, 480 ); // If glutFullScreen wasn’t called this is the window size
glutCreateWindow ( “Move Camera With Arrow Keys” ); // Window Title (argv[0] for current directory as title)
init ();
glutDisplayFunc ( display ); // Matching Earlier Functions To Their Counterparts
glutReshapeFunc ( reshape );
glutKeyboardFunc ( keyboard );
glutSpecialFunc ( xkeyboard );
glutMainLoop ( ); // Initialize The Main Loop
}

//-------------------------------------------------------------------------------------------------------------
//-- OTHER FUNCTIONS –
//-------------------------------------------------------------------------------------------------------------

//----------------------------------
//-- Convert degrees to radians –
//----------------------------------
double deg2rad ( double x )
{
double radians = 0.0;

radians = x * PI / 180;
return(radians);
}

//---------------------------------------------------------------------
//-- Determine Max Y value that is viewable at the glLookAt Target –
//---------------------------------------------------------------------
double getyMax(double angle, double length)
{
double yMax = 0.0;

yMax = tan(deg2rad(angle) / 2) * length;
return(yMax);
}

When you wanna get a redraw, use

glutPostRedisplay();

Currently, although you process the keyboard input you don’t get it to re-display your screen - thus the camera does not move…

That was it!!!

Thanks a lot for the help.

I don’t know why I forgot about that function but this is probably the third program where I have tried to get this functionality to work and couldn’t figure out how.

Thanks again,
jpummill