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.
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:
Add an corresponding input (same name) for the color to the fragment shader and assign the input to
gl_FragColor
: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:
You've to create 2 buffers, 1 for the triangles and one for the colors and you've to specify both vertex attribute arrays:
Furthermore the last parameter of
glDrawArrays
has to be the number of vertices (15) rather than primitives (5):glDrawArrays(GL_TRIANGLES, 0, 5)