def _init_vao_(self): self.vao_ = GL.glGenVertexArrays(1) GL.glBindVertexArray(self.vao_) GL.glBindBuffer(GL.GL_ARRAY_BUFFER, self.vbo_) GL.glEnableVertexAttribArray(self.attribLoc_[0]) GL.glVertexAttribPointer(self.attribLoc_[0], 4, GL.GL_FLOAT, GL.GL_FALSE, 7 * FLOAT_SIZE, GL.GLvoidp(0)) GL.glEnableVertexAttribArray(self.attribLoc_[1]) GL.glVertexAttribPointer(self.attribLoc_[1], 3, GL.GL_FLOAT, GL.GL_FALSE, 7 * FLOAT_SIZE, GL.GLvoidp(4 * FLOAT_SIZE)) GL.glBindBuffer(GL.GL_ARRAY_BUFFER, self.vbo2_) GL.glEnableVertexAttribArray(self.attribLoc_[2]) GL.glVertexAttribPointer(self.attribLoc_[2], 1, GL.GL_INT, GL.GL_FALSE, 0, GL.GLvoidp(0)) GL.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, self.ibo_) GL.glBindVertexArray(0)
def create_object(shader): # Create a new VAO (Vertex Array Object) and bind it vertex_array_object = GL.glGenVertexArrays(1) GL.glBindVertexArray(vertex_array_object) # Generate buffers to hold our vertices vertex_buffer = GL.glGenBuffers(1) GL.glBindBuffer(GL.GL_ARRAY_BUFFER, vertex_buffer) # Get the position of the 'position' in parameter of our shader and bind it. position = GL.glGetAttribLocation(shader, 'position') GL.glEnableVertexAttribArray(position) coord = GL.glGetAttribLocation(shader, 'coord') GL.glEnableVertexAttribArray(coord) # Describe the 'position' data layout in the buffer GL.glVertexAttribPointer(position, 2, GL.GL_FLOAT, False, 16, GL.GLvoidp(0)) GL.glVertexAttribPointer(coord, 2, GL.GL_FLOAT, False, 16, GL.GLvoidp(8)) # Send the data over to the buffer GL.glBufferData(GL.GL_ARRAY_BUFFER, 96, vertices, GL.GL_STATIC_DRAW) # Unbind the VAO first (Important) GL.glBindVertexArray(0) # Unbind other stuff GL.glDisableVertexAttribArray(position) GL.glDisableVertexAttribArray(coord) GL.glBindBuffer(GL.GL_ARRAY_BUFFER, 0) return vertex_array_object
def init_vao(self): self.vao1 = GL.glGenVertexArrays(1) GL.glBindVertexArray(self.vao1) color_data_offset = 3 * self.size_float * NUMBER_OF_VERTICES GL.glBindBuffer(GL.GL_ARRAY_BUFFER, self.vbo) GL.glEnableVertexAttribArray(0) GL.glEnableVertexAttribArray(1) GL.glVertexAttribPointer(0, 3, GL.GL_FLOAT, GL.GL_FALSE, 0, None) GL.glVertexAttribPointer(1, 4, GL.GL_FLOAT, GL.GL_FALSE, 0, GL.GLvoidp(color_data_offset)) GL.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, self.ibo) GL.glBindVertexArray(0) self.vao2 = GL.glGenVertexArrays(1) GL.glBindVertexArray(self.vao2) pos_data_offset = 3 * self.size_float * (NUMBER_OF_VERTICES / 2) color_data_offset += 4 * self.size_float * (NUMBER_OF_VERTICES / 2) #Use the same buffer object previously bound to GL_ARRAY_BUFFER. GL.glEnableVertexAttribArray(0) GL.glEnableVertexAttribArray(1) GL.glVertexAttribPointer(0, 3, GL.GL_FLOAT, GL.GL_FALSE, 0, GL.GLvoidp(pos_data_offset)) GL.glVertexAttribPointer(1, 4, GL.GL_FLOAT, GL.GL_FALSE, 0, GL.GLvoidp(color_data_offset)) GL.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, self.ibo) GL.glBindVertexArray(0)
def __init__(self, data, numElements=None, usage=GL.GL_DYNAMIC_DRAW): if isinstance(data, np.ndarray): spec = data.dtype elif isinstance(data, np.dtype): spec = data else: raise ValueError( 'first argument should be numpy data type or array') self.id = GL.glGenVertexArrays(1) GL.glBindVertexArray(self.id) self.buffer = GL.glGenBuffers(1) GL.glBindBuffer(GL.GL_ARRAY_BUFFER, self.buffer) self.dtype = spec self.itemsize = spec.itemsize if isinstance(data, np.ndarray): GL.glBufferData(GL.GL_ARRAY_BUFFER, data, usage) self._len = len(data) else: GL.glBufferData(GL.GL_ARRAY_BUFFER, spec.itemsize * numElements, None, usage) self._len = numElements if numElements is None: raise ValueError( 'if VertexArray is created with numpy data type, must specify number of elements as second argument' ) for loc, name in enumerate(spec.names): if name.startswith('_'): continue dt, offset = spec.fields[name] glType = npToGl(dt.base) num = int(np.prod(dt.shape, dtype='i')) GL.glEnableVertexAttribArray(loc) if glType in {GL.GL_INT, GL.GL_UNSIGNED_INT}: GL.glVertexAttribIPointer(loc, num, glType, self.itemsize, GL.GLvoidp(offset)) else: GL.glVertexAttribPointer(loc, num, glType, GL.GL_FALSE, self.itemsize, GL.GLvoidp(offset)) GL.glBindBuffer(GL.GL_ARRAY_BUFFER, 0) GL.glBindVertexArray(0)
def draw(self, num=None, offset=0): if not hasattr(self, 'elements'): raise ValueError('to draw, first attach elements!') GL.glBindVertexArray(self.id) GL.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, self.elements) if num is None: num = self.numElements - offset GL.glDrawElements(self.mode, num, self.elementsType, GL.GLvoidp(offset * self.elementsItemSize))
def init_vao(self): self.vao = GL.glGenVertexArrays(1) GL.glBindVertexArray(self.vao) color_data_offset = 3*self.size_float*NUMBER_OF_VERTICES GL.glBindBuffer(GL.GL_ARRAY_BUFFER,self.vbo) GL.glEnableVertexAttribArray(0) GL.glEnableVertexAttribArray(1) GL.glVertexAttribPointer(0,3,GL.GL_FLOAT,GL.GL_FALSE,0,None) GL.glVertexAttribPointer(1,4,GL.GL_FLOAT,GL.GL_FALSE,0,GL.GLvoidp(color_data_offset)) GL.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER,self.ibo) GL.glBindVertexArray(0)
def _init_vao_(self): self.vao_ = GL.glGenVertexArrays(1) GL.glBindVertexArray(self.vao_) GL.glBindBuffer(GL.GL_ARRAY_BUFFER, self.vbo_) vertexSize = 0 for currElem in self.elements_: vertexSize += currElem accumVertexSize = 0 for it, currElem in enumerate(self.elements_): GL.glEnableVertexAttribArray(it) GL.glVertexAttribPointer(it, currElem, GL.GL_FLOAT, GL.GL_FALSE, vertexSize * FLOAT_SIZE, GL.GLvoidp(accumVertexSize * FLOAT_SIZE)) accumVertexSize += currElem GL.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, self.ibo_) GL.glBindVertexArray(0)
def display(self): GL.glClearColor(0, 0, 0, 0) GL.glClear(GL.GL_COLOR_BUFFER_BIT) GL.glUseProgram(self.shader) GL.glBindBuffer(GL.GL_ARRAY_BUFFER, self.vbo) GL.glEnableVertexAttribArray(0) GL.glEnableVertexAttribArray(1) GL.glVertexAttribPointer(0, VERT_COMPONENTS, GL.GL_FLOAT, False, 0, None) GL.glVertexAttribPointer(1, VERT_COMPONENTS, GL.GL_FLOAT, False, 0, GL.GLvoidp(48)) GL.glDrawArrays(GL.GL_TRIANGLES, 0, 3) GL.glDisableVertexAttribArray(0) GL.glDisableVertexAttribArray(1) GL.glUseProgram(0)
def display(self): GL.glClearColor(0, 0, 0, 0) GL.glClear(GL.GL_COLOR_BUFFER_BIT) GL.glUseProgram(self.shader) GL.glUniform2f(self.offset_location, 1.5, 0.5) color_data = GL.GLvoidp((len(self.vertices) * self.size_float) / 2) GL.glBindBuffer(GL.GL_ARRAY_BUFFER, self.vbo) GL.glEnableVertexAttribArray(0) GL.glEnableVertexAttribArray(1) GL.glVertexAttribPointer(0, self.vert_comp, GL.GL_FLOAT, GL.GL_FALSE, 0, None) GL.glVertexAttribPointer(1, self.vert_comp, GL.GL_FLOAT, GL.GL_FALSE, 0, color_data) GL.glDrawArrays(GL.GL_TRIANGLES, 0, 36) GL.glDisableVertexAttribArray(0) GL.glDisableVertexAttribArray(1) GL.glUseProgram(0)
def __init__(self, arraydescriptor, data, shaderprogram): # type: (VertexArrayData, str, ShaderProgram) -> None # Create a new VAO (Vertex Array Object) and bind it vertex_array_object = GL.glGenVertexArrays(1) GL.glBindVertexArray(vertex_array_object) # Generate buffers to hold our vertices vertex_buffer = GL.glGenBuffers(1) GL.glBindBuffer(GL.GL_ARRAY_BUFFER, vertex_buffer) stride = arraydescriptor.stride # for (name, primitive) in args: # stride += primitive.sizeof offset = 0 for name in arraydescriptor.descriptor: attrib_location = GL.glGetAttribLocation(shaderprogram.id, name) GL.glEnableVertexAttribArray(attrib_location) dtype = arraydescriptor.descriptor[name] # type: DTypeInfo # Describe the attribute data layout in the buffer GL.glVertexAttribPointer(attrib_location, dtype.size, dtype.gl_size, False, stride, GL.GLvoidp(offset)) offset += dtype.byte_size # Send the data over to the buffer num_bytes = len(arraydescriptor[data]) GL.glBufferData(GL.GL_ARRAY_BUFFER, num_bytes, arraydescriptor[data], GL.GL_DYNAMIC_DRAW) # print("num_bytes:", num_bytes) # Unbind the VAO first (Important) GL.glBindVertexArray(0) GL.glBindBuffer(GL.GL_ARRAY_BUFFER, 0) self.array = arraydescriptor[data] self.vao = vertex_array_object self.vbo = vertex_buffer self._program = shaderprogram self._num_vertices = num_bytes // stride