Archived

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

SilverDragon

3d to 2d coordinates

Recommended Posts

SilverDragon    122
I have my viewport coordinates vx, vy and vz.. and i have object coordinates x, y, z. How would i get the objx, objy and objz needed by gluProject from these? Or is there another way I should go about transforming 3d coordinates into screen coords? Im trying to make a target reticle like the ones in spacesims.. Its a bounding box around other ships.. But how would i get the screen x and y from the shipx, shipy and shipz..

Share this post


Link to post
Share on other sites
Vlion    151
uhm....
Instead of having a square like most sims do, have a rectangular solid around the ship in your 3d space.
This is easily generated and easily rendered.
Its advantage is that it is novel and easy.
Disdvantage of course is that it is non-standard.

I do think there is a gluUnproject func.

-edit-

Not what you need.
"The gluUnProject function maps window coordinates to object coordinates."
-MSDN

);



Bugle4d

[edited by - Vlion on March 19, 2002 3:15:12 PM]

Share this post


Link to post
Share on other sites
Rasmadrak    196
if you know the position of the 3d object and want to know where on the screen (2D image output) it´s drawn; use gluProject(...);

if you want the 3D position of a known 2D position, use gluUnproject(...);


I guess you wanted the first one? =)
I used this to center text around my spaceships.

 
//this uses gluProject(), 2d coordinates are saved into the winx,winy,winz variables...


double winx,winy,winz;

double modelviewmatrix [16];
double projectionmatrix [16];
int viewport[4];
glGetIntegerv (GL_VIEWPORT, viewport);

glGetDoublev (GL_MODELVIEW_MATRIX, modelviewmatrix);
glGetDoublev (GL_PROJECTION_MATRIX, projectionmatrix);
bool result = gluProject ( Ships[i].posX,Ships[i].posY,Ships[i].posZ,(const double *)modelviewmatrix,(const double *)projectionmatrix,viewport,&winx,&winy,&winz);

/*
to prevent the text/target system from wanking about on the screen, set some limits for the variables;
this ensures that if the targeted ship is behind you, the text will be on the side of the screen, so you know that it is still targeted - but out of your view! :)
*/

if (winx > 1024) winx = 1024;
if (winx < 0) winx = 0;
if (winy > 768) winy = 768;
if (winy < 0) winy = 0;

/*

i.e glPrint(winx,winy,"this text follows the ship above");
*/



Hope it helps!!

Cheers mate!

Share this post


Link to post
Share on other sites