我已经用vbo用glDrawArrays
绘制了三角形。现在我想用不同的颜色给每个三角形上色。我试图遵循以前制作顶点的方式,但是徒劳无功。
我需要你的帮助。
这是ii试图绘制没有颜色的三角形的尝试:
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()
正如我所说,我试图复制同一行的颜色,但是没有用。
答案 0 :(得分:0)
您必须向颜色的顶点着色器添加另一个属性。进一步为颜色定义一个顶点着色器输出,并将属性分配给输出:
#version 330
in vec4 position;
in vec4 color;
out vec4 v_color;
void main() {
v_color = color;
gl_Position = position;
}
将相应的颜色输入(相同名称)添加到片段着色器,并将输入分配给gl_FragColor
:
#version 330
in vec4 v_color;
void main() {
gl_FragColor = v_color;
}
您必须为每个顶点坐标指定一个color属性。这意味着15个顶点坐标具有15种颜色。如果要均匀地为每个三角形着色,则1个三角形的3个顶点中的每个顶点必须具有相同的颜色。 5种不同的颜色,每种颜色连续3次给出颜色属性数组:
colors = [[1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 1, 0], [1, 1, 1]]
colorAttrib = [color for color in colors for corner in range(3)]
您必须创建2个缓冲区,其中1个用于三角形,一个用于颜色,并且必须指定两个顶点属性数组:
position = glGetAttribLocation(shaderProgram, 'position')
color = glGetAttribLocation(shaderProgram, 'color')
triangles = np.array(triangles, dtype=np.float32)
colorAttrib = np.array(colorAttrib, dtype=np.float32)
VBO = glGenBuffers(2)
glBindBuffer(GL_ARRAY_BUFFER, VBO[0])
glBufferData(GL_ARRAY_BUFFER, triangles.nbytes, triangles, GL_STATIC_DRAW)
glBindBuffer(GL_ARRAY_BUFFER, VBO[1])
glBufferData(GL_ARRAY_BUFFER, colorAttrib.nbytes, colorAttrib, GL_STATIC_DRAW)
glBindBuffer(GL_ARRAY_BUFFER, VBO[0])
glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 0, None)
glEnableVertexAttribArray(position)
glBindBuffer(GL_ARRAY_BUFFER, VBO[1])
glVertexAttribPointer(color, 3, GL_FLOAT, GL_FALSE, 0, None)
glEnableVertexAttribArray(color)
此外,glDrawArrays
的最后一个参数必须是顶点数(15)而不是图元(5):
glDrawArrays(GL_TRIANGLES, 0, 5)
glDrawArrays(GL_TRIANGLES, 0, 15)