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 on_draw(dt): if self.vertices is None: return gl.glEnable(gl.GL_DEPTH_TEST) self.framebuffer.activate() window.clear() self.mesh_shader.draw(gl.GL_TRIANGLES) if self.click is not None: gl.glReadBuffer(gl.GL_COLOR_ATTACHMENT1) r, g, b, a = gl.glReadPixels(self.click[0], self.click[1], 1, 1, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE) if type(r) is not int: r = ord(r) if type(g) is not int: g = ord(g) if type(b) is not int: b = ord(b) index = b + 256 * g + 256 * 256 * r self.mesh_shader['select_id'] = index self.click = None if self.click_callback is None: print('Click callback function not defined') else: self.click_callback(index - 1) self.framebuffer.deactivate() window.clear() self.render()
def on_draw(dt): gl.glEnable(gl.GL_DEPTH_TEST) framebuffer.activate() window.clear() program.draw(gl.GL_POINTS) if mouse is not None: gl.glReadBuffer(gl.GL_COLOR_ATTACHMENT1, gl.GL_FRONT) r,g,b,a = gl.glReadPixels(mouse[0],mouse[1],1,1, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE) index = ord(b) + 256*ord(g) + 256*256*ord(r) if index < len(program): program["bg_color"][index] = 0,0,0,1 framebuffer.deactivate() gl.glDisable(gl.GL_DEPTH_TEST) quad.draw(gl.GL_TRIANGLE_STRIP)
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 on_draw(dt): global index framebuffer.activate() window.clear() polys.draw(), paths.draw() if mouse is not None: gl.glReadBuffer(gl.GL_COLOR_ATTACHMENT1, gl.GL_FRONT) r,g,b,a = gl.glReadPixels(mouse[0],mouse[1], 1, 1, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE) if type(r) is not int: r = ord(r) if type(g) is not int: g = ord(g) if type(b) is not int: b = ord(b) new_index = b + 256*g + 256*256*r if -1 < new_index < len(polys): index = new_index polys["color"] = 1,1,1,0.75 polys[index]["color"] = 1,1,1,0 framebuffer.deactivate() quad.draw(gl.GL_TRIANGLE_STRIP)
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 on_draw(dt): global index framebuffer.activate() window.clear() polys.draw(), paths.draw() if mouse is not None: gl.glReadBuffer(gl.GL_COLOR_ATTACHMENT1, gl.GL_FRONT) r, g, b, a = gl.glReadPixels(mouse[0], mouse[1], 1, 1, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE) if type(r) is not int: r = ord(r) if type(g) is not int: g = ord(g) if type(b) is not int: b = ord(b) new_index = b + 256 * g + 256 * 256 * r if -1 < new_index < len(polys): index = new_index polys["color"] = 1, 1, 1, 0.75 polys[index]["color"] = 1, 1, 1, 0 framebuffer.deactivate() quad.draw(gl.GL_TRIANGLE_STRIP)
def on_draw(dt): data["time"] += dt quad["time"] = data["time"] # window.clear() gl.glViewport(0, 0, window.width, window.height) # framebuffer.activate() # gl.glViewport(0, 0, width, height) # quad["time"] = 0.2 # quad.draw(gl.GL_TRIANGLE_STRIP) # quad2["time"] = 0.1 # quad2["color"] = 0,0,1,1 # quad2.draw(gl.GL_TRIANGLE_STRIP) # gl.glReadBuffer(gl.GL_COLOR_ATTACHMENT0, gl.GL_FRONT) # values = gl.glReadPixels(0, 0, width, height, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE) # values = np.fromstring(values, np.ubyte).reshape((height, width, 4)) # red = gl.glReadPixels(0, 0, width, height, gl.GL_RED, gl.GL_UNSIGNED_BYTE) # #red = np.fromstring(red, np.ubyte).reshape((480, 640)) # #red = np.transpose(red) # red = np.fromstring(red, np.ubyte).reshape((height, width)) # #red = np.transpose(red) # print(values.shape) # print(np.sum(values[..., 0])) # print(np.sum(values[..., 3])) # print(np.sum(red)) # print(np.min(red)) # print(np.max(red)) # print(red[320,240]) # img = values[..., [2, 1, 0, 3]] # #img = red # cv2.imwrite("img.png", img) # cv2.imshow("img", img) # cv2.waitKey(50) # framebuffer.deactivate() # framebuffer.color[0][:] = 0 # framebuffer.color[1][:] = 0 framebuffer.activate() gl.glViewport(0, 0, width, height) # gl.glDrawBuffer(gl.GL_COLOR_ATTACHMENT0 | gl.GL_COLOR_ATTACHMENT1) gl.glDrawBuffers( np.array([gl.GL_COLOR_ATTACHMENT0, gl.GL_COLOR_ATTACHMENT1], dtype=np.uint32)) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glClear(gl.GL_DEPTH_BUFFER_BIT) global phi, theta # Filled cube gl.glDisable(gl.GL_BLEND) # gl.glDisable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_POLYGON_OFFSET_FILL) cube['u_color'] = np.array([1, 1, 1, 1]) cube.draw(gl.GL_TRIANGLES, faces) # Outlined cube # gl.glDisable(gl.GL_POLYGON_OFFSET_FILL) # gl.glEnable(gl.GL_BLEND) # gl.glDepthMask(gl.GL_FALSE) # cube['u_color'] = np.array([0, 0, 0, 1]) # cube.draw(gl.GL_LINES, outline) # gl.glDepthMask(gl.GL_TRUE) gl.glReadBuffer(gl.GL_COLOR_ATTACHMENT0) # gl.glReadBuffer(gl.GL_COLOR_ATTACHMENT1) pixels = gl.glReadPixels(0, 0, width, height, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE) gl.glReadBuffer(gl.GL_COLOR_ATTACHMENT0) # pixels = gl.glReadPixels(0, 0, width, height, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE) framebuffer.deactivate() gl.glDisable(gl.GL_BLEND) gl.glDisable(gl.GL_DEPTH_TEST) # pixels = 100 * np.ones((height, width, 4), np.ubyte) # pixels[:, :, 3] = 1 # pixels[:, :, 2] = 0 gl.glDrawPixels(width, height, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, pixels) # gl.glDisable(gl.GL_DEPTH_TEST) # # quad["time"] = 0.2 # quad.draw(gl.GL_TRIANGLE_STRIP) # quad2["time"] = 0.1 # quad2["color"] = 0, 0, 1, 1 # quad2.draw(gl.GL_TRIANGLE_STRIP) # gl.glEnable(gl.GL_DEPTH_TEST) # Make cube rotate theta += 0.5 # degrees phi += 0.5 # degrees model = np.eye(4, dtype=np.float32) glm.rotate(model, theta, 0, 0, 1) glm.rotate(model, phi, 0, 1, 0) cube['u_model'] = model
# window.attach(cube['transform']) glumpy.app.run() import sys sys.exit(0) glumpy.app.__init__(backend=glumpy.app.__backend__) # print(np.sum(fb)) # print(fb.pending_data) framebuffer.activate() quad.draw(gl.GL_TRIANGLE_STRIP) framebuffer.deactivate() framebuffer.activate() gl.glReadBuffer(gl.GL_COLOR_ATTACHMENT0, gl.GL_FRONT) values = gl.glReadPixels(0, 0, 640, 480, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE) values = np.fromstring(values, np.ubyte).reshape((640, 480, 4)) print(values.shape) # print(fb.pending_data) # print(np.sum(fb)) print(np.sum(values[..., 0])) print(np.sum(values[..., 1])) print(np.sum(values[..., 2])) print(np.sum(values[..., 3])) #plydata = PlyData.read(sys.argv[1]) #for i, elements in enumerate(plydata.elements): # print(i, elements.name) # #print(" ", elements.data.keys()) # print(" ", len(elements.data))