2016-11-17 9 views
-1

gluUnProjectが間違った座標を見つける。私は黒の背景にセル寸法1x1の欲望を描いています。私はセル内のクリックを検出しようとしますが、座標が間違っています(1 - 1ではありません)。 私はそれがOpenGL 1.1であることを知っています。私はそれでうまくいかず、私が2.1または3.1を使うほとんどの時間。OpenGL gluUnProjectが間違った座標を見つける

コードレンダリング:

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(); 

クリックしてコード:

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; 

答えて

0

glPopMatrix()はあなたの最後の行列を破棄されます。 glGetxxx()は別の行列を返します。

+0

私は家に帰るときに挑戦します。 – nekicneko99

関連する問題