相关阅读
实例化卡了一个月...网上能找到OpenGL实例化,能找到pyopengl,但是找不到pyopengl实例化.最后对着learnopengl网站上的C+OpenGL搞出来了,分享下代码以及个人理解(注释)
#处理部分
【资料图】
#偏移值,这里为xy坐标
L = [[0.5,0.5],
[0.5,-0.5],
[-0.5,-0.5]]
data = numpy.array(L,dtype=numpy.float32)
#实例化的VBO
instanceVBO = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, instanceVBO)
glBufferData(GL_ARRAY_BUFFER, data.nbytes, data, GL_STATIC_DRAW)
glBindBuffer(GL_ARRAY_BUFFER, 0)
#原值,这里为拼接矩形的两个三角形的六个顶点坐标
quadVertice = [-0.03,-0.03,
0.03,-0.03,
-0.03,0.03,
-0.03,0.03,
0.03,-0.03,
0.03,0.03]
quadVertices = numpy.array(quadVertice,dtype=numpy.float32)
#原值的VAO和VBO
quadVAO = glGenVertexArrays(1)
quadVBO = glGenBuffers(1)
glBindVertexArray(quadVAO)
glBindBuffer(GL_ARRAY_BUFFER, quadVBO)
glBufferData(GL_ARRAY_BUFFER, quadVertices.nbytes, quadVertices, GL_STATIC_DRAW)
#0表示顶点着色器中layout (location = 0),即aPos,1则是aOffset
glEnableVertexAttribArray(0)
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, None)#上面已经是原值的VBO了,不需要再绑一次
glEnableVertexAttribArray(1)
glBindBuffer(GL_ARRAY_BUFFER, instanceVBO)#使用之前实例化的VBO
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, None)
glBindBuffer(GL_ARRAY_BUFFER, 0)
glVertexAttribDivisor(1, 1)#每隔一个实例改变一次属性,据说设为1比不设置效率高
#绘制部分
glBindVertexArray(quadVAO)#使用原值的VAO
glDrawArraysInstanced(GL_TRIANGLES, 0, 6, 3)#绘制3个图形,每个6个顶点
glBindVertexArray(0)
以下为测试截图,实例化的效果对得起翻一个月的资料
然后在解决内存泄露问题时又发现了Transform Feedback,可以在gpu中计算顶点位置变化,感觉是比实例化更大的坑(
相关阅读
2023-02-04
2023-02-04
2023-02-04
2023-02-03
2023-02-03
2023-02-03
2023-02-03
2023-02-03
2023-02-03
2023-02-03
2021-12-02
2021-12-02
2021-12-02
2021-12-02