Selecting a point on a 3D plane
In OpenGL, I created a X-Z plane with some rotations.
...
glRotatef(20, 1, 0, 0);
glRotatef(-45, 0, 1, 0);
glBegin(GL_LINES);
glColor3f(1.0, 0.0, 0.0);
for (int i = -10; i <= 10; i=i+2)
{
glVertex3f(-10.0, 0.0, i);
glVertex3f(10.0, 0.0, i);
glVertex3f(i, 0.0, -10.0);
glVertex3f(i, 0.0, 10.0);
}
glEnd();
...
So I have a 10x10 grid on the screen.
Now, is there anyway I can calculate the (x,y,z) coordinate on the grid when I press a mouse button on certain point say (x1, y1)?
hello,
of course you can,
you need to do some mathematic, geometric transformation and it depends on the way you want to do this.
the simplest way is to transform your click coordinate in relation with the transformations you have made (rotations, translations, care of the sense !).
Normally (i don''t use it), gluUnProject can give you the coordinate of the point where you have clicked.
i can''t say more,
i hope this help you a bit.
Fratt
of course you can,
you need to do some mathematic, geometric transformation and it depends on the way you want to do this.
the simplest way is to transform your click coordinate in relation with the transformations you have made (rotations, translations, care of the sense !).
Normally (i don''t use it), gluUnProject can give you the coordinate of the point where you have clicked.
i can''t say more,
i hope this help you a bit.
Fratt
Thanks.
glGetDoublev(GL_MODELVIEW_MATRIX, model);
glGetDoublev(GL_PROJECTION_MATRIX, proj);
glGetIntegerv(GL_VIEWPORT, view);
gluUnProject(xPos, yPos, 0, model, proj, view, &objX, &objY, &objZ);
I tried the code above, and it works, but it seems that the coordinates returned are those after translation.
Is there any calculation such that I can find its absolute coordinate in the world? I mean, if I draw a triangle
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(1.0, 0.0, 0.0);
glVertex3f(0.0, 1.0, 0.0);
I want the return value to be (0, 0, 0) when I click on the point at the 90 degree angle no matter how the triangle is transformed.
Thanks a lot.
glGetDoublev(GL_MODELVIEW_MATRIX, model);
glGetDoublev(GL_PROJECTION_MATRIX, proj);
glGetIntegerv(GL_VIEWPORT, view);
gluUnProject(xPos, yPos, 0, model, proj, view, &objX, &objY, &objZ);
I tried the code above, and it works, but it seems that the coordinates returned are those after translation.
Is there any calculation such that I can find its absolute coordinate in the world? I mean, if I draw a triangle
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(1.0, 0.0, 0.0);
glVertex3f(0.0, 1.0, 0.0);
I want the return value to be (0, 0, 0) when I click on the point at the 90 degree angle no matter how the triangle is transformed.
Thanks a lot.
Just a note:
gluUnproject on my card is etremely slow. Try using picking instead...
http://www.lighthouse3d.com/opengl/picking/
gluUnproject on my card is etremely slow. Try using picking instead...
http://www.lighthouse3d.com/opengl/picking/
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement