void CGLFWTestProjectDlg::MouseMoved(int x, int y)
{
GLdouble dx, dy, dz;
GLint viewport [4] ;
glGetIntegerv( GL_VIEWPORT, viewport ) ;
GLdouble modmatrix[16], projmatrix[16];
glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);
glGetDoublev(GL_MODELVIEW_MATRIX, modmatrix);
gluUnProject(x, y, 1, modmatrix, projmatrix, viewport, &dx, &dy, &dz);
CString S;
S.Format("%lf, %lf, %lf\r\n", dx,dy, dz);
ATLTRACE(S);
}
// the world is setup like this:
// Get current time
t = glfwGetTime();
// Get window size
glfwGetWindowSize( &width, &height );
// Make sure that height is non-zero to avoid division by zero
height = height < 1 ? 1 : height;
// Set viewport
glViewport( 0, 0, width, height );
// Clear color and depht buffers
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
// Set up projection matrix
glMatrixMode( GL_PROJECTION ); // Select projection matrix
glLoadIdentity(); // Start with an identity matrix
gluPerspective( // Set perspective view
65.0, // Field of view = 65 degrees
(double)width/(double)height, // Window aspect (assumes square pixels)
1.0, // Near Z clipping plane
100.0 // Far Z clippling plane
);
// Set up modelview matrix
glMatrixMode( GL_MODELVIEW ); // Select modelview matrix
glLoadIdentity(); // Start with an identity matrix
gluLookAt( // Set camera position and orientation
0.0, 0.0, 10.0, // Camera position (x,y,z)
0.0, 0.0, 0.0, // View point (x,y,z)
0.0, 1.0, 0.0 // Up-vector (x,y,z)
);
// Here is where actual OpenGL rendering calls would begin...
// Let us draw a triangle, with color!
//glRotatef( 360.0f * (float)t, 0.0f, 1.0f, 0.0f );
drawOneLine(-5,0, 5, 0);
drawOneLine(-5,0, -5, 5);
drawOneLine(-5,5,5,5);
drawOneLine(5,5, 5,0);
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
by: StormSeedPosted on 2009-01-20 at 03:00:57ID: 23418822
Hi,
The important thing to remember here is that a point on the screen represents a line in the 3-D world.
The third parameter of gluUnProject is the screen Z coordinate, which represents how deep into the screen you are clicking. 0 is on the near clipping plane and 1 is on the far clipping plane and any number in between is between them.
If you're trying to select an in-world object with the mouse, i would recommend using OpenGL's picking functionality instead.
If, however, you are trying to select a certain point in the world, you are on the right track. You should use gluUnProject with windowZ equal to 0 and equal to 1 to get the two vertices of your trace line-segment and calculate the intersection between this and whatever plane the user should be selecting the point on. (Like the ground, for instance.)