3D to 2D
Is there any standard method / function for converting a 3D coordinate to it''s corresponding 2D coordinate?
The question is not "why a talking monkey," but rather, "why not a talking monkey." -Monkey Island 4
It depends on what, exactly, you mean by a 2D representation.
Are you trying to figure out where it appears on the screen, with perspective and everything? If so, learn about matrix math and homogenous coordinates, as well as perspective division.
Are you trying to figure out where it appears on the screen, with perspective and everything? If so, learn about matrix math and homogenous coordinates, as well as perspective division.
GLU has some helper functions for this type of situation. See gluProject and gluUnProject. If they can''t do what you want, see Sneftel''s post again
.



Sneftel, yeah, trying to figure out where it appears on the screen.
All: Thanks for the tips!
All: Thanks for the tips!
The question is not "why a talking monkey," but rather, "why not a talking monkey." -Monkey Island 4
Okay I got this from the GLU docs:
So would the call look something like this: (?)
But WHY OH WHY do my window coordinates include a z??? A 2D plain with depth ... I''m so confused. Any explanations?
int gluProject( GLdouble objx, GLdouble objy,GLdouble objz, const GLdouble modelMatrix[16],const GLdouble projMatrix[16], const GLint viewport[4],GLdouble *winx, GLdouble *winy, GLdouble *winz );
So would the call look something like this: (?)
GLint viewport[4];GLdouble projMatrix[16];GLdouble modelMatrix[16];int x, y, z;glGetDouble(GL_PROJECTION, projMatrix);glGetDouble(GL_MODELVIEW, modelMatrix);glGetIntegerv(GL_VIEWPORT, viewport);gluProject( oX, oY, oZ, modelMatrix, projMatrix, viewport, &x, &y, &z);
But WHY OH WHY do my window coordinates include a z??? A 2D plain with depth ... I''m so confused. Any explanations?
The question is not "why a talking monkey," but rather, "why not a talking monkey." -Monkey Island 4
the z is how far away it is from your screen, as your objects can be more near and far (its a 3dworld you see on screen, so it has depth)
"take a look around" - limp bizkit
www.google.com
"take a look around" - limp bizkit
www.google.com
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia
My Page davepermen.net | My Music on Bandcamp and on Soundcloud
Okay, z is for how far away it is on the screen WHEN WE REFER TO OBJECT COORDINATES. I''m trying to figure out where the object is ON MY WINDOW. IE, in 2D coordinates. There need be no Z.
But anyway, that''s irrelevant. Right now I''m using the commands as I outlined above, like this:
With the ofstream stuff there for debugging. Checking it''s output, it *always* gives be winx=the width of the screen, winy=the height of the screen, winz=1. So I''m clearly doing something wrong .. but what???
But anyway, that''s irrelevant. Right now I''m using the commands as I outlined above, like this:
int DrawGLScene(GLvoid) // Here''s Where We Do All The Drawing{ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer glLoadIdentity(); // Reset The View GLint viewport[4]; GLdouble projMatrix[16]; GLdouble modelMatrix[16]; glGetDoublev(GL_PROJECTION, projMatrix); glGetDoublev(GL_MODELVIEW, modelMatrix); glGetIntegerv(GL_VIEWPORT, viewport); gluProject( -1.5f, -1.5f, -5.0, modelMatrix, projMatrix, viewport, &winx, &winy, &winz);ofstream out("log.txt");out << winx << endl << winy << endl << winz << "done\n\n\n";
With the ofstream stuff there for debugging. Checking it''s output, it *always* gives be winx=the width of the screen, winy=the height of the screen, winz=1. So I''m clearly doing something wrong .. but what???
The question is not "why a talking monkey," but rather, "why not a talking monkey." -Monkey Island 4
well, even in WINDOWCOORDINATES your vertex has a DISTANCE from screen.. this is the value used then in the so called Z-BUFFER.. this has NOTHING to do with the objectspace the vertex was in..
why its not working? not sure, but it looks like your matrices are always the same, your vertex you project is always the same.. this results in always the same output.. why this output i don''t know, i never used gluProject yet, no need for knowing where it is on screen myself..
"take a look around" - limp bizkit
www.google.com
why its not working? not sure, but it looks like your matrices are always the same, your vertex you project is always the same.. this results in always the same output.. why this output i don''t know, i never used gluProject yet, no need for knowing where it is on screen myself..
"take a look around" - limp bizkit
www.google.com
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia
My Page davepermen.net | My Music on Bandcamp and on Soundcloud
I tried putting in a glTranslate call before all my gluProject() code, but that didn''t change anything.
this makes no sense ...

The question is not "why a talking monkey," but rather, "why not a talking monkey." -Monkey Island 4
Note that the values returned are NORMALIZED DEVICE COORDINATES. That is to say, they won't be pixel values. Instead, think of your screen as -1.0 < x < 1.0, and -1.0 < y < 1.0. Of course, your screen isn't square... but you get the point.
If you want pixel coordinates, it should be pretty easy to multiply out by your screen resolution. Remember that these numbers are relative to the center of your screen.
Also, you might want to have the thing print out your model-view and projection matrices, just to make sure they still have what you put in there (no extraneous pops).
[edited by - sneftel on May 19, 2002 2:02:38 PM]
If you want pixel coordinates, it should be pretty easy to multiply out by your screen resolution. Remember that these numbers are relative to the center of your screen.
Also, you might want to have the thing print out your model-view and projection matrices, just to make sure they still have what you put in there (no extraneous pops).
[edited by - sneftel on May 19, 2002 2:02:38 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement