def draw(self, projection, view, model, primitives=GL.GL_TRIANGLES): """ skinning object draw method """ GL.glUseProgram(self.shader.glid) # texture access setups GL.glActiveTexture(GL.GL_TEXTURE0) GL.glBindTexture(GL.GL_TEXTURE_2D, self.texture.glid) GL.glUniform1i(self.loc['diffuse_map'], 0) # setup light parameters GL.glUniform3fv(self.loc['light_dir'], 1, self.light_dir) # setup material parameters GL.glUniform3fv(self.loc['k_a'], 1, self.k_a) GL.glUniform3fv(self.loc['k_d'], 1, self.k_d) GL.glUniform3fv(self.loc['k_s'], 1, self.k_s) GL.glUniform1f(self.loc['s'], max(self.s, 0.001)) # world camera position for Phong illumination specular component w_camera_position = np.linalg.inv(view)[:, 3] GL.glUniform3fv(self.loc['w_camera_position'], 1, w_camera_position) # bone world transform matrices need to be passed for skinning world_transforms = [node.world_transform for node in self.bone_nodes] bone_matrix = world_transforms @ self.bone_offsets loc = GL.glGetUniformLocation(self.shader.glid, 'bone_matrix') GL.glUniformMatrix4fv(loc, len(self.bone_nodes), True, bone_matrix) super().draw(projection, view, model, primitives)
def on_display(): gl.glClearColor(1,1,1,1); gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glEnable(gl.GL_BLEND) shader.bind() shader.uniformi( 'uniforms', 0 ) shape = collection.uniforms_shape or (1,1) shader.uniformf( 'uniforms_shape', shape[0], shape[1]) gl.glActiveTexture( gl.GL_TEXTURE0 ) gl.glBindTexture( gl.GL_TEXTURE_2D, collection.uniforms_id ) _,_,width,height = gl.glGetIntegerv( gl.GL_VIEWPORT ) P = orthographic( 0, width, 0, height, -1, +1 ) V = np.eye(4).astype( np.float32 ) M = np.eye(4).astype( np.float32 ) shader.uniform_matrixf( 'M', M ) shader.uniform_matrixf( 'V', V ) shader.uniform_matrixf( 'P', P ) collection.draw( ) shader.unbind() glut.glutSwapBuffers()
def render(self, scene: Scene, shader: ShaderProgram): entity = scene.entity animated_model = entity.animated_model model = animated_model.raw_model gl.glBindVertexArray(model.vaoID) gl.glEnableVertexAttribArray(0) gl.glEnableVertexAttribArray(1) gl.glEnableVertexAttribArray(2) gl.glEnableVertexAttribArray(3) gl.glEnableVertexAttribArray(4) trans_matrix = create_transformation_matrix(entity.position, entity.rotation, entity.scale) shader.load_transformation_matrix(trans_matrix) shader.load_view_matrix(scene.camera) shader.load_joint_transforms_array(entity) # TODO notice this call. we pass GL_TEXTURE0 because of uniform sampler2D in the fragment shader (tutorial 6) gl.glActiveTexture(gl.GL_TEXTURE0) gl.glBindTexture(gl.GL_TEXTURE_2D, animated_model.textureID) # glDrawElements is the function that let's opengl know that we're using an index buffer (indices) gl.glDrawElements( gl.GL_TRIANGLES, model.vertex_count, gl.GL_UNSIGNED_INT, gl.ctypes.c_void_p( 0)) # TODO: not sure if c_void_p or something else gl.glDisableVertexAttribArray(0) gl.glDisableVertexAttribArray(1) gl.glDisableVertexAttribArray(2) gl.glDisableVertexAttribArray(3) gl.glDisableVertexAttribArray(4) gl.glBindVertexArray(0)
def paint(self, texture): """Draw the given texture at full screen.""" # make sure we can use VBOs gl.glEnableVertexAttribArray(0) # activate the program gl.glUseProgram(self.program) # indicate the texture will be set by GL_TEXTURE0 gl.glUniform1i(self.texture1, 0) # turn on texture mapping and bind to the given texture gl.glEnable(gl.GL_TEXTURE_2D) gl.glActiveTexture(gl.GL_TEXTURE0) gl.glBindTexture(gl.GL_TEXTURE_2D, texture) # activate the square to be rendered self.square.bind() # indicate we are setting vertex and tex_coord data in the VBO gl.glEnableClientState(gl.GL_VERTEX_ARRAY); gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY); # vertex is first 3 floats in the VBO gl.glVertexAttribPointer(0, 3, gl.GL_FLOAT, gl.GL_FALSE, 5*4, None) # texture coords are the 2 floats after the first 3 in the VBO gl.glTexCoordPointer(2, gl.GL_FLOAT, 5*4, self.square + 3*4) # draw the textured square gl.glDrawArrays(gl.GL_TRIANGLES, 0, 6)
def v_render(): ''' render to vr and window ''' global hmd, ctx, window # resolve multi-sample offscreen buffer gl.glBindFramebuffer(gl.GL_READ_FRAMEBUFFER, ctx.con.offFBO) gl.glReadBuffer(gl.GL_COLOR_ATTACHMENT0) gl.glBindFramebuffer(gl.GL_DRAW_FRAMEBUFFER, ctx.con.offFBO_r) gl.glDrawBuffer(gl.GL_COLOR_ATTACHMENT0) gl.glBlitFramebuffer(0, 0, 2 * hmd.width, hmd.height, 0, 0, 2 * hmd.width, hmd.height, gl.GL_COLOR_BUFFER_BIT, gl.GL_NEAREST) # blit to window, left only, window is half-size gl.glBindFramebuffer(gl.GL_READ_FRAMEBUFFER, ctx.con.offFBO_r) gl.glReadBuffer(gl.GL_COLOR_ATTACHMENT0) gl.glBindFramebuffer(gl.GL_DRAW_FRAMEBUFFER, 0) gl.glDrawBuffer(gl.GL_BACK if ctx.con.windowDoublebuffer else gl.GL_FRONT) gl.glBlitFramebuffer(0, 0, hmd.width, hmd.height, 0, 0, hmd.width // 2, hmd.height // 2, gl.GL_COLOR_BUFFER_BIT, gl.GL_NEAREST) # blit to vr texture gl.glActiveTexture(gl.GL_TEXTURE2) gl.glBindFramebuffer(gl.GL_DRAW_FRAMEBUFFER, ctx.con.offFBO_r) gl.glFramebufferTexture2D(gl.GL_FRAMEBUFFER, gl.GL_COLOR_ATTACHMENT1, gl.GL_TEXTURE_2D, hmd.idtex, 0) gl.glDrawBuffer(gl.GL_COLOR_ATTACHMENT1) gl.glBlitFramebuffer(0, 0, 2 * hmd.width, hmd.height, 0, 0, 2 * hmd.width, hmd.height, gl.GL_COLOR_BUFFER_BIT, gl.GL_NEAREST) gl.glFramebufferTexture2D(gl.GL_FRAMEBUFFER, gl.GL_COLOR_ATTACHMENT1, gl.GL_TEXTURE_2D, 0, 0) gl.glDrawBuffer(gl.GL_COLOR_ATTACHMENT0) openvr.VRCompositor().submit(openvr.Eye_Left, hmd.vTex, hmd.boundLeft) openvr.VRCompositor().submit(openvr.Eye_Right, hmd.vTex, hmd.boundRight) # swap if window is double-buffered, flush just in case if ctx.con.windowDoublebuffer: glfw.swap_buffers(window) gl.glFlush()
def on_display( ): global shader gl.glClearColor(1,1,1,1) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glActiveTexture(gl.GL_TEXTURE0) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glEnable(gl.GL_BLEND) gl.glColor(0,0,0,1) shader.bind() radius = 255.0 theta, dtheta = 0, 5.5/180.0*math.pi support = 0.75 thickness = 1.0 for i in range(500): x = 256+radius*math.cos(theta); y = 32+256+radius*math.sin(theta); r = 10.1-i*0.02 circle( (x,y,r), thickness=thickness, support=support ) radius -= 0.45 theta += dtheta for i in range(0,39): r = 4 thickness = (i+1)/10.0 x = 20+i*12.5 - r y = 16 circle( (x,y,r), thickness=thickness, support=support ) glut.glutSwapBuffers( )
def bind_texture(self): ''' Bind the current texture to the OpenGL context for rendering. You can release the texture in the usual way by binding the zero texture. i.e with GL.glBindTexture(GL.GL_TEXTURE_2D, 0) Alternatively, using the texture rendering code within the context manager defined by an instance of the class will bind the textures automatically. Use this as: with texture_stream_2d_instance: rendering code goes here ''' GL.glActiveTexture(self.__texture_unit) if self.__n_pixel_buffers is 1: texture_id = self.__textures[0] else: self.__update_read_idx() texture_id = self.__textures[self.__read_idx] GL.glBindTexture(GL.GL_TEXTURE_2D, texture_id)
def paintEvent(self, event): self.makeCurrent() gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glUseProgram(self.shaders_program) gl.glUniform1i(self.colormap_location, 0) gl.glActiveTexture(gl.GL_TEXTURE0 + 0) gl.glBindTexture(gl.GL_TEXTURE_1D, self.colormap_id) gl.glBegin(gl.GL_QUAD_STRIP) bar_start = -1 + self.text_height / self.height() * 2 bar_height = (1 - 2 * self.text_height / self.height()) * 2 steps = 40 for i in range(steps + 1): y = i * (1 / steps) # gl.glColor(y, 0, 0) gl.glVertex(-0.5, (bar_height * y + bar_start), y) gl.glVertex(0.5, (bar_height * y + bar_start), y) gl.glEnd() p = QPainter(self) p.drawText((self.width() - self.vmax_width) / 2, self.text_ascent, self.vmax_str) p.drawText((self.width() - self.vmin_width) / 2, self.height() - self.text_height + self.text_ascent, self.vmin_str) p.end()
def draw(self): if self._dirty: self.upload() gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glEnable(gl.GL_BLEND) _,_,width,height = gl.glGetIntegerv( gl.GL_VIEWPORT ) P = orthographic( 0, width, 0, height, -1, +1 ) V = np.eye(4).astype( np.float32 ) M = np.eye(4).astype( np.float32 ) shader = self.shader shader.bind() gl.glActiveTexture( gl.GL_TEXTURE0 ) shader.uniformi( 'u_uniforms', 0 ) gl.glBindTexture( gl.GL_TEXTURE_2D, self._ubuffer_id ) if self.dash_atlas: gl.glActiveTexture( gl.GL_TEXTURE1 ) shader.uniformi('u_dash_atlas', 1) gl.glBindTexture( gl.GL_TEXTURE_2D, self.dash_atlas.texture_id ) shader.uniform_matrixf( 'u_M', M ) shader.uniform_matrixf( 'u_V', V ) shader.uniform_matrixf( 'u_P', P ) shape = self._ubuffer_shape shader.uniformf( 'u_uniforms_shape', shape[1]//4, shape[0]) self._vbuffer.draw( ) shader.unbind()
def draw(self): loc = gl.glGetUniformLocation(self.program, "tex") gl.glUniform1i(loc, 0) gl.glActiveTexture(gl.GL_TEXTURE0) gl.glBindTexture(gl.GL_TEXTURE_2D, self.tex) super(texquad, self).draw()
def __init__(self, width, height, data=None, interp=True, float_tex=False): tex_id = gl.glGenTextures(1) gl.glActiveTexture(gl.GL_TEXTURE0 + tex_id) gl.glBindTexture(gl.GL_TEXTURE_2D, tex_id) gl.glEnable(gl.GL_TEXTURE_2D) gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 1) if float_tex: gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGBA16F, width, height, 0, gl.GL_RGBA, gl.GL_FLOAT, data) else: gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGBA, width, height, 0, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, data) gl.glTexParameterf(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S, gl.GL_MIRRORED_REPEAT) gl.glTexParameterf(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T, gl.GL_MIRRORED_REPEAT) gl.glTexParameterf(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR if interp else gl.GL_NEAREST) gl.glTexParameterf(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR if interp else gl.GL_NEAREST) self.width = width self.height = height self.tex_id = tex_id self.bound = False
def draw(self): for polIdx, pol in zip(range(len(self.verts)), self.verts): GL.glActiveTexture(GL.GL_TEXTURE0+polIdx) GL.glBindTexture(GL.GL_TEXTURE_2D, self.texIdx[polIdx]) shaders.glUseProgram(self.shader) loc = GL.glGetUniformLocation(self.shader, "u_translate") GL.glUniformMatrix4fv(loc, 1, GL.GL_FALSE, self.translationMatrix.matrix) loc = GL.glGetUniformLocation(self.shader, "u_rotate") GL.glUniformMatrix4fv(loc, 1, GL.GL_FALSE, self.rotationMatrix.matrix) loc = GL.glGetUniformLocation(self.shader, "u_scale") GL.glUniformMatrix4fv(loc, 1, GL.GL_FALSE, self.scalingMatrix.matrix) loc = GL.glGetUniformLocation(self.shader, "u_texture") GL.glUniform1i(loc, polIdx) loc = GL.glGetUniformLocation(self.shader, "u_rotationCenter") GL.glUniform4fv(loc, 1, self.rotationCenter) GL.glBegin(GL.GL_POLYGON) GL.glTexCoord2f(1.0-self.uvs[polIdx][0][0], 1.0-self.uvs[polIdx][0][1]) GL.glVertex3f(pol[0][0], pol[0][1], pol[0][2]) GL.glTexCoord2f(1.0-self.uvs[polIdx][0][0], 1.0-self.uvs[polIdx][1][1]) GL.glVertex3f(pol[1][0], pol[1][1], pol[1][2]) GL.glTexCoord2f(1.0-self.uvs[polIdx][1][0], 1.0-self.uvs[polIdx][1][1]) GL.glVertex3f(pol[2][0], pol[2][1], pol[2][2]) GL.glTexCoord2f(1.0-self.uvs[polIdx][1][0], 1.0-self.uvs[polIdx][0][1]) GL.glVertex3f(pol[3][0], pol[3][1], pol[3][2]) GL.glEnd() shaders.glUseProgram(0)
def draw_text(self, text, color=(1.0, 1.0, 1.0, 0.0), view_matrix=None, projection_matrix=None): gl.glUseProgram(self._program_id) gl.glActiveTexture(gl.GL_TEXTURE0+0) gl.glBindTexture(gl.GL_TEXTURE_2D, self._texture_id) gl.glBindSampler(0, self._sampler_id) gl.glUniform1i(self._uniform_locations['u_fonttex'], 0) gl.glUniform4f(self._uniform_locations['u_color'], *color) if view_matrix is not None: self._matrix.dot(view_matrix, out=self._modelview_matrix) gl.glUniformMatrix4fv(self._uniform_locations['u_modelviewMatrix'], 1, False, self._modelview_matrix) if projection_matrix is not None: gl.glUniformMatrix4fv(self._uniform_locations['u_projectionMatrix'], 1, False, projection_matrix) gl.glEnableVertexAttribArray(0) gl.glEnableVertexAttribArray(1) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) x = 0.0 text = [ord(c) - 32 for c in text] for i in text: if i >= 0 and i < 95: gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self._buffer_ids[i]) gl.glVertexAttribPointer(0, 2, gl.GL_FLOAT, False, 0, c_void_p(0)) gl.glVertexAttribPointer(1, 2, gl.GL_FLOAT, False, 0, c_void_p(8*4)) gl.glUniform1f(self._uniform_locations['advance'], x) gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 0, 4) x += self._advance[i] gl.glDisableVertexAttribArray(0) gl.glDisableVertexAttribArray(1) gl.glDisable(gl.GL_BLEND)
def upload_uniforms(self): gl.glActiveTexture( gl.GL_TEXTURE0 ) data = self._ubuffer.data.view(np.float32) shape = self._ushape if not self._ubuffer_id: self._ubuffer_id = gl.glGenTextures(1) gl.glBindTexture( gl.GL_TEXTURE_2D, self._ubuffer_id ) gl.glPixelStorei( gl.GL_UNPACK_ALIGNMENT, 1 ) gl.glPixelStorei( gl.GL_PACK_ALIGNMENT, 1 ) gl.glTexParameterf( gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_NEAREST ) gl.glTexParameterf( gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST ) gl.glTexParameterf( gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP_TO_EDGE ) gl.glTexParameterf( gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP_TO_EDGE ) gl.glTexParameterf(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_BASE_LEVEL, 0) gl.glTexParameterf(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAX_LEVEL, 0) gl.glTexImage2D( gl.GL_TEXTURE_2D, 0, gl.GL_RGBA32F, shape[1], shape[0], 0, gl.GL_RGBA, gl.GL_FLOAT, data ) gl.glActiveTexture( gl.GL_TEXTURE0 ) gl.glBindTexture( gl.GL_TEXTURE_2D, self._ubuffer_id ) gl.glTexImage2D( gl.GL_TEXTURE_2D, 0, gl.GL_RGBA32F, shape[1], shape[0], 0, gl.GL_RGBA, gl.GL_FLOAT, data )
def draw(self, shader, texture, lighting=True): gl.glActiveTexture(gl.GL_TEXTURE0) gl.glBindTexture(gl.GL_TEXTURE_2D, self.texture_keys[texture]) if lighting: # define material properties shader.set_value("material.ambient", self.mtl['Ka']) shader.set_value("material.diffuse", self.mtl['Kd']) shader.set_value("material.specular", self.mtl['Ks']) shader.set_value("material.shininess", self.mtl['Ns'][0]) self.ebo.bind() self.vbo.bind() try: gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glVertexPointer(3, gl.GL_FLOAT, 4 * 8, self.vbo) gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY) gl.glTexCoordPointer(2, gl.GL_FLOAT, 4 * 8, self.vbo + (4*6)) gl.glEnableClientState(gl.GL_NORMAL_ARRAY) gl.glNormalPointer(gl.GL_FLOAT, 4 * 8, self.vbo + (4*3)) gl.glDrawElements(gl.GL_TRIANGLES, self.num_faces * 3, gl.GL_UNSIGNED_INT, None) finally: self.ebo.unbind() self.vbo.unbind() gl.glDisableClientState(gl.GL_VERTEX_ARRAY) gl.glDisableClientState(gl.GL_NORMAL_ARRAY) gl.glDisableClientState(gl.GL_TEXTURE_COORD_ARRAY)
def flip(): global frameTexture if useFBO: FB.glBindFramebufferEXT(FB.GL_FRAMEBUFFER_EXT, 0) GL.glDisable(GL.GL_BLEND) #GL.glBlendEquation(GL.GL_FUNC_ADD) #GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA) #GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_DST_ALPHA) #before flipping need to copy the renderBuffer to the frameBuffer GL.glColor4f(1,1,1,1) GL.glActiveTexture(GL.GL_TEXTURE0) GL.glBindTexture(GL.GL_TEXTURE_2D, frameTexture) GL.glBegin( GL.GL_QUADS ) GL.glMultiTexCoord2f(GL.GL_TEXTURE0, 0.0, 0.0 ) GL.glVertex2f( -1.0,-1.0 ) GL.glMultiTexCoord2f(GL.GL_TEXTURE0, 0.0, 1.0 ) GL.glVertex2f( -1.0, 1.0 ) GL.glMultiTexCoord2f(GL.GL_TEXTURE0,1.0, 1.0 ) GL.glVertex2f( 1.0, 1.0 ) GL.glMultiTexCoord2f(GL.GL_TEXTURE0, 1.0, 0.0 ) GL.glVertex2f( 1.0, -1.0 ) GL.glEnd() pygame.display.flip() if useFBO: GL.glBindTexture(GL.GL_TEXTURE_2D, 0) GL.glDisable(GL.GL_TEXTURE_2D) GL.glEnable(GL.GL_BLEND) FB.glBindFramebufferEXT(FB.GL_FRAMEBUFFER_EXT, frameBuffer)
def createBufferTexture(texID, texw, texh): """createBufferTexture - create an 8-bit texture render target""" gl.glActiveTexture(gl.GL_TEXTURE0) gl.glBindTexture(gl.GL_TEXTURE_2D, texID) black = (0., 0., 0., 0.) # The special shader used to render this texture performs a # per-pixel image processing where point sampling is required, # so specify nearest neighbor sampling. # # Also, the flood fill shader handles its own edge clamping, so # texture mode GL_REPEAT is inconsequential. "Zero outside" would # be useful, but separate edge values are deprecated in OpenGL. # gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_NEAREST) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP_TO_EDGE) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP_TO_EDGE) gl.glTexParameterfv(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_BORDER_COLOR, black) gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGBA, texw, texh, 0, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, '\x00' * texw*texh*4) gl.glBindTexture(gl.GL_TEXTURE_2D, 0) checkGLError()
def draw(self): if self._dirty: self.upload() gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glEnable(gl.GL_BLEND) _, _, width, height = gl.glGetIntegerv(gl.GL_VIEWPORT) P = orthographic(0, width, 0, height, -1, +1) V = np.eye(4).astype(np.float32) M = np.eye(4).astype(np.float32) shader = self.shader shader.bind() gl.glActiveTexture(gl.GL_TEXTURE0) shader.uniformi('u_uniforms', 0) gl.glBindTexture(gl.GL_TEXTURE_2D, self._ubuffer_id) if self.dash_atlas: gl.glActiveTexture(gl.GL_TEXTURE1) shader.uniformi('u_dash_atlas', 1) gl.glBindTexture(gl.GL_TEXTURE_2D, self.dash_atlas.texture_id) shader.uniform_matrixf('u_M', M) shader.uniform_matrixf('u_V', V) shader.uniform_matrixf('u_P', P) shape = self._ubuffer_shape shader.uniformf('u_uniforms_shape', shape[1] // 4, shape[0]) self._vbuffer.draw() shader.unbind()
def render(shader, vao, tex, tex2): gl.glClearColor(0.2, 0.3, 0.3, 0.4) gl.glClear(gl.GL_COLOR_BUFFER_BIT|gl.GL_DEPTH_BUFFER_BIT) gl.glBindTexture(gl.GL_TEXTURE_2D, tex) gl.glUseProgram(shader) gl.glActiveTexture(gl.GL_TEXTURE0) gl.glBindTexture(gl.GL_TEXTURE_2D, tex) gl.glUniform1i(gl.glGetUniformLocation(shader, "ourTexture1"), 0) gl.glActiveTexture(gl.GL_TEXTURE1) gl.glBindTexture(gl.GL_TEXTURE_2D, tex2) gl.glUniform1i(gl.glGetUniformLocation(shader, "ourTexture2"), 1) view = gl.glGetUniformLocation(shader, "view") view_matrix = array([ [0.8, 0, 0, 0], [0, 0.8, 0, 0], [0, 0, 0.8, -4], [0, 0, 0, 1], ]) gl.glUniformMatrix4fv(view, 1, gl.GL_TRUE, view_matrix) projection = gl.glGetUniformLocation(shader, "projection") projection_matrix = get_projection_matrix(45, WINDOW_WIDTH/WINDOW_HEIGHT, 0.1, 100) gl.glUniformMatrix4fv(projection, 1, gl.GL_FALSE, projection_matrix) gl.glBindVertexArray(vao) for idx in range(0, len(CUBEPOSITIONS)): model = gl.glGetUniformLocation(shader, "model") angel = glfw.get_time()%360 model_matrix = get_model_matrix(idx, angel) gl.glUniformMatrix4fv(model, 1, gl.GL_TRUE, model_matrix) gl.glDrawArrays(gl.GL_TRIANGLES, 0, VERTEXS.size) gl.glBindVertexArray(0) gl.glUseProgram(0)
def render(self, mvp): with ExitStack() as stack: shaders.glUseProgram(self._cam_model_program) stack.callback(lambda: shaders.glUseProgram(0)) GL.glUniformMatrix4fv( GL.glGetUniformLocation(self._cam_model_program, 'mvp'), 1, True, mvp) self._cam_model_vertex_positions.bind() position_loc = GL.glGetAttribLocation(self._cam_model_program, 'in_position') GL.glEnableVertexAttribArray(position_loc) stack.callback(lambda: GL.glDisableVertexAttribArray(position_loc)) GL.glVertexAttribPointer(position_loc, 3, GL.GL_FLOAT, False, 0, self._cam_model_vertex_positions) self._cam_model_vertex_positions.unbind() self._cam_model_uvs.bind() uv_loc = GL.glGetAttribLocation(self._cam_model_program, 'in_uv') GL.glEnableVertexAttribArray(uv_loc) stack.callback(lambda: GL.glDisableVertexAttribArray(uv_loc)) GL.glVertexAttribPointer(uv_loc, 2, GL.GL_FLOAT, False, 0, self._cam_model_uvs) self._cam_model_uvs.unbind() GL.glActiveTexture(GL.GL_TEXTURE0) GL.glBindTexture(GL.GL_TEXTURE_2D, self._cam_model_texture) GL.glUniform1i( GL.glGetUniformLocation(self._cam_model_program, 'texture_sampler'), 0) GL.glDrawArrays(GL.GL_TRIANGLES, 0, len(self._cam_model_vertex_positions))
def paintGL(self): """ Called whenever a repaint is needed. Calling ``update()`` will schedule a repaint. """ GL.glClearColor(0.0, 0.0, 0.0, 1.0) GL.glClear(GL.GL_COLOR_BUFFER_BIT) if self._shader_program is not None: GL.glUseProgram(self._shader_program) self._use_ocio_tex() self._use_ocio_uniforms() # Set uniforms mvp_mat = self._proj_mat * self._model_view_mat mvp_mat_loc = GL.glGetUniformLocation(self._shader_program, "mvpMat") GL.glUniformMatrix4fv(mvp_mat_loc, 1, GL.GL_FALSE, self._m44f_to_ndarray(mvp_mat)) image_tex_loc = GL.glGetUniformLocation(self._shader_program, "imageTex") GL.glUniform1i(image_tex_loc, 0) # Bind texture, VAO, and draw GL.glActiveTexture(GL.GL_TEXTURE0 + 0) GL.glBindTexture(GL.GL_TEXTURE_2D, self._image_tex) GL.glBindVertexArray(self._plane_vao) GL.glDrawElements(GL.GL_TRIANGLES, 6, GL.GL_UNSIGNED_INT, ctypes.c_void_p(0)) GL.glBindVertexArray(0)
def flip(): global frameTexture if useFBO: FB.glBindFramebufferEXT(FB.GL_FRAMEBUFFER_EXT, 0) GL.glDisable(GL.GL_BLEND) #GL.glBlendEquation(GL.GL_FUNC_ADD) #GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA) #GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_DST_ALPHA) #before flipping need to copy the renderBuffer to the frameBuffer GL.glColor4f(1, 1, 1, 1) GL.glActiveTexture(GL.GL_TEXTURE0) GL.glBindTexture(GL.GL_TEXTURE_2D, frameTexture) GL.glBegin(GL.GL_QUADS) GL.glMultiTexCoord2f(GL.GL_TEXTURE0, 0.0, 0.0) GL.glVertex2f(-1.0, -1.0) GL.glMultiTexCoord2f(GL.GL_TEXTURE0, 0.0, 1.0) GL.glVertex2f(-1.0, 1.0) GL.glMultiTexCoord2f(GL.GL_TEXTURE0, 1.0, 1.0) GL.glVertex2f(1.0, 1.0) GL.glMultiTexCoord2f(GL.GL_TEXTURE0, 1.0, 0.0) GL.glVertex2f(1.0, -1.0) GL.glEnd() pygame.display.flip() if useFBO: GL.glBindTexture(GL.GL_TEXTURE_2D, 0) GL.glDisable(GL.GL_TEXTURE_2D) GL.glEnable(GL.GL_BLEND) FB.glBindFramebufferEXT(FB.GL_FRAMEBUFFER_EXT, frameBuffer)
def upload(self): # Update uniform index for all objects # TODO: vectorize for i in range(len(self.objects)): _,vstart,vend,_,_,_,_ = self.objects[i] self.vertices_data[vstart:vend]['index'] = i/float(len(self.uniforms_data)) VertexBuffer.upload(self) if not self._uniforms_id: self._uniforms_id = gl.glGenTextures(1) gl.glActiveTexture( gl.GL_TEXTURE0 ) gl.glPixelStorei( gl.GL_UNPACK_ALIGNMENT, 1 ) gl.glPixelStorei( gl.GL_PACK_ALIGNMENT, 1 ) gl.glBindTexture( gl.GL_TEXTURE_2D, self._uniforms_id ) gl.glTexParameterf( gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_NEAREST ) gl.glTexParameterf( gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST ) gl.glTexParameterf( gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP_TO_EDGE ) gl.glTexParameterf( gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP_TO_EDGE ) data = self.uniforms_data.view(np.float32) self.uniforms_shape = data.shape[1]//4, data.shape[0] gl.glTexImage2D( gl.GL_TEXTURE_2D, 0, gl.GL_RGBA32F_ARB, data.shape[1]//4, data.shape[0], 0, gl.GL_RGBA, gl.GL_FLOAT, data )
def render(self): gl.glClearColor(0, 0, 0, 1) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) with self.shader_prog: # Activate array gl.glBindVertexArray(self.vao_id) # FIXME: Why does the following work? tex_uniform is 1, # palette_uniform is 0, but I have to set the uniform for # tex_uniform to 0 and the uniform for palette_uniform to 1. # Obviously I'm not understanding something. # # See: http://stackoverflow.com/questions/26622204 # https://www.opengl.org/discussion_boards/showthread.php/174926-when-to-use-glActiveTexture # Activate texture print(self.tex_uniform, self.palette_uniform, self.sample_texture, self.palette_id) gl.glActiveTexture(gl.GL_TEXTURE0 + self.tex_uniform) gl.glBindTexture(gl.GL_TEXTURE_2D, self.sample_texture) gl.glUniform1i(self.tex_uniform, 0) # Activate palette texture gl.glActiveTexture(gl.GL_TEXTURE0 + self.palette_uniform) gl.glBindTexture(gl.GL_TEXTURE_BUFFER, self.palette_texture) gl.glTexBuffer(gl.GL_TEXTURE_BUFFER, gl.GL_RGBA8, self.palette_id) gl.glUniform1i(self.palette_uniform, 1) # # Activate array # gl.glBindVertexArray(self.vao_id) # draw triangle gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 0, 4)
def render(self): GL.glUseProgram(self.shader.program) GL.glActiveTexture(GL.GL_TEXTURE0) GL.glUniform1i(self.tex_uniform, 0) GL.glUniform2f(self.tex_scale_uniform, *self.get_texture_scale()) GL.glBindTexture(GL.GL_TEXTURE_2D, self.texture.gltex) GL.glUniformMatrix4fv(self.proj_matrix_uniform, 1, GL.GL_FALSE, self.get_projection_matrix()) GL.glUniformMatrix4fv(self.view_matrix_uniform, 1, GL.GL_FALSE, self.get_view_matrix()) GL.glUniform3f(self.position_uniform, self.x, self.y, self.z) GL.glUniform3f(self.scale_uniform, self.scale_x, self.scale_y, self.scale_z) GL.glUniform1f(self.alpha_uniform, self.alpha) if self.app.use_vao: GL.glBindVertexArray(self.vao) else: GL.glBindBuffer(GL.GL_ARRAY_BUFFER, self.vert_buffer) GL.glVertexAttribPointer(self.pos_attrib, 2, GL.GL_FLOAT, GL.GL_FALSE, 0, ctypes.c_void_p(0)) GL.glEnableVertexAttribArray(self.pos_attrib) if self.blend: GL.glEnable(GL.GL_BLEND) GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA) GL.glDrawArrays(GL.GL_TRIANGLE_STRIP, 0, self.vert_count) if self.blend: GL.glDisable(GL.GL_BLEND) if self.app.use_vao: GL.glBindVertexArray(0) GL.glUseProgram(0)
def makefont(self, filename, fontsize): face = freetype.Face(filename) face.set_pixel_sizes(0, fontsize) gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 1) gl.glActiveTexture(gl.GL_TEXTURE0) self.top_bearing = 0 for c in range(128): face.load_char(chr(c), freetype.FT_LOAD_RENDER) glyph = face.glyph bitmap = glyph.bitmap size = bitmap.width, bitmap.rows bearing = glyph.bitmap_left, glyph.bitmap_top self.top_bearing = max(self.top_bearing, bearing[1]) advance = glyph.advance.x # create glyph texture texObj = gl.glGenTextures(1) gl.glBindTexture(gl.GL_TEXTURE_2D, texObj) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP_TO_EDGE) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP_TO_EDGE) gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_R8, *size, 0, gl.GL_RED, gl.GL_UNSIGNED_BYTE, bitmap.buffer) self.characters.append((texObj, size, bearing, advance)) gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 4) gl.glBindTexture(gl.GL_TEXTURE_2D, 0)
def __init__(self, image, mode=1, format=GL.GL_RGBA, texformat=GL.GL_RGBA): self.texid = None if isinstance(image, (str, unicode)): from pyformex.plugins.imagearray import qimage2numpy image = qimage2numpy(image, indexed=False) else: image = np.asarray(image) s = "Texture: type %s, size %s" % (image.dtype, image.shape) image = np.require(image, dtype='ubyte', requirements='C') print(s + "; Converted to: type %s, size %s" % (image.dtype, image.shape)) ny, nx = image.shape[:2] # Generate a texture id self.texid = GL.glGenTextures(1) self.texun = None # Make our new texture the current 2D texture GL.glEnable(GL.GL_TEXTURE_2D) #GL.glTexEnvf(GL.GL_TEXTURE_ENV,GL.GL_TEXTURE_ENV_MODE,GL.GL_MODULATE) # select texture unit 0 GL.glActiveTexture(GL.GL_TEXTURE0) GL.glBindTexture(GL.GL_TEXTURE_2D, self.texid) GL.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1) # Copy the texture data into the current texture GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, texformat, nx, ny, 0, format, GL.GL_UNSIGNED_BYTE, image) self.mode = mode
def draw(self, shader): normal_nr = 1 height_nr = 1 diffuse_nr = 1 specular_nr = 1 for idx, texture in enumerate(self.textures): gl.glActiveTexture(gl.GL_TEXTURE0 + idx) number = str() name = texture.type if name == 'texture_diffuse': number = str(diffuse_nr) diffuse_nr += 1 elif name == 'texture_specular': number = str(specular_nr) specular_nr += 1 elif name == 'texture_normal': number = str(normal_nr) normal_nr += 1 elif name == 'texture_height': number = str(height_nr) height_nr += 1 shader.set_int(name + number, idx) gl.glBindTexture(gl.GL_TEXTURE_2D, texture.id) gl.glBindVertexArray(self.vao) gl.glDrawElements(gl.GL_TRIANGLES, len(self.indices), gl.GL_UNSIGNED_INT, c_void_p(0)) gl.glBindVertexArray(0) gl.glActiveTexture(gl.GL_TEXTURE0)
def draw_text(self, text, color=(1.0, 1.0, 0.0, 0.0), screen_position=(0.0, 0.0)): import OpenGL.GL as gl gl.glUseProgram(self._program_id) gl.glActiveTexture(gl.GL_TEXTURE0 + self._texture_unit) gl.glBindTexture(gl.GL_TEXTURE_2D, self._texture_id) #gl.glBindSampler(tex_unit, self._sampler_id) gl.glUniform1i(self._uniform_locations['u_fonttex'], self._texture_unit) gl.glUniform4f(self._uniform_locations['u_color'], *color) gl.glUniform2f(self._uniform_locations['u_screen_size'], *self._screen_size) gl.glUniform2f(self._uniform_locations['u_char_size'], self._width, self._height) gl.glUniform2f(self._uniform_locations['u_screen_position'], *screen_position) gl.glUniform2f(self._uniform_locations['u_fonttex_size'], self._image_width, self._image_height) nchars = len(text) gl.glUniform1ui(self._uniform_locations['u_nchars'], nchars) self._texcoords[:nchars] = [self._char_to_texcoords[c] for c in text] gl.glUniform2fv(self._uniform_locations['u_texcoords'], nchars, self._texcoords) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 0, 4) gl.glDisable(gl.GL_BLEND)
def bindTextures(self, shader): index = 0 for sampler, entry in self.textures.items(): shader.uniformi(sampler, index) gl.glActiveTexture(gl.GL_TEXTURE0 + index) gl.glBindTexture(gl.GL_TEXTURE_2D, entry.glTexture) index += 1
def __init__(self, skybox_name, cam_proj): # self.program = make_program('skybox_simple.vert', 'skybox_simple.frag') self.program = make_program('skybox.vert', 'skybox.frag', 'skybox.glsl') gl.glUseProgram(self.program) # Get uniforms: handles for projection and resolution do not change in one simulation. The view does. proj_handle = gl.glGetUniformLocation(self.program, "projection") self.skybox_view_handle = gl.glGetUniformLocation(self.program, "view") gl.glUniformMatrix4fv(proj_handle, 1, False, cam_proj) ### Activate texture unit 1 tex_unit = 1 gl.glActiveTexture(gl.GL_TEXTURE0 + tex_unit) self.texobj_skybox_id = load_cubemap(skybox_name) skybox_texture_loc = gl.glGetUniformLocation(self.program, "skybox_cubemap") gl.glUniform1i(skybox_texture_loc, tex_unit) gl.glActiveTexture(gl.GL_TEXTURE0) ### # Make a VAO self.vao_id = gl.glGenVertexArrays(1) gl.glBindVertexArray(self.vao_id) self.vao_len = 36 # Make a VBO self.vbo_id = gl.glGenBuffers(1) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self.vbo_id) # Bind VBO to VAO with the index of the correct attribute (loc) loc = gl.glGetAttribLocation(self.program, 'position') gl.glEnableVertexAttribArray(loc) # Describe the position data layout in the buffer gl.glVertexAttribPointer( loc, 3, # 3 vertices per triangle gl.GL_FLOAT, # Type False, # Values already normalised, no need to 0, # Stride ctypes.c_void_p(0)) # Array buffer offset (None) # Send the data over to the buffer gl.glBufferData(gl.GL_ARRAY_BUFFER, self.cube_vert_coords.nbytes, self.cube_vert_coords, gl.GL_STATIC_DRAW) # Unbind from the VAO first (Important ! Will fail otherwise) gl.glBindVertexArray(0) # Unbind the buffer gl.glBindBuffer(gl.GL_ARRAY_BUFFER, 0) # Release bound program gl.glUseProgram(0)
def paint_slid(self): gl.glClear(gl.GL_COLOR_BUFFER_BIT); # activate the program and set parameters gl.glUseProgram(self.program) gl.glUniform1i(self.texture1, 0) # indicate we use GL_TEXTURE0 # set up the texture to map gl.glEnable(gl.GL_TEXTURE_2D) gl.glActiveTexture(gl.GL_TEXTURE0) if self.usingA: gl.glBindTexture(gl.GL_TEXTURE_2D, self.textureB) else: gl.glBindTexture(gl.GL_TEXTURE_2D, self.textureA) # a simple square filling the whole view self.square.bind() # activate using VBOs for the vertices gl.glEnableVertexAttribArray(0) # indicate that the VBO has texture and vertex data gl.glEnableClientState(gl.GL_VERTEX_ARRAY); gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY); # indicate where the vertex and texture data is gl.glVertexAttribPointer(0, 3, gl.GL_FLOAT, gl.GL_FALSE, 5*4, None) gl.glTexCoordPointer(2, gl.GL_FLOAT, 5*4, self.square + 3*4) # draw the square gl.glDrawArrays(gl.GL_TRIANGLES, 0, 6)
def upload(self): if not self._dirty: return Collection.upload( self ) gl.glActiveTexture( gl.GL_TEXTURE2 ) data = self._gbuffer.data.view(np.float32) shape = len(self._gbuffer), 4*1024 if not self._gbuffer_id: self._gbuffer_id = gl.glGenTextures(1) gl.glBindTexture( gl.GL_TEXTURE_2D, self._gbuffer_id ) gl.glPixelStorei( gl.GL_UNPACK_ALIGNMENT, 1 ) gl.glPixelStorei( gl.GL_PACK_ALIGNMENT, 1 ) gl.glTexParameterf( gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_NEAREST ) gl.glTexParameterf( gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST ) gl.glTexParameterf( gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP_TO_EDGE ) gl.glTexParameterf( gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP_TO_EDGE ) gl.glTexParameterf(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_BASE_LEVEL, 0) gl.glTexParameterf(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAX_LEVEL, 0) gl.glTexImage2D( gl.GL_TEXTURE_2D, 0, gl.GL_RGBA32F, shape[1]//4, shape[0], 0, gl.GL_RGBA, gl.GL_FLOAT, data ) gl.glActiveTexture( gl.GL_TEXTURE2 ) gl.glBindTexture( gl.GL_TEXTURE_2D, self._gbuffer_id ) gl.glTexImage2D( gl.GL_TEXTURE_2D, 0, gl.GL_RGBA32F, shape[1]//4, shape[0], 0, gl.GL_RGBA, gl.GL_FLOAT, data ) self._dirty = False
def draw(self, projection, view, model, texture_shader=None, win=None, **_kwargs): self.shader = texture_shader # some interactive elements if glfw.get_key(win, glfw.KEY_F6) == glfw.PRESS: self.wrap_mode = next(self.wrap) self.texture = Texture(self.file, self.wrap_mode, *self.filter_mode) if glfw.get_key(win, glfw.KEY_F7) == glfw.PRESS: self.filter_mode = next(self.filter) self.texture = Texture(self.file, self.wrap_mode, *self.filter_mode) GL.glUseProgram(self.shader.glid) # projection geometry loc = GL.glGetUniformLocation(self.shader.glid, 'modelviewprojection') GL.glUniformMatrix4fv(loc, 1, True, projection @ view @ model) # texture access setups loc = GL.glGetUniformLocation(self.shader.glid, 'diffuseMap') GL.glActiveTexture(GL.GL_TEXTURE0) GL.glBindTexture(GL.GL_TEXTURE_2D, self.texture.glid) GL.glUniform1i(loc, 0) self.vertex_array.execute(GL.GL_TRIANGLES) # leave clean state for easier debugging GL.glBindTexture(GL.GL_TEXTURE_2D, 0) GL.glUseProgram(0)
def render_text(self, text, pos, scale=1, dir=(1,0), halign='left', valign='bottom'): offset = glm.vec3() if halign in ('center', 'right'): width = self.width_of(text, scale) offset.x -= width if halign == 'center': offset.x /= 2 if valign in ('center', 'top'): offset.y += self.top_bearing if valign == 'center': offset.y /= 2 gl.glActiveTexture(gl.GL_TEXTURE0) gl.glBindVertexArray(self.vao) angle_rad = math.atan2(dir[1], dir[0]) rotateM = glm.rotate(glm.mat4(1), angle_rad, glm.vec3(0, 0, 1)) transOriginM = glm.translate(glm.mat4(1), glm.vec3(*pos, 0) + offset) gl.glUniform3f(self.uniform("textColor"), .0, .0, .0) char_x = 0 for c in text: c = ord(c) ch = self.characters[c] w, h = ch[1][0] * scale, ch[1][1] * scale xrel, yrel = char_x + ch[2][0] * scale, (ch[1][1] - ch[2][1]) * scale char_x += (ch[3] >> 6) * scale scaleM = glm.scale(glm.mat4(1), glm.vec3(w, h, 1)) transRelM = glm.translate(glm.mat4(1), glm.vec3(xrel, yrel, 0)) modelM = transOriginM * rotateM * transRelM * scaleM gl.glUniformMatrix4fv(self.uniform("model"), 1, gl.GL_FALSE, glm.value_ptr(modelM)) gl.glBindTexture(gl.GL_TEXTURE_2D, ch[0]) gl.glDrawArrays(gl.GL_TRIANGLES, 0, 6) gl.glBindVertexArray(0) gl.glBindTexture(gl.GL_TEXTURE_2D, 0)
def paintGL(self): if self.update_vbo: gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self.vertices_id) gl.glBufferData(gl.GL_ARRAY_BUFFER, self.vertex_data, gl.GL_DYNAMIC_DRAW) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, 0) self.update_vbo = False gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glUniform1i(self.colormap_location, 0) gl.glActiveTexture(gl.GL_TEXTURE0 + 0) gl.glBindTexture(gl.GL_TEXTURE_1D, self.colormap_id) gl.glPushClientAttrib(gl.GL_CLIENT_VERTEX_ARRAY_BIT) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self.vertices_id) gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, self.indices_id) gl.glVertexPointer(3, gl.GL_FLOAT, 0, c_void_p(None)) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glDrawElements(gl.GL_TRIANGLES, self.indices.size, gl.GL_UNSIGNED_INT, None) gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, 0) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, 0) gl.glPopClientAttrib()
def glTextureFromSurface(surface): width = surface.get_width() height = surface.get_height() textureId = (gl.GLuint * 1)() surface.lock() BYTEP = ctypes.POINTER(ctypes.c_ubyte) ptr = ctypes.cast(surface._pixels_address, BYTEP) gl.glGenTextures(1, textureId) gl.glEnable(gl.GL_TEXTURE_2D) gl.glActiveTexture(gl.GL_TEXTURE0) gl.glPixelStorei(gl.GL_UNPACK_ROW_LENGTH, surface.get_pitch() // surface.get_bytesize()) gl.glBindTexture(gl.GL_TEXTURE_2D, textureId[0]) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP_TO_EDGE) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP_TO_EDGE) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR) gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGBA, width, height, 0, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, ptr) gl.glBindTexture(gl.GL_TEXTURE_2D, 0) gl.glPixelStorei(gl.GL_UNPACK_ROW_LENGTH, 0) surface.unlock() return textureId[0], width, height
def on_display(): gl.glClearColor(1, 1, 1, 1) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glEnable(gl.GL_BLEND) shader.bind() shader.uniformi('uniforms', 0) shape = collection.uniforms_shape or (1, 1) shader.uniformf('uniforms_shape', shape[0], shape[1]) gl.glActiveTexture(gl.GL_TEXTURE0) gl.glBindTexture(gl.GL_TEXTURE_2D, collection.uniforms_id) _, _, width, height = gl.glGetIntegerv(gl.GL_VIEWPORT) P = orthographic(0, width, 0, height, -1, +1) V = np.eye(4).astype(np.float32) M = np.eye(4).astype(np.float32) shader.uniform_matrixf('M', M) shader.uniform_matrixf('V', V) shader.uniform_matrixf('P', P) collection.draw() shader.unbind() glut.glutSwapBuffers()
def draw(self): loc = gl.glGetUniformLocation(self.program, "tex") gl.glUniform1i(loc, 0) gl.glActiveTexture(gl.GL_TEXTURE0) gl.glBindTexture(gl.GL_TEXTURE_RECTANGLE, self.tex) geometry.base.draw(self)
def _begin(self, i): gl.glActiveTexture(gl.GL_TEXTURE0 + i) gl.glBindTexture(gl.GL_TEXTURE_2D, self._tex) if self._should_update_data: gl.glTexImage2D( gl.GL_TEXTURE_2D, 0, self._format, self._width, self._height, 0, self._format, gl.GL_UNSIGNED_BYTE, self._data.tobytes(), ) self._should_update_data = False gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP_TO_EDGE) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP_TO_EDGE) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, self._filter) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, self._filter) gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 1)
def create_teapot_textured(texture_list): """ Creates a teapot textured. :param texture_list: Texture OpenGL list :return: Object list """ obj = _gl.glGenLists(1) _gl.glNewList(obj, _gl.GL_COMPILE) _gl.glPushMatrix() for _i in range(len(texture_list)): _gl.glActiveTexture(_gl.GL_TEXTURE0 + _i) _gl.glEnable(_gl.GL_TEXTURE_2D) _gl.glBindTexture(_gl.GL_TEXTURE_2D, texture_list[_i]) _gl.glRotate(90, 1, 0, 0) # noinspection PyBroadException try: _glut.glutSolidTeapot(1.0) except: if not _FIGURES_ERRS[4]: _print_gl_error( 'OpenGL actual version does not support glutSolidTeapot function' ) _FIGURES_ERRS[4] = True for _i in range(len(texture_list)): _gl.glActiveTexture(_gl.GL_TEXTURE0 + _i) _gl.glDisable(_gl.GL_TEXTURE_2D) _gl.glPopMatrix() _gl.glEndList() return obj
def draw(self, P=None, V=None, M=None): atlas = self.font_manager.atlas if self._dirty: self.upload() gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glEnable(gl.GL_BLEND) _,_,width,height = gl.glGetIntegerv( gl.GL_VIEWPORT ) P = orthographic( 0, width, 0, height, -1, +1 ) V = np.eye(4).astype( np.float32 ) M = np.eye(4).astype( np.float32 ) shader = self.shader shader.bind() gl.glActiveTexture( gl.GL_TEXTURE0 ) shader.uniformi( 'u_uniforms', 0 ) gl.glBindTexture( gl.GL_TEXTURE_2D, self._ubuffer_id ) shape = self._ubuffer_shape shader.uniformf( 'u_uniforms_shape', shape[1]//4, shape[0]) gl.glActiveTexture( gl.GL_TEXTURE1 ) shader.uniformi( 'u_font_atlas', 1 ) shader.uniformf( 'u_font_atlas_shape', atlas.width, atlas.height, atlas.depth) gl.glBindTexture( gl.GL_TEXTURE_2D, self.font_manager.atlas.texid) shader.uniform_matrixf( 'u_M', M ) shader.uniform_matrixf( 'u_V', V ) shader.uniform_matrixf( 'u_P', P ) gl.glDisable( gl.GL_DEPTH_TEST ) self._vbuffer.draw( ) shader.unbind()
def draw(self, projection, view, model, win=None, **_kwargs): self.shader = _kwargs['texture_shader_skybox'] GL.glUseProgram(self.shader.glid) # projection geometry loc = GL.glGetUniformLocation(self.shader.glid, 'viewprojection') GL.glUniformMatrix4fv(loc, 1, True, projection @ view) # texture access setups loc = GL.glGetUniformLocation(self.shader.glid, 'skybox') GL.glActiveTexture( GL.GL_TEXTURE0 ) # activate the 0th texture unit ; ith unit to generalize GL.glBindTexture( GL.GL_TEXTURE_CUBE_MAP, self.texture.glid ) # associates an existing OpenGL texture to the corresponding texture unit GL.glUniform1i( loc, 0 ) # sampler is treated as a uniform integer variable, to which we pass the number of the texture unit self.vertex_array.execute(GL.GL_TRIANGLES) # leave clean state for easier debugging GL.glBindTexture(GL.GL_TEXTURE_CUBE_MAP, 0) GL.glUseProgram(0)
def active_texture(target, unit): """Returns the active texture for the specified target and unit. .. note:: This requires calls to query and change the texture unit. """ # store the currently active unit unit_ = active_unit() # change to our texture's unit if unit_ != unit: GL.glActiveTexture(GL.GL_TEXTURE0 + unit) # get the active texture handle # TODO: # TODO: support more enums # TODO: query_enums = { GL.GL_TEXTURE_1D: GL.GL_TEXTURE_BINDING_1D, GL.GL_TEXTURE_2D: GL.GL_TEXTURE_BINDING_2D, GL.GL_TEXTURE_3D: GL.GL_TEXTURE_BINDING_3D, } enum = query_enums[target] handle = GL.glGetInteger(enum) # restore the active unit if unit_ != unit: GL.glActiveTexture(GL.GL_TEXTURE0 + unit_) return handle
def v_initPost(): ''' init vr after MuJoCo init ''' global hmd for n in range(2): znear, zfar = 0.05, 50.0 left, right, top, bottom = hmd.system.getProjectionRaw(n) ctx.scn.camera[n].frustum_bottom = -bottom * znear ctx.scn.camera[n].frustum_top = -top * znear ctx.scn.camera[n].frustum_center = 0.5 * (left + right) * znear ctx.scn.camera[n].frustum_near = znear ctx.scn.camera[n].frustum_far = zfar gl.glActiveTexture(gl.GL_TEXTURE2) hmd.idtex = gl.glGenTextures(1) gl.glBindTexture(gl.GL_TEXTURE_2D, hmd.idtex) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_NEAREST) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP_TO_EDGE) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP_TO_EDGE) gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGBA8, 2 * hmd.width, hmd.height, 0, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, None) hmd.poses = (openvr.TrackedDevicePose_t * openvr.k_unMaxTrackedDeviceCount)() hmd.boundLeft = openvr.VRTextureBounds_t(0., 0., 0.5, 1.) hmd.boundRight = openvr.VRTextureBounds_t(0.5, 0., 1., 1.) hmd.vTex = openvr.Texture_t(hmd.idtex, openvr.TextureType_OpenGL, openvr.ColorSpace_Gamma)
def set_material_state(material_name, gltf): if set_material_state.current_material == material_name: return set_material_state.current_material = material_name set_material_state.n_tex = 0 material = gltf['materials'][material_name] set_technique_state(material['technique'], gltf) technique = gltf['techniques'][material['technique']] program = gltf['programs'][technique['program']] textures = gltf.get('textures', {}) samplers = gltf.get('samplers', {}) material_values = material.get('values', {}) for uniform_name, parameter_name in technique['uniforms'].items(): parameter = technique['parameters'][parameter_name] if 'semantic' in parameter: continue value = material_values.get(parameter_name, parameter.get('value', _DEFAULT_MATERIAL_VALUES_BY_PARAM_TYPE.get(parameter['type']))) if value is None: raise Exception('''could not determine a value to use for material "%s", parameter "%s": %s %s''' % (material_name, parameter_name, parameter['type'], uniform_name)) if isinstance(value, (tuple, list)): value = np.array(value, dtype=np.float32) if uniform_name in program['uniform_locations']: location = program['uniform_locations'][uniform_name] else: location = gl.glGetUniformLocation(program['id'], uniform_name) program['uniform_locations'][uniform_name] = location if parameter['type'] == gl.GL_SAMPLER_2D: texture = textures[value] gl.glActiveTexture(gl.GL_TEXTURE0 + set_material_state.n_tex) gl.glBindTexture(texture['target'], texture['id']) gl.glBindSampler(gl.GL_TEXTURE0 + set_material_state.n_tex, samplers[texture['sampler']]['id']) gl.glUniform1i(location, set_material_state.n_tex) set_material_state.n_tex += 1 elif parameter['type'] == gl.GL_INT: gl.glUniform1i(location, value) elif parameter['type'] == gl.GL_FLOAT: gl.glUniform1f(location, value) elif parameter['type'] == gl.GL_FLOAT_VEC2: gl.glUniform2f(location, *value) elif parameter['type'] == gl.GL_FLOAT_VEC3: gl.glUniform3f(location, *value) elif parameter['type'] == gl.GL_FLOAT_VEC4: gl.glUniform4f(location, *value) elif parameter['type'] == gl.GL_FLOAT_MAT2: gl.glUniformMatrix2fv(location, 1, False, value) elif parameter['type'] == gl.GL_FLOAT_MAT3: gl.glUniformMatrix3fv(location, 1, False, value) elif parameter['type'] == gl.GL_FLOAT_MAT4: gl.glUniformMatrix4fv(location, 1, False, value) else: raise Exception('unhandled parameter type: %s' % parameter['type']) if CHECK_GL_ERRORS: if gl.glGetError() != gl.GL_NO_ERROR: raise Exception('error setting material state')
def draw(self): loc = gl.glGetUniformLocation(self.program, "tex") gl.glUniform1i(loc, 0) gl.glActiveTexture(gl.GL_TEXTURE0) gl.glBindTexture(gl.GL_TEXTURE_2D, self.tex) gl.glGenerateMipmap(gl.GL_TEXTURE_2D) super(signalgenerator, self).draw()
def drawScene(): GL.glClear(GL.GL_COLOR_BUFFER_BIT|GL.GL_DEPTH_BUFFER_BIT) GL.glPushMatrix() GL.glActiveTexture(GL.GL_TEXTURE0) GL.glEnable(GL.GL_TEXTURE_2D) GL.glBindTexture(GL.GL_TEXTURE_2D, texID)#bind that name to the target #first square GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA) GL.glColor4f(1,0,1,0.5) GL.glBegin(GL.GL_POLYGON) # Start drawing a polygon GL.glMultiTexCoord2f(GL.GL_TEXTURE0,0.0,2.0) GL.glVertex3f(0, sqrSize*0.8,-.8) # Top left GL.glMultiTexCoord2f(GL.GL_TEXTURE0,2.0,2.0) GL.glVertex3f(sqrSize*0.8, sqrSize*0.8,-.80) # Top right GL.glMultiTexCoord2f(GL.GL_TEXTURE0,2.0,0.0) GL.glVertex3f(sqrSize*0.8, 0,-.8) # Bottom Right GL.glMultiTexCoord2f(GL.GL_TEXTURE0,0.0,0.0) GL.glVertex3f(0, 0,-.8) # Bottom Left try: GL.glEnd() except: print GL.GLerror, 1 alpha = 0.5 #2nd square GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA) GL.glRotatef(45,0.0,0.0,1.0) GL.glDisable(GL.GL_TEXTURE_2D) GL.glColor4f(0.5,0.5,0.5,alpha) GL.glBegin(GL.GL_POLYGON) # Start drawing a polygon GL.glVertex3f(0, sqrSize*0.8,-.7) # Top left GL.glVertex3f(sqrSize*0.8, sqrSize*0.8,-.70) # Top right GL.glVertex3f(sqrSize*0.8, 0,-.7) # Bottom Right GL.glVertex3f(0, 0,-.7) # Bottom Left GL.glEnd() GL.glEnable(GL.GL_TEXTURE_2D) GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA) GL.glColor4f(1,0,1,alpha) GL.glBegin(GL.GL_POLYGON) # Start drawing a polygon GL.glMultiTexCoord2f(GL.GL_TEXTURE0,0.0,2.0) GL.glVertex3f(0, sqrSize*0.8,-.8) # Top left GL.glMultiTexCoord2f(GL.GL_TEXTURE0,2.0,2.0) GL.glVertex3f(sqrSize*0.8, sqrSize*0.8,-.80) # Top right GL.glMultiTexCoord2f(GL.GL_TEXTURE0,2.0,0.0) GL.glVertex3f(sqrSize*0.8, 0,-.8) # Bottom Right GL.glMultiTexCoord2f(GL.GL_TEXTURE0,0.0,0.0) GL.glVertex3f(0, 0,-.8) # Bottom Left try: GL.glEnd() except: print GL.GLerror.message, 2 GL.glPopMatrix()
def draw(self, time, layout): gl.glActiveTexture(gl.GL_TEXTURE0) gl.glBindTexture(gl.GL_TEXTURE_2D, self.atlas.texid) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glEnable(gl.GL_BLEND) with self.shader: gl.glUniform1i(self.l_tex, 0) gl.glUniform1f(self.l_time, time) layout.draw(time, self)
def load(self, texUnit): """ sets up the texture matrix for using the reflection map in the scene rendering pass. """ gl.glMatrixMode(gl.GL_TEXTURE) gl.glActiveTexture(gl.GL_TEXTURE0 + texUnit) self.loadMatrix() gl.glMatrixMode(gl.GL_MODELVIEW)
def trace_colors(steps, last=360, offset=0): def rgb_from_hsv(h, s, v, a=None): hi = h // 60 f = (h / 60) - hi p = v * (1 - s) q = v * (1 - s * f) t = v * (1 - s * (1 - f)) if hi in [0, 6]: r, g, b = v, t, p elif hi == 1: r, g, b = q, v, p elif hi == 2: r, g, b = p, v, t elif hi == 3: r, g, b = p, q, v elif hi == 4: r, g, b = t, p, v elif hi == 5: r, g, b = v, p, q else: raise ValueError("h_i out of range") if a is None: return (r, g, b) else: return (r, g, b, a) def color(index, alpha=None): return tuple([min(max(0, int(component * 255)), 255) for component in rgb_from_hsv((index % 361), 0.75 + index / (4 * steps), 0.75 + index / (4 * steps), alpha)]) surface = pygame.surface.Surface((steps, 1), flags=pygame.SRCALPHA).convert_alpha() for x in range(0, steps): surface.set_at((x, 0), color(offset + x * (last / steps), alpha=1.0)) data = pygame.image.tostring(surface, "RGBA", True) tex = GL.glGenTextures(1) GL.glActiveTexture(GL.GL_TEXTURE0) GL.glBindTexture(GL.GL_TEXTURE_1D, tex) GL.glTexParameteri(GL.GL_TEXTURE_1D, GL.GL_TEXTURE_WRAP_S, GL.GL_REPEAT) GL.glTexParameteri(GL.GL_TEXTURE_1D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR) GL.glTexParameteri(GL.GL_TEXTURE_1D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR) GL.glTexImage1D(GL.GL_TEXTURE_1D, 0, GL.GL_RGBA, steps, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, data) return tex
def paintGL(self): if GL.glCheckFramebufferStatus(GL.GL_FRAMEBUFFER) == 33305: return GL.glClear(GL.GL_COLOR_BUFFER_BIT) if not self.texture: return if not self.shader: self.compileShaders() if not self.vbo: self.vbo = GL.glGenBuffers(1) shaders.glUseProgram(self.shader) GL.glBindBuffer(GL.GL_ARRAY_BUFFER, self.vbo) vertices = numpy.array( [-self.scale + self.cx, -self.scale + self.cy , self.scale + self.cx, -self.scale + self.cy, self.scale + self.cx, self.scale + self.cy, -self.scale + self.cx, self.scale + self.cy, 0,0,1,0,1,1,0,1], dtype = numpy.float32) GL.glBufferData(GL.GL_ARRAY_BUFFER, 64, vertices, GL.GL_STATIC_DRAW) loc = GL.glGetAttribLocation(self.shader, "positionIn") GL.glEnableVertexAttribArray(loc) GL.glVertexAttribPointer(loc, 2, GL.GL_FLOAT, 0, 8, c_void_p(0)) loc = GL.glGetAttribLocation(self.shader, "texIn") GL.glEnableVertexAttribArray(loc) GL.glVertexAttribPointer(loc, 2, GL.GL_FLOAT, 0, 8, c_void_p(32)) def _uniformLoc(name): return GL.glGetUniformLocation(self.shader,name) GL.glUniform1f(_uniformLoc("g"), self.gamma); GL.glUniform1f(_uniformLoc("m"), math.pow(2, self.exposure + 2.47393)) GL.glUniform1f(_uniformLoc("s"), math.pow(2, -3.5 * self.gamma)) GL.glUniform1i(_uniformLoc("hdr_mode"), self.hdr_mode); GL.glUniform1i(_uniformLoc("texture"), 0); GL.glActiveTexture(GL.GL_TEXTURE0); GL.glBindTexture(GL.GL_TEXTURE_2D, self.texture) GL.glDrawArrays(GL.GL_QUADS, 0, 4); GL.glBindTexture(GL.GL_TEXTURE_2D, 0) loc = GL.glGetAttribLocation(self.shader, "positionIn") GL.glDisableVertexAttribArray(loc) loc = GL.glGetAttribLocation(self.shader, "texIn") GL.glDisableVertexAttribArray(loc) GL.glBindBuffer(GL.GL_ARRAY_BUFFER, 0) shaders.glUseProgram(0) if self.rightBtnDown: self.renderPixelInfo()
def render(): global sampleTexture global shaderProgram global texUnitUniform global VAO GL.glClearColor(0, 0, 0, 1) GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) # active shader program GL.glUseProgram(shaderProgram) try: # Activate texture GL.glActiveTexture(GL.GL_TEXTURE0) GL.glBindTexture(GL.GL_TEXTURE_2D, sampleTexture) GL.glUniform1i(texUnitUniform, 0) # Activate array GL.glBindVertexArray(VAO) # draw triangle GL.glDrawArrays(GL.GL_TRIANGLES, 0, 3) finally: GL.glBindVertexArray(0) GL.glUseProgram(0)
def __initOpenGL(self): ''' Initialize OpenGL ''' # Setup depth test gl.glEnable(gl.GL_DEPTH_TEST) gl.glDepthFunc(gl.GL_LESS) gl.glClearDepth(1.0) # Setup cull face gl.glEnable(gl.GL_CULL_FACE) # Setup alpha blending gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) # Create shader program self.shaderProgram.init("Vertex.vert", "Fragment.frag") gl.glUseProgram(self.shaderProgram.getProgramObject()) # Create texture objects gl.glActiveTexture(gl.GL_TEXTURE0) tex_ids = gl.glGenTextures(TEXT_COUNT) for i in range(TEXT_COUNT): self.textures.append(Texture(tex_ids[i])) # Initialize some shader uniforms gl.glUniform1i(TEXTURE_SAMPLER_LOCATION, 0) gl.glUniform1i(TEXTURE_FLAG_LOCATION, 0) gl.glUniform1i(LIGHTING_FLAG_LOCATION, 0)
def init_texture(self): """Initialize textures.""" source_path = os.path.join( os.path.dirname(__file__), '../../_images/illvelo.png') img = Image.open(source_path).resize((256, 256)) assert img.mode == 'RGBA' tex_id = GL.glGetUniformLocation( self.program_manager.program_id, b"texture_sampler") GL.glUniform1i(tex_id, 0) GL.glActiveTexture(GL.GL_TEXTURE0) self.texture = GL.glGenTextures(1) GL.glBindTexture(GL.GL_TEXTURE_2D, self.texture) GL.glTexImage2D( GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, img.size[0], img.size[1], 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, img.tobytes()) GL.glTexParameterf( GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_REPEAT) GL.glTexParameterf( GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_REPEAT) GL.glTexParameterf( GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR) GL.glTexParameterf( GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR)