View Full Version : Newbie with "3D environment" question

01-03-2013, 09:39 AM
Hello again forum...

Finished coding a "simple" 3D environment in which a "player" walks around a room with a doorway to the outside world - basically a very simple first-person simulator.

I use "glfrustum" to add perspective, and set the clip planes to be 0.3 and 20 (near and far).

Everything is working well but I can't seem to solve the effect of near-plane clipping, i.e.:

1. When the player walks up to a wall and turns around (Yaw) then the wall pivots exactly at the player position and when end-on (player at 90 degrees facing along the wall) then the display shows both sides of the wall, i.e. outside the room as well as inside.

2. When the player walks through a doorway, near planes are clipped and removed, so the display shows a sort of cut-through version of the room walls either side of the door before the disappear behind the player for good.

3. Other objects in the room appear "cut through" as the player walks up to them and they are clipped.

These two effects obviously don't plague commercial releases of software.

Are there any tricks and tips anyone could provide me to solve the near-plane issues?

If I change the near plane for something larger than 0.3 then walls disappear even if I walk the player straight up to them (square-on) as if they have passed behind the player.

If I change the near plane for something smaller, the perspective effect is too pronounced - I get a very stretched view of the "room".

I could stop the player earlier and not allow them too close to a wall, etc. but this seems like cheating and would restrict player movement.

I'm trying to think what early software like "Doom" does when the player is up against a wall and turns around - does the program just blank everything not in the room to provide a "dull grey" wall cut-through section?

01-03-2013, 12:48 PM

lets start with giving the near/far values some meaning. Lets say your world is defined in meters, so 1.0f in vertex coordinates is one meter. Then you will clip away everything that's further away than 20 meters. If that works depends on your scene. For a FPS 30cm near clipping might be too high. In the real world I would set like 3cm as the near plane as I can't get closer to a wall than 3cm - at least my eyes can't as my nose gets in the way! Similarly you will have to prevent your player from getting too close to objects, the actual value might be higher, might be smaller. Just think about what fits your application. So yes, cheating is the answer!

So what if you want to reduce the near plane from 30cm to 3 without changing the 3D impression? When you look at what glFrustum does[1], we see that what defines the "3D impression" is the opening angle (it's like what lens you use on your photo camera - fish eye or tele etc.). And you see that to retain the opening angle you have to adjust the near value but also the values for left/right and top/bottom (just changing the near value will move the green square closer to the eye and widen up the opening angle)! As an alternative you can use something like gluPerspective[2] to define the same matrix - the so called projection matrix - with other values: an opening angle (field of view), the window aspect ratio (e.g. 16/9 in case of wide screen) and the near and far values. Here you can adjust the near plane without having to change the other values. You can also change the FoV to adjust the "3D impression".

You can build your projection matrix yourself without any GL calls and just load that onto the matrix stack - in modern OpenGL there is no matrix stack anymore so you will have to look into the math anyway. The GLM library already provides some useful classes for that.

[1] http://wiki.delphigl.com/index.php/glFrustum (it's related to delphi, also: ignore the text, its german: but it has a nice picture and the actual matrix)
[2] http://www.opengl.org/sdk/docs/man2/xhtml/gluPerspective.xml

01-04-2013, 08:11 AM
I've basically done what you suggested - I've closed down the top/left/bottom.right frustum values to 0.1 but currently still have the nea plan set to 0.3. I'll try closing this down further now to see if I can get the problem to go away once and for all.

Thanks for the reply. People on here really do know everything...