0
\$\begingroup\$

I am currently creating a software renderer (I know they aren't particularly useful anymore, it is purely from a knowledge and learning standpoint). I am using GLM and following OpenGL rules(as GLM is designed for OpenGL).

It's currently pretty basic, just a cube being rendered as either a pixel for each vertex or as a wireframe. Everything was going fine with it till I implemented camera controls. The camera works fine until there is a model directly behind it in the world space. The model behind the camera will be rendered, but flipped (hard to tell which axis it has been flipped on due to it being wireframe). The controls for moving forward and back become inverted. If I put one object in front of the camera and one behind, they are both visible but when the camera moves forward one model moves closer while the other moves further away.

I am not sure if this is a bug or a limitation of the mathematics involved. From what I can tell it is a problem with the perspective division (which is being handled by GLM). It seems that when the camera crosses the Z of an object, it gets flipped. If I spin the camera around the object everything is fine, this only happens when the object is behind the camera. Is this likely to be a bug in my code, or is this normal and anything not in the camera's field of view needs to be culled to stop this happening? (I know things will need to be culled at some point for performance anyway).

The code is a mess at the moment so I am just cleaning it up so it is readable, in case this is likely a bug in my code.

\$\endgroup\$
  • \$\begingroup\$ This sounds perfectly sensible to me. Taking a model of perspective projection such as this one, we can see that, indeed, extrapolating the projection behind the viewer creates a secondary, reversed viewing frustum. The math doesn't care, it's your job to implement clipping for things that get bahind your near plane. \$\endgroup\$ – Quentin Dec 26 '17 at 14:10
  • \$\begingroup\$ Ahhh so it is me not completely understanding the maths, and I just need to clip/cull things outside of FoV, I thought GLM's perspective function would clip near plane but I guess that does not apply once it gets behind the camera. Thank you. \$\endgroup\$ – KinAJR Dec 26 '17 at 14:41
  • \$\begingroup\$ @Quentin it looks like this answered OP's question, so if you post it as an Answer they'll be able to accept it and you can attract some upvotes. :) \$\endgroup\$ – DMGregory Dec 26 '17 at 15:40
  • \$\begingroup\$ @DMGregory I guess I shied away because math is usually not my forté, but here it goes :) \$\endgroup\$ – Quentin Dec 26 '17 at 15:50
2
\$\begingroup\$

Perspective projection as you have probably implemented it is a pure mathematical transformation. It warps your world space by making all of the lines orthogonal to your viewing plane cross at the camera's position.

In the figure z- would be in front of the camera, and z+ would be behind. But the transformation does not have these concepts: if you extrapolate by continuing the lines "behind" the camera, you'll see that a second viewing frustum appears. Since the lines have crossed, it is inverted (rotated by 180°, in fact).

This means that you need to implement the distinction between "front" and "back" yourself, by clipping what falls bahind the camera.

Note: if you haven't got there yet, the near and far clipping planes are used when doing depth buffering. They put limits on the closest and furthest object you can see, so that the distance values can be normalized and stored without losing too much precision.

\$\endgroup\$

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.