def draw(self, projection, view, width, height): if self._draw_mode == self.DRAW_PIXELS: np_type = self._framebuffer.color_attachments[ self._color_index].dtype gl_type = opengl_utils.get_gl_type(np_type) pixels = self._framebuffer.read_rgba_pixels(self._color_index) if self._color_scale != 1.0: pixels *= self._color_scale if self._color_offset != 1.0: pixels += self._color_offset width = min(width, self._framebuffer.width) height = min(height, self._framebuffer.height) gl.glDrawPixels(width, height, gl.GL_RGBA, gl_type, pixels) elif self._draw_mode == self.DRAW_QUAD: gl.glDisable(gl.GL_DEPTH_TEST) gl.glDisable(gl.GL_CULL_FACE) with self._quad.activate(): self._quad["u_color_offset"] = self._color_offset self._quad["u_color_scale"] = self._color_scale self._quad.draw(gl.GL_TRIANGLE_STRIP) gl.glEnable(gl.GL_CULL_FACE) gl.glEnable(gl.GL_DEPTH_TEST) elif self._draw_mode == self.BLIT: gl.glBindFramebuffer(gl.GL_READ_FRAMEBUFFER, self._framebuffer.native_framebuffer.handle) gl.glReadBuffer(gl.GL_COLOR_ATTACHMENT0 + self._color_index) gl.glBindFramebuffer(gl.GL_DRAW_FRAMEBUFFER, 0) gl.glBlitFramebuffer(0, 0, self._framebuffer.width, self._framebuffer.height, 0, 0, width, height, gl.GL_COLOR_BUFFER_BIT, gl.GL_LINEAR) else: raise RuntimeError("Unknown drawing mode")
def draw_rgba_pixels(self, pixels, color_index=0, activate=True): if activate: with self.activate(): return self.draw_rgba_pixels(pixels, color_index, activate=False) gl_type = opengl_utils.get_gl_type(pixels.dtype) gl.glDrawBuffers( np.array([gl.GL_COLOR_ATTACHMENT0 + color_index], dtype=np.uint32)) gl.glDrawPixels(self.width, self.height, gl.GL_RGBA, gl_type, pixels)
def read_rgba_pixels(self, activate=True, dtype=np.ubyte): if activate: with self.activate(): return self.read_rgba_pixels(activate=False) gl_type = opengl_utils.get_gl_type(dtype) pixels = gl.glReadPixels(0, 0, self.width, self.height, gl.GL_RGBA, gl_type, outputType=dtype) pixels = pixels.reshape((self.height, self.width, -1)) gl.glReadBuffer(gl.GL_COLOR_ATTACHMENT0) return pixels
def read_depth_pixels(self, activate=True): if activate: with self.activate(): return self.read_depth_pixels(activate=False) np_type = self._depth_buffer.dtype gl_type = opengl_utils.get_gl_type(np_type) pixels = gl.glReadPixels(0, 0, self.width, self.height, gl.GL_DEPTH_COMPONENT, gl.GL_FLOAT, outputType=np_type) # Bug in OpenGL binding. OpenGL is stored in row-major order (row being a horizontal line of an image). pixels = pixels.reshape((self.height, self.width, -1)) return pixels
def draw(self, projection, view, width, height): model_matrix = self._transform.matrix # Set uniforms self._program["u_projection"] = projection # Note: OpenGL matrix multiplication works on column-major oriented storage (as least for pre-multiplication). # Also glumpy.glm is using column-major assumption for its operations. view_model_matrix = np.transpose( np.matmul(np.transpose(view), np.transpose(model_matrix))) self._program["u_view_model_matrix"] = view_model_matrix # self._program["u_view"] = view # self._program["u_model"] = self._model view_model_normal_matrix = np.transpose( np.linalg.inv(view_model_matrix)) self._program["u_view_model_normal_matrix"] = view_model_normal_matrix self._program["u_color_scale"] = self._color_scale self._program["u_normal_scale"] = self._normal_scale self._program["u_depth_scale"] = self._depth_scale if self._shader_type == self.PHONG: self._program["u_light_position"] = self._light_position self._program[ "u_light_ambient_intensity"] = 0.4 * self._light_intensity self._program[ "u_light_diffuse_intensity"] = 0.4 * self._light_intensity self._program[ "u_light_specular_intensity"] = 0.2 * self._light_intensity self._program["u_material_ambient"] = self._material self._program["u_material_diffuse"] = self._material self._program["u_material_specular"] = self._material self._program["u_material_shininess"] = 32 with self._program.activate(): # Bind index buffer and draw if self.use_depth_test: gl.glEnable(gl.GL_DEPTH_TEST) else: gl.glDisable(gl.GL_DEPTH_TEST) if self.use_face_culling: gl.glFrontFace(gl.GL_CCW) gl.glCullFace(gl.GL_BACK) gl.glEnable(gl.GL_CULL_FACE) else: gl.glDisable(gl.GL_CULL_FACE) gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, self._gl_index_buffer) gl.glDrawElements(gl.GL_TRIANGLES, 3 * len(self._faces), opengl_utils.get_gl_type(self._faces), None) gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, 0)
def read_rgba_pixels(self, color_index=0, activate=True): if activate: with self.activate(): return self.read_rgba_pixels(color_index, activate=False) np_type = self._color_attachments[color_index].dtype gl_type = opengl_utils.get_gl_type(np_type) gl.glReadBuffer(gl.GL_COLOR_ATTACHMENT0 + color_index) pixels = gl.glReadPixels(0, 0, self._framebuffer.width, self._framebuffer.height, gl.GL_RGBA, gl_type, outputType=np_type) # Bug in OpenGL binding. OpenGL is stored in row-major order (row being a horizontal line of an image). pixels = pixels.reshape((self.height, self.width, -1)) gl.glReadBuffer(gl.GL_COLOR_ATTACHMENT0) return pixels
def draw_rgba_pixels(self, pixels, activate=True): if activate: with self.activate(): return self.draw_rgba_pixels(pixels, activate=False) gl_type = opengl_utils.get_gl_type(pixels.dtype) gl.glDrawPixels(self.width, self.height, gl.GL_RGBA, gl_type, pixels)