1

我已经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()

正如我所说,我尝试为颜色复制同一行,但没有奏效。

4

1 回答 1

1

您必须为颜色的顶点着色器添加另一个属性。进一步为颜色定义一个顶点着色器输出并将属性分配给输出:

#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;
}

对于每个顶点坐标,您必须指定一个颜色属性。这意味着 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)

于 2019-12-13T14:33:09.673 回答