PDA

View Full Version : Screen coordinates to world coordinates



iverson1407
04-20-2011, 07:54 AM
I have a question about a special case translating from screen coordinates to world coordinates.

I wanna do with a interactive particle system.

So that we can click on the screen and emit some particles.

I've come through a lot of comments on this topic. However my case is a little tricky..

I just wanna get the coordinates with a given z axis value of world coordinate.

for example, I wanna get the world coordinate from the screen given the z = -5.0.

How can I do that? Can you help me with some codes?

My initialized and configuration function is as the following:
void initRendering()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_DEPTH);
}


void handleResize(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,(double)w/(double)h,1.0,200.0);
}

void drawscene()
{
float a = 0.0f;
float b = 0.0f;
float c = 0.0f;
particle *p;
particle *p2;
if(e->particlecount!=0)
{
p = e->anchorparticle->next;
}
if(e2->particlecount!=0)
{
p2 = e2->anchorparticle->next;
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPointSize(2.0f);
glPushMatrix();

.......
glPopMatrix()
}


Thx in advance for people who will help me.Thx very much.

Snow_56767
08-20-2011, 07:32 PM
Try generating the world coordinates yourself based off the screen coordinates. This way you are keeping track of the world and screen coordinates real-time so you can then translate between the two accordingly. A good example of this is you generate an virtual grid (meaning you don't render it) and lets say that 10px by 10px equals 1 world unit. So then when some when clicks at say (50,10) you can the translate to your custom world units by going, ok, 5 on X and 1 on Y.