Jump to content
  • Advertisement
Sign in to follow this  
Specchum

Rendering in 2D. Another way?

This topic is 4477 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
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).

Share this post


Link to post
Share on other sites
also remember thiers the project/unproject methods which kinda convert from and to screen space

Share this post


Link to post
Share on other sites
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
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
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!