world:screen relationship

Started by
5 comments, last by FlyingWren 20 years, 8 months ago
OK, here''s an question that should be easily answered: I am trying to set up a purely 2-D openGL projection engine, as follows: ------------------------------------------------------- // Reset viewport glViewport(0, 0, width, height); // select the projection matrix and clear it out glMatrixMode(GL_PROJECTION); glLoadIdentity(); // set the perspective glOrtho(0.0f, width, 0.0f, height, -100, 100); // select modelview matrix and clear it out glMatrixMode(GL_MODELVIEW); glLoadIdentity(); ------------------------------------------------------- I then draw stuff to the screen, like so: ------------------------------------------------------- glBegin(GL_POINTS); glColor3f(1.0f, 0.0f, 0.0f); // plot a red pixel at 0,0 glVertex3f(0.0f, 0.0f, 1.0f); glColor3f(0.0f, 1.0f, 0.0f); // plot a green pixel at 1,1 glVertex3f(1.0f, 1.0f, 1.0f); glColor3f(0.0f, 0.0f, 1.0f); // plot a blue pixel at 2,2 glVertex3f(2.0f, 2.0f, 1.0f); glEnd(); ------------------------------------------------------- ...The red pixel doesn''t show up. The green pixel shows, but it''s at (0,1) instead of (1,1), and the blue pixel is at (1,2) instead of (2,2). Also, it appears that the coordinates on the other side of the window are screwy too, ie, if i defined ''Width'' to be 800 and plot a pixel at X=800, it doesn''t show. Rather, X=790ish puts it at the very edge of the window. THE QUESTION: How do you get a proper relationship between gl primitive location, and screen x,y location? This is driving me nutso!
Advertisement
I have this question as well. How do you convert a 3D coordinate to a 2D screen coordinate? I imagine it involves using the projection matrix, but I''m not sure how to do it.
I have seen this question unanswered in a few other threads as well. It sounds to me like this is a fairly common hurdle, yet none of the resources that I have seem to address this world:screen discrepancy.

Surely some people out there must know the fix :-l
one part is easy to answer. if your width is 800 there are 800 pixels, as the first one is 0 there obviously isnt a 800, the last one should be 799.

whats your window size?
dont use vertex3f if you want int coordinates, just in case something is doing some strange rounding.
try using a larger windows and place the viewport in the middle. still no red pixel?
f@dzhttp://festini.device-zero.de
I never do 2d, but OpenGL has a 2d ortho mode that should be doing all of this for you. Otherwise you have to find where the ray connecting the vertex of each polygon and the camera position intersects the viewing plane to get a screen coordinate.


EDIT: nevermind, i didn't see you already are using ortho *shrug*


[edited by - Shadow12345 on August 13, 2003 11:20:13 PM]
Why don't alcoholics make good calculus teachers?Because they don't know their limits!Oh come on, Newton wasn't THAT smart...
If you don''t mind, how do you find the viewing plane?
If you are comfortable with vector math finding this stuff isn't too bad, otherwise it won't make sense.

the near plane distance should be known by you, I use 10

float nearplanedist = x; //whatever dist, 10 is a common one

that plane's normal is the (normalized)view vector. that plane's distance is the dot product between the (normalized) view vector and a point on that plane. A point on that plane is the camera's position plus (view * nearplanedist)

plane nearplane;
nearplane.Normal = camera.View; //view is already normalized

Vector PointOnPlane = camera.Position + (View * nearplanedist);
nearplane.Dist = DotProduct(camera.View, PointOnPlane)

I drew a picture and posted it here:

http://www.thedevelopersalliance.com/~shadow12345/illustration.jpg



[edited by - Shadow12345 on August 14, 2003 12:36:26 PM]
Why don't alcoholics make good calculus teachers?Because they don't know their limits!Oh come on, Newton wasn't THAT smart...

This topic is closed to new replies.

Advertisement