我已经glDrawArrays
用 vbo 绘制了我的三角形。现在我想用不同的颜色为每个三角形着色。我试图按照我用来制作顶点的方式进行操作,但徒劳无功。我需要你的帮助。
这是我尝试绘制没有颜色的三角形的方法:
from OpenGL.GLUT import *
from OpenGL.GL import *
from OpenGL.GL import shaders
import numpy as np
VERTEX_SHADER = """
#version 330
in vec4 position;
void main() {
gl_Position = position;
}
"""
FRAGMENT_SHADER = """
#version 330
void main() {
gl_FragColor =
vec4(1.0f, 0.0f,0.0f,1.0f);
}
"""
shaderProgram = None
def initliaze():
global VERTEXT_SHADER
global FRAGMEN_SHADER
global shaderProgram
vertexshader = shaders.compileShader(VERTEX_SHADER, GL_VERTEX_SHADER)
fragmentshader = shaders.compileShader(FRAGMENT_SHADER, GL_FRAGMENT_SHADER)
shaderProgram = shaders.compileProgram(vertexshader, fragmentshader)
triangles = [-0.5, -0.5, 0.0 ,
0.5 , -0.5 , 0.0 ,
0.0 , 0.5 , 0.0 ,
0.5 , -0.5 , 0.0 ,
1.5 , -0.5 , 0.0 ,
1.0 , 0.5 , 0.0 ,
-0.5 , 0.5 , 0.0 ,
0.5 , 0.5 , 0.0 ,
0.0 , 1.5 , 0.0 ,
- 1.5 , -0.5 , 0.0 ,
-0.5 , -0.5 , 0.0 ,
-1.0 , 0.5 , 0.0 ,
-0.5 , -1.5 , 0.0,
0.5 , -1.5 , 0.0 ,
0.0 , -0.5 , 0.0
]
triangles = np.array(triangles, dtype=np.float32)
VBO = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, VBO)
glBufferData(GL_ARRAY_BUFFER, triangles.nbytes, triangles, GL_STATIC_DRAW)
position = glGetAttribLocation(shaderProgram, 'position')
glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 0, None)
glEnableVertexAttribArray(position)
def render():
global shaderProgram
global VAO
glClearColor(0, 0, 0, 1)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glUseProgram(shaderProgram)
glDrawArrays(GL_TRIANGLES, 0, 5)
glUseProgram(0)
glutSwapBuffers()
def main():
glutInit([])
glutInitWindowSize(640, 480)
glutCreateWindow("pyopengl with glut")
initliaze()
glutDisplayFunc(render)
glutMainLoop()
if __name__ == '__main__':
main()
正如我所说,我尝试为颜色复制同一行,但没有奏效。