def draw_translucent_fast(self): gl.glEnable(gl.GL_BLEND) gl.glDisable(gl.GL_CULL_FACE) gl.glDepthMask(gl.GL_FALSE) for render_chunk in self.sorted_chunks: render_chunk.draw_translucent(gl.GL_TRIANGLES) gl.glDepthMask(gl.GL_TRUE) gl.glEnable(gl.GL_CULL_FACE) gl.glDisable(gl.GL_BLEND)
def set_state(self): gl.glTexGeni( gl.GL_S, gl.GL_TEXTURE_GEN_MODE, gl.GL_OBJECT_LINEAR ) gl.glTexGeni( gl.GL_T, gl.GL_TEXTURE_GEN_MODE, gl.GL_OBJECT_LINEAR ) gl.glTexGenfv(gl.GL_S, gl.GL_OBJECT_PLANE, self.plane) gl.glTexGenfv(gl.GL_T, gl.GL_OBJECT_PLANE, self.plane) gl.glEnable( gl.GL_TEXTURE_GEN_S ) gl.glEnable( gl.GL_TEXTURE_GEN_T ) gl.glDepthMask( gl.GL_TRUE ) gl.glBindTexture( gl.GL_TEXTURE_2D, self.texture.id )
def _use(self): """Internal use that do not change the global active framebuffer""" if self.ctx.active_framebuffer == self: return gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, self._glo) # NOTE: gl.glDrawBuffer(GL_NONE) if no texture attachments (future) # NOTE: Default framebuffer currently has this set to None if self._draw_buffers: gl.glDrawBuffers(len(self._draw_buffers), self._draw_buffers) gl.glDepthMask(self._depth_mask) gl.glViewport(*self._viewport)
def draw_lem (self): with push_attrib(gl.GL_DEPTH_BUFFER_BIT | gl.GL_LIGHTING_BIT): gl.glDepthMask(gl.GL_TRUE) gl.glEnable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_LIGHTING) gl.glEnable(gl.GL_LIGHT0) gl.glEnable(gl.GL_NORMALIZE) gl.glShadeModel(gl.GL_SMOOTH) gl.glLightModelfv(gl.GL_LIGHT_MODEL_AMBIENT, float_ctype_array(1.0, 1.0, 1.0, 1.0)) gl.glLightfv(gl.GL_LIGHT0, gl.GL_AMBIENT, float_ctype_array(0.0, 0.0, 0.0, 1.0)) gl.glLightfv(gl.GL_LIGHT0, gl.GL_DIFFUSE, float_ctype_array(1.0, 1.0, 1.0, 1.0)) gl.glLightfv(gl.GL_LIGHT0, gl.GL_SPECULAR, float_ctype_array(0.5, 0.5, 0.5, 1.0)) gl.glLightfv(gl.GL_LIGHT0, gl.GL_POSITION, float_ctype_array(1.0, 1.0, 1.0, 0.0)) self.draw_lem_model()
def draw_translucent_fancy(self): gl.glDepthMask(gl.GL_FALSE) gl.glFrontFace(gl.GL_CW) gl.glEnable(gl.GL_BLEND) for render_chunk in self.sorted_chunks: render_chunk.draw_translucent(gl.GL_TRIANGLES) gl.glFrontFace(gl.GL_CCW) for render_chunk in self.sorted_chunks: render_chunk.draw_translucent(gl.GL_TRIANGLES) gl.glDisable(gl.GL_BLEND) gl.glDepthMask(gl.GL_TRUE)
def setBuffer(self, buffer, clear=True): """Set the eye buffer to draw to. Subsequent draw calls will be diverted to the specified eye. Parameters ---------- buffer : str Eye buffer to draw to. Values can either be 'left' or 'right'. clear : bool Clear the buffer prior to drawing. """ # check if the buffer name is valid if buffer not in ('left', 'right', 'back'): raise RuntimeError("Invalid buffer name specified.") # don't change the buffer if same, but allow clearing if buffer != self.buffer: if not self._directDraw: # handle when the back buffer is selected if buffer == 'back': if self.useFBO: GL.glBindFramebuffer( GL.GL_FRAMEBUFFER, self._eyeBuffers[buffer]['frameBuffer']) else: GL.glBindFramebuffer(GL.GL_FRAMEBUFFER, 0) else: GL.glBindFramebuffer( GL.GL_FRAMEBUFFER, self._eyeBuffers[buffer]['frameBuffer']) self.viewport = self.scissor = self._bufferViewports[buffer] GL.glEnable(GL.GL_SCISSOR_TEST) self.buffer = buffer # set buffer string if clear: self.setColor(self.color) # clear the buffer to the window color GL.glClearDepth(1.0) GL.glDepthMask(GL.GL_TRUE) GL.glClear( GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT | GL.GL_STENCIL_BUFFER_BIT) GL.glDisable(GL.GL_TEXTURE_2D) GL.glEnable(GL.GL_BLEND)
def draw_lem(self): with push_attrib(gl.GL_DEPTH_BUFFER_BIT | gl.GL_LIGHTING_BIT): gl.glDepthMask(gl.GL_TRUE) gl.glEnable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_LIGHTING) gl.glEnable(gl.GL_LIGHT0) gl.glEnable(gl.GL_NORMALIZE) gl.glShadeModel(gl.GL_SMOOTH) gl.glLightModelfv(gl.GL_LIGHT_MODEL_AMBIENT, float_ctype_array(1.0, 1.0, 1.0, 1.0)) gl.glLightfv(gl.GL_LIGHT0, gl.GL_AMBIENT, float_ctype_array(0.0, 0.0, 0.0, 1.0)) gl.glLightfv(gl.GL_LIGHT0, gl.GL_DIFFUSE, float_ctype_array(1.0, 1.0, 1.0, 1.0)) gl.glLightfv(gl.GL_LIGHT0, gl.GL_SPECULAR, float_ctype_array(0.5, 0.5, 0.5, 1.0)) gl.glLightfv(gl.GL_LIGHT0, gl.GL_POSITION, float_ctype_array(1.0, 1.0, 1.0, 0.0)) self.draw_lem_model()
def on_draw(self): """F.on_draw() Renders the fog.""" gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glDepthMask(gl.GL_FALSE) x_rep, y_rep, z_rep = self.__config.glass_repetitions() w, h, d = self.__config.grid_size() for layer in self.__layers: with layer: self.__camera.clear() self.__camera.add(*self.__cam.gl_matrix()) self.__camera.set() if not self.__color.filled(): self.__color.add(*self.__config.fog_color()) self.__color.set() for x_copy in range(x_rep): for y_copy in range(y_rep): for z_copy in range(z_rep): shift = ( w * x_copy, h * y_copy, d * z_copy) self.__copy_shift.clear() self.__copy_shift.add(*shift) self.__copy_shift.set() layer.draw() gl.glDepthMask(gl.GL_TRUE) gl.glDisable(gl.GL_BLEND)
def on_draw(self): gl.glDepthMask(gl.GL_TRUE) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT | gl.GL_STENCIL_BUFFER_BIT) gl.glDepthMask(gl.GL_FALSE) with push_matrix(gl.GL_PROJECTION): self.set_camera_projection() with push_matrix(gl.GL_MODELVIEW): gl.glLoadIdentity() gl.gluLookAt(self.camera_x, self.camera_y, self.camera_z, self.camera_x, self.camera_y, 0.0, 0.0, 1.0, 0.0) with push_matrix(gl.GL_MODELVIEW): gl.gluLookAt(0, 0, 0, 0, -1, 0, 0, 0, -1) self.draw_ground_plane() self.target.draw() # self.shadow.draw() self.draw_shadow_lem() self.draw_lem() with push_attrib(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT): gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE) gl.glEnable(gl.GL_DEPTH_TEST) with bound_texture(self.puff_texture.target, self.puff_texture.id): Thruster.draw(self.particles) if self.simulator.crashed: self.crashed.draw() if self.simulator.landed: self.landed.draw() self.fps_display.draw()
def on_draw (self): gl.glDepthMask(gl.GL_TRUE) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT | gl.GL_STENCIL_BUFFER_BIT) gl.glDepthMask(gl.GL_FALSE) with push_matrix(gl.GL_PROJECTION): self.set_camera_projection() with push_matrix(gl.GL_MODELVIEW): gl.glLoadIdentity() gl.gluLookAt (self.camera_x, self.camera_y, self.camera_z, self.camera_x, self.camera_y, 0.0, 0.0, 1.0, 0.0) with push_matrix(gl.GL_MODELVIEW): gl.gluLookAt(0, 0, 0, 0, -1, 0, 0, 0, -1) self.draw_ground_plane() self.target.draw() # self.shadow.draw() self.draw_shadow_lem() self.draw_lem() with push_attrib(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT): gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE) gl.glEnable(gl.GL_DEPTH_TEST) with bound_texture (self.puff_texture.target, self.puff_texture.id): Thruster.draw(self.particles) if self.simulator.crashed: self.crashed.draw() if self.simulator.landed: self.landed.draw() self.fps_display.draw()
def render(self, position, chunk_meshes): gl.glViewport(0, 0, *self.size) gl.glDisable(gl.GL_BLEND) gl.glDisable(gl.GL_ALPHA_TEST) gl.glEnable(gl.GL_CULL_FACE) gl.glEnable(gl.GL_DEPTH_TEST) gl.glDepthFunc(gl.GL_LEQUAL) gl.glDepthMask(gl.GL_TRUE) x, y, z = position view_matrices = [ Matrix4.new_scale(-1, -1, 1).rotatey(pi / 2).translate(-x, -y, -z), # +X Matrix4.new_scale(-1, -1, 1).rotatey(-pi / 2).translate(-x, -y, -z), # -X Matrix4.new_rotatex(-pi / 2).translate(-x, -y, -z), # +Y Matrix4.new_rotatex(pi / 2).translate(-x, -y, -z), # -Y Matrix4.new_scale(-1, -1, 1).rotatey(pi).translate(-x, -y, -z), # +Z Matrix4.new_scale(-1, -1, 1).translate(-x, -y, -z) # -Z ] model_matrix = Matrix4.new_scale( 1, 1, constants.WALL_TEXTURE_HEIGHT / (constants.WALL_TEXTURE_WIDTH / sqrt(2) * (sqrt(3)))) gl.glUniformMatrix4fv(1, 1, gl.GL_FALSE, gl_matrix(model_matrix)) with self.program, self.name: view_matrix = make_view_matrix_persp(position) gl.glUniformMatrix4fv(0, 1, gl.GL_FALSE, gl_matrix(view_matrix)) for chunk, mesh in chunk_meshes.items(): mesh.draw()
def depth_mask(self, value: bool): self._depth_mask = value # Set state if framebuffer is active if self._ctx.active_framebuffer == self: gl.glDepthMask(self._depth_mask)
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)