我目前正在使用Python和glfw和OpenGL. GL库开发一个简单的OpenGL程序.我try 渲染两个三角形,但出于某种原因,只有第二个三角形被渲染,第一个三角形不可见.

我把问题缩小到了这个最小的例子:

from OpenGL.GL import *
import numpy
import ctypes
import glfw

vssrc = '''
#version 330

layout ( location = 0 ) in vec3 aPos;
layout ( location = 1 ) in vec3 aNormal;

void main(void)
{
    gl_Position = vec4 ( aPos.x, aPos.y, aPos.z, 1.0 );
}
'''

# initialize fragment shader
fssrc = '''
#version 330

out vec4 FragColor;

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

glfw.init()

window=glfw.create_window(800,600,'test',None,None)
glfw.make_context_current(window)
glViewport ( 0, 0, 800, 600)

vao=glGenVertexArrays(1)
glBindVertexArray(vao)

vbo1=glGenBuffers(1)
vs=glCreateShader(GL_VERTEX_SHADER)
glShaderSource ( vs, vssrc )
glCompileShader(vs)

fs=glCreateShader(GL_FRAGMENT_SHADER)
glShaderSource ( fs, fssrc )
glCompileShader(fs)

p1=glCreateProgram()
glAttachShader(p1, vs)
glAttachShader(p1, fs)
glLinkProgram(p1)
glUseProgram(0)

vbo2=glGenBuffers(1)

vs2=glCreateShader(GL_VERTEX_SHADER)
glShaderSource ( vs2, vssrc )
glCompileShader(vs2)

fs2=glCreateShader(GL_FRAGMENT_SHADER)
glShaderSource ( fs2, fssrc )
glCompileShader(fs2)

p2=glCreateProgram()
glAttachShader(p2, vs2)
glAttachShader(p2, fs2)
glLinkProgram(p2)
glUseProgram(0)

glDeleteShader(vs)
glDeleteShader(fs)
glDeleteShader(vs2)
glDeleteShader(fs2)

#           vertices        normals
buffer1 = [-0.5, -0.5, 0,   0, 0, 1,
            0.5, -0.5, 0,   0, 0, 1,  
            0.5,  0.5, 0,   0, 0, 1]

#           vertices        normals
buffer2 = [ 0.5,  0.5, 0,   0, 0, 1,
           -0.5,  0.5, 0,   0, 0, 1, 
           -0.5, -0.5, 0,   0, 0, 1]

glBindBuffer(GL_ARRAY_BUFFER, vbo1)
glBufferData(GL_ARRAY_BUFFER, 72, numpy.array(buffer1, dtype=numpy.float32), GL_STATIC_DRAW)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(None))
glEnableVertexAttribArray(0)
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(12))
glEnableVertexAttribArray(1)
glBindBuffer(GL_ARRAY_BUFFER, 0)

glBindBuffer(GL_ARRAY_BUFFER, vbo2)
glBufferData(GL_ARRAY_BUFFER, 72, numpy.array(buffer2, dtype=numpy.float32), GL_STATIC_DRAW)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(None))
glEnableVertexAttribArray(0)
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(12))
glEnableVertexAttribArray(1)
glBindBuffer(GL_ARRAY_BUFFER, 0)

glBindVertexArray(0)

glClearColor(0.0, 0.0, 0.0, 1.0)
glEnable ( GL_DEPTH_TEST )

while not glfw.window_should_close(window):
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
    glBindVertexArray(vao)

    glUseProgram(p1)
    glBindBuffer(GL_ARRAY_BUFFER, vbo1)
    glDrawArrays(GL_TRIANGLES,0,3)
    glBindBuffer(GL_ARRAY_BUFFER, 0)

    glUseProgram(p2)
    glBindBuffer(GL_ARRAY_BUFFER, vbo2)
    glDrawArrays(GL_TRIANGLES,0,3)
    glBindBuffer(GL_ARRAY_BUFFER, 0)

    glUseProgram(0)

    glBindVertexArray(0)

    glfw.swap_buffers(window)
    glfw.poll_events()

glfw.terminate()`

在本例中,我为两个三角形创建了两组顶点数据(buffer1和buffer2),并将它们绑定到单独的顶点缓冲对象(VBO).我还使用两个单独的着色程序(p1和p2)来渲染每个三角形.

尽管为两个三角形设置了数据和着色器,但屏幕上只有第二个三角形可见.我已经判断了顶点数据、着色程序和缓冲区绑定,但似乎找不到问题所在.

导致此问题的原因可能是什么?

推荐答案

You must create 2 Vertex Array Objects. Changing the GL_ARRAY_BUFFER binding has no effect on the draw call. The buffer is connected with the vertex array attribute and this connection is stored in the state vector of the VAO. The connection is created when glVertexAttribPointer is called, then the ID of the buffer currently bound to the target GL_ARRAY_BUFFER is stored in the VAO together with the attribute specification.
Therefore, create a VAO for each mesh and bind the VAO if you want to draw the mesh.

vao1 = glGenVertexArrays(1)
glBindVertexArray(vao1)
glBindBuffer(GL_ARRAY_BUFFER, vbo1)
glBufferData(GL_ARRAY_BUFFER, 72, numpy.array(buffer1, dtype=numpy.float32), GL_STATIC_DRAW)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(None))
glEnableVertexAttribArray(0)
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(12))
glEnableVertexAttribArray(1)

vao2 = glGenVertexArrays(1)
glBindVertexArray(vao2)
glBindBuffer(GL_ARRAY_BUFFER, vbo2)
glBufferData(GL_ARRAY_BUFFER, 72, numpy.array(buffer2, dtype=numpy.float32), GL_STATIC_DRAW)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(None))
glEnableVertexAttribArray(0)
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(12))
glEnableVertexAttribArray(1)

# [...]

while not glfw.window_should_close(window):
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

    glUseProgram(p1)
    glBindVertexArray(vao1)
    glDrawArrays(GL_TRIANGLES,0,3)

    glUseProgram(p2)
    glBindVertexArray(vao2)
    glDrawArrays(GL_TRIANGLES,0,3)

    glfw.swap_buffers(window)
    glfw.poll_events()

Python相关问答推荐

Python daskValue错误:无法识别的区块管理器dask -必须是以下之一:[]

Pandas实际上如何对基于自定义的索引(integer和非integer)执行索引

仿制药的类型铸造

为什么tkinter框架没有被隐藏?

从收件箱中的列中删除html格式

将两只Pandas rame乘以指数

我们可以为Flask模型中的id字段主键设置默认uuid吗

Godot:需要碰撞的对象的AdditionerBody2D或Area2D以及queue_free?

Streamlit应用程序中的Plotly条形图中未正确显示Y轴刻度

DataFrames与NaN的条件乘法

将9个3x3矩阵按特定顺序排列成9x9矩阵

Django RawSQL注释字段

在Python中使用yaml渲染(多行字符串)

为什么在FastAPI中创建与数据库的连接时需要使用生成器?

处理Gekko的非最优解

获取PANDA GROUP BY转换中的组的名称

504未连接IB API TWS错误—即使API连接显示已接受''

SpaCy:Regex模式在基于规则的匹配器中不起作用

在pandas中,如何在由两列加上一个值列组成的枢轴期间或之后可靠地设置多级列的索引顺序,

为什么按下按钮后屏幕的 colored颜色 保持不变?