def build(self) -> None: self._vao = glGenVertexArrays(1) vbos = glGenBuffers(2) glBindVertexArray(self._vao) self._material.build_shader() vertices = np.array(self._vertices, dtype=np.float32) indices = np.array(self._indices, dtype=np.int32) glBindBuffer(GL_ARRAY_BUFFER, vbos[0]) glEnableVertexAttribArray(0) # shader layout location glVertexAttribPointer(0, 3, GL_FLOAT, False, 0, ctypes.c_void_p(0)) glBufferData(GL_ARRAY_BUFFER, vertices.nbytes, vertices, GL_STATIC_DRAW) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbos[1]) glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.nbytes, indices, GL_STATIC_DRAW) self._vertex_count = len(indices) glBindVertexArray(0) # glDisableVertexAttribArray(0) glBindBuffer(GL_ARRAY_BUFFER, 0) glDeleteBuffers(2, vbos) if self.static: # we can clear this data to free some more memory self._vertices = [] self._indices = []
def paintGL(self): if self.crashFlag: #run cleanup operations glUseProgram(0) glDisableVertexAttribArray(self.attrID) glDeleteBuffers(1,[self.vertexBuffer]) glDeleteVertexArrays(1, [self.vertexArrayID]) glLoadIdentity() gluPerspective(45, self.sizeX / self.sizeY, 0.1, 110.0) #set perspective glTranslatef(0, 0, self.zoomLevel) glRotatef(self.rotateDegreeV + self.vOffset, 1, 0, 0) glRotatef(self.rotateDegreeH, 0, 0, 1) glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) self.vertexData = [ -1, 1, 0, 0, -1, 0, 1, 1, 0 ] arrayType = GLfloat * len(self.vertexData) target = GL_ARRAY_BUFFER offset = 0 size = len(self.vertexData) * ctypes.sizeof(ctypes.c_float) data = arrayType(*self.vertexData) glBufferSubData(target, offset, size, data) glDrawArrays(GL_TRIANGLES, 0, 3)
def __del__(self): """Destructor. Destroys the VAO and related buffers associated with mesh. """ glDeleteVertexArrays(1, [self.vao]) glDeleteBuffers(len(self.buffers), self.buffers)
def unload(self): """Unload all opengl data""" log.debug(f"unload {self}") if self._vbo is not None: glDeleteBuffers(1, int(self._vbo)) self._vbo = None if self._vao is not None: glDeleteVertexArrays(1, int(self._vao)) self._vao = None
def destroy(self) -> None: """ Deletes all the VAOs and VBOs from the video-memory when the application is closed. """ for vao in self.__vaos: glDeleteVertexArrays(1, vao) self.__vaos = [] for vbo in self.__vbos: glDeleteBuffers(1, vbo) self.__vbos = []
def create_vaos(self) -> None: """Bind VAOs to define vertex data.""" self._vao_gridlines, self._vao_bounding_box = glGenVertexArrays(2) vbo = glGenBuffers(5) vertices, colors = self._get_gridlines() self._count_gridlines = vertices.size // 3 glBindVertexArray(self._vao_gridlines) # gridlines glBindBuffer(GL_ARRAY_BUFFER, vbo[0]) glBufferData(GL_ARRAY_BUFFER, vertices.nbytes, vertices, GL_STATIC_DRAW) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0)) glEnableVertexAttribArray(0) glBindBuffer(GL_ARRAY_BUFFER, vbo[1]) glBufferData(GL_ARRAY_BUFFER, colors.nbytes, colors, GL_STATIC_DRAW) glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0)) glEnableVertexAttribArray(1) # --- points, indices = self._get_bounding_box() self._count_bounding_box = indices.length glBindVertexArray(self._vao_bounding_box) # bounding box glBindBuffer(GL_ARRAY_BUFFER, vbo[2]) glBufferData(GL_ARRAY_BUFFER, points.nbytes, points, GL_STATIC_DRAW) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(0)) glEnableVertexAttribArray(0) glBindBuffer(GL_ARRAY_BUFFER, vbo[3]) glBufferData(GL_ARRAY_BUFFER, points.nbytes, points, GL_STATIC_DRAW) glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(12)) glEnableVertexAttribArray(1) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[4]) glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.nbytes, indices.ptr, GL_STATIC_DRAW) self._axes.create_vaos() glBindVertexArray(0) glDeleteBuffers(5, vbo)
def __del__(self): glDeleteBuffers(1, self.m_RendererID)
def __del__(self): glDeleteBuffers(1, GLuint(self.buffer))
def create_vaos(self) -> None: """Bind VAOs to define vertex data.""" self._vao_axes, *self._vao_arrows = glGenVertexArrays(3) vbo = glGenBuffers(5) build_dimensions = self.parent.build_dimensions x = build_dimensions[0] - build_dimensions[3], build_dimensions[3] y = build_dimensions[1] - build_dimensions[4], build_dimensions[4] z = build_dimensions[2] - build_dimensions[5], build_dimensions[5] points = np.array([ x[0], 0.0, 0.0, 1.0, 0.0, 0.0, -x[1], 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, y[0], 0.0, 0.0, 1.0, 0.0, 0.0, -y[1], 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, z[0], 0.0, 0.0, 1.0, 0.0, 0.0, -z[1], 0.0, 0.0, 1.0, ], dtype=np.float32) glBindVertexArray(self._vao_axes) # colored axes lines glBindBuffer(GL_ARRAY_BUFFER, vbo[0]) glBufferData(GL_ARRAY_BUFFER, points.nbytes, points, GL_STATIC_DRAW) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(0)) glEnableVertexAttribArray(0) glBindBuffer(GL_ARRAY_BUFFER, vbo[1]) glBufferData(GL_ARRAY_BUFFER, points.nbytes, points, GL_STATIC_DRAW) glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(12)) glEnableVertexAttribArray(1) # --- vertices, colors = self._get_cones() glBindVertexArray(self._vao_arrows[0]) # colored axes arrows, cone glBindBuffer(GL_ARRAY_BUFFER, vbo[2]) glBufferData(GL_ARRAY_BUFFER, vertices.nbytes, vertices, GL_STATIC_DRAW) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0)) glEnableVertexAttribArray(0) glBindBuffer(GL_ARRAY_BUFFER, vbo[3]) glBufferData(GL_ARRAY_BUFFER, colors.nbytes, colors, GL_STATIC_DRAW) glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0)) glEnableVertexAttribArray(1) # --- vertices[0] = x[0] vertices[17 * 3 + 1] = y[0] vertices[17 * 6 + 2] = z[0] glBindVertexArray(self._vao_arrows[1]) # colored axes arrows, base glBindBuffer(GL_ARRAY_BUFFER, vbo[4]) glBufferData(GL_ARRAY_BUFFER, vertices.nbytes, vertices, GL_STATIC_DRAW) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0)) glEnableVertexAttribArray(0) glBindBuffer(GL_ARRAY_BUFFER, vbo[3]) glBufferData(GL_ARRAY_BUFFER, colors.nbytes, colors, GL_STATIC_DRAW) glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0)) glEnableVertexAttribArray(1) # --- glBindVertexArray(0) glDeleteBuffers(5, vbo)
def create_vaos(self) -> None: """Bind VAOs to define vertex data.""" self._vaos['box'], self._vaos['side'], \ self._vaos['top'] = glGenVertexArrays(3) vbo = glGenBuffers(4) vertices = np.array( [ -0.5, -1.0, -1.0, # bottom 0.5, -1.0, -1.0, 0.5, -1.0, 1.0, -0.5, -1.0, 1.0, -0.5, 1.0, -1.0, # right 0.5, 1.0, -1.0, 0.5, -1.0, -1.0, -0.5, -1.0, -1.0, -0.5, 1.0, 1.0, # top 0.5, 1.0, 1.0, 0.5, 1.0, -1.0, -0.5, 1.0, -1.0, -0.5, -1.0, 1.0, # left 0.5, -1.0, 1.0, 0.5, 1.0, 1.0, -0.5, 1.0, 1.0, 0.5, 1.0, -1.0, # back 0.5, 1.0, 1.0, 0.5, -1.0, 1.0, 0.5, -1.0, -1.0, -0.5, -1.0, -1.0, # front -0.5, -1.0, 1.0, -0.5, 1.0, 1.0, -0.5, 1.0, -1.0, ], dtype=np.float32) glBindVertexArray(self._vaos['box']) glBindBuffer(GL_ARRAY_BUFFER, vbo[0]) glBufferData(GL_ARRAY_BUFFER, vertices.nbytes, vertices, GL_STATIC_DRAW) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0)) glEnableVertexAttribArray(0) # --- below are unused vaos for rendering a cylinder (for the camerae model) # keep them, but TODO implement general object class thetas = np.linspace(0, 2 * np.pi, 24, endpoint=True) y = np.cos(thetas) * 0.7 z = np.sin(thetas) * 0.7 vertices = np.zeros(6 * 24, dtype=np.float32) vertices[::3] = np.tile(np.array([1.0, 0.5], dtype=np.float32), 24) vertices[1::3] = np.repeat(y, 2) vertices[2::3] = np.repeat(z, 2) glBindVertexArray(self._vaos['side']) glBindBuffer(GL_ARRAY_BUFFER, vbo[1]) glBufferData(GL_ARRAY_BUFFER, vertices.nbytes, vertices, GL_STATIC_DRAW) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0)) glEnableVertexAttribArray(0) # --- vertices = np.concatenate( (np.array([1.0, 0.0, 0.0]), vertices)).astype(np.float32) indices = np.insert(np.arange(24) * 2 + 1, 0, 0).astype(np.uint16) glBindVertexArray(self._vaos['top']) glBindBuffer(GL_ARRAY_BUFFER, vbo[2]) glBufferData(GL_ARRAY_BUFFER, vertices.nbytes, vertices, GL_STATIC_DRAW) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0)) glEnableVertexAttribArray(0) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[3]) glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.nbytes, indices, GL_STATIC_DRAW) # --- glBindVertexArray(0) glDeleteBuffers(4, vbo)
def build(self) -> bool: """ Build OpenGL Vertex Array for the object This function gets automatically called if `self._vao` does not exists in the first render cycle. Once the vba is built, geometry changes or material display mode changes will not be automatically effected. So, in every geometry or display mode change, a `build` call is necessary. If `self.static` is `True`, then the system assumes that another update call is not expected, thus frees `_normals`, `_textcoords`, `_vertices` and `_indices` lists to free memory. So in this case, calling `build` function twice will result in an invisible object (will not be drawn). Returns: bool """ if len(self._indices) == 0: return False # If we don't have a VAO yet, we need to create one if not self.has_vao: # Generate Vertex Array self._vao = glGenVertexArrays(1) # We need 5 buffers (vertex, normal, texcoord, color, indices) self._vbos = glGenBuffers(5) glBindVertexArray(self._vao) # Material shader must be built when there is an active binding # to vertex array self.material.build_shader() else: # Ok, we already have vertex array object, just bind it to modify glBindVertexArray(self._vao) # Turn python arrays into C type arrays using Numpy. # This is required for OpenGL. Python memory model is a bit # different than raw memory model of C (OpenGL) vertices = np.array(self._vertices, dtype=np.float32).flatten() normals = np.array(self._normals, dtype=np.float32).flatten() texcoords = np.array(self._texcoords, dtype=np.float32).flatten() colors = np.array(self._vertex_colors, dtype=np.float32).flatten() indices = np.array(self._indices, dtype=np.int32).flatten() self._calc_bounds() # OpenGL allocates buffers in different mechanisms between # STATIC and DYNAMIC draw modes. If you select STATIC, then OpenGL # will assume that object buffer will not change and allocate it in a # more suitable way. draw = GL_STATIC_DRAW if not self.static: draw = GL_DYNAMIC_DRAW # Buffer overflow, we need more space. if self._buffer_size < vertices.nbytes: self._buffer_size = vertices.nbytes self._buffer_size_changed = True if self._t_buffer_size < texcoords.nbytes: self._t_buffer_size = texcoords.nbytes self._t_buffer_size_changed = True # Bind Vertices glBindBuffer(GL_ARRAY_BUFFER, self._vbos[0]) glEnableVertexAttribArray(0) # shader layout location glVertexAttribPointer(0, 3, GL_FLOAT, False, 0, ctypes.c_void_p(0)) if self._buffer_size_changed: # glBufferData creates a new data area glBufferData(GL_ARRAY_BUFFER, self._buffer_size, vertices, draw) else: # glBufferSubData just replaces memory area in buffer so it is # much more efficient way to handle things. glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.nbytes, vertices) # Bind Normals glBindBuffer(GL_ARRAY_BUFFER, self._vbos[1]) glEnableVertexAttribArray(1) # shader layout location glVertexAttribPointer(1, 3, GL_FLOAT, False, 0, ctypes.c_void_p(0)) if self._buffer_size_changed: glBufferData(GL_ARRAY_BUFFER, self._buffer_size, normals, draw) else: glBufferSubData(GL_ARRAY_BUFFER, 0, normals.nbytes, normals) # Bind TexCoords if len(self._texcoords) == len(self._vertices): glBindBuffer(GL_ARRAY_BUFFER, self._vbos[2]) glEnableVertexAttribArray(2) # shader layout location glVertexAttribPointer(2, 2, GL_FLOAT, False, 0, ctypes.c_void_p(0)) if self._t_buffer_size_changed: glBufferData( GL_ARRAY_BUFFER, self._t_buffer_size, texcoords, draw ) else: glBufferSubData( GL_ARRAY_BUFFER, 0, texcoords.nbytes, texcoords ) # Bind Vertex Colors if len(self._vertex_colors) == len(self._vertices): glBindBuffer(GL_ARRAY_BUFFER, self._vbos[4]) glEnableVertexAttribArray(3) # shader layout location glVertexAttribPointer(3, 3, GL_FLOAT, False, 0, ctypes.c_void_p(0)) self._has_vertex_colors = True if self._buffer_size_changed: glBufferData(GL_ARRAY_BUFFER, self._buffer_size, colors, draw) else: glBufferSubData(GL_ARRAY_BUFFER, 0, colors.nbytes, colors) self._buffer_size_changed = False self._t_buffer_size_changed = False # Bind Indices glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self._vbos[3]) glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.nbytes, indices, draw) self._vertex_count = len(indices) glBindVertexArray(0) glBindBuffer(GL_ARRAY_BUFFER, 0) if self.static: # we can clear this data to free some more memory glDeleteBuffers(4, self._vbos) self._vbos = [] self._needs_update = False return True
def dispose_buffer(buffer): if buffer is not None: glDeleteBuffers(1, (buffer, ))
def release(self, conn): if self._hnd is not None: conn.send_msg(lambda: glDeleteBuffers(self._hnd))
def create_vaos(self) -> None: """Bind VAOs to define vertex data.""" self._vao, self._vao_picking, self._vao_outline = glGenVertexArrays(3) vbo = glGenBuffers(6) # --- standard viewcube --- vertices = glm.array.from_numbers( ctypes.c_float, 1.0, 1.0, 1.0, 0.7, 0.7, 0.7, 1.0, -1.0, 1.0, 0.7, 0.7, 0.7, 1.0, -1.0, -1.0, 0.4, 0.4, 0.4, 1.0, 1.0, -1.0, 0.4, 0.4, 0.4, -1.0, 1.0, -1.0, 0.4, 0.4, 0.4, -1.0, 1.0, 1.0, 0.7, 0.7, 0.7, -1.0, -1.0, 1.0, 0.7, 0.7, 0.7, -1.0, -1.0, -1.0, 0.4, 0.4, 0.4, ) indices = glm.array.from_numbers( ctypes.c_uint, 0, 1, 2, 2, 3, 0, 0, 3, 4, 4, 5, 0, 0, 5, 6, 6, 1, 0, 1, 6, 7, 7, 2, 1, 7, 4, 3, 3, 2, 7, 4, 7, 6, 6, 5, 4, ) glBindVertexArray(self._vao) glBindBuffer(GL_ARRAY_BUFFER, vbo[0]) glBufferData(GL_ARRAY_BUFFER, vertices.nbytes, vertices.ptr, GL_STATIC_DRAW) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(0)) glEnableVertexAttribArray(0) glBindBuffer(GL_ARRAY_BUFFER, vbo[1]) glBufferData(GL_ARRAY_BUFFER, vertices.nbytes, vertices.ptr, GL_STATIC_DRAW) glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(12)) glEnableVertexAttribArray(1) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[2]) glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.nbytes, indices.ptr, GL_STATIC_DRAW) # --- viewcube for picking --- vertices = glm.array.from_numbers( ctypes.c_float, 1.0, -1.0, 1.0, # 1 front (id = 0) -1.0, -1.0, 1.0, # 6 -1.0, -1.0, -1.0, # 7 1.0, -1.0, -1.0, # 2 1.0, 1.0, 1.0, # 0 top (id = 1) -1.0, 1.0, 1.0, # 5 -1.0, -1.0, 1.0, # 6 1.0, -1.0, 1.0, # 1 1.0, 1.0, 1.0, # 0 right (id = 2) 1.0, -1.0, 1.0, # 1 1.0, -1.0, -1.0, # 2 1.0, 1.0, -1.0, # 3 -1.0, -1.0, -1.0, # 7 bottom (id = 3) -1.0, 1.0, -1.0, # 4 1.0, 1.0, -1.0, # 3 1.0, -1.0, -1.0, # 2 -1.0, -1.0, -1.0, # 7 left (id = 4) -1.0, -1.0, 1.0, # 6 -1.0, 1.0, 1.0, # 5 -1.0, 1.0, -1.0, # 4 1.0, 1.0, 1.0, # 0 back (id = 5) 1.0, 1.0, -1.0, # 3 -1.0, 1.0, -1.0, # 4 -1.0, 1.0, 1.0, # 5 ) colors = np.zeros(72, dtype=np.float32) colors[::3] = np.arange(6).repeat(4) / 255.0 glBindVertexArray(self._vao_picking) glBindBuffer(GL_ARRAY_BUFFER, vbo[3]) glBufferData(GL_ARRAY_BUFFER, vertices.nbytes, vertices.ptr, GL_STATIC_DRAW) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0)) glEnableVertexAttribArray(0) glBindBuffer(GL_ARRAY_BUFFER, vbo[4]) glBufferData(GL_ARRAY_BUFFER, colors.nbytes, colors, GL_STATIC_DRAW) glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0)) glEnableVertexAttribArray(1) # --- outlined face of viewcube --- border_colors = glm.array.from_numbers(ctypes.c_float, 0.0000, 0.4088, 0.9486).repeat(24) glBindVertexArray(self._vao_outline) glBindBuffer(GL_ARRAY_BUFFER, vbo[3]) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0)) glEnableVertexAttribArray(0) glBindBuffer(GL_ARRAY_BUFFER, vbo[5]) glBufferData(GL_ARRAY_BUFFER, border_colors.nbytes, border_colors.ptr, GL_STATIC_DRAW) glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0)) glEnableVertexAttribArray(1) # --- glBindVertexArray(0) glDeleteBuffers(6, vbo)
def __del__(self): glDeleteBuffers(1, [self.id])