I would like to ask a question about how do display a color of each vertex. VBO is created with:
def geometry(self):
# generate a new VBO and get the associated vbo_id
_id = 1
self.vbo_id = glGenBuffers (_id)
# bind VBO in order to use
glBindBuffer(GL_ARRAY_BUFFER, self.vbo_id)
# upload data to VBO
data = model_loader.Model_loader(filename = "body_1_geometry.stl")
vertices = data.vertices
normals = data.normals
self.N_vertices = len(vertices)
# data size in bytes
self.data_size_vertices = arrays.ArrayDatatype.arrayByteCount(vertices)
self.data_size_normals = arrays.ArrayDatatype.arrayByteCount(normals)
color = np.array([1.0, 0.0, 0.0], dtype='float32')
colors = np.tile(color, self.N_vertices)
colors = np.array(np.random.rand(3*self.N_vertices), dtype='float32')
data_size_colors = arrays.ArrayDatatype.arrayByteCount(colors)
glBufferData(GL_ARRAY_BUFFER,
self.data_size_vertices+self.data_size_normals+data_size_colors,
None,
GL_STATIC_DRAW)
glBufferSubData(GL_ARRAY_BUFFER,
0,
self.data_size_vertices,
vertices)
# delete data after saved to VBO
del vertices
glBufferSubData(GL_ARRAY_BUFFER,
self.data_size_vertices,
self.data_size_normals,
normals)
# delete data after saved to VBO
del normals
glBufferSubData(GL_ARRAY_BUFFER,
self.data_size_vertices+self.data_size_normals,
data_size_colors,
colors)
glBindBuffer(GL_ARRAY_BUFFER, self.vbo_id)
I create the VBO that has the data in the form of VBO = [vertices, normals, colors], vertices[0] = [x0, y0, z0], normals[0] = [xn0, yn0, zn0], colors[0] = [R0, G0, B0]. As I import data from .stl file, a normal vector is defined per triangle that is defined by 3 vertices and color vector is defined for every vertex.
VBO is displayed with:
glPushMatrix()
glLightfv(GL_LIGHT0, GL_POSITION, np.array([0.0, 0.0, 0.0]))
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, np.array([0.0, 10.0, 10.0]))
glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE )
glEnableClientState(GL_VERTEX_ARRAY)
glVertexPointer(3,
GL_FLOAT,
0,
None)
glEnableClientState(GL_COLOR_ARRAY)
glColorPointer(3,
GL_FLOAT,
0,
self.data_size_vertices+self.data_size_normals)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.vbo_id)
glDrawArrays(GL_TRIANGLES,
0,
self.N_vertices)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) # reset
glDisableClientState(GL_COLOR_ARRAY)
glDisableClientState(GL_VERTEX_ARRAY)
glPopMatrix()
I have problem with assigning each vertex the color from VBO, that each vertex will have its own color, or that the color of each triangle will be a mix of colors of all vertices of a triangle. The entire body is the same color, but it should be colorful