View Full Version : getting mouse co-ord or selecting an area on the screen

09-23-2002, 07:37 AM

I am developing one application using MFC and OpenGL.

I am displaying some 3D surface (This surface is determined through my simulation program)

Now I want to measure the length of that surface ..(the interface should be similar to that of any CAD package). So what I want to do is I will click the mouse at one particular location and then click on the other..then I should get to know the length between those two points...

Also the other option which I want is the select a rectangular area using mouse click and then find ...things like surface area ..etc....

So my question is how should I go about getting these clicked points co-ordinates...


09-23-2002, 08:32 AM
I have written following code in
OnLbuttondown event

GLdouble model[16];
double x;
double y;
double z;
GLdouble proj[16];
GLint view[4];
glGetDoublev(GL_MODELVIEW_MATRIX, model);
glGetDoublev(GL_PROJECTION_MATRIX, proj);
glGetIntegerv(GL_VIEWPORT, view);
gluUnProject((GLdouble)point.x, (GLdouble)point.y, (GLdouble)0, model, proj, view, &x, &y, &z );
CString mstr;

mstr.Format("x = %d, y = %d, z = %d", x, y, z);

But I am getting wierd coordinates
like x=-1000030243, y=-1293012312,z=-1231231232

I am using Perspective view..

Can any body help me what should i do..


09-23-2002, 10:55 AM
Odd - I see your z co-ord is zero? This is surely not correct. You should have a z coord in order to recover the correct x and y for the projection, given the projection z coord.

Usually (often), picking is used to recover a z coord in the buffer associated with a given x, y screen-space coord (your mouse position) - gluUnProject will then recover the correct x, y for that z.

Hope this helps!

(you can use OpenGL selection to get the correct z coord).

09-23-2002, 01:47 PM
I did not understand Z co-ordinate zero..?
How did you get deduce the z value to zero...
I didn't understand...

I am relatively new to the Opengl...

Can you explain in little bit more detail..


09-23-2002, 02:26 PM
He means when you call gluUnProject() the third parameter is zero.


09-24-2002, 07:03 AM
Ok I got it...thanks a lot
May be I was too tired to pay proper attention to my code...

Can anybody explain ...as to how can I get the z value..
Or what should I write in my gluunproject()for the z co-ordinate...

I will appreciate your help