|
|
gluUnProject
此函数的具体用途是将一个OpenGL视区内的二维点转换为与其对应的场景中的三维坐标。
这个函数在glu.h中的原型定义如下:
int APIENTRY gluUnProject (
GLdouble winx,
GLdouble winy,
GLdouble winz,
const GLdouble modelMatrix[16],
const GLdouble projMatrix[16],
const GLint viewport[4],
GLdouble *objx,
GLdouble *objy,
GLdouble *objz);
其中前三个值表示窗口坐标,中间三个分别为模型视图矩阵(Model/View Matrix),投影矩阵(Projection Matrix)和视区(ViewPort),最后三个为输出的世界坐标值。
现在我想通过这个函数用于物体随鼠标移动 代码如下:
gluPerspective (45.0f, (GLfloat)(width)/(GLfloat)(height),1.0f, 100.0f);
glLoadIdentity(); // 重设矩阵
glPushMatrix();
GLdouble modelview[16];
GLdouble projection[16];
GLint viewport[4];
RECT window; // 用来存窗口位置
GetClientRect (g_window->hWnd,&window); // 取窗口位置
glGetDoublev (GL_MODELVIEW_MATRIX, modelview);
glGetDoublev (GL_PROJECTION_MATRIX, projection);
glGetIntegerv (GL_VIEWPORT, viewport);
gluUnProject( (GLdouble) mouse_x, (GLdouble)window.bottom-mouse_y, -0.06,
modelview, projection, viewport,
&move_x, &move_y, &world_z);
glTranslated(move_x,move_y,-6.0f);
Object(0.14,0.04,0); //nehe例子 传入参数画图象
glPopMatrix();
问题是关于Z轴的问题
GLdouble winz,
深度值winZ的范围为0.0~1.0,近截面处为0.0,远截面处为1.0,其他介于0.0和1.0之间。
我认为在 gluPerspective (45.0f, (GLfloat)(width)/(GLfloat)(height),1.0f, 100.0f);
这句后 我的Z轴变换应该是 1.0-100.f 对应的winz应该是除以100 也就是我在 -6.0上画图象,应该用 -0.06传入
可是结果图象只在一个很小的区域内活动,我是哪里理解错了吗?
[em4] |
|