在PyOpenGL中绘制十字准线

问题描述 投票:0回答:1

我在OpenGL的3D场景中有一个正在工作的相机,因此我决定在中间绘制十字准线。为此,我想为十字线(最终是HUD)和3D场景使用单独的着色器。我设法使用glDrawArrays和其中带有线顶点的VBO使某些东西起作用。

此设置的问题是,即使我指定了较小的坐标,十字也一直延伸到窗口的边界,所以它恰好位于屏幕的中心。

这里是我的着色器(依次是顶点着色器和片段着色器:]

#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.

这是我的窗口外观:

OpenGL Screenshot

提前感谢!

python opengl shader vbo pyopengl
1个回答
0
投票

问题是Homogeneous剪辑空间坐标。标准化设备空间坐标是通过将.xyzgl_Position分量除以.w分量(Perspective divide)来计算的。由于.w分量为0,因此得到的距离变为无限(坐标本身为0除外)。

gl_Position = vec4(aPos, 0, 0);

gl_Position = vec4(aPos, 0, 1.0);
© www.soinside.com 2019 - 2024. All rights reserved.