如何用opengl实现鼠标拖动模型【在线等】
我的想法是:把鼠标点下的点和鼠标移动的点转换成世界坐标,然后计算偏移量,再调用gltranslatef函数,但移动偏差很大。屏幕坐标转世界坐标代码:
void CPointCloudView::screen2world(int x, int y, GLdouble* worldC)
{
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
GLfloat winX, winY, winZ;
GLdouble posX=0, posY=0, posZ=0;
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
glGetIntegerv(GL_VIEWPORT, viewport);
winX = (float)x;
winY = (float)viewport[3] - (float)y;
glReadPixels(x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);
gluUnProject(winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
worldC[0] = posX;
worldC[1] = posY;
worldC[2] = posZ;
}