Jump to content


Camera - modelview and projection matrix


1 reply to this topic

#1 boombatower

    New Member

  • Members
  • Pip
  • 5 posts

Posted 11 June 2007 - 09:24 PM

I have been working on a camera class but to little avail. I was hoping that someone may be able to point out some problem. The following code is written in Java and the matrices are converted to FloatBuffers before being sent to OpenGL (via JOGL).

If I use instead of making the projection matrix then it appears that it works decently. (seems like one of the axes are backwards) If I use the projection matrix then nothing shows up.
glu.gluPerspective(zoom * fieldOfView, (float) GraphicsInfo.getWidth() / (float) GraphicsInfo.getHeight(), NEAR, FAR);

Vector viewPoint = MathUtils.add(position, viewDirection);

Vector f = MathUtils.add(viewPoint, MathUtils.multiply(position, -1));
Vector fNormalized = MathUtils.normalize(f);

Vector upNormalized = MathUtils.normalize(up);

Vector s = MathUtils.crossProduct(fNormalized, upNormalized);
Vector u = MathUtils.crossProduct(s, fNormalized);

// setup orientation matrix
orientation.matrix[0][0] = s.x;
orientation.matrix[0][1] = s.y;
orientation.matrix[0][2] = s.z;
orientation.matrix[0][3] = 0;

orientation.matrix[1][0] = u.x;
orientation.matrix[1][1] = u.y;
orientation.matrix[1][2] = u.z;
orientation.matrix[1][3] = 0;

orientation.matrix[2][0] = -f.x;
orientation.matrix[2][1] = -f.y;
orientation.matrix[2][2] = -f.z;
orientation.matrix[2][3] = 0;

orientation.matrix[3][0] = 0;
orientation.matrix[3][1] = 0;
orientation.matrix[3][2] = 0;
orientation.matrix[3][3] = 1;

float fd = 1 / (MathUtils.tan(fieldOfView / 2));

projection.matrix[0][0] = fd / aspect;
projection.matrix[0][1] = 0;
projection.matrix[0][2] = 0;
projection.matrix[0][3] = 0;

projection.matrix[1][0] = 0;
projection.matrix[1][1] = fd;
projection.matrix[1][2] = 0;
projection.matrix[1][3] = 0;

projection.matrix[2][0] = 0;
projection.matrix[2][1] = 0;
projection.matrix[2][2] = (FAR + NEAR) / (FAR - NEAR);
projection.matrix[2][3] = 1;

projection.matrix[3][0] = 0;
projection.matrix[3][1] = 0;
projection.matrix[3][2] = (2 * FAR * NEAR) / (FAR - NEAR);
projection.matrix[3][3] = 0;
//		projection.inverse();

generateProjectionBuffer();

// projection
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
gl.glMultMatrixf(projectionBuffer);

// camera position matrix
orientationBuffer.rewind();
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity(); // necessary since using multMatrix


#2 boombatower

    New Member

  • Members
  • Pip
  • 5 posts

Posted 13 June 2007 - 08:34 PM

Thanks anyway. After a long, frustrating attempt I finally figured it out. It had to do with the ordering of my matrices. Mine were row ordered and OpenGL uses column ordered.

I had this right when i did the orientation matrix, but not for the projection matrix.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users