在 PyOpenGL 中绘制十字准线

Drawing a crosshair in PyOpenGL

我在 OpenGL 的 3D 场景中有一个工作相机,我决定在中间画一个十字准线。为此,我想为十字准线(以及最终的 HUD)和 3D 场景使用单独的着色器。我设法使用 glDrawArrays 和一个带有线顶点的 VBO 来完成一些工作。

此设置的问题是十字一直延伸到 window 的边界,即使我指定了小坐标以便它正好位于屏幕中央。

这是我的着色器(按顺序是顶点着色器和片段着色器):

#version 450 core

layout (location = 0) in vec2 aPos;

void main() {
    gl_Position = vec4(aPos, 0, 0);
}
#version 450 core

out vec4 FragColor;

void main() {
    FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}

如您所见,它们非常简单,几乎不使用坐标。绘图过程看起来像这样:

crosshair = np.array([
  -0.02, 0,
   0.02, 0,
   0, -0.02,
   0,  0.02], dtype = 'float32')
vbo_2d, vao_2d = glGenBuffers(1), glGenVertexArrays(1)

glBindVertexArray(vao_2d)
glBindBuffer(GL_ARRAY_BUFFER, vbo_2d)
glBufferData(GL_ARRAY_BUFFER, crosshair, GL_STATIC_DRAW)

glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 8, ctypes.c_void_p(0))
glEnableVertexAttribArray(0)

while not window.check_if_closed():
    #render 3d stuff with another shader program and other vbos ad vaos

    glBindVertexArray(0)

    shader_program_2d.use()
    glBindVertexArray(vao_2d)
    glDrawArrays(GL_LINES, 0, 4)

    #swap buffers etc.

这是我的 window 的样子:

提前致谢!

问题是 Homogeneous clip space coordinate in the vertex shader. The normalized device space coordinate is computed by dividing the .xyz components of gl_Position by it's .w component (Perspective divide) 的计算。
由于 .w 分量为 0,因此得到的坐标变为无穷大(坐标本身为 0 除外)。

gl_Position = vec4(aPos, 0, 0);

gl_Position = vec4(aPos, 0, 1.0);