Advertisement

mouse 3d coordinates

Started by January 28, 2003 03:36 PM
4 comments, last by penetrator 22 years, 1 month ago
Hi, i have a simple terrain engine built on a 1024x1024 triangle strip. I''d like, when i move the mouse on a vertex, to get the x,y,z coordinates. So i coded the following mousemove routine: xPos = LOWORD(lParam); // horizontal position of cursor yPos = HIWORD(lParam); // vertical position of cursor glGetIntegerv (GL_VIEWPORT, viewport); glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix); glGetDoublev (GL_PROJECTION_MATRIX, projmatrix); realy = viewport[3] - (GLint) yPos - 1; glReadPixels(xPos,viewport[3]-yPos-19,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,(void *)pixel_depth_cp); gluUnProject ((GLdouble) xPos, (GLdouble) realy, pixel_depth_cp[0], mvmatrix, projmatrix, viewport, &wx, &wy, &wz); Is this code correct ? I.e. wx, wy, wz really hold the vertex coordinates ? If i want to draw a simple 3d point exactly where i click the mouse, do i have to glTranslatef to wx,wy,wz ?.
Your code is correct so far, but wx,wy,wz are not your point. You''ll need to use gluUnproject twice to find a line, then intersect that line with your terrain. Read the docs on gluUnproject for details.

And don''t crosspost.


Don''t listen to me. I''ve had too much coffee.
Advertisement
sorry for cross-posting, it''s just that i''m getting nut with this problem. I have read the documentation, but why do i have to use glunproject twice ? It it used to get 3d coordinates, so why i cant just draw an object on screen using them ?

It isn''t used to get REAL 3d coordinates. It can''t, since any 2D point on a screen could potentially represent an infinite number of points in a 3D scene. The system has no way of knowing whether you''re pointing at a point three inches "deep" from your screen, or 3 miles. Since it doesn''t remember what it''s drawn, it can''t find the closest thing to point to. That''s why you need a line: so that you can perform the intersection yourself.


Don''t listen to me. I''ve had too much coffee.
ok, so i should cal glunproject twice ? And later ? How do i build the line and find the intersection point with the terrain ?

It works for me. I use it to move vertice tick in my model editor.
You get the distance with read pixel. and unproject once for the 'x, y, z' of the mouse at that distance.

Is there an error? why else post? EAH.
glReadPixels(xPos,viewport[3]-yPos-19,1,1,

i notice this:
viewport[3]-ypos '-19'<-
maybe this is the cause of a problem

Explain alittle more,
Bobby

PS:
float depth;
GLint viewport[4];
glGetIntegerv (GL_VIEWPORT, viewport);
GLint realy = viewport[3] - (GLint) mousey - 1;
glReadPixels(mousex,realy,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,&depth);

GLdouble mvmatrix[16], projmatrix[16];
GLint realy; // OpenGL y coordinate position
GLdouble wx, wy, wz;
glGetIntegerv (GL_VIEWPORT, viewport);
glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);
glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);

gluUnProject ((GLdouble) mousex, (GLdouble) realy, depth,
mvmatrix, projmatrix, viewport, &wx, &wy, &wz);


[edited by - BGCJR on January 29, 2003 6:54:27 PM]
Game Core

This topic is closed to new replies.

Advertisement