I made a simple OpenGL program but I can figure out why the camera is not working, here it's a little fragment of the Camera class:

public Matrix4f getView() { // initializes the view matrix
    return new Matrix4f().lookAt(
            new Vector3f(0f, 0f, 1f), // camera position at 0,0,1
            new Vector3f(0f, 0f, 0f), // camera target at 0,0,0
            new Vector3f(0f, 1f, 0f)); // up axis set to "worldUp" (0,1,0)

public Matrix4f getProjection() {
    return new Matrix4f().perspective(
            (float) Math.toRadians(fieldOfView), // the fov has a value between 0f and 180f, by default I set it to 90° 
            viewportAspectRatio, // the aspect ratio is equal to 1024 / 960 (screen height / screen width)... even if I've not understood what is it...
            0.1f, 1000f); // I've not really understood what near and far planes are...

public Matrix4f getMatrix() { // with this function I obtain the final camera matrix
    return getView().mul(getProjection());

And it's how I handle the camera Matrix in GLSL, created using camera.getMatrix():

gl_Position = camera * model * vec4(position, 1.0);

Without the camera all is fine: here's the program running using gl_Position = model * vec4(position, 1.0);:


(Yeah, it's a cube)

But using the camera in the way I showed you before, increasing the FOV, I get this: enter image description here

Could anyone look at my code and tell me where I'm wrong? I would be really happy... D:

  • \$\begingroup\$ Are your vertices defined in clip-space? \$\endgroup\$ – Jon Mar 17 '16 at 6:30
  • \$\begingroup\$ In clip-space? I think yes, look at the cube without camera, all seems fine. \$\endgroup\$ – lory Mar 17 '16 at 6:32
  • \$\begingroup\$ That would be -1:1 components with model set to identity? \$\endgroup\$ – Jon Mar 17 '16 at 6:33
  • \$\begingroup\$ I'm using modern opengl so i don't really know what identity is. With the VBO buffer i loaded correct vertices, i'm sure of that. With the model Matrix i Made a 'null' Matrix which has 1,1,1 of scale and 0,0,0 of translate. \$\endgroup\$ – lory Mar 17 '16 at 6:41
  • \$\begingroup\$ Let just eliminate viewproj as a possible issue first. Please add the array of values produced by getMatrix to your answer and I'll compare them to my DX RH methods. \$\endgroup\$ – Jon Mar 17 '16 at 6:48

I solved, the problem was on getMatrix. Has Jon say I had to flip getView().mul(getProjection()) to obtain getProjection().mul(getView()). At first I though that this would not change anything but I was wrong. Thank you Jon!

| improve this answer | |
  • 2
    \$\begingroup\$ Matrix multiplication is not commutative. \$\endgroup\$ – Bálint Apr 18 '16 at 8:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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