Skip to main content
gamedev.net
🔒 Locked

Pixel-precise plotting

Started by Bucket_Head 1 reply 450+ views
Original Post
Bucket_Head
Bucket_Head
Hello fellow GL coders! I have a question, something I''ve been puzzling over for quite some time...Of the things I hope to master in OpenGL, among them is the ability to plot things on the screen with pixel precision. All that I know I have to work with is putting stuff out in 3 space in front of the camera, and TRYING to line it up. I actually made my own little GL_Cursor by trial and error of trying to find the right value to scale the cursor out from the center by, and finally got something that looks right. Of course, there''s a problem with this - If you wanted to render multiple things with pixel precision, that could possibly obscure each other, one or the other may not be rendered, being covered up by the other. As such, one might have to find several different scalars, for several different layers of items to be shown. For example, say you had a menu; 3D text in front of the camera, and a GL_Cursor. If you had the text and the cursor at the same depth, they could obscure each other, so you''d have to move one of them (probably the text) back a little to a slightly different depth, and that would then require a different scaler value to bright it out from the center. Is there a better way to do this? And is there a general formula that takes the viewing frustrum and the depth into account to find the right scalar value? Because until now, I''ve just used trial and error, which takes time and may not be completely accurate. Thank you very much for your time! - Danny McCue
Hai, watashi no chichi no kuruma ga oishikatta desu!...or, in other words, "Yes, my dad's car was delicious!"
Bucket_Head
Bucket_Head
Anyone? Anyone? Bueller? Bueller? Fry?

Well I did some more investigations, and I found that one main function to be considered is this:

gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

The first part, as I understand it, is the fovy. Is this only fovy? Why do you get an fovy but not an fovx? Are they assumed to be the same? Or...?
The second is the aspect ratio, width/height. I don''t think this will really affect things, although width and height will have to be taken into account in our eventual formula in order to scale things appropriately from our screen resolution.
The final two parts, the near and far clipping planes, should be of no consequence for our doings here.

I found that at the given input for gluPerspective(), at 0.1 dist the scalar value was approx. 0.001725 and that at 5.0 dist the scalar value was approx. 0.00864 ...What does this look like? Would the graph be linear? Perhaps I should, via brute force, (ugh) determine the scalar value for five values (maybe 0.1, 0.2, 0.3, 0.4, 0.5, etc) and graph it...maybe some values farther apart would be better. If I can determine some kind of relationship that''s useful, all will be gravy...I would be greatly thankful to anyone that could help...

So far, that I can tell, we need to take these into account:

fovy, width (of screen, in pixels), height (ditto), depth (distance from camera our x/y plane is), screen coords (pixels where we want to plot)

Any thoughts?

Thank you for your time,
- Danny McCue
Hai, watashi no chichi no kuruma ga oishikatta desu!...or, in other words, "Yes, my dad's car was delicious!"
delphi3d
delphi3d
Better late then never, I suppose. I understand you''re trying to draw a cursor, so here''s what you could do:

Render your entire 3D scene using perspective projection as always. Then change back to ortho, using glOrtho(0, ScreenWidth, 0, ScreenHeight, -1, 1). If you do this, one OpenGL unit will correspond exactly to one screen pixel. You can then draw whatever 2D sprites that need to be aligned exactly to a certain pixel. Afterwards, restore the perspective and move on to the next frame.

An alternative approach that I like to use is this: use glOrtho(-320, 320, -240, 240, -1, 1). This gives me a 640x480 2D canvas with the origin in the middle, and if I render to it, the results will look the same regardless of screen resolution. It''s not pixel-perfect, but it works just as well.



Tom Nuydens
delphi3d@gamedeveloper.org
www.gamedeveloper.org/delphi3d
Tom Nuydens delphi3d@gamedeveloper.org www.gamedeveloper.org/delphi3d

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.