Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Im trying to switch from the glTranslate etc to my own Matrices, but for some reason it does not work. Here are my 2 functions to create the view- and projection matrix:

public Matrix4f getViewMatrix() {
    Matrix4f viewMatrix = new Matrix4f();
    viewMatrix.rotate(game.player.rotation.x, new Vector3f(1.0f, 0.0f, 0.0f));
    viewMatrix.rotate(game.player.rotation.y, new Vector3f(0.0f, 1.0f, 0.0f));
    viewMatrix.rotate(game.player.rotation.z, new Vector3f(0.0f, 0.0f, 1.0f));
    return viewMatrix;

public Matrix4f getProjectionMatrix() {
    FloatBuffer projectionBuffer = BufferUtils.createFloatBuffer(16);
    GL11.glGetFloat(GL_MODELVIEW_MATRIX, projectionBuffer);
    Matrix4f projectionMatrix = new Matrix4f();
    return projectionMatrix;

I send those 2 matrices to the Vertex shader with a uniform, and use:

gl_Position = view_matrix * proj_matrix * vec4(in_position, 1.0);

Where in_position is the coordinate of the vertex.

I do see some things on the screen, but it's very, very buggy, and nothing is right about it. If I use the build-in gl_ModelViewProjectionMatrix in the shader and use glTranslate and glRotate in OpenGL, it works perfectly fine.

What am I doing wrong here?

Here is the output of me view projection together with the camera position:

0.5095141 0.0 -0.8604623 1.1625774
0.7321221 0.52541286 0.43351877 16.980185
0.45209795 -0.8508474 0.26770526 0.8665553
0.0 0.0 0.0 1.0

Vector3f[1.1625774, 16.980185, 0.8665553]
share|improve this question
Try (proj_matrix * view_matrix) * vec4(in_position, 1.0); – DaleyPaley May 23 '13 at 22:26
If that doesn't work, I'd suggest attaching your generated matrices so we can tell whether it's your generation, uploading, shader, etc – zacaj May 23 '13 at 22:40
That didn't work. I added the output of the View matrix in the post. – Basaa May 23 '13 at 23:55
wait, why do you have GL_MODELVIEW_MATRIX in getProjectionMatrix()? Should be GL_PROJECTION_MATRIX – DaleyPaley May 24 '13 at 0:04
Your right. I fixed it, but I still see the weird flippy things on my screen. – Basaa May 24 '13 at 8:11
up vote 0 down vote accepted

Okay it seemed that my projection matrix was completely wrong. Here is my new version which works great:

public Matrix4f getProjectionMatrix() {
    // Setup projection matrix
    Matrix4f projectionMatrix = new Matrix4f();
    float fieldOfView = 40.0f;
    float aspectRatio = (float)Display.getWidth() / (float)Display.getHeight();
    float near_plane = 0.1f;
    float far_plane = 1000f;

    float y_scale = coTangent((float) Math.toRadians(fieldOfView / 2f));
    float x_scale = y_scale / aspectRatio;
    float frustum_length = far_plane - near_plane;

    projectionMatrix.m00 = x_scale;
    projectionMatrix.m11 = y_scale;
    projectionMatrix.m22 = -((far_plane + near_plane) / frustum_length);
    projectionMatrix.m23 = -1;
    projectionMatrix.m32 = -((2 * near_plane * far_plane) / frustum_length);
    projectionMatrix.m33 = 0;
    return projectionMatrix;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.