OpenGL coords in MFC

Started by
5 comments, last by RavNaz 21 years, 11 months ago
I''m developing a level editor...and have 4 different views, eg top , side, front and rendered. Now...when moving the mouse over these views and clicking obviously I want to be able to create a geometric object and that coord...but how do I get the opengl coords and not the coord of the mouse icon which is over the view??
Advertisement
If your plane views have ortho projections, you can translate mouse coords into "view" coords and use those. For a rendered view, there is more than one point corresponding to each screen pixel, so if you want to use that as well, you''ll have to pick some arbitarary point at which to place the object.
---visit #directxdev on afternet <- not just for directx, despite the name
OK...how do I do that??
How do you make your views? Child windows, or something else? For child windows, you can do:
POINT pt = mouse coord in your main window;ClientToScreen(hMainWnd, &pt);ScreenToClient(hChildViewWnd, &pt); 
---visit #directxdev on afternet <- not just for directx, despite the name
If you want to have world coords of your current mouse position to place objects etc, follow these steps:
- get the 2D-coords in the current view
- call gluUnProject( , , 0, ...) to get the 3D point on the near plane
- then call again gluUnProject( , , 1, ...) to get the 3D point on the far plane
- use these two points as a vector and calculate the intersection between this vector and the corresponding plane in your view

Gero Gerber
So would you say this is correct:

GLdouble nx, ny, nz, fx, fy, fz;
GLdouble modelmat[16], projmat[16];
GLint viewport[4];

glGetDoublev(GL_MODELVIEW_MATRIX , modelmat);
glGetDoublev(GL_PROJECTION_MATRIX , projmat);
glGetIntegerv(GL_VIEWPORT , viewport);

gluUnProject(point.x, point.y, 0, modelmat, projmat, viewport, &nx, &ny, &nz);
gluUnProject(point.x, point.y, 1, modelmat, projmat, viewport, &fx, &fy, &fz);

cVector i(fx-nx, fy-ny, fz-nz); // farplane Vector - nearplane

At this point I need to calc the intersection?
If my memeory serves correct...the intersection calc is done by the dot product of the ray vector (i) and the normal of the plane?? Is this correct? If not...how then? and how is having the result of this dot product going to help??

Thanks in advance...
My math book says the following:

your vector is defined as:
vx = va + t * vb (where va is the starting point, t is the length scalar and vb is the direction vector)

the plane is defined as:
vn . vx = d (where vn is the normal vector, vx is a point on the plane and d is the distance of the point to the origin of the coordinate system)

so the intersection is the defined as followed:
vi = va + t * vb with t = (d - vn . va) / (vn . vb)

vi is the point of intersection

Hope this helps,
Gero Gerber

This topic is closed to new replies.

Advertisement