Sign in to follow this  
Specchum

Rendering in 2D. Another way?

Recommended Posts

Specchum    242
The way I've been drawing 2D objects (hud and like) in my game has been to switch to ortho2d mode and render the objects 1 by one. However, I believe there is another way of drawing the objects which doesn't involve switching to ortho mode but instead drawing them directly in eye-space co-ordinates. I've tried this but unfortunately I don't see anything. All I did was set modelview to identity, disable depth testing and drawing a quad (with z = 0) at the origin 0,0,0. I'd assumed this was all that was required to draw in eyespace but apparently not. Can someone help me out here? What am I doing wrong? Am I missing out some important step?

Share this post


Link to post
Share on other sites
bpoint    464
You can draw 2D objects in 3D space if you really want to (as it's just a matter of calculation), but it's a lot more difficult and also window view size dependant.

First of all, drawing at Z=0 with an identity matrix will result in the polygon being clipped because it is behind the near clip plane. You also cannot set the near clip plane to zero for a perspective projection.

Try moving your Z out to just a small amount past the near clip plane and you should see your quad.

Also to take into consideration is that 0,0 is in the middle of the screen, and you are no longer working in pixel coordinates, since the aspect ratio and camera FOV define the projection.

Really, it's a lot easier to just do it in orthographic mode (that's why it's there).

Share this post


Link to post
Share on other sites
Specchum    242
Quote:
Original post by bpoint
First of all, drawing at Z=0 with an identity matrix will result in the polygon being clipped because it is behind the near clip plane. You also cannot set the near clip plane to zero for a perspective projection.


Yeah, I thought this was a problem but setting my Z to a non-zero value didn't solve the problem.

Quote:

Really, it's a lot easier to just do it in orthographic mode (that's why it's there).


I'm beginning to think so too! :)

Share this post


Link to post
Share on other sites
Hnefi    386
How expensive is the switch between ortho and perspective? My latest project is already starting to bog down resources on most computers, so I'm hesitant as to whether I should draw the hud in 3D (which isn't resolution dependant as long as the ratio between width and height is the same) or switch to 2D and back every frame.

Also, is it more efficient to push/pop the projection matrix instead of reissuing the same glPerspective command every fram?

Share this post


Link to post
Share on other sites
nefthy    184
Since the projection matrix usualy only needs to be set whet its parameters change, you can avoid seting it up every frame. So push/pop is the way to go. Matrix push and pop aren't expensive operations anyway.

Share this post


Link to post
Share on other sites
Monder    993
Quote:
How expensive is the switch between ortho and perspective?


Well forming a new projection matrix or ortho matrix is not going to be expensive. Switching the projection matrix alters state which you do want to avoid wherever possible, however as you alter the modelview matrix for every different model you draw one extra matrix change per frame probably won't hurt. Of course the only way to actually know is to try it and find out.

As for drawing a HUD without using an orthographic projection use screen alligned billboards as described here. To work out the size something will appear on the screen is pretty trivial. Look at the diagram below of a 2D view from above the camera frustum



Alpha is half the FOV, the first line is the near plane, and the far line represents a billboard that covers the entire screen. You can use similar triangles to work out how big the billboard that covers the entire screen is (you can then use fractions of this size for other billboards) given its distance from the camera (which can be any number you want as long as it doesn't put it beyond the far plane of the frustum). You need to work out what half the size of the near plane is which you can do using trig namely size = tan(alpha) * near plane distance. Then you work out the size of the billboard like so: size = 2 * half near plane size * billboard distance / near plane distance. You can then do similar calculations to work out the height of the billboard. You'll want to do these calculations in eye space. You'll also want to disable depth testing and writing when you draw this stuff, or clear the depth buffer.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this