title: OpenGL Keyboard Camera Controls
body: <p>I have been following this tutorial series for <a href="http://en.wikipedia.org/wiki/OpenGL" rel="nofollow noreferrer">OpenGL</a>: <a href="http://en.wikipedia.org/wiki/OpenGL_Utility_Toolkit" rel="nofollow noreferrer">GLUT</a>:</p>
  
  <pre><code>http://www.lighthouse3d.com/opengl/glut/
  </code></pre>
  
  <p>I have reached the stage of implementing camera controls using the keyboard:</p>
  
  <pre><code>http://www.lighthouse3d.com/opengl/glut/index.php?8
  </code></pre>
  
  <p>When doing the advanced tutorial it stops working. I've pretty much just copied and pasted it. When I run its version of the code it works. Mine just doesn't seem to work. It should rotate the camera view when moving left and right and move forward and backwards when using up and down keys.</p>
  
  <p>My code is here broken down:</p>
  
  <p>This part of my code renders components in the scene with init() which initilizes values etc.:</p>
  
  <pre><code>void display(void)
  {
   if (deltaMove)
    moveMeFlat(deltaMove);
   if (deltaAngle) {
    angle += deltaAngle;
    orientMe(angle);
   }
  
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glBindTexture(GL_TEXTURE_2D, texture[0]);
   RenderSkyDome();
   glBindTexture(GL_TEXTURE_2D, NULL);
  
   glutSwapBuffers();
  
  }
  
  void init(void)
  {
   glClearColor(0.0, 0.0, 0.0, 0.0);
   glClearDepth(1.0f);
   glColor3f(0.0, 0.0, 1.0);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glEnable(GL_TEXTURE_2D);
   glEnable(GL_DEPTH_TEST);
   glDepthFunc(GL_LEQUAL);
   glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  
   LoadTextures("clouds2.bmp", 0);
   GenerateDome(600.0f, 5.0f, 5.0f, 1.0f, 1.0f);
  
   snowman_display_list = createDL();
  }
  </code></pre>
  
  <p>This is my main loop function:</p>
  
  <pre><code>int main ()
  {
   glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
   glutInitWindowSize(800, 600);
   glutInitWindowPosition(0, 0);
   glutCreateWindow("Captain Ed's Adventures: Great Wall of China");
   init();
  
   //Glut Input Commands
   glutIgnoreKeyRepeat(1);
   glutSpecialFunc(pressKey);
   glutSpecialUpFunc(releaseKey);
   glutKeyboardFunc(processNormalKeys);
  
   glutDisplayFunc(display);
   glutIdleFunc(display);
   glutReshapeFunc(reshape);  // This redraws everything on screen when window size is changed.               
   glutMainLoop();
   return 0;
  }
  </code></pre>
  
  <p>Here are my input functions which are called:</p>
  
  <pre><code>void pressKey(int key, int x, int y) {
  
   switch (key) {
    case GLUT_KEY_LEFT : deltaAngle = -0.10f;break;
    case GLUT_KEY_RIGHT : deltaAngle = 0.10f;break;
    case GLUT_KEY_UP : deltaMove = 50;break;
    case GLUT_KEY_DOWN : deltaMove = -50;break;
   }
  }
  
  void releaseKey(int key, int x, int y) {
  
   switch (key) {
    case GLUT_KEY_LEFT : if (deltaAngle &lt; 0.0f) 
           deltaAngle = 0.0f;
          break;
    case GLUT_KEY_RIGHT : if (deltaAngle &gt; 0.0f) 
           deltaAngle = 0.0f;
          break;
    case GLUT_KEY_UP :  if (deltaMove &gt; 0) 
           deltaMove = 0;
          break;
    case GLUT_KEY_DOWN : if (deltaMove &lt; 0) 
           deltaMove = 0;
          break;
   }
  }
  
  void processNormalKeys(unsigned char key, int x, int y) {
  
   if (key == 27) 
    exit(0);
  }
  </code></pre>
  
  <p>Variables that get used by the camera and the functions that should change it: </p>
  
  <pre><code>static float angle=0.0,deltaAngle = 0.0,ratio;
  static float x=0.0f,y=1.75f,z=5.0f;
  static float lx=0.0f,ly=0.0f,lz=-1.0f;
  static int deltaMove=0;
  
  void orientMe(float ang) {
  
   lx = sin(ang);
   lz = -cos(ang);
   glLoadIdentity();
   gluLookAt(x, y, z, 
          x + lx,y + ly,z + lz,
       0.0f,1.0f,0.0f);
  } 
  
  void moveMeFlat(int i) {
   x = x + i*(lx)*0.1;
   z = z + i*(lz)*0.1;
   glLoadIdentity();
   gluLookAt(x, y, z, 
          x + lx,y + ly,z + lz,
       0.0f,1.0f,0.0f);
  }
  </code></pre>
  
  <p>I believe that's pretty much everything I am working with so basically what should happen is that when I press UP key deltaMove should = 50 and when this happens the if statement in void display(void) should do moveMeFlat(deltaMove); I don't know if I am doing this wrong or if there is a better result.... </p>
  
  <p>I can move "moveMeFlat(deltaMove)" within the relevant switch cases but this does not allow me to have the movement that I want. It seems to work using the source code from the tutorials above with the right functionality but not in my case so my assumption is that it's to do with the if statement in display.</p>
  
  <p>The end result I am looking for is to be able to have forwards and backwards working with left and right rotating the camera. I would like to be able to press forward and left key and see the camera <a href="http://en.wiktionary.org/wiki/swerve" rel="nofollow noreferrer">swerve</a> left like in a racing game...</p>