def draw(self): """Draw the object to the display buffer""" from pyglet import gl gl.glUseProgram(self._program) for kind in ('fill', 'line'): if self._counts[kind] > 0: if kind == 'line': if self._line_width <= 0.0: continue gl.glLineWidth(self._line_width) if self._line_loop: mode = gl.GL_LINE_LOOP else: mode = gl.GL_LINE_STRIP cmd = partial(gl.glDrawArrays, mode, 0, self._counts[kind]) else: gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, self._buffers[kind]['index']) cmd = partial(gl.glDrawElements, gl.GL_TRIANGLES, self._counts[kind], gl.GL_UNSIGNED_INT, 0) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self._buffers[kind]['array']) loc_pos = gl.glGetAttribLocation(self._program, b'a_position') gl.glEnableVertexAttribArray(loc_pos) gl.glVertexAttribPointer(loc_pos, 2, gl.GL_FLOAT, gl.GL_FALSE, 0, 0) loc_col = gl.glGetUniformLocation(self._program, b'u_color') gl.glUniform4f(loc_col, *self._colors[kind]) cmd() # The following line is probably only necessary because # Pyglet makes some assumptions about the GL state that # it perhaps shouldn't. Without it, Text might not # render properly (see #252) gl.glDisableVertexAttribArray(loc_pos) gl.glUseProgram(0)
def on_draw(self): # Prevent trying to draw before things have been set up if not hasattr(self, "offscreen_buffer"): return # Model matrix we'll use to position the main model suzanne_model_matrix = (Matrix4.new_identity().rotatex( -math.pi / 2).rotatez(time())) # Rotate over time plane_model_matrix = Matrix4.new_rotatey(math.pi).translate(0, 0, 2) # Render to an offscreen buffer with self.offscreen_buffer, self.view_program, \ enabled(gl.GL_DEPTH_TEST), disabled(gl.GL_CULL_FACE): gl.glDepthMask(gl.GL_TRUE) w, h = self.size aspect = h / w # Calculate a view frustum; this is basically our camera. near = 5 far = 15 width = 2 height = 2 * aspect frustum = (Matrix4.new(near / width, 0, 0, 0, 0, near / height, 0, 0, 0, 0, -(far + near) / (far - near), -1, 0, 0, -2 * far * near / (far - near), 0)) # The view matrix positions the camera in the scene view_matrix = (Matrix4.new_identity().translate(0, 0, -8)) # Send the matrices to GL gl.glUniformMatrix4fv(0, 1, gl.GL_FALSE, gl_matrix(frustum * view_matrix)) gl.glUniformMatrix4fv(1, 1, gl.GL_FALSE, gl_matrix(suzanne_model_matrix)) gl.glUniform4f(2, 0.3, 0.3, 1, 1) # Set the "color" uniform to blue self.suzanne.draw() # We'll also draw a simple plane behind the main model gl.glUniformMatrix4fv(1, 1, gl.GL_FALSE, gl_matrix(plane_model_matrix)) gl.glUniform4f(2, 0.3, 1, 0.3, 1) # Set the "color" uniform to green self.plane.draw(mode=gl.GL_TRIANGLE_STRIP) # Render shadow buffer # Basically the same scene as above, but to a different buffer and from a different view with self.shadow_buffer, self.view_program, enabled( gl.GL_DEPTH_TEST), disabled(gl.GL_CULL_FACE): gl.glDepthMask(gl.GL_TRUE) frustum = Matrix4.new_perspective(1, 1, 1, 12) view_matrix = (Matrix4.new_identity().translate( 0, 0, -4).rotatey(0.5).rotatex(0.3)) light_pos = (view_matrix.inverse() * Point3(0, 0, 0)) light_view_matrix = frustum * view_matrix gl.glUniformMatrix4fv(0, 1, gl.GL_FALSE, gl_matrix(light_view_matrix)) gl.glUniformMatrix4fv(1, 1, gl.GL_FALSE, gl_matrix(suzanne_model_matrix)) gl.glUniform4f(2, 0.9, 0.3, 0.4, 1) self.suzanne.draw() gl.glUniformMatrix4fv(1, 1, gl.GL_FALSE, gl_matrix(plane_model_matrix)) self.plane.draw(mode=gl.GL_TRIANGLE_STRIP) # Now draw the offscreen buffer to another buffer, combining it with the # lighting information to get a nice image. # Note: This step is pretty pointless here, as we might just draw directly to screen. # Just demonstrates how to do it. with self.vao, self.offscreen_buffer2, self.lighting_program, disabled( gl.GL_CULL_FACE, gl.GL_DEPTH_TEST): gl.glUniform3f(0, *light_pos) gl.glUniformMatrix4fv(1, 1, gl.GL_FALSE, gl_matrix(light_view_matrix)) # Bind some of the offscreen buffer's textures so the shader can read them. with self.offscreen_buffer["color"], self.offscreen_buffer["normal"], \ self.offscreen_buffer["position"], self.shadow_buffer["depth"]: gl.glDrawArrays(gl.GL_TRIANGLES, 0, 6) # Now render the finished image to the screen with self.vao, self.copy_program, disabled(gl.GL_CULL_FACE, gl.GL_DEPTH_TEST): with self.offscreen_buffer2["color"]: gl.glDrawArrays(gl.GL_TRIANGLES, 0, 6)
def _uvec4(uniform, data): gl.glUniform4f(uniform, *data)