Archived

This topic is now archived and is closed to further replies.

FlyingWren

world:screen relationship

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites