def draw(self, shader): attribute_table = shader.attribute_table self.indexed_vbo.bind() # Bind all attributes. location_list = [] # Used to disable all VertexAttribArrays. for name, vbo in self.vbo_array.items(): vbo.bind() attribute_location = attribute_table[name] location_list.append(attribute_location) glEnableVertexAttribArray(attribute_location) # specify that the data for the attribute will be pulled from the buffer that is currently bound to # GL_ARRAY_BUFFER. (http://stackoverflow.com/questions/30016833/multiple-in-attributes-in-shader-opengl) glVertexAttribPointer(attribute_location, vbo.dimension, vbo.data_type, GL_FALSE, 0, 0) # Draw. # glDrawArrays(GL_TRIANGLES, 0, vbo.count) glDrawElements(GL_TRIANGLES, self.indexed_vbo.count, self.indexed_vbo.data_type, 0) # Disable and unbind everything. for location in location_list: glDisableVertexAttribArray(location) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) glBindBuffer(GL_ARRAY_BUFFER, 0)
def draw_world_items(self, glyphs): ''' Draw all passed glyphs ''' shader = None for position, orientation, glyph in glyphs: gl.glPushMatrix() gl.glTranslatef(*position) if orientation and orientation != Orientation.Identity: gl.glMultMatrixf(orientation.matrix) if glyph.shader is not shader: shader = glyph.shader shader.use() gl_wrap.glBindVertexArray(glyph.vao) gl.glDrawElements( gl.GL_TRIANGLES, len(glyph.glindices), glyph.index_type, glyph.glindices ) gl.glPopMatrix() gl_wrap.glBindVertexArray(0) Shader.unuse()
def __init__(self): pts = generate_cyl_points().T colors = np.zeros_like(pts) colors[:,1]=1.0 # all green n_pts = len(pts) pts = map(float,pts.flat) # convert to flat list of floats colors = map(float, colors.flat) # Create ctypes arrays of the lists vertices = (gl.GLfloat * (n_pts*3))(*pts) colors = (gl.GLfloat * (n_pts*3))(*colors) # Create a list of triangle indices. indices = range(n_pts) indices = (gl.GLuint * n_pts)(*indices) # Compile a display list self.list = gl.glGenLists(1) gl.glNewList(self.list, gl.GL_COMPILE) gl.glPushClientAttrib(gl.GL_CLIENT_VERTEX_ARRAY_BIT) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glVertexPointer(3, gl.GL_FLOAT, 0, vertices) gl.glEnableClientState(gl.GL_COLOR_ARRAY) gl.glColorPointer(3, gl.GL_FLOAT, 0, colors) gl.glDrawElements(gl.GL_LINES, len(indices), gl.GL_UNSIGNED_INT, indices) gl.glPopClientAttrib() gl.glEndList()
def draw(self, mode=gl.GL_TRIANGLES, indices=None): if indices: with indices: gl.glDrawElements(mode, len(indices), gl.GL_UNSIGNED_INT, 0) else: with self.index_buffer: gl.glDrawElements(mode, self.length, gl.GL_UNSIGNED_INT, 0)
def draw_lights(lights, shader, models): """ Dependencies are location, rotation, scale, and model (optionally texture). Args: lights: shader: models: Returns: """ attribute_location = shader.attribute_location location_location = attribute_location['location'] for entity in lights: shader.load_uniform_matrix(entity.transformation, name='transform') model = models[entity.model] glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, model.indexed_vbo) glBindBuffer(GL_ARRAY_BUFFER, model.vbo_array['location']) glEnableVertexAttribArray(location_location) glVertexAttribPointer(location_location, 3, GL_FLOAT, GL_FALSE, 0, 0) glDrawElements(GL_TRIANGLES, model.indexed_vbo.count, GL_UNSIGNED_INT, 0) glDisableVertexAttribArray(location_location) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) glBindBuffer(GL_ARRAY_BUFFER, 0)
def __init__(self): pts = generate_cyl_points().T colors = np.zeros_like(pts) colors[:,1]=1.0 # all green n_pts = len(pts) pts = map(float,pts.flat) # convert to flat list of floats colors = map(float, colors.flat) # Create ctypes arrays of the lists vertices = (gl.GLfloat * (n_pts*3))(*pts) colors = (gl.GLfloat * (n_pts*3))(*colors) # Create a list of triangle indices. indices = range(n_pts) indices = (gl.GLuint * n_pts)(*indices) # Compile a display list self.list = gl.glGenLists(1) gl.glNewList(self.list, gl.GL_COMPILE) gl.glPushClientAttrib(gl.GL_CLIENT_VERTEX_ARRAY_BIT) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glVertexPointer(3, gl.GL_FLOAT, 0, vertices) gl.glEnableClientState(gl.GL_COLOR_ARRAY) gl.glColorPointer(3, gl.GL_FLOAT, 0, colors) gl.glDrawElements(gl.GL_POINTS, len(indices), gl.GL_UNSIGNED_INT, indices) gl.glPopClientAttrib() gl.glEndList()
def on_draw(self): # create projection matrix self.p_matrix.load_identity() self.p_matrix.perspective(90, float(self.width) / self.height, 0.1, 500) # create model view matrix self.mv_matrix.load_identity() self.mv_matrix.translate(0, 0, -1) self.mv_matrix.rotate_2d(self.x, math.sin(self.x / 3 * 2) / 2) # multiply the two matrices together and send to the shader program mvp_matrix = self.mv_matrix * self.p_matrix self.shader.uniform_matrix(self.shader_matrix_location, mvp_matrix) # draw stuff gl.glClearColor(0.0, 0.0, 0.0, 1.0) self.clear() gl.glDrawElements(gl.GL_TRIANGLES, len(indices), gl.GL_UNSIGNED_INT, None)
def drawnvc( self, normals, points, colors, idxs ): '''draw tri mesh using glDrawElements using input normals points colors and indexes''' n = 1 for dim in idxs.shape: n *= dim iptr = vecutil.numpy2pointer(idxs) nptr = vecutil.numpy2pointer(normals) vptr = vecutil.numpy2pointer(points) cptr = vecutil.numpy2pointer(colors) mode = self.ui.fillmodes[self.ui.fillmode] gl.glPolygonMode( gl.GL_FRONT, mode ) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glEnableClientState(gl.GL_NORMAL_ARRAY) gl.glEnableClientState(gl.GL_COLOR_ARRAY) gl.glVertexPointer(3, gl.GL_FLOAT, 0, vptr) gl.glNormalPointer( gl.GL_FLOAT, 0, nptr) gl.glColorPointer(3, gl.GL_FLOAT, 0, cptr) gl.glDrawElements(gl.gl.GL_TRIANGLES, n, gl.GL_UNSIGNED_INT, iptr) gl.glDisableClientState(gl.GL_VERTEX_ARRAY) gl.glDisableClientState(gl.GL_NORMAL_ARRAY) gl.glDisableClientState(gl.GL_COLOR_ARRAY) gl.glPolygonMode( gl.GL_FRONT, gl.GL_FILL )
def draw_direct_advanced(self, mode): if not self.mesh_quad_count: return gl.glBindVertexArray(self.vao) gl.glUniform2i(self.shader_chunk_offset_location, self.chunk_position[0], self.chunk_position[2]) gl.glBeginQuery(gl.GL_ANY_SAMPLES_PASSED, self.occlusion_query) gl.glDrawElements( mode, self.mesh_quad_count * 6, gl.GL_UNSIGNED_INT, None, ) gl.glEndQuery(gl.GL_ANY_SAMPLES_PASSED) gl.glBeginConditionalRender(self.occlusion_query, gl.GL_QUERY_BY_REGION_WAIT) gl.glDrawElements( mode, self.mesh_quad_count * 6, gl.GL_UNSIGNED_INT, None, ) gl.glEndConditionalRender()
def draw(self): if not self.mesh_index_counter: return gl.glBindVertexArray(self.vao) gl.glDrawElements(gl.GL_TRIANGLES, len(self.mesh_indices), gl.GL_UNSIGNED_INT, None)
def on_draw(self): gl.glClearColor(0.0, 0.0, 0.0, 1.0) self.clear() gl.glDrawElements( gl.GL_TRIANGLES, len(indices), gl.GL_UNSIGNED_INT, None)
def draw(self): """Draw the text to the back buffer""" # Border width self.box.setLineWidth(self.pallette['lineWidth']) # Use 1 as base if border width is none #self.borderWidth = self.box.lineWidth # Border colour self.box.setLineColor(self.pallette['lineColor'], colorSpace='rgb') #self.borderColor = self.box.lineColor # Background self.box.setFillColor(self.pallette['fillColor'], colorSpace='rgb') #self.fillColor = self.box.fillColor if self._needVertexUpdate: self._updateVertices() if self.fillColor is not None or self.borderColor is not None: self.box.draw() # self.boundingBox.draw() # could draw for debug purposes gl.glPushMatrix() self.win.setScale('pix') gl.glActiveTexture(gl.GL_TEXTURE0) gl.glBindTexture(gl.GL_TEXTURE_2D, self.glFont.textureID) gl.glEnable(gl.GL_TEXTURE_2D) gl.glDisable(gl.GL_DEPTH_TEST) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glEnableClientState(gl.GL_COLOR_ARRAY) gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glVertexPointer(2, gl.GL_FLOAT, 0, self.verticesPix.ctypes) gl.glColorPointer(4, gl.GL_FLOAT, 0, self._colors.ctypes) gl.glTexCoordPointer(2, gl.GL_FLOAT, 0, self._texcoords.ctypes) self.shader.bind() self.shader.setInt('texture', 0) self.shader.setFloat('pixel', [1.0 / 512, 1.0 / 512]) nVerts = len(self.text)*4 gl.glDrawElements(gl.GL_QUADS, nVerts, gl.GL_UNSIGNED_INT, np.arange(nVerts, dtype=int).ctypes) self.shader.unbind() # removed the colors and font texture gl.glDisableClientState(gl.GL_COLOR_ARRAY) gl.glDisableClientState(gl.GL_TEXTURE_COORD_ARRAY) gl.glDisableVertexAttribArray(1) gl.glDisableClientState(gl.GL_VERTEX_ARRAY) gl.glActiveTexture(gl.GL_TEXTURE0) gl.glBindTexture(gl.GL_TEXTURE_2D, 0) gl.glDisable(gl.GL_TEXTURE_2D) if self.hasFocus: # draw caret line self.caret.draw() gl.glPopMatrix()
def draw_entities(entities, shader, models): """ Dependencies are location, rotation, scale, and model (optionally texture). Args: shader: models: Returns: """ attribute_location = shader.attribute_location location_location = attribute_location['location'] texture_location = attribute_location['texture_coordinate'] normal_location = attribute_location['normal'] for entity in entities: model = models[entity.model] shader.set_uniform_matrix('transform', entity.transformation) # glActiveTexture(GL_TEXTURE0) # texture = textures[entity.diffuse] # glBindTexture(GL_TEXTURE_2D, texture.id) # glActiveTexture(GL_TEXTURE0 + 1) # texture = textures[entity.specular] # glBindTexture(GL_TEXTURE_2D, texture.id) # glActiveTexture(GL_TEXTURE0 + 2) # texture = textures[entity.emission] # glBindTexture(GL_TEXTURE_2D, texture.id) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, model.indexed_vbo) glBindBuffer(GL_ARRAY_BUFFER, model.vbo_array['location']) glEnableVertexAttribArray(location_location) glVertexAttribPointer(location_location, 3, GL_FLOAT, GL_FALSE, 0, 0) glBindBuffer(GL_ARRAY_BUFFER, model.vbo_array['texture_coordinate']) glEnableVertexAttribArray(texture_location) glVertexAttribPointer(texture_location, 2, GL_FLOAT, GL_FALSE, 0, 0) glBindBuffer(GL_ARRAY_BUFFER, model.vbo_array['normal']) glEnableVertexAttribArray(normal_location) glVertexAttribPointer(normal_location, 3, GL_FLOAT, GL_FALSE, 0, 0) glDrawElements(GL_TRIANGLES, model.indexed_vbo.count, GL_UNSIGNED_INT, 0) Entity.needs_update.clear() glDisableVertexAttribArray(location_location) glDisableVertexAttribArray(texture_location) glDisableVertexAttribArray(normal_location) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) glBindBuffer(GL_ARRAY_BUFFER, 0)
def __init__(self, radius, inner_radius, slices, inner_slices): # Create the vertex and normal arrays. vertices = [] normals = [] u_step = 2 * math.pi / (slices - 1) v_step = 2 * math.pi / (inner_slices - 1) u = 0. for i in range(slices): cos_u = math.cos(u) sin_u = math.sin(u) v = 0. for j in range(inner_slices): cos_v = math.cos(v) sin_v = math.sin(v) d = (radius + inner_radius * cos_v) x = d * cos_u y = d * sin_u z = inner_radius * sin_v nx = cos_u * cos_v ny = sin_u * cos_v nz = sin_v vertices.extend([x, y, z]) normals.extend([nx, ny, nz]) v += v_step u += u_step # Create ctypes arrays of the lists vertices = (gl.GLfloat * len(vertices))(*vertices) normals = (gl.GLfloat * len(normals))(*normals) # Create a list of triangle indices. indices = [] for i in range(slices - 1): for j in range(inner_slices - 1): p = i * inner_slices + j indices.extend([p, p + inner_slices, p + inner_slices + 1]) indices.extend([p, p + inner_slices + 1, p + 1]) indices = (gl.GLuint * len(indices))(*indices) # Compile a display list self.list = gl.glGenLists(1) gl.glNewList(self.list, gl.GL_COMPILE) gl.glPushClientAttrib(gl.GL_CLIENT_VERTEX_ARRAY_BIT) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glEnableClientState(gl.GL_NORMAL_ARRAY) gl.glVertexPointer(3, gl.GL_FLOAT, 0, vertices) gl.glNormalPointer(gl.GL_FLOAT, 0, normals) gl.glDrawElements(gl.GL_TRIANGLES, len(indices), gl.GL_UNSIGNED_INT, indices) gl.glPopClientAttrib() gl.glEndList()
def __init__(self, radius, inner_radius, slices, inner_slices): # Create the vertex and normal arrays. vertices = [] normals = [] u_step = 2 * pi / (slices - 1) v_step = 2 * pi / (inner_slices - 1) u = 0. for i in range(slices): cos_u = cos(u) sin_u = sin(u) v = 0. for j in range(inner_slices): cos_v = cos(v) sin_v = sin(v) d = (radius + inner_radius * cos_v) x = d * cos_u y = d * sin_u z = inner_radius * sin_v nx = cos_u * cos_v ny = sin_u * cos_v nz = sin_v vertices.extend([x, y, z]) normals.extend([nx, ny, nz]) v += v_step u += u_step # Create ctypes arrays of the lists vertices = (GLfloat * len(vertices))(*vertices) normals = (GLfloat * len(normals))(*normals) # Create a list of triangle indices. indices = [] for i in range(slices - 1): for j in range(inner_slices - 1): p = i * inner_slices + j indices.extend([p, p + inner_slices, p + inner_slices + 1]) indices.extend([p, p + inner_slices + 1, p + 1]) indices = (GLuint * len(indices))(*indices) # Compile a display list self.list = glGenLists(1) glNewList(self.list, GL_COMPILE) glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT) glEnableClientState(GL_VERTEX_ARRAY) glEnableClientState(GL_NORMAL_ARRAY) glVertexPointer(3, GL_FLOAT, 0, vertices) glNormalPointer(GL_FLOAT, 0, normals) glDrawElements(GL_TRIANGLES, len(indices), GL_UNSIGNED_INT, indices) glPopClientAttrib() glEndList()
def draw(self): if not self.mesh_index_counter: # make sure there actually is data in the mesh return # draw the VAO gl.glBindVertexArray(self.vao) gl.glDrawElements(gl.GL_TRIANGLES, len(self.mesh_indices), gl.GL_UNSIGNED_INT, None)
def draw_direct(self, mode): if not self.mesh_quad_count: return gl.glBindVertexArray(self.vao) gl.glUniform2i(self.shader_chunk_offset_location, self.chunk_position[0], self.chunk_position[2]) gl.glDrawElements( mode, self.mesh_quad_count * 6, gl.GL_UNSIGNED_INT, None, )
def draw(self): """ This function depends on: 1. The bound shader. 2. The bound textures. 3. The uploaded uniforms Returns: """ glDrawElements(GL_TRIANGLES, self.count, GL_UNSIGNED_INT, 0)
def draw(self): """Render the particle system.""" if not self.live_particles: return if self.dirty_indices: indices = [p.slot for p in self.live_particles if not p.dead] self.indices = (gl.GLuint * len(indices))(*indices) self.dirty_indices = False gl.glPointSize(self.particle_class.size) gl.glInterleavedArrays(gl.GL_C4F_N3F_V3F, 0, self.data) gl.glDrawElements(gl.GL_POINTS, len(self.indices), gl.GL_UNSIGNED_INT, self.indices)
def draw(self): gl.glBindVertexArray(self.vao) if self.texture is not None: gl.glEnable(self.texture.target) gl.glBindTexture(self.texture.target, self.texture.id) gl.glDrawElements( self.mode, self.indices_size, gl.GL_UNSIGNED_INT, 0 # mode # count # type ) # indices gl.glBindVertexArray(0) if self.texture is not None: gl.glBindTexture(self.texture.target, 0)
def drawlines( self, verts, colors, idxs ): 'helper to draw lines from numpy arrays of verts/colors/indexes' vptr = vecutil.numpy2pointer(verts) iptr = vecutil.numpy2pointer(idxs) if colors is not None: cptr = vecutil.numpy2pointer(colors) gl.glEnableClientState(gl.GL_COLOR_ARRAY) gl.glColorPointer(3, gl.GL_FLOAT, 0, cptr) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glVertexPointer(3, gl.GL_FLOAT, 0, vptr) gl.glDrawElements(gl.GL_LINES, len(idxs), gl.GL_UNSIGNED_INT, iptr) gl.glDisableClientState(gl.GL_VERTEX_ARRAY) gl.glDisableClientState(gl.GL_COLOR_ARRAY)
def on_draw(self): self.camera.update_matrices() # bind textures gl.glActiveTexture(gl.GL_TEXTURE0) gl.glBindTexture(gl.GL_TEXTURE_2D_ARRAY, self.texture_manager.texture_array) gl.glUniform1i(self.shader_sampler_location, 0) # draw stuff gl.glEnable(gl.GL_DEPTH_TEST) gl.glClearColor(0.0, 0.0, 0.0, 1.0) self.clear() gl.glDrawElements(gl.GL_TRIANGLES, len(self.grass.indices), gl.GL_UNSIGNED_INT, None)
def on_draw(self): # create projection matrix self.p_matrix.load_identity() self.p_matrix.perspective(90, float(self.width) / self.height, 0.1, 500) # create modelview matrix self.mv_matrix.load_identity() self.mv_matrix.translate(0, 0, -3) self.mv_matrix.rotate_2d(self.x, math.sin(self.x / 3 * 2) / 2) # modelviewprojection matrix mvp_matrix = self.p_matrix * self.mv_matrix self.shader.uniform_matrix(self.shader_matrix_location, mvp_matrix) # bind textures gl.glActiveTexture( gl.GL_TEXTURE0 ) # set our active texture unit to the first texture unit gl.glBindTexture(gl.GL_TEXTURE_2D_ARRAY, self.texture_manager. texture_array) # bind our texture manager's texture gl.glUniform1i( self.shader_sampler_location, 0 ) # tell our sampler our texture is bound to the first texture unit # draw stuff gl.glEnable( gl.GL_DEPTH_TEST ) # enable depth testing so faces are drawn in the right order gl.glClearColor(0.0, 0.0, 0.0, 1.0) self.clear() gl.glDrawElements(gl.GL_TRIANGLES, len(self.grass.indices), gl.GL_UNSIGNED_INT, None)
def render(self, scene): """ Add a ring to the view. :param scene: The view to render the model into :type scene: pyglet_helper.objects.View """ if self.degenerate: return # The number of subdivisions around the hoop's radial direction. if self.thickness: band_coverage = scene.pixel_coverage(self.pos, self.thickness) else: band_coverage = scene.pixel_coverage(self.pos, self.radius * 0.1) if band_coverage < 0: band_coverage = 1000 bands = sqrt(band_coverage * 4.0) bands = clamp(4, bands, 40) # The number of subdivisions around the hoop's tangential direction. ring_coverage = scene.pixel_coverage(self.pos, self.radius) if ring_coverage < 0: ring_coverage = 1000 rings = sqrt(ring_coverage * 4.0) rings = clamp(4, rings, 80) slices = int(rings) inner_slices = int(bands) radius = self.radius inner_radius = self.thickness # Create the vertex and normal arrays. vertices = [] normals = [] outer_angle_step = 2 * pi / (slices - 1) inner_angle_step = 2 * pi / (inner_slices - 1) outer_angle = 0. for i in range(slices): cos_outer_angle = cos(outer_angle) sin_outer_angle = sin(outer_angle) inner_angle = 0. for j in range(inner_slices): cos_inner_angle = cos(inner_angle) sin_inner_angle = sin(inner_angle) diameter = (radius + inner_radius * cos_inner_angle) vertex_x = diameter * cos_outer_angle vertex_y = diameter * sin_outer_angle vertex_z = inner_radius * sin_inner_angle normal_x = cos_outer_angle * cos_inner_angle normal_y = sin_outer_angle * cos_inner_angle normal_z = sin_inner_angle vertices.extend([vertex_x, vertex_y, vertex_z]) normals.extend([normal_x, normal_y, normal_z]) inner_angle += inner_angle_step outer_angle += outer_angle_step # Create ctypes arrays of the lists vertices = (gl.GLfloat *len(vertices))(*vertices) normals = (gl.GLfloat * len(normals))(*normals) # Create a list of triangle indices. indices = [] for i in range(slices - 1): for j in range(inner_slices - 1): pos = i * inner_slices + j indices.extend([pos, pos + inner_slices, pos + inner_slices + 1]) indices.extend([pos, pos + inner_slices + 1, pos + 1]) indices = (gl.GLuint * len(indices))(*indices) # Compile a display list self.list = gl.glGenLists(1) gl.glNewList(self.list, gl.GL_COMPILE) self.color.gl_set(self.opacity) gl.glPushClientAttrib(gl.GL_CLIENT_VERTEX_ARRAY_BIT) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glEnableClientState(gl.GL_NORMAL_ARRAY) self.model_world_transform(scene.gcf, Vector([self.radius, self.radius, self.radius])).gl_mult() gl.glVertexPointer(3, gl.GL_FLOAT, 0, vertices) gl.glNormalPointer(gl.GL_FLOAT, 0, normals) gl.glDrawElements(gl.GL_TRIANGLES, len(indices), gl.GL_UNSIGNED_INT, indices) gl.glPopClientAttrib() gl.glEndList() gl.glCallList(self.list)
def draw(self, shader, models, textures): """ Dependencies are location, rotation, scale, and model (optionally texture). Args: shader: models: textures: Returns: """ attribute_location = shader.attribute_location location_location = attribute_location['position'] texture_location = attribute_location['texture_coordinate'] normal_location = attribute_location['normal'] transformation = self.transformation diffuse = self.diffuse specular = self.specular emission = self.emission for entity in numpy.where( (self.mask & World.COMPONENT_SPRITE) == World.COMPONENT_SPRITE)[0]: model = models[self.model[entity]] shader.load_uniform_matrix(transformation[entity], name='transform') glActiveTexture(GL_TEXTURE0) texture = textures[diffuse[entity]] glBindTexture(GL_TEXTURE_2D, texture.id) glActiveTexture(GL_TEXTURE0 + 1) texture = textures[specular[entity]] glBindTexture(GL_TEXTURE_2D, texture.id) glActiveTexture(GL_TEXTURE0 + 2) texture = textures[emission[entity]] glBindTexture(GL_TEXTURE_2D, texture.id) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, model.indexed_vbo) glBindBuffer(GL_ARRAY_BUFFER, model.vbo_array['location']) glEnableVertexAttribArray(location_location) glVertexAttribPointer(location_location, 3, GL_FLOAT, GL_FALSE, 0, 0) glBindBuffer(GL_ARRAY_BUFFER, model.vbo_array['texture_coordinate']) glEnableVertexAttribArray(texture_location) glVertexAttribPointer(texture_location, 2, GL_FLOAT, GL_FALSE, 0, 0) glBindBuffer(GL_ARRAY_BUFFER, model.vbo_array['normal']) glEnableVertexAttribArray(normal_location) glVertexAttribPointer(normal_location, 3, GL_FLOAT, GL_FALSE, 0, 0) glDrawElements(GL_TRIANGLES, model.indexed_vbo.count, GL_UNSIGNED_INT, 0) glDisableVertexAttribArray(location_location) glDisableVertexAttribArray(texture_location) glDisableVertexAttribArray(normal_location) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) glBindBuffer(GL_ARRAY_BUFFER, 0)
def draw_light(self, shader, models): """ Dependencies are location, rotation, scale, and model (optionally texture). Args: shader: models: Returns: """ attribute_location = shader.attribute_location location_location = attribute_location['position'] for entity in numpy.where(self.mask == World.COMPONENT_LIGHT)[0]: shader.load_uniform_matrix(create_transformation_matrix( *self.location[entity], *self.rotation[entity], *self.scale[entity]), name='transform') model = models[self.model[entity]] glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, model.indexed_vbo) glBindBuffer(GL_ARRAY_BUFFER, model.vbo_array['location']) glEnableVertexAttribArray(location_location) glVertexAttribPointer(location_location, 3, GL_FLOAT, GL_FALSE, 0, 0) glDrawElements(GL_TRIANGLES, model.indexed_vbo.count, GL_UNSIGNED_INT, 0) glDisableVertexAttribArray(location_location) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) glBindBuffer(GL_ARRAY_BUFFER, 0) # # # # # class World: # # Must be of same data type as 'mask' array. # COMPONENT_VOID = numpy.uint(0) # COMPONENT_DISPLACEMENT = numpy.uint(1 << 0) # COMPONENT_MODEL = numpy.uint(1 << 1) # COMPONENT_TEXTURE = numpy.uint(1 << 2) # # COMPONENT_LIGHT = COMPONENT_DISPLACEMENT | COMPONENT_MODEL # COMPONENT_SPRITE = COMPONENT_DISPLACEMENT | COMPONENT_MODEL | COMPONENT_TEXTURE # # def __init__(self, max_entities): # self.max_entities = max_entities # self.entity_count = 0 # # self.mask = numpy.zeros(shape=max_entities, dtype=numpy.uint, order='C') # # # Displacement. # self.location = numpy.zeros(shape=(max_entities, 3), dtype=numpy.float32, order='C') # self.rotation = numpy.zeros(shape=(max_entities, 3), dtype=numpy.float32, order='C') # self.scale = numpy.zeros(shape=(max_entities, 3), dtype=numpy.float32, order='C') # self.transformation = numpy.zeros(shape=(max_entities, 4, 4), dtype=numpy.float32, order='C') # # # Model. # self.model = numpy.zeros(shape=max_entities, dtype=numpy.uint, order='C') # # # Texture. # self.diffuse = numpy.zeros(shape=max_entities, dtype=numpy.uint, order='C') # self.specular = numpy.zeros(shape=max_entities, dtype=numpy.uint, order='C') # self.emission = numpy.zeros(shape=max_entities, dtype=numpy.uint, order='C') # # # def create_entity(world, **attributes) -> int: # """ # Generates a handle for an entity. # # Returns: # int # """ # handle = world.entity_count # world.entity_count += 1 # # if world.entity_count > world.max_entities: # raise ValueError('Maximum entity count of %i reached!' % world.max_entities) # # for key, value in attributes.items(): # attribute_array = getattr(world, key) # attribute_array[handle][:] = value # # return handle # # # def remove_entity(world, entity): # world.mask[entity] = World.COMPONENT_VOID # # # def draw(world, mask, shader, models, textures): # """ # # # Dependencies are location, rotation, scale, and model (optionally texture). # # Args: # world: # mask: # shader: # models: # textures: # # Returns: # # """ # attribute_location = shader.attribute_location # location_location = attribute_location['location'] # texture_location = attribute_location['texture_coordinate'] # normal_location = attribute_location['normal'] # # for entity in numpy.where((world.mask & mask) == mask)[0]: # model = models[world.model[entity]] # # shader.load_uniform_matrix(world.transformation[entity], name='transform') # # glActiveTexture(GL_TEXTURE0) # texture = textures[world.diffuse[entity]] # glBindTexture(GL_TEXTURE_2D, texture.id) # glActiveTexture(GL_TEXTURE0 + 1) # texture = textures[world.specular[entity]] # glBindTexture(GL_TEXTURE_2D, texture.id) # glActiveTexture(GL_TEXTURE0 + 2) # texture = textures[world.emission[entity]] # glBindTexture(GL_TEXTURE_2D, texture.id) # # glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, model.indexed_vbo) # # glBindBuffer(GL_ARRAY_BUFFER, model.vbo_array['location']) # glEnableVertexAttribArray(location_location) # glVertexAttribPointer(location_location, 3, GL_FLOAT, GL_FALSE, 0, 0) # # glBindBuffer(GL_ARRAY_BUFFER, model.vbo_array['texture_coordinate']) # glEnableVertexAttribArray(texture_location) # glVertexAttribPointer(texture_location, 2, GL_FLOAT, GL_FALSE, 0, 0) # # glBindBuffer(GL_ARRAY_BUFFER, model.vbo_array['normal']) # glEnableVertexAttribArray(normal_location) # glVertexAttribPointer(normal_location, 3, GL_FLOAT, GL_FALSE, 0, 0) # # glDrawElements(GL_TRIANGLES, model.indexed_vbo.count, GL_UNSIGNED_INT, 0) # # glDisableVertexAttribArray(location_location) # glDisableVertexAttribArray(texture_location) # glDisableVertexAttribArray(normal_location) # glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) # glBindBuffer(GL_ARRAY_BUFFER, 0) # # # def draw_light(world, shader, models): # """ # # # Dependencies are location, rotation, scale, and model (optionally texture). # # Args: # world: # shader: # models: # # Returns: # # """ # attribute_location = shader.attribute_location # location_location = attribute_location['location'] # # for entity in numpy.where(world.mask == World.COMPONENT_LIGHT)[0]: # shader.load_uniform_matrix(world.transformation[entity], name='transform') # # model = models[world.model[entity]] # # glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, model.indexed_vbo) # # glBindBuffer(GL_ARRAY_BUFFER, model.vbo_array['location']) # glEnableVertexAttribArray(location_location) # glVertexAttribPointer(location_location, 3, GL_FLOAT, GL_FALSE, 0, 0) # # glDrawElements(GL_TRIANGLES, model.indexed_vbo.count, GL_UNSIGNED_INT, 0) # # glDisableVertexAttribArray(location_location) # glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) # glBindBuffer(GL_ARRAY_BUFFER, 0)
def render(self, draw_data): # perf: local for faster access io = self.io display_width, display_height = io.display_size fb_width = int(display_width * io.display_fb_scale[0]) fb_height = int(display_height * io.display_fb_scale[1]) if fb_width == 0 or fb_height == 0: return draw_data.scale_clip_rects(*io.display_fb_scale) # backup GL state # todo: provide cleaner version of this backup-restore code last_program = gl.GLint() gl.glGetIntegerv(gl.GL_CURRENT_PROGRAM, byref(last_program)) last_texture = gl.GLint() gl.glGetIntegerv(gl.GL_TEXTURE_BINDING_2D, byref(last_texture)) last_active_texture = gl.GLint() gl.glGetIntegerv(gl.GL_ACTIVE_TEXTURE, byref(last_active_texture)) last_array_buffer = gl.GLint() gl.glGetIntegerv(gl.GL_ARRAY_BUFFER_BINDING, byref(last_array_buffer)) last_element_array_buffer = gl.GLint() gl.glGetIntegerv(gl.GL_ELEMENT_ARRAY_BUFFER_BINDING, byref(last_element_array_buffer)) last_vertex_array = gl.GLint() gl.glGetIntegerv(gl.GL_VERTEX_ARRAY_BINDING, byref(last_vertex_array)) last_blend_src = gl.GLint() gl.glGetIntegerv(gl.GL_BLEND_SRC, byref(last_blend_src)) last_blend_dst = gl.GLint() gl.glGetIntegerv(gl.GL_BLEND_DST, byref(last_blend_dst)) last_blend_equation_rgb = gl.GLint() gl.glGetIntegerv(gl.GL_BLEND_EQUATION_RGB, byref(last_blend_equation_rgb)) last_blend_equation_alpha = gl.GLint() gl.glGetIntegerv(gl.GL_BLEND_EQUATION_ALPHA, byref(last_blend_equation_alpha)) last_viewport = (gl.GLint*4)() gl.glGetIntegerv(gl.GL_VIEWPORT, last_viewport) last_scissor_box = (gl.GLint*4)() gl.glGetIntegerv(gl.GL_SCISSOR_BOX, last_scissor_box) last_enable_blend = gl.GLint() gl.glIsEnabled(gl.GL_BLEND, byref(last_enable_blend)) last_enable_cull_face = gl.GLint() gl.glIsEnabled(gl.GL_CULL_FACE, byref(last_enable_cull_face)) last_enable_depth_test = gl.GLint() gl.glIsEnabled(gl.GL_DEPTH_TEST, byref(last_enable_depth_test)) last_enable_scissor_test = gl.GLint() gl.glIsEnabled(gl.GL_SCISSOR_TEST, byref(last_enable_scissor_test)) gl.glEnable(gl.GL_BLEND) gl.glBlendEquation(gl.GL_FUNC_ADD) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glDisable(gl.GL_CULL_FACE) gl.glDisable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_SCISSOR_TEST) gl.glActiveTexture(gl.GL_TEXTURE0) gl.glViewport(0, 0, int(fb_width), int(fb_height)) ortho_projection = [ 2.0/display_width, 0.0, 0.0, 0.0, 0.0, 2.0/-display_height, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, -1.0, 1.0, 0.0, 1.0 ] gl.glUseProgram(self._shader_handle) gl.glUniform1i(self._attrib_location_tex, 0) gl.glUniformMatrix4fv(self._attrib_proj_mtx, 1, gl.GL_FALSE, (gl.GLfloat * 16)(*ortho_projection)) gl.glBindVertexArray(self._vao_handle) for commands in draw_data.commands_lists: idx_buffer_offset = 0 gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self._vbo_handle) # todo: check this (sizes) gl.glBufferData(gl.GL_ARRAY_BUFFER, commands.vtx_buffer_size * imgui.VERTEX_SIZE, c_void_p(commands.vtx_buffer_data), gl.GL_STREAM_DRAW) gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, self._elements_handle) # todo: check this (sizes) gl.glBufferData(gl.GL_ELEMENT_ARRAY_BUFFER, commands.idx_buffer_size * imgui.INDEX_SIZE, c_void_p(commands.idx_buffer_data), gl.GL_STREAM_DRAW) # todo: allow to iterate over _CmdList for command in commands.commands: gl.glBindTexture(gl.GL_TEXTURE_2D, command.texture_id) # todo: use named tuple x, y, z, w = command.clip_rect gl.glScissor(int(x), int(fb_height - w), int(z - x), int(w - y)) if imgui.INDEX_SIZE == 2: gltype = gl.GL_UNSIGNED_SHORT else: gltype = gl.GL_UNSIGNED_INT gl.glDrawElements(gl.GL_TRIANGLES, command.elem_count, gltype, c_void_p(idx_buffer_offset)) idx_buffer_offset += command.elem_count * imgui.INDEX_SIZE # restore modified GL state gl.glUseProgram(cast((c_int*1)(last_program), POINTER(c_uint)).contents) gl.glActiveTexture(cast((c_int*1)(last_active_texture), POINTER(c_uint)).contents) gl.glBindTexture(gl.GL_TEXTURE_2D, cast((c_int*1)(last_texture), POINTER(c_uint)).contents) gl.glBindVertexArray(cast((c_int*1)(last_vertex_array), POINTER(c_uint)).contents) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, cast((c_int*1)(last_array_buffer), POINTER(c_uint)).contents) gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, cast((c_int*1)(last_element_array_buffer), POINTER(c_uint)).contents) gl.glBlendEquationSeparate(cast((c_int*1)(last_blend_equation_rgb), POINTER(c_uint)).contents, cast((c_int*1)(last_blend_equation_alpha), POINTER(c_uint)).contents) gl.glBlendFunc(cast((c_int*1)(last_blend_src), POINTER(c_uint)).contents, cast((c_int*1)(last_blend_dst), POINTER(c_uint)).contents) if last_enable_blend: gl.glEnable(gl.GL_BLEND) else: gl.glDisable(gl.GL_BLEND) if last_enable_cull_face: gl.glEnable(gl.GL_CULL_FACE) else: gl.glDisable(gl.GL_CULL_FACE) if last_enable_depth_test: gl.glEnable(gl.GL_DEPTH_TEST) else: gl.glDisable(gl.GL_DEPTH_TEST) if last_enable_scissor_test: gl.glEnable(gl.GL_SCISSOR_TEST) else: gl.glDisable(gl.GL_SCISSOR_TEST) gl.glViewport(last_viewport[0], last_viewport[1], last_viewport[2], last_viewport[3]) gl.glScissor(last_scissor_box[0], last_scissor_box[1], last_scissor_box[2], last_scissor_box[3])
def on_draw(self): gl.glClearColor(1.0, 0.5, 1.0, 1.0) self.clear() gl.glDrawElements(gl.GL_TRIANGLES, len(indices), gl.GL_UNSIGNED_INT, None) # draw bound buffers to the screen
def render(self): glDrawElements(self.draw_mode, self.ibo.count, self.ibo.type, 0)
def draw(self, entity): draw_count = entity.asset.index_buffers[entity.asset.current_index_buffer_id].draw_count glDrawElements(entity.asset.index_buffers[entity.asset.current_index_buffer_id].draw_type, draw_count, GL_UNSIGNED_INT, None) self.vertex_count += draw_count
def _draw_elements(self, mode=gl.GL_TRIANGLES): with self.element_array_buffer as el_array: gl.glDrawElements(mode, el_array.ndarray.shape[0], gl.GL_UNSIGNED_INT, 0)
def on_draw(self): # Clear the window self.clear() # Draw the mesh glDrawElements(GL_LINES, self.suzanne[2], GL_UNSIGNED_SHORT, 0)