从窗口坐标系转换为世界坐标系的深度分量

3
我正在开发一个绘制100x100网格并允许用户单击单元格更改颜色的程序。
目前单击功能已经实现,但仅当从正面观察网格时(即camPos.z等于camLook.z),并且网格位于屏幕中心时才有效。
我最近几天卡在了从不同相机位置或不同屏幕区域查看网格时选择正确单元格上。
我的唯一猜测是深度缓冲区可能没有反映出相机的当前位置,或者缓冲区深度范围与相机的近和远值之间存在某些不一致。或者我应用投影/视图矩阵的方式可以用于显示图像,但在回传到管道时出现了问题。但我无法完全弄清楚。
(代码已更新/重构自原始发布) 顶点着色器:
#version 330

layout(location = 0) in vec4 position;

smooth out vec4 theColor;

uniform vec4 color;
uniform mat4 pv;

void main() {
  gl_Position = pv * position;
  theColor = color;
}

相机类(由projectionViewMatrix()的结果pv uniform生成):

Camera::Camera()
{
  camPos = glm::vec3(1.0f, 5.0f, 2.0f);
  camLook = glm::vec3(1.0f, 0.0f, 0.0f);

  fovy = 90.0f;
  aspect = 1.0f;
  near = 0.1f;
  far = 1000.0f;
}

glm::mat4 Camera::projectionMatrix()
{
  return glm::perspective(fovy, aspect, near, far);
}

glm::mat4 Camera::viewMatrix()
{
  return glm::lookAt(
    camPos,
    camLook,
    glm::vec3(0.0f, 1.0f, 0.0f)
  );
}

glm::mat4 Camera::projectionViewMatrix()
{
  return projectionMatrix() * viewMatrix();
}

// view controls

void Camera::moveForward()
{
  camPos.z -= 1.0f;
  camLook.z -= 1.0f;
}

void Camera::moveBack()
{
  camPos.z += 1.0f;
  camLook.z += 1.0f;
}

void Camera::moveLeft()
{
  camPos.x -= 1.0f;
  camLook.x -= 1.0f;
}

void Camera::moveRight()
{
  camPos.x += 1.0f;
  camLook.x += 1.0f;
}

void Camera::zoomIn()
{
  camPos.y -= 1.0f;
}

void Camera::zoomOut()
{
  camPos.y += 1.0f;
}

void Camera::lookDown()
{
  camLook.z += 0.1f;
}

void Camera::lookAtAngle()
{
  if (camLook.z != 0.0f)
    camLook.z -= 0.1f;
}

我正在尝试获取世界坐标的相机类中的特定函数(xy是屏幕坐标):

glm::vec3 Camera::experiment(int x, int y)
{
  GLint viewport[4];
  glGetIntegerv(GL_VIEWPORT, viewport);

  GLfloat winZ;
  glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);
  printf("DEPTH: %f\n", winZ);

  glm::vec3 pos = glm::unProject(
    glm::vec3(x, viewport[3] - y, winZ),
    viewMatrix(),
    projectionMatrix(),
    glm::vec4(0.0f, 0.0f, viewport[2], viewport[3])
  );

  printf("POS: (%f, %f, %f)\n", pos.x, pos.y, pos.z);

  return pos;
}

初始化和展示:

void init(void)
{
  glewExperimental = GL_TRUE;
  glewInit();

  glEnable(GL_DEPTH_TEST);
  glDepthMask(GL_TRUE);
  glDepthFunc(GL_LESS);
  glDepthRange(0.0f, 1.0f);

  InitializeProgram();
  InitializeVAO();
  InitializeGrid();

  glEnable(GL_CULL_FACE);
  glCullFace(GL_BACK);
  glFrontFace(GL_CW);
}

void display(void)
{
  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glUseProgram(theProgram);
  glBindVertexArray(vao);

  glUniformMatrix4fv(projectionViewMatrixUnif, 1, GL_FALSE, glm::value_ptr(camera.projectionViewMatrix()));

  DrawGrid();

  glBindVertexArray(0);
  glUseProgram(0);

  glutSwapBuffers();
  glutPostRedisplay();
}

int main(int argc, char** argv)
{
  glutInit(&argc, argv);

  glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH);
  glutInitContextVersion(3, 2);
  glutInitContextProfile(GLUT_CORE_PROFILE);

  glutInitWindowSize(500, 500);
  glutInitWindowPosition(300, 200);

  glutCreateWindow("testing");

  init();

  glutDisplayFunc(display);
  glutReshapeFunc(reshape);
  glutKeyboardFunc(keyboard);
  glutMouseFunc(mouse);
  glutMainLoop();
  return 0;
}

1
你现在在顶点着色器中手动应用的“offset”应该是你的ModelView矩阵的一部分。目前,你有一个将世界坐标系转换为视图坐标系的视图矩阵,并手动以Object-World的方式进行平移,但这种方式glm::unProject (...)并不知道,因为它没有在你的变换矩阵中。按照你当前的方式,对于任何非零偏移量,都不应该正确工作。 - Andon M. Coleman
@AndonM.Coleman 好的,这真的很有帮助。谢谢!我现在对问题有了一个想法。基本上,我是在重复使用同一个VBO来绘制每个单元格(每个单元格的坐标相同),并且每次调用显示函数时都设置了该统一偏移量(这是一个相当糟糕的解决方案)。接下来我想尝试的是为每个单元格创建一个单独的VBO(将其保存在我的Cell C++对象中,并具有正确的顶点位置),并完全删除该偏移量统一变量。希望我明天能有时间进行实现。 - andmcgregor
1个回答

3
实现拾取功能,将光线投射到光标下方其实非常简单。它几乎适用于任何投影和模型视图矩阵(除了某些无效的奇异情况,例如将整个场景转换为无穷大等)。
我编写了一个小演示程序,使用已过时的固定管线以保持简洁,但该代码也适用于着色器。它首先从OpenGL中读取矩阵:
glm::mat4 proj, mv;
glGetFloatv(GL_PROJECTION_MATRIX, &proj[0][0]);
glGetFloatv(GL_MODELVIEW_MATRIX, &mv[0][0]);
glm::mat4 mvp = proj * mv;

这里的mvp是你要传递给顶点着色器的参数。然后我们定义了两个点:
glm::vec4 nearc(f_mouse_x, f_mouse_y, 0, 1);
glm::vec4 farc(f_mouse_x, f_mouse_y, 1, 1);

这些是归一化空间中的近距离和远距离光标坐标(因此 f_mouse_xf_mouse_y[-1, 1] 区间内)。请注意,z 坐标不需要为 0 和 1,它们只需要是两个不同的任意数字。现在,我们可以使用 mvp 将它们转换为世界空间中的坐标。
nearc = glm::inverse(mvp) * nearc;
nearc /= nearc.w; // dehomog
farc = glm::inverse(mvp) * farc;
farc /= farc.w; // dehomog

注意在这里同质分割很重要。这给了我们光标在世界空间中的位置,其中定义了您的对象(除非它们有自己的模型矩阵,但很容易合并)。
最后,演示计算射线在纹理平面上(即您的100x100网格)与nearcfarc之间的交点:
glm::vec3 plane_normal(0, 0, 1); // plane normal
float plane_d = 0; // plane distance from origin
// this is the plane with the grid

glm::vec3 ray_org(nearc), ray_dir(farc - nearc);
ray_dir = glm::normalize(ray_dir);
// this is the ray under the mouse cursor

float t = glm::dot(ray_dir, plane_normal);
if(fabs(t) > 1e-5f)
    t = -(glm::dot(ray_org, plane_normal) + plane_d) / t;
else
    t = 0; // no intersection, the plane and ray is collinear
glm::vec3 isect = ray_org + t * ray_dir;
// calculate ray-plane intersection

float grid_x = N * (isect.x + 1) / 2;
float grid_y = N * (isect.y + 1) / 2;
if(t && grid_x >= 0 && grid_x < N && grid_y >= 0 && grid_y < N) {
    int x = int(grid_x), y = int(grid_y);
    // calculate integer coordinates

    tex_data[x + N * y] = 0xff0000ff; // red
    glBindTexture(GL_TEXTURE_2D, n_texture);
    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, N, N, GL_RGBA, GL_UNSIGNED_BYTE, &tex_data[0]);
    // change the texture to see
}
// calculate grid position in pixels

输出相当不错:

Texture paint

这只是一个20x20的纹理,但很容易扩展到100x100。你可以在这里获取完整的演示源代码和预编译的win32二进制文件。它依赖于glm。你可以用鼠标旋转或用WASD移动。
比平面更复杂的对象也是可能的,本质上是光线追踪。使用光标下的深度组件(窗口z)同样简单 - 只需注意规范化坐标([0, 1] vs. [-1, 1])。还要注意,读取z值可能会降低性能,因为它需要CPU/GPU同步。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接