How to move the object to the position of mouse(opengl) -
as title, trying gltranslate(). have know relation between window screen coordinates , opengl coordinate.
i mouse position in window screen coordinate glutmousefunc(). , according have found online, need function gluunproject() transfer window screen coordinate glutmousefunc() opengl coordinate.
the result correct without function glulookat() , glfrustum(). these 2 function, got complete wrong answers.
this initial setting:
glclear(gl_color_buffer_bit | gl_depth_buffer_bit ); glmatrixmode(gl_modelview); glloadidentity(); glulookat(1.9, 1.75, 1.7, 0.25, 0.25, 0.25, 0, 1, 0); glpolygonmode(gl_front_and_back, gl_line);/*show planes of polygon lines*/ glpointsize(20.0); glbegin(gl_points); glvertex3f(0.5, 0.5, 0.5); glvertex3f(0, 0, 0); glvertex3f(0.25, 0.25, 0.25); glend(); glmatrixmode(gl_projection); glloadidentity(); glfrustum(-0.4, 0.4, -0.4, 0.4, 1.1, 10); glutswapbuffers();
in glutmousefunc(mouse):
void mouse(int button, int state, int mouse_x, int mouse_y){ if(button == glut_left_button && state == glut_down){ int viewport[4]; double modelview[16]; double projection[16]; double windowx,windowy,z_cursor; double x,y,z; glgetintegerv(gl_viewport, viewport); glgetdoublev(gl_modelview_matrix, modelview); glgetdoublev(gl_projection_matrix, projection); windowx= (double)mouse_x; windowy= (double)viewport[3]-(double)mouse_y; glreadpixels(windowx, windowy, 1, 1, gl_depth_component, gl_float, &z_cursor); gluunproject(windowx, windowy, z_cursor, modelview, projection, viewport, &x, &y, &z); printf("%lf %lf %lf\n", x, y, z); } }
does know why errors happened , how fix it? or there way achieve goal "moving object position of mouse"?
Comments
Post a Comment