how to color each triangle differently using vbo

2.5k views Asked by At

I already draw my triangles with glDrawArrays with vbo. Now i wanna color each triangle with a different color. I tried to follow the way i used to make the vertex, but in vain. I need your help.

Here what ii tried for drawing the triangles without colors:

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()

As I said I tried to copy the same line for the colors but it didn't worked.

1

There are 1 answers

0
Rabbid76 On BEST ANSWER

You've to add another attribute to the vertex shader for the color. Further define a vertex shader output for the color and assign the attribute to the output:

#version 330

in vec4 position;
in vec4 color;

out vec4 v_color; 

void main() {
    v_color = color;
    gl_Position = position;
}

Add an corresponding input (same name) for the color to the fragment shader and assign the input to gl_FragColor:

#version 330

in vec4 v_color;

void main() {
    gl_FragColor =  v_color;
}

For each vertex coordinate you've to specify a color attribute. That means 15 colors for 15 vertex coordinates. If you want to color each triangle uniformly, the each of the 3 vertices of 1 triangle has to have the same color. 5 different colors, each color 3 times in a row gives the array of color attributes:

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)]

You've to create 2 buffers, 1 for the triangles and one for the colors and you've to specify both vertex attribute arrays:

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)

Furthermore the last parameter of glDrawArrays has to be the number of vertices (15) rather than primitives (5):

glDrawArrays(GL_TRIANGLES, 0, 5)

glDrawArrays(GL_TRIANGLES, 0, 15)