title: OpenGL: creating my own camera
body: <p>I'm trying to create a camera to move around a 3d space and am having some problems setting it up. I'm doing this is Java, and apparently using gluPerspective and gluLookAt together creates a conflict (the screen starts flickering like mad).</p>
  
  <p>gluPerspective is set like this:</p>
  
  <pre><code>gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
  gl.glLoadIdentity();
  glu.gluPerspective(50.0f, h, 1.0, 1000.0);
  gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
  </code></pre>
  
  <p>I then create a camera matrix, making use of eye coordinates, forward and up vectors (<a href="http://people.freedesktop.org/~idr/glu3/form_4.png" rel="nofollow">http://people.freedesktop.org/~idr/glu3/form_4.png</a>) (lets assume the code for the camera is correct.</p>
  
  <p>Lastly, before I draw any thing I have:</p>
  
  <pre><code>gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
  gl.glLoadIdentity();
  gl.glMultMatrixf(camera.matrix);
  </code></pre>
  
  <p>And then I call my drawing routines (which do some translation/rotation on their own by calling glRotatef and glTranslatef).</p>
  
  <p>Without the call to glMultMatrixf the camera shows the items I need to see in the centre of the screen as it should. With glMulMatrixf however, all I get is a black screen. I tried using glLoadMatrixf instead and it didn't work either. Am I doing something wrong? Am I putting something out of place? If not, and this is how it should be done let me know and I'll post some of the camera code that might be creating the conflicts.</p>
  
  <p>EDIT: Here is the camera matrix creation code:</p>
  
  <pre><code>private void createMatrix()
  {
      float[] f = new float[3]; //forward (centre-eye)
      float[] s = new float[3]; //side (f x up)
      float[] u = new float[3]; //'new up' (s x f)        
      for(int i=0;i&lt;3;i++){
          f[i] = centre[i]-eye[i];
      }
      f = Maths.normalize(f);
      s = Maths.crossProduct(f,upVec);
      u = Maths.crossProduct(s,f);
  
      float[][] mtx = new float[4][4];
      float[][] mtx2 = new float[4][4];   
              //initializing matrices to all 0s   
      for (int i = 0; i &lt; mtx.length; i++) {
          for (int j = 0; j &lt; mtx[0].length; j++) {
              mtx[i][j] = 0;
              mtx2[i][j] = 0;
          }
      }
  
              //mtx =  [ [s] 0,[u] 0,[-f] 0, 0 0 0 1]
              //mtx2 = [1 0 0 -eye(x), 0 1 0 -eye(y), 0 0 1 -eye(z), 0 0 0 1]
      for(int i=0;i&lt;3;i++){
          mtx[0][i] = s[i];
          mtx[1][i] = u[i];
          mtx[2][i] = -f[i];
  
          mtx2[i][3]=-eye[i];
          mtx2[i][3]=-eye[i];
          mtx2[i][3]=-eye[i];
      }
      mtx[3][3] = 1;
      mtx2[0][0]=1;mtx2[1][1] = 1;mtx2[2][2] = 1;mtx2[3][3] = 1;
  
      mtx = Maths.matrixMultiply(mtx,mtx2);
      for(int i=0;i&lt;4;i++){
          for(int j=0;j&lt;4;j++){
                              // this.mtx is a float[16] for glMultMatrixf
              this.mtx[i*4+j] = mtx[i][j];
          }
      }
  
  }
  </code></pre>
  
  <p>I'm hopping the error is somewhere in this piece of code, if not, I'll have a look at my maths functions to see whats going on.. </p>
  
  <p>EDIT2: Though I should mention that at least the initial vectors (eye,centre,up) are correct and do put teh camera where it should be (worked with gluLookAt but had teh flickering issue).</p>