Rendering in 2D. Another way?

Started by
5 comments, last by Monder 17 years, 12 months ago
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?
"There is no dark side of the moon." - Pink Floyd
Advertisement
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).
also remember thiers the project/unproject methods which kinda convert from and to screen space
Bring more Pain
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! :)

"There is no dark side of the moon." - Pink Floyd
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?
-------------Please rate this post if it was useful.
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.
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.

This topic is closed to new replies.

Advertisement