C++ OpenGL gluUnProject查找错误坐标
glunproject查找错误的坐标。我在黑色背景上绘制了贪婪,单元格尺寸为1x1。我试图检测单元格中的点击,但我得到了错误的坐标(不是1-1)。 我知道那是OpenGL 1.1旧版,我必须使用它。我对它不太在行,大多数时候我使用2.1或3.1 渲染代码:C++ OpenGL gluUnProject查找错误坐标,c++,opengl,glu,C++,Opengl,Glu,glunproject查找错误的坐标。我在黑色背景上绘制了贪婪,单元格尺寸为1x1。我试图检测单元格中的点击,但我得到了错误的坐标(不是1-1)。 我知道那是OpenGL 1.1旧版,我必须使用它。我对它不太在行,大多数时候我使用2.1或3.1 渲染代码: glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluPerspective(45.0f, width() / (float)heigh
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluPerspective(45.0f, width() / (float)height(), 0.1f, 100.0f);
gluLookAt(m_camera->getX(), m_camera->getY(), m_camera->getZ(),
m_camera->getX(), m_camera->getY(), m_camera->getZ() - 1.0f,
0.0f, 1.0f, 0.0f);
glPushMatrix();
int i;
/*
* Render grid
*/
for(i = 0; i <= m_mapProperties->getWidth(); i++)
{
glm::vec3 position(i * m_mapProperties->getGridWidth(), 0.0f, 0.0f);
glm::vec3 scale(0.05f, m_mapProperties->getHeight() * m_mapProperties->getGridHeight(), 0.0f);
RenderUtil::render(position, scale);
}
for(i = 0; i <= m_mapProperties->getHeight(); i++)
{
glm::vec3 position(0.0f, i * m_mapProperties->getGridHeight(), 0.0f);
glm::vec3 scale(m_mapProperties->getWidth() * m_mapProperties->getGridWidth(), 0.05f, 0.0f);
RenderUtil::render(position, scale);
}
for(i = 0; i < m_width * m_height; i++)
if(m_map[i])
m_map[i]->render();
glPopMatrix();
glClear(GL\u颜色\u缓冲\u位);
glMatrixMode(GLU模型视图);
glLoadIdentity();
(45.0f,宽度()/(浮动)高度(),0.1f,100.0f);
gluLookAt(m_camera->getX(),m_camera->getY(),m_camera->getZ(),
m_camera->getX(),m_camera->getY(),m_camera->getZ()-1.0f,
0.0f、1.0f、0.0f);
glPushMatrix();
int i;
/*
*渲染网格
*/
对于(i=0;i getWidth();i++)
{
glm::vec3位置(i*m_mapProperties->getGridWidth(),0.0f,0.0f);
glm::vec3比例(0.05f,m_mapProperties->getHeight()*m_mapProperties->getGridHeight(),0.0f);
RenderUtil::渲染(位置、比例);
}
对于(i=0;i getHeight();i++)
{
glm::vec3位置(0.0f,i*m_mapProperties->getGridHeight(),0.0f);
glm::vec3比例(m_mapProperties->getWidth()*m_mapProperties->getGridWidth(),0.05f,0.0f);
RenderUtil::渲染(位置、比例);
}
对于(i=0;irender();
glPopMatrix();
单击“代码”:
QPoint cursorPos = event->pos();
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
glGetIntegerv(GL_VIEWPORT, viewport);
GLfloat winX, winY, winZ = 0.0f;
GLdouble x, y, z;
winX = (float)cursorPos.x();
winY = (float)viewport[3] - (float)cursorPos.y();
glReadPixels(winX, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);
winZ = 0.0f;
winZ = -m_camera->getZ();
gluUnProject(winX, winY, winZ, modelview, projection, viewport, &x, &y, &z);
qDebug() << x
<< y;
QPoint cursorPos=event->pos();
闪烁视口[4];
GLdouble modelview[16];
gl双投影[16];
glGetDoublev(GL_模型视图_矩阵,模型视图);
glGetDoublev(GL_投影矩阵,投影);
glGetIntegerv(GL_视口,视口);
GLfloat winX,winY,winZ=0.0f;
glx,y,z;
winX=(float)cursorPos.x();
winY=(float)视口[3]-(float)cursorPos.y();
glReadPixels(winX、winY、1、1、GL_深度分量、GL_浮点和winZ);
winZ=0.0f;
winZ=-m_摄像头->getZ();
gluUnProject(winX、winY、winZ、模型视图、投影、视口、&x、&y、&z);
qDebug()glPopMatrix()
正在丢弃上一个矩阵。glGetxxx()将返回另一个矩阵。我回家后会尝试。