I'm writing a managed OpenGL library in C++, and I'm having a pretty annoying problem. Whenever I set an objects Z coordinate to -2.0 or lower, the object disappears. And at coordinates 0 through -1.9, it doesn't appear to move away from them camera. I have a world matrix (which is multiplied by the objects position to move it), a view matrix (which is just the identity matrix currently) and a projection matrix, which is setup like this:

  • FOV: 45.0f
  • Aspect Ratio: 1
  • zNear: 0.1f
  • zFar: 100.0f

using GLMs glm::perspective method. The order the matrices are multiplied by in the shader is world, view, projection, then position.

I can move the object along the X and Y axis perfectly fine. I have depth testing enabled, using GL_LEQUAL. I can change the actually vertices positions to anything I want, and they move away from the camera or towards it perfectly fine. It just seems to be the world matrix acting up. I'm using glm::mat4 for the world matrix, and glm::vec3 for positions. Whats going on here?

I'm also using OpenGL 3.1, GLSL version 140 (1.4?).

  • 2
    \$\begingroup\$ How can you have a negative distance from the camera? Or are you talking about coordinates. Please give camera and object transforms entries or even better show the code. You may have scale or skew going on. \$\endgroup\$ – milkplus Oct 13 '12 at 3:37
  • \$\begingroup\$ @milkplus Oh, I can see how you were confused. I'm talking about XYZ coordinates. Sorry I'm not very good a terminology when it comes to this stuff... I don't know what code to post, there's a lot of code related to this in many different places. \$\endgroup\$ – smoth190 Oct 14 '12 at 3:09
  • \$\begingroup\$ What's the position of the camera? If the object is disappearing at -2.0, and your camera is placed with a Z of -2.0, then that would be expected that the object disappears, because it's behind the near clipping plane. \$\endgroup\$ – kevintodisco Oct 14 '12 at 7:02

Okay, first off, I want to say that for your Projection's Aspect Ratio, you might want to divide the width by the height. (This will give you your aspect ratio, like 1920/1080 = 16:9 Ratio)

Anyway, where are you getting your model matrix? You're saying you're multiplying the world matrix by your objects position? I wanna say that could be a problem. (Although, this could just be me misunderstanding what you're saying)

All I can say is make sure your objects have their own matrices. That would basically be their World Matrix. Also make sure you multiply your ModelViewProj Matrix with your vertex position in your Vertex Shader.


As it turns out, the order I was multiplying my matrices in the shader was wrong. Originally, I had:

gl_Position = worldMatrix * viewMatrix * projectionMatrix * vec4(in_Position, 1.0);

But it works with:

gl_Position = projectionMatrix * viewMatrix * worldMatrix * vec4(in_Position, 1.0);

I always thought MVP was the order they were supposed to be multiplied in, not PVM. Maybe I'm doing something wrong, but as of now this works.

  • \$\begingroup\$ While I think the original question may be a bit too localized for GDSE, please accept your own answer so the question. :) So far as helping with your understanding of the math, MVP is the order they are applied to the vector. When using post-multiplication (the vector is on the right side of the matrix-vector multiplication expression), it's common for the matrices to be built in such a way that they apply "in reverse", so that the matrix closest to the vector in the expression is applied first. The actual matrix multiplication is still left-associative, but that isn't important here. \$\endgroup\$ – Sean Middleditch Oct 15 '12 at 1:10
  • 2
    \$\begingroup\$ Oh, and since I took the time to write it specifically for these circumstances... take a look at this article I wrote trying to explain the notation and terms used in game math libraries (which might clear it up further for you): seanmiddleditch.com/journal/2012/08/… \$\endgroup\$ – Sean Middleditch Oct 15 '12 at 1:11
  • \$\begingroup\$ @SeanMiddleditch Thank you for the article. Matrix math was always an enigma to me, hopefully this will help me out a little. \$\endgroup\$ – smoth190 Oct 20 '12 at 1:58

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.