def widowSetting(): window = app.Window(1024,1024, color=(0,0,0,1)) framebuffer = np.zeros((window.height, window.width * 3), dtype=np.uint8) @window.event def on_draw(dt): window.clear() # Filled program_cube program_ptCloud.draw(gl.GL_POINTS) # Make program_cube rotate program_ptCloud['u_model'] = matrix_model() def matrix_model(): model = np.eye(4, dtype=np.float32) #model *= size glm.rotate(model, theta, 1, 0, 0) glm.rotate(model, -phi, 0, 1, 0) glm.translate(model, tx, ty, 0) glm.scale(model, size) #model[3,3] = 1 return model @window.event def on_resize(width, height): ratio = width / float(height) program_ptCloud['u_projection'] = glm.perspective(45.0, ratio, 0.001, 10000.0) @window.event def on_init(): gl.glEnable(gl.GL_DEPTH_TEST) @window.event def on_mouse_scroll(x, y, dx, dy): global zoom, size if(size + dy*0.1 > 0.1): size += dy*0.1 else: size = 0.1 #zoom += dy*1 #program_ptCloud['u_view'] = glm.translation(0, 0, zoom) @window.event def on_mouse_drag(x, y, dx, dy, button): if button == 2: global theta, phi, tx, ty theta += dy # degrees phi -= dx # degrees elif button == 8: tx += dx ty -= dy @window.event def on_key_press(symbol, modifiers): gl.glReadPixels(0, 0, window.width, window.height, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, framebuffer) png.from_array(framebuffer, 'RGB').save('screenshot.png') #print('Key pressed (symbol=%s, modifiers=%s)'% (symbol,modifiers)) program_ptCloud['color_sel'] = 1 - program_ptCloud['color_sel'] app.run()
def render(model, im_size, K, R, t, clip_near=100, clip_far=2000, surf_color=None, bg_color=(0.0, 0.0, 0.0, 0.0), ambient_weight=0.1, mode='rgb+depth'): # Process input data #--------------------------------------------------------------------------- # Make sure vertices and faces are provided in the model assert ({'pts', 'faces'}.issubset(set(model.keys()))) # Set color of vertices if not surf_color: if 'colors' in model.keys(): assert (model['pts'].shape[0] == model['colors'].shape[0]) colors = model['colors'] if colors.max() > 1.0: colors /= 255.0 # Color values are expected to be in range [0, 1] else: colors = np.ones((model['pts'].shape[0], 3), np.float32) * 0.5 else: colors = np.tile(list(surf_color) + [1.0], [model['pts'].shape[0], 1]) vertices_type = [ ('a_position', np.float32, 3), #('a_normal', np.float32, 3), ('a_color', np.float32, colors.shape[1]) ] vertices = np.array(list(zip(model['pts'], colors)), vertices_type) # Rendering #--------------------------------------------------------------------------- render_rgb = mode in ['rgb', 'rgb+depth'] render_depth = mode in ['depth', 'rgb+depth'] # Model matrix mat_model = np.eye(4, dtype=np.float32) # From object space to world space # View matrix (transforming also the coordinate system from OpenCV to # OpenGL camera space) mat_view = np.eye(4, dtype=np.float32) # From world space to eye space mat_view[:3, :3], mat_view[:3, 3] = R, t.squeeze() #print(mat_view) #plt.show() yz_flip = np.eye(4, dtype=np.float32) yz_flip[1, 1], yz_flip[2, 2] = -1, -1 mat_view = yz_flip.dot(mat_view) # OpenCV to OpenGL camera system mat_view = mat_view.T # OpenGL expects column-wise matrix format #print(mat_view) # Projection matrix mat_proj = _compute_calib_proj(K, 0, 0, im_size[0], im_size[1], clip_near, clip_far) # # Create buffers vertex_buffer = vertices.view(gloo.VertexBuffer) index_buffer = model['faces'].flatten().astype(np.uint32).view( gloo.IndexBuffer) #print(model['faces']) #plt.show() sss window = app.Window(visible=False) #plt.show() fff global rgb, depth rgb = None depth = None #plt.show() failed @window.event def on_draw(dt): window.clear() shape = (im_size[1], im_size[0]) if render_rgb: # Render color image global rgb rgb = draw_color(shape, vertex_buffer, index_buffer, mat_model, mat_view, mat_proj, ambient_weight, bg_color) if render_depth: # Render depth image global depth depth = draw_depth(shape, vertex_buffer, index_buffer, mat_model, mat_view, mat_proj) app.run(framecount=0) # The on_draw function is called framecount+1 times window.close() #plt.show() # Set output #--------------------------------------------------------------------------- if mode == 'rgb': return rgb elif mode == 'depth': return depth elif mode == 'rgb+depth': return rgb, depth else: print('Error: Unknown rendering mode.') exit(-1)
def glumpyVisualize(voronoiCrystal): from glumpy import app, gloo, gl, data from glumpy.transforms import Position, Trackball from glumpy.graphics.filter import Filter vertex = """ uniform vec3 light_position; attribute vec3 position; attribute vec3 color; attribute float radius; varying float v_size; varying vec3 v_color; varying float v_radius; varying vec4 v_eye_position; varying vec3 v_light_direction; void main (void) { v_color = color; v_radius = radius; v_eye_position = <transform.trackball_view> * <transform.trackball_model> * vec4(position,1.0); v_light_direction = normalize(light_position); gl_Position = <transform(position)>; // stackoverflow.com/questions/8608844/... // ... resizing-point-sprites-based-on-distance-from-the-camera vec4 p = <transform.trackball_projection> * vec4(radius, radius, v_eye_position.z, v_eye_position.w); v_size = 512.0 * p.x / p.w; gl_PointSize = v_size + 5.0; } """ fragment = """ #include "antialias/outline.glsl" varying float v_size; varying vec3 v_color; varying float v_radius; varying vec4 v_eye_position; varying vec3 v_light_direction; void main() { vec2 P = gl_PointCoord.xy - vec2(0.5,0.5); float point_size = v_size + 5.0; float distance = length(P*point_size) - v_size/2; vec2 texcoord = gl_PointCoord* 2.0 - vec2(1.0); float x = texcoord.x; float y = texcoord.y; float d = 1.0 - x*x - y*y; if (d <= 0.0) discard; float z = sqrt(d); vec4 pos = v_eye_position; pos.z += v_radius*z; vec3 pos2 = pos.xyz; pos = <transform.trackball_projection> * pos; gl_FragDepth = 0.5*(pos.z / pos.w)+0.5; vec3 normal = vec3(x,y,z); float diffuse = clamp(dot(normal, v_light_direction), 0.0, 1.0); vec4 color = vec4((0.5 + 0.5*diffuse)*v_color, 1.0); gl_FragColor = outline(distance, 1.0, 1.0, vec4(0,0,0,1), color); // gl_FragColor = color; } """ window = app.Window(width=800, height=800, color=(1, 1, 1, 1)) particles = gloo.Program(vertex, fragment) particles['light_position'] = 0., 0., 2. particles["transform"] = Trackball(Position(), distance=500) particles.bind( getBindDataFromVoronoiCrystals(voronoiCrystal).view(gloo.VertexBuffer)) @window.event def on_draw(dt): window.clear() particles.draw(gl.GL_POINTS) @window.event def on_init(): gl.glEnable(gl.GL_DEPTH_TEST) window.attach(particles["transform"]) app.run()
# ----------------------------------------------------------------------------- # Copyright (c) 2009-2016 Nicolas P. Rougier. All rights reserved. # Distributed under the (new) BSD License. # ----------------------------------------------------------------------------- from glumpy import app window = app.Window() @window.event def on_draw(dt): window.clear() app.run(interactive=True) print("Try to type 'window.color = (1,1,1,1)' in the console")
def add_shape(shape): shapes.append(shape) if running == True: app.quit() app.run(framerate=60)
@window.event def on_resize(width, height): program['u_projection'] = glm.ortho(0, width, 0, height, -1, +1) @window.timer(1/60.) def timer(fps): data['a_fg_color'][..., 3] -= 0.01 data['a_size'] += 1.0 @window.event def on_mouse_motion(x, y, dx, dy): global index _, _, _, h = gl.glGetInteger(gl.GL_VIEWPORT) data['a_size'][index] = 25 data['a_position'][index] = x, h - y data['a_fg_color'][index] = 1, 1, 1, 1 index = (index + 1) % len(data) data = np.zeros(150, [('a_position', np.float32, 2), ('a_fg_color', np.float32, 4), ('a_size', np.float32, 1)]) data = data.view(gloo.VertexBuffer) index = 0 program = gloo.Program(vertex, fragment) program.bind(data) program['u_antialias'] = 1.00 program['u_linewidth'] = 1.00 app.run(framerate=60)
UV[:,:,2] = UV[:,:,0] UV[:,:,3] = UV[:,:,1] pingpong = 1 compute = gloo.Program(compute_vertex, compute_fragment, count=4) compute["params"] = P compute["texture"] = UV compute["texture"].interpolation = gl.GL_NEAREST compute["texture"].wrapping = gl.GL_CLAMP_TO_EDGE compute["position"] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)] compute["texcoord"] = [(0, 0), (0, 1), (1, 0), (1, 1)] compute['dt'] = dt compute['dx'] = 1.0 / w compute['dy'] = 1.0 / h compute['dd'] = dd compute['pingpong'] = pingpong render = gloo.Program(render_vertex, render_fragment, count=4) render["position"] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)] render["texcoord"] = [(0, 0), (0, 1), (1, 0), (1, 1)] render["texture"] = compute["texture"] render["texture"].interpolation = gl.GL_LINEAR render["texture"].wrapping = gl.GL_CLAMP_TO_EDGE render['pingpong'] = pingpong framebuffer = gloo.FrameBuffer(color=compute["texture"], depth=gloo.DepthBuffer(w, h)) app.run(framerate=0)
('position', np.float32, 2)] quad_arrays_0 = np.zeros(4, dtype).view(gloo.VertexArray) # Four colors quad_arrays_0['color'] = [ (1,0,0,1), (0,1,0,1), (0,0,1,1), (1,1,0,1) ] quad_arrays_0['position'] = [ (-1,-1), (-1,+1), (+1,-1), (+1,+1) ] quad_arrays_1 = np.zeros(4, dtype).view(gloo.VertexArray) # All red data quad_arrays_1['color'] = [ (1,0,0,1), (1,0,0,1), (1,0,0,1), (1,0,0,1) ] quad_arrays_1['position'] = [ (-1,-1), (-1,+1), (+1,-1), (+1,+1) ] quad['scale'] = 1.0 # Tell glumpy what needs to be done at each redraw count = 0 @window.event def on_draw(dt): global count window.clear() if count % 2 == 0: quad.bind(quad_arrays_0) quad.draw(gl.GL_TRIANGLE_STRIP) else: quad.bind(quad_arrays_1) quad.draw(gl.GL_TRIANGLE_STRIP) count += 1 # Run the app app.run(framerate=1)
V = V.ravel() ellipses.bind(V.view(gloo.VertexBuffer)) ellipses["size"] = a, b I = np.zeros((n, 6), dtype=np.uint32) I[:] = 0, 1, 2, 0, 2, 3 I += 4 * np.arange(0, n, dtype=np.uint32).reshape(n, 1) I = I.ravel() I = I.view(gloo.IndexBuffer) @window.event def on_resize(width, height): ellipses["resolution"] = width, height theta = 0 @window.event def on_draw(dt): global theta theta += 0.5 * np.pi / 180.0 ellipses["theta"] = theta window.clear() ellipses.draw(gl.GL_TRIANGLES, I) app.run() # framerate=60, framecount = 360/30/0.1)
def opengl(): # The vertex shader for augmented reality objects vertex = """ uniform mat4 model; // Model matrix uniform mat4 view; // View matrix uniform mat4 projection; // Projection matrix attribute vec3 position; // Vertex position attribute vec3 u_color; // Color defined on whether surface (custom color) or edge (black) varying vec3 v_color; void main() { v_color = u_color; gl_Position = projection * view * model * vec4(position,1.0); } """ # The fragment shader for augmented reality objects # Also used as fragment shader for interpolated path object fragment = """ varying vec3 v_color; void main() { gl_FragColor = vec4(v_color, 1.0); } """ # The vertex shader for the background texture (i.e -> camera image) vertex2 = """ attribute vec3 position; attribute vec2 texcoord; varying vec2 v_texcoord; void main() { gl_Position = vec4(position,1.0); v_texcoord = texcoord; } """ # The fragment shader for the background texture (i.e -> camera image) fragment2 = """ uniform sampler2D texture; varying vec2 v_texcoord; void main() { gl_FragColor = texture2D(texture, v_texcoord); } """ # The fragment shader for the background texture (i.e -> camera image) vertex3 = """ attribute vec3 last_position; attribute float side; uniform mat4 projection; // Projection matrix defined in function on_resize() attribute vec3 position; // Vertex position attribute vec3 u_color; // Color defined on whether surface (custom color) or edge (black) varying vec3 v_color; void main() { // line_vec is a vector from last position to current position vec3 line_vec; line_vec = position - last_position; // Unit normal vector to line_vec and parallel to ground plane, used to define the width of the curve // by establishing two points on the left and right of the current position vec3 normal; normal = vec3(-line_vec.z, 0.0, line_vec.x); normal = normal/(sqrt(normal.x*normal.x + normal.z*normal.z))*0.8; // Add normal vector to position to define new position, side of new_position is unknown vec3 new_position; new_position = position + normal; // vector from last position to new position vec3 new_line_vec; new_line_vec = new_position - last_position; // multiplier is used to identify if new_psoition is on the correct side defined by float side. If yes, // multiplier is +1.0 else -1.0 float multiplier; multiplier = 1.0; // Check if new_position is on correct side else change multiplier to -1.0 if (-line_vec.x*new_line_vec.y + line_vec.y*new_line_vec.x < 0.0){ if(side == 1.0){ multiplier = -1.0; } }else{ if(side == -1.0){ multiplier = -1.0; } } // Redefine new_position using multiplier to ge the point on the correct side new_position = position + normal*multiplier; // Define the view_matrix as pos mat4 pos; pos[0] = vec4(1.0, 0.0, 0.0, 0.0); pos[1] = vec4(0.0, 1.0, 0.0, 0.0); pos[2] = vec4(0.0, 0.0, 1.0, 0.0); pos[3] = vec4(new_position.x, new_position.y, new_position.z, 1.0); v_color = u_color; gl_Position = projection * pos * vec4(0.0, 0.0, 0.0, 1.0); } """ # Define the vertices and indices for each augmented reality object x = 0.1 V = np.zeros(8, [("position", np.float32, 3)]) V["position"] = [ [x * 1, x * 1, x * 1], [x * -1, x * 1, x * 1], [x * -1, x * -1, x * 1], [x * 1, x * -1, x * 1], [x * 1, x * -1, x * -1], [x * 1, x * 1, x * -1], [x * -1, x * 1, x * -1], [x * -1, x * -1, x * -1], ] V = V.view(gloo.VertexBuffer) I = np.array( [ 0, 1, 2, 0, 2, 3, 0, 3, 4, 0, 4, 5, 0, 5, 6, 0, 6, 1, 1, 6, 7, 1, 7, 2, 7, 4, 3, 7, 3, 2, 4, 7, 6, 4, 6, 5, ], dtype=np.uint32, ) I = I.view(gloo.IndexBuffer) # Index buffer for object edges O = np.array( [ 0, 1, 1, 2, 2, 3, 3, 0, 4, 7, 7, 6, 6, 5, 5, 4, 0, 5, 1, 6, 2, 7, 3, 4 ], dtype=np.uint32, ) O = O.view(gloo.IndexBuffer) for iterator in range(4): cube = gloo.Program(vertex, fragment) cube.bind(V) cube["model"] = np.eye(4, dtype=np.float32) cube["view"] = glm.translation(0, 0, -30) bag[iterator] = cube # Define vertex for background texture and index buffer global quad quad = gloo.Program(vertex2, fragment2, count=4) quad["position"] = [(-1, -1, 0), (-1, +1, 0), (+1, -1, 0), (+1, +1, 0)] quad["texcoord"] = [(0, 1), (0, 0), (1, 1), (1, 0)] quad["texture"] = initial_image[..., ::-1] global path path = gloo.Program(vertex3, fragment, count=4 * 25 * 2) # Initialise the vertex buffers for path path["position"] = np.zeros((4 * 25 * 2, 3)) path["last_position"] = np.zeros((4 * 25 * 2, 3)) path["side"] = np.zeros(4 * 25 * 2) # Color of path path["u_color"] = 0, 0.5, 0 # Define the Index Buffer for edges of the augmented reality path. bline_I = list() bline_I += [2, 3] for i in range(2, 4 * 25 * 2 - 2, 2): bline_I += [i, i + 2] bline_I += [i + 1, i + 3] bline_I = np.array(bline_I, dtype=np.uint32) bline_I = bline_I.view(gloo.IndexBuffer) # Initialise the OpenGL window window = app.Window(width=img_width, height=img_height, color=(1, 1, 1, 1)) @window.event def on_draw(dt): global phi, theta # Phi and Theta are the cube rotation paramters window.clear() lock.acquire() try: # Disable depth of OpenGL to update background tecture gl.glDisable(gl.GL_DEPTH_TEST) quad.draw(gl.GL_TRIANGLE_STRIP) # R-enable depth gl.glEnable(gl.GL_DEPTH_TEST) # Color of path path["u_color"] = 0, 1, 1 # Filled path path.draw(gl.GL_TRIANGLE_STRIP) # Mask depth gl.glDepthMask(gl.GL_FALSE) # Color of edge lines of path path["u_color"] = 0, 0, 0 # Width of edge lines gl.glLineWidth(10.0) # Draw edge lines with index buffer bline_I path.draw(gl.GL_LINES, bline_I) # Reset line width gl.glLineWidth(1.0) gl.glDepthMask(gl.GL_TRUE) # Define the model matrix with updated rotation model = np.eye(4, dtype=np.float32) glm.rotate(model, theta, 0, 0, 1) glm.rotate(model, phi, 0, 1, 0) for obj in bag.values(): obj["u_color"] = 1, 0, 0 # Filled cube obj.draw(gl.GL_TRIANGLES, I) # Another method to disable depth, instead of disabling it, mask it gl.glDepthMask(gl.GL_FALSE) # Black color for edge lines of cube obj["u_color"] = 0, 0, 0 # Draw the edge lines with the given index buffer obj.draw(gl.GL_LINES, O) # Unmask OpenGL depth aparamter gl.glDepthMask(gl.GL_TRUE) # Model matrix is used to define orientation ,in this case, used to rotate cube obj["model"] = model # Update cube rotations theta += 2.0 # degrees phi += 2.0 # degrees finally: lock.release() @window.event def on_resize(width, height): # Redefine projection matrix from OpenCV style to OpenGL style # OpenCV defines 3d image from left top corner as (0,0,0) and x and y increase towards right and down respectively. # z increases positively outwards # OpenGL defines 3d image from center as (0,0,0) and x and y increase towards right and up respectiively. # z increases negatively outwards # Clipping (1m - 30m) # Source - https://blog.noctua-software.com/opencv-opengl-projection-matrix.html view_matrix = np.zeros((4, 4)) view_matrix[0][0] = 2.0 * projection_matrix[0] / img_width view_matrix[1][1] = -2.0 * projection_matrix[5] / img_height view_matrix[2][0] = 1.0 - 2.0 * projection_matrix[2] / img_width view_matrix[2][1] = 2.0 * projection_matrix[6] / img_height - 1.0 view_matrix[2][2] = (30 + 1) / float(1 - 30) view_matrix[2][3] = -1.0 view_matrix[3][2] = 2.0 * 30 * 1 / (1 - 30) for obj in bag.values(): obj["projection"] = view_matrix # Use same projection amtrix for path but redefine clipping to (0.01m - 30m) view_matrix[2][2] = (30 + 0.01) / float(0.01 - 30) view_matrix[3][2] = 2.0 * 30 * 0.01 / (0.01 - 30) path["projection"] = view_matrix @window.event def on_init(): gl.glEnable(gl.GL_DEPTH_TEST) app.run(framerate=30)
def show(self): self.window.push_handlers(self.viewport) self.window.push_handlers(self) app.run()
def scatter(self, mode = 'point'): #----------------------------------------------------------------------# # import vertex and fragment shader if mode == 'point': # point mode, no light # shader and program vertex = shader.vertex_point # vertex shader fragment = shader.fragment_point # fragmenet shader program = gloo.Program(vertex, fragment, count=len(self.qi)) # program elif mode == 'diffuse': # difffuse mode, diffuse light scattering # shader and program vertex = shader.vertex_diffuse # vertex shader fragment = shader.fragment_diffuse # fragmenet shader program = gloo.Program(vertex, fragment, count=len(self.qi)) # program program['light'] = self.light # add light source elif mode == 'specular': # difffuse mode, diffuse light scattering # shader and program vertex = shader.vertex_specular # vertex shader fragment = shader.fragment_specular # fragmenet shader program = gloo.Program(vertex, fragment, count=len(self.qi)) # program program['light'] = self.light # add light source else: raise ValueError('This mode has not been developed yet! \n'+ 'Please choose from point, diffuse and specular.') #----------------------------------------------------------------------# # open a window back_color = color.RGB1[self.back_color] window = app.Window(self.window_width, self.window_height, color=(back_color[0], back_color[1], back_color[2], self.back_alpha)) # shader variable setting program['position'] = self.position # position program['radius'] = self.r # point radius program['bg_color'] = self.color # point color program['model'] = np.eye(4, dtype=np.float32) program['projection'] = np.eye(4, dtype=np.float32) program['view'] = glm.translate(np.eye(4, dtype = np.float32), 0, 0, self.translate) #----------------------------------------------------------------------# @window.event def on_draw(dt): window.clear() program.draw(gl.GL_POINTS) # make rotation self.theta += self.r_theta self.phi += self.r_phi self.chi += self.r_chi model = np.eye(4, dtype=np.float32) #""" ------------------------------------------------------- #rotate(M, angle, x, y, z, point=None) #---------------------------------------------------------""" glm.rotate(model, self.theta, 0, 0, 1) glm.rotate(model, self.phi, 0, 1, 0) glm.rotate(model, self.chi, 1, 0, 0) program['model'] = model #----------------------------------------------------------------------# @window.event def on_resize(width,height): program['projection'] = glm.perspective(45.0, width / float(height), 1.0, 1000.0) gl.glEnable(gl.GL_DEPTH_TEST) app.run()
float y = s*position.x + c*position.y; gl_Position = vec4(0.7*vec2(x,y), 0.0, 1.0); v_color = color; } """ fragment = """ varying vec4 v_color; void main() { gl_FragColor = v_color; } """ window = app.Window(color=(1, 1, 1, 1)) quad = gloo.Program(vertex, fragment, count=4) quad['position'] = (-1, +1), (+1, +1), (-1, -1), (+1, -1) quad['color'] = (1, 1, 0, 1), (1, 0, 0, 1), (0, 0, 1, 1), (0, 1, 0, 1) angle = 0.0 @window.event def on_draw(dt): global angle angle += 1.0 * math.pi / 180.0 window.clear() quad["theta"] = angle quad.draw(gl.GL_TRIANGLE_STRIP) # We set the framecount to 360 in order to record a movie that can # loop indefinetly. Run this program with: # python quad-scale.py --record quad-scale.mp4 app.run(framecount=360)
def start_world(width=1024, height=512, overlay=False, frames=60): global running, frame, window_height, window_width, image_program, segments, instances window_width = width window_height = height window = app.Window(width=width, height=height, color=get_color(23,23,23)) frame = np.zeros((window.height, window.width * 3), dtype=np.uint8) def render_segments(): window.clear(get_color(23,23,23)) for shape in shapes: shape.use_segment_color() shape.draw() def render_instances(): window.clear((1,1,1, 1)) for shape in shapes: shape.use_instance_color() shape.draw() def capture_frame(): gl.glDisable(gl.GL_DEPTH_TEST) gl.glReadPixels(0, 0, window_width, window_height, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, frame) out = np.flipud(np.array(frame)).reshape(height, width, 3) gl.glEnable(gl.GL_DEPTH_TEST) return out def move_shapes(): for shape in shapes: # shape.set_rotation((time, 0, 1, 0)) # shape.set_translation((0, -time*5+2, cos(time)/2-1)) if shape.shape_type == "cube": x, y, z= shape.position shape.set_translation((x, y - time*25, z)) @window.event def on_resize(width, height): for shape in shapes: shape.projection(glm.perspective(45.0, width / float(height), 2.0, 100.0)) @window.event def on_init(): gl.glEnable(gl.GL_DEPTH_TEST) @window.event def on_draw(dt): global iters, time, shapes, frame, window_height, window_width, image_program move_shapes() time += dt # Capture segmentation map render_segments() segment_map = capture_frame() # Capture instance map render_instances() instance_map = capture_frame() Image.fromarray(segment_map).save('../datasets/test/test_A/image_' + str(iters).zfill(4) + '.png') # img = Image.open('./world/examples/segments/image_'+str(iters)+'.png') # segment_map = np.array(img) Image.fromarray(instance_map).save('../datasets/test/test_inst/image_' + str(iters).zfill(4) + '.png') # img = Image.open('./world/examples/instances/image_'+str(iters)+'.png') # instance_map = np.array(img) # Generate frame # segments.append(segment_map) # instances.append(instance_map) # if len(segments) == 3: # gen_frame = convert_image(segments, instances) # image_program['texture'] = gen_frame # image_program.draw(gl.GL_TRIANGLE_STRIP) # segments.pop() # instances.pop() # Image.fromarray(gen_frame).save('./world/examples/results/image_' + str(iters) + '.png') iters += 1 if (iters >= frames): app.quit() shapes.append(cube) image_program = gloo.Program(vertex, fragment, count=4) image_program['position'] = [(-1,-1), (-1,+1), (+1,-1), (+1,+1)] image_program['texcoord'] = [( 0, 1), ( 0, 0), ( 1, 1), ( 1, 0)] image_program['texture'] = np.random.randint(1, 255, size=(512, 1024, 3), dtype=np.uint8) app.run(framerate=60) running = True
def render(model, im_size, K, R, t, clip_near=50, clip_far=10000, texture=None, surf_color=None, bg_color=(0.0, 0.0, 0.0, 0.0), ambient_weight=0.5, shading='flat', mode='rgb+depth', light_src=[0, 0, 0]): # Process input data #--------------------------------------------------------------------------- # Make sure vertices and faces are provided in the model assert({'pts', 'faces'}.issubset(set(model.keys()))) # Set texture / color of vertices if texture is not None: if texture.max() > 1.0: texture = texture.astype(np.float32) / 255.0 texture = np.flipud(texture) texture_uv = model['texture_uv'] colors = np.zeros((model['pts'].shape[0], 3), np.float32) else: texture_uv = np.zeros((model['pts'].shape[0], 2), np.float32) if not surf_color: if 'colors' in model.keys(): assert(model['pts'].shape[0] == model['colors'].shape[0]) colors = model['colors'] if colors.max() > 1.0: colors /= 255.0 # Color values are expected in range [0, 1] else: colors = np.ones((model['pts'].shape[0], 3), np.float32) * 0.5 else: colors = np.tile(list(surf_color) + [1.0], [model['pts'].shape[0], 1]) # Set the vertex data if mode == 'depth': vertices_type = [('a_position', np.float32, 3), ('a_color', np.float32, colors.shape[1])] vertices = np.array(list(zip(model['pts'], colors)), vertices_type) else: if shading == 'flat': vertices_type = [('a_position', np.float32, 3), ('a_color', np.float32, colors.shape[1]), ('a_texcoord', np.float32, 2)] vertices = np.array(list(zip(model['pts'], colors, texture_uv)), vertices_type) else: # shading == 'phong' vertices_type = [('a_position', np.float32, 3), ('a_normal', np.float32, 3), ('a_color', np.float32, colors.shape[1]), ('a_texcoord', np.float32, 2)] vertices = np.array(list(zip(model['pts'], model['normals'], colors, texture_uv)), vertices_type) # Rendering #--------------------------------------------------------------------------- render_rgb = mode in ['rgb', 'rgb+depth'] render_depth = mode in ['depth', 'rgb+depth'] # Model matrix mat_model = np.eye(4, dtype=np.float32) # From object space to world space # View matrix (transforming also the coordinate system from OpenCV to # OpenGL camera space) mat_view = np.eye(4, dtype=np.float32) # From world space to eye space mat_view[:3, :3], mat_view[:3, 3] = R, t.squeeze() yz_flip = np.eye(4, dtype=np.float32) yz_flip[1, 1], yz_flip[2, 2] = -1, -1 mat_view = yz_flip.dot(mat_view) # OpenCV to OpenGL camera system mat_view = mat_view.T # OpenGL expects column-wise matrix format # Projection matrix mat_proj = _compute_calib_proj(K, 0, 0, im_size[0], im_size[1], clip_near, clip_far) # Create buffers vertex_buffer = vertices.view(gloo.VertexBuffer) index_buffer = model['faces'].flatten().astype(np.uint32).view(gloo.IndexBuffer) # Create window # config = app.configuration.Configuration() # Number of samples used around the current pixel for multisample # anti-aliasing (max is 8) # config.samples = 8 # config.profile = "core" # window = app.Window(config=config, visible=False) window = app.Window(visible=False) global rgb, depth rgb = None depth = None @window.event def on_draw(dt): window.clear() shape = (im_size[1], im_size[0]) if render_rgb: # Render color image global rgb rgb = draw_color(shape, vertex_buffer, index_buffer, texture, mat_model, mat_view, mat_proj, ambient_weight, bg_color, shading, light_src=light_src) if render_depth: # Render depth image global depth depth = draw_depth(shape, vertex_buffer, index_buffer, mat_model, mat_view, mat_proj) app.run(framecount=0) # The on_draw function is called framecount+1 times window.close() # Set output #--------------------------------------------------------------------------- if mode == 'rgb': return rgb elif mode == 'depth': return depth elif mode == 'rgb+depth': return rgb, depth else: print('Error: Unknown rendering mode.') exit(-1)
#! /usr/bin/env python # -*- coding: utf-8 -*- # ----------------------------------------------------------------------------- # Copyright (c) 2014, Nicolas P. Rougier # Distributed under the (new) BSD License. See LICENSE.txt for more info. # ----------------------------------------------------------------------------- from glumpy import app window = app.Window() @window.event def on_draw(dt): window.clear() app.run(framerate=60, duration=5.0)
program['data'] = matrix_p#np.random.uniform(0,1,(xdim,ydim)) program.draw(gl.GL_TRIANGLE_STRIP) #print time.clock() - start_time, "seconds" else: print("empty") @window.event def on_key_press(key, modifiers): if key == app.window.key.SPACE: transform.reset() @window.event def on_resize(width, height): program['viewport'] = 0, 0, width, height program = gloo.Program(vertex, fragment, count=4) program['position'] = [(-1,-1), (-1,1), (1,-1), (1,1)] program['texcoord'] = [( 0, 1), ( 0, 0), ( 1, 1), ( 1, 0)] program['data'] = np.random.uniform(0,1,(xdim,ydim)) program['data_shape'] = program['data'].shape[:2] transform = PanZoom(Position("position"),aspect=1) program['transform'] = transform window.attach(transform) control = caer_communication.caer_communication(host='localhost') p = ProducerThread(control, name='producer') p.start() app.run(framerate=120)
def run(): app.run()
# ----------------------------------------------------------------------------- # Copyright (c) 2009-2016 Nicolas P. Rougier. All rights reserved. # Distributed under the (new) BSD License. # ----------------------------------------------------------------------------- from glumpy import app window = app.Window() @window.event def on_draw(dt): window.clear() app.run(framerate=60, duration=5.0)
for dx, dy in offset: framebuffer_1.activate() window.clear() scene["offset"] = (2 * dx - 1) / width, (2 * dy - 1) / height scene.draw(gl.GL_TRIANGLE_STRIP) # scene.draw(gl.GL_LINE_LOOP) framebuffer_1.deactivate() framebuffer_2.activate() gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_CONSTANT_ALPHA, gl.GL_ONE) gl.glBlendColor(0, 0, 0, 1 / len(offset)) ssaa.draw(gl.GL_TRIANGLE_STRIP) gl.glDisable(gl.GL_BLEND) framebuffer_2.deactivate() gl.glViewport(0, 0, window.width, window.height) window.clear() final.draw(gl.GL_TRIANGLE_STRIP) gl.glReadPixels(0, 0, window.width, window.height, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, framebuffer) framebuffer[...] = framebuffer[::-1, :] # filename = "triangle-ssaa-outlined-%s.png" % offset_name filename = "triangle-ssaa-filled-%s.png" % offset_name png.from_array(framebuffer, 'RGB').save(filename) offset_index += 1 app.run(framecount=len(offsets) - 1)
def widowSetting(self): window = app.Window(1024, 1024, color=(0, 0, 0, 1)) framebuffer = np.zeros((window.height, window.width * 3), dtype=np.uint8) @window.event def on_draw(dt): window.clear() # Filled program_cube self.program_axis.draw(gl.GL_LINES) self.program.draw(gl.GL_POINTS) # Make program_cube rotate self.program['u_model'] = matrix_model() def matrix_model(): model = np.eye(4, dtype=np.float32) glm.scale(model, self.size, self.size, self.size) glm.rotate(model, self.deg_y, 1, 0, 0) glm.rotate(model, self.deg_x, 0, 1, 0) # glm.translate(model, -self.deg_x/100, -self.deg_y/100, 0) # model[3,3] = 1 return model @window.event def on_resize(width, height): ratio = width / float(height) self.program['u_projection'] = glm.perspective(45.0, ratio, 0.001, 10000.0) @window.event def on_init(): gl.glEnable(gl.GL_DEPTH_TEST) @window.event def on_mouse_scroll(x, y, dx, dy): self.size += dy * 0.1 if self.size < 0: self.size = 0.1 # self.zoom += dy*1 # self.program['u_view'] = glm.translation(0, 0, self.zoom) @window.event def on_mouse_drag(x, y, dx, dy, button): self.deg_y += dy # degrees self.deg_x += dx # degrees @window.event def on_key_press(symbol, modifiers): if symbol == 88: # x self.view_orth_vector = np.array([self.radius, 0, 0]) self.program['u_view'] = glm.translation(self.view_orth_vector[0], self.view_orth_vector[1], self.view_orth_vector[2]) elif symbol == 89: # y self.view_orth_vector = np.array([0, self.radius, 0]) self.program['u_view'] = glm.translation(self.view_orth_vector[0], self.view_orth_vector[1], self.view_orth_vector[2]) elif symbol == 90: # z self.view_orth_vector = np.array([0, 0, self.radius]) self.program['u_view'] = glm.translation(self.view_orth_vector[0], self.view_orth_vector[1], self.view_orth_vector[2]) elif symbol == 70: # f self.view_orth_vector = -self.view_orth_vector self.program['u_view'] = glm.translation(self.view_orth_vector[0], self.view_orth_vector[1], self.view_orth_vector[2]) elif symbol == 80: # p gl.glReadPixels(0, 0, window.width, window.height, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, framebuffer) png.from_array(framebuffer, 'RGB').save('screenshot.png') print('Key pressed (symbol=%s, modifiers=%s)' % (symbol, modifiers)) # self.program['color_sel'] = 1 - self.program['color_sel'] self.program['color'] = (1, 0, 0, 1) self.program['color_sel'] = 1 self.program['u_model'] = np.eye(4, dtype=np.float32) self.program['u_view'] = glm.translation(0, 0, -50) self.program['a_pointSize'] = 5 app.run()
def render(model, im_size, K, R, t, clip_near=100, clip_far=2000, surf_color=None, bg_color=(0.0, 0.0, 0.0, 0.0), ambient_weight=0.1, mode='rgb+depth'): # Process input data #--------------------------------------------------------------------------- # Make sure vertices and faces are provided in the model assert({'pts', 'faces'}.issubset(set(model.keys()))) # Set color of vertices if not surf_color: if 'colors' in model.keys(): assert(model['pts'].shape[0] == model['colors'].shape[0]) colors = model['colors'] if colors.max() > 1.0: colors /= 255.0 # Color values are expected to be in range [0, 1] else: colors = np.ones((model['pts'].shape[0], 3), np.float32) * 0.5 else: colors = np.tile(list(surf_color) + [1.0], [model['pts'].shape[0], 1]) vertices_type = [('a_position', np.float32, 3), #('a_normal', np.float32, 3), ('a_color', np.float32, colors.shape[1])] vertices = np.array(zip(model['pts'], colors), vertices_type) # Rendering #--------------------------------------------------------------------------- render_rgb = mode in ['rgb', 'rgb+depth'] render_depth = mode in ['depth', 'rgb+depth'] # Model matrix mat_model = np.eye(4, dtype=np.float32) # From object space to world space # View matrix (transforming also the coordinate system from OpenCV to # OpenGL camera space) mat_view = np.eye(4, dtype=np.float32) # From world space to eye space mat_view[:3, :3], mat_view[:3, 3] = R, t.squeeze() yz_flip = np.eye(4, dtype=np.float32) yz_flip[1, 1], yz_flip[2, 2] = -1, -1 mat_view = yz_flip.dot(mat_view) # OpenCV to OpenGL camera system mat_view = mat_view.T # OpenGL expects column-wise matrix format # Projection matrix mat_proj = _compute_calib_proj(K, 0, 0, im_size[0], im_size[1], clip_near, clip_far) # Create buffers vertex_buffer = vertices.view(gloo.VertexBuffer) index_buffer = model['faces'].flatten().astype(np.uint32).view(gloo.IndexBuffer) window = app.Window(visible=False) global rgb, depth rgb = None depth = None @window.event def on_draw(dt): window.clear() shape = (im_size[1], im_size[0]) if render_rgb: # Render color image global rgb rgb = draw_color(shape, vertex_buffer, index_buffer, mat_model, mat_view, mat_proj, ambient_weight, bg_color) if render_depth: # Render depth image global depth depth = draw_depth(shape, vertex_buffer, index_buffer, mat_model, mat_view, mat_proj) app.run(framecount=0) # The on_draw function is called framecount+1 times window.close() # Set output #--------------------------------------------------------------------------- if mode == 'rgb': return rgb elif mode == 'depth': return depth elif mode == 'rgb+depth': return rgb, depth else: print 'Error: Unknown rendering mode.' exit(-1)
def show(self): cubesize = 5e-2 elecsize = 5e-2 window = app.Window() vertex = """ uniform mat4 u_model; // Model matrix uniform mat4 u_view; // View matrix uniform mat4 u_projection; // Projection matrix attribute vec3 a_position; // Vertex position attribute vec4 a_color; varying vec4 v_color; //varying imemcolor void main() { v_color = a_color; gl_Position = u_projection * u_view * u_model * vec4(a_position,1.0); } """ fragment = """ varying vec4 v_color; void main() { gl_FragColor = v_color; } """ xnorm = np.max(self.cell.xstart) ynorm = np.max(self.cell.ystart) znorm = np.max(self.cell.zstart) norm = np.max([xnorm, ynorm, znorm]) / 3 cubes = [] elecs = [] print(len(self.cell.xstart)) for i in range(len(self.cell.xstart)): centre1 = np.array([ self.cell.xstart[i] / norm, self.cell.ystart[i] / norm, self.cell.zstart[i] / norm ]) centre2 = np.array([ self.cell.xend[i] / norm, self.cell.yend[i] / norm, self.cell.zend[i] / norm ]) mid = np.mean([centre1, centre2], axis=0) # print(mid) print("seg:", mid) V = np.zeros(8, [("a_position", np.float32, 3), ("a_color", np.float32, 4)]) V["a_position"] = [ mid + [cubesize, cubesize, cubesize], mid + [0, +cubesize, +cubesize], mid + [0, 0, +cubesize], mid + [+cubesize, 0, +cubesize], mid + [+cubesize, 0, 0], mid + [+cubesize, +cubesize, 0], mid + [0, +cubesize, 0], mid + [0, 0, 0] ] V["a_color"] = [[0, 1, 1, 1], [0, 0, 1, 1], [0, 0, 0, 1], [0, 1, 0, 1], [1, 1, 0, 1], [1, 1, 1, 1], [1, 0, 1, 1], [1, 0, 0, 1]] I = np.array([ 0, 1, 2, 0, 2, 3, 0, 3, 4, 0, 4, 5, 0, 5, 6, 0, 6, 1, 1, 6, 7, 1, 7, 2, 7, 4, 3, 7, 3, 2, 4, 7, 6, 4, 6, 5 ], dtype=np.uint32) V = V.view(gloo.VertexBuffer) I = I.view(gloo.IndexBuffer) cube = gloo.Program(vertex, fragment) cube["a_position"] = V["a_position"] cube["a_color"] = V["a_color"] view = np.eye(4, dtype=np.float32) model = np.eye(4, dtype=np.float32) projection = np.eye(4, dtype=np.float32) glm.translate(view, 0, 0, -5) cube['u_model'] = model cube['u_view'] = view cube['u_projection'] = projection cubes.append(cube) if self.elec: cubesize = elecsize for i in range(round(len(self.elec.X.flatten()) / 123)): i = i * 123 mid = np.array([ self.elec.X.flatten()[i], self.elec.Y.flatten()[i], self.elec.Z.flatten()[i] ]) / norm V = np.zeros(8, [("a_position", np.float32, 3), ("a_color", np.float32, 4)]) V["a_position"] = [ mid + [cubesize, cubesize, cubesize], mid + [0, +cubesize, +cubesize], mid + [0, 0, +cubesize], mid + [+cubesize, 0, +cubesize], mid + [+cubesize, 0, 0], mid + [+cubesize, +cubesize, 0], mid + [0, +cubesize, 0], mid + [0, 0, 0] ] V["a_color"] = repmat([0, 1, 0, 1], 8, 1).tolist() I = np.array([ 0, 1, 2, 0, 2, 3, 0, 3, 4, 0, 4, 5, 0, 5, 6, 0, 6, 1, 1, 6, 7, 1, 7, 2, 7, 4, 3, 7, 3, 2, 4, 7, 6, 4, 6, 5 ], dtype=np.uint32) V = V.view(gloo.VertexBuffer) I = I.view(gloo.IndexBuffer) cube = gloo.Program(vertex, fragment) cube["a_position"] = V["a_position"] cube["a_color"] = V["a_color"] view = np.eye(4, dtype=np.float32) model = np.eye(4, dtype=np.float32) projection = np.eye(4, dtype=np.float32) glm.translate(view, 0, 0, -5) cube['u_model'] = model cube['u_view'] = view cube['u_projection'] = projection elecs.append(cube) print("cubes loaded") self.phi, self.theta = 0, 0 self.cubes = cubes self.elecs = elecs @window.event def on_resize(width, height): ratio = width / float(height) for cube in self.cubes: cube['u_projection'] = glm.perspective(100, ratio, 2.0, 100.0) for cube in self.elecs: cube['u_projection'] = glm.perspective(100, ratio, 2.0, 100.0) @window.event def on_draw(dt): window.clear() for cube in self.cubes: cube.draw(gl.GL_TRIANGLES, I) for cube in self.elecs: cube.draw(gl.GL_TRIANGLES, I) # Make cube rotate self.theta += .5 # degrees self.phi += 0.5 # degrees model = np.eye(4, dtype=np.float32) glm.rotate(model, self.theta, 0, 1, 0) colordata = self.conn.recv() self.maxcolor = np.max([self.maxcolor, np.max(colordata)]) self.mincolor = np.min([self.mincolor, np.min(colordata)]) for cube, col in zip(self.cubes, colordata): cube['u_model'] = model cube['a_color'] = repmat( [(col - self.mincolor) / (self.maxcolor - self.mincolor), 0, 1 - ((col - self.mincolor) / (self.maxcolor - self.mincolor)), .3], 8, 1).tolist() for cube in self.elecs: cube['u_model'] = model cube['a_color'] = repmat([0, 1, 0, 1], 8, 1).tolist() @window.event def on_init(): gl.glEnable(gl.GL_DEPTH_TEST) app.run()
window.clear() program.draw(gl.GL_TRIANGLE_STRIP) program["iGlobalTime"] += dt today = datetime.datetime.now() seconds = (today.hour * 60 * 60 + today.minute * 60 + today.second) program["iDate"] = today.year, today.month, today.day, seconds @window.event def on_resize(width, height): program["iResolution"] = width, height, 0 @window.event def on_mouse_drag(x, y, dx, dy, button): buttons = { app.window.mouse.NONE: 0, app.window.mouse.LEFT: 1, app.window.mouse.MIDDLE: 2, app.window.mouse.RIGHT: 3 } program["iMouse"] = x, y, buttons[button], 0 program = gloo.Program(vertex, fragment, count=4) program['position'] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)] program["iGlobalTime"] = 0 gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) app.run(framerate=60)
#! python # ----------------------------------------------------------------------------- # Copyright (c) 2009-2016 Nicolas P. Rougier. All rights reserved. # Distributed under the (new) BSD License. # ----------------------------------------------------------------------------- import numpy as np from glumpy import app, gl from glumpy.ext import png window = app.Window(color=(1, 0, 0, 1)) framebuffer = np.zeros((window.height, window.width * 3), dtype=np.uint8) @window.event def on_draw(dt): window.clear() gl.glReadPixels(0, 0, window.width, window.height, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, framebuffer) png.from_array(framebuffer, 'RGB').save('screenshot' + str(dt) + '.png') app.run(framecount=5)
# ----------------------------------------------------------------------------- # Copyright (c) 2009-2016 Nicolas P. Rougier. All rights reserved. # Distributed under the (new) BSD License. # ----------------------------------------------------------------------------- import numpy as np from glumpy import app, gl from glumpy.ext import png window = app.Window(color=(1, 0, 0, 1)) framebuffer = np.zeros((window.height, window.width * 3), dtype=np.uint8) @window.event def on_draw(dt): window.clear() gl.glReadPixels(0, 0, window.width, window.height, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, framebuffer) png.from_array(framebuffer, 'RGB').save('screenshot.png') app.run(framecount=1)
def render(self, model_idx, r, t, light_position, light_intensity, brightness_k=0, r_type="quat"): """ :param r: :param t: :param light_position: :param light_intensity: :param brightness_k: choose which brightness in __init__ :param r_type: :return: """ if r_type == "quat": R = quat2mat(r) elif r_type == "mat": R = r self.model_path = self.model_path_list[model_idx] self.brightness_k = brightness_k self.render_kernels[ self.model_path][brightness_k]["u_view"] = self._get_view_mtx( R, t) self.render_kernels[ self.model_path][brightness_k]["u_light_position"] = light_position self.render_kernels[ self.model_path][brightness_k]["u_normal"] = np.array( np.matrix( np.dot( self.render_kernels[self.model_path][brightness_k] ["u_view"].reshape(4, 4), self.render_kernels[self.model_path][brightness_k] ["u_model"].reshape(4, 4), )).I.T) self.render_kernels[self.model_path][brightness_k][ "u_light_intensity"] = light_intensity app.run(framecount=0) rgb_buffer = np.zeros((self.height, self.width, 4), dtype=np.float32) gl.glReadPixels(0, 0, self.width, self.height, gl.GL_RGBA, gl.GL_FLOAT, rgb_buffer) rgb_gl = np.copy(rgb_buffer) rgb_gl.shape = 480, 640, 4 rgb_gl = rgb_gl[::-1, :] rgb_gl = np.round(rgb_gl[:, :, :3] * 255).astype( np.uint8) # Convert to [0, 255] bgr_gl = rgb_gl[:, :, [2, 1, 0]] depth_buffer = np.zeros((self.height, self.width), dtype=np.float32) gl.glReadPixels(0, 0, self.width, self.height, gl.GL_DEPTH_COMPONENT, gl.GL_FLOAT, depth_buffer) depth_gl = np.copy(depth_buffer) depth_gl.shape = 480, 640 depth_gl = depth_gl[::-1, :] depth_bg = depth_gl == 1 depth_gl = 2 * self.zFar * self.zNear / (self.zFar + self.zNear - (self.zFar - self.zNear) * (2 * depth_gl - 1)) depth_gl[depth_bg] = 0 return bgr_gl, depth_gl
@window.event def on_draw(dt): window.clear() points.draw() # lin-lin transform = Position(LinearScale('.xy', domain=(0, 10))) # log-lin transform = Position(LogScale('.x', domain=(-1, 1)), LinearScale('.y', domain=(0, 10))) # lin-log transform = Position(LinearScale('.x', domain=(0, 10)), LogScale('.y', domain=(-1, 1))) # log-log # transform = Position(LogScale('.xy', domain=(-1,1))) points = PointCollection("agg", transform=transform, color='local') X = np.linspace(0.01, 10.0, 10000).reshape(10000, 1) Z = np.zeros((len(X), 1)) points.append(np.hstack((X, X, Z)), color=(1, 0, 0, 1)) points.append(np.hstack((X, np.log(X), Z)), color=(0, 1, 0, 1)) points.append(np.hstack((X, 10**X, Z)), color=(0, 0, 1, 1)) window.attach(points["transform"]) window.attach(points["viewport"]) app.run()
def render(shape, n_samples, imgsize, fixed_z=True, show=False): pbar = tqdm.tqdm(total=n_samples, dynamic_ncols=True) root = Path(shape) root.mkdir(exist_ok=True) (root / 'no_rotation').mkdir(exist_ok=True) (root / 'no_translation').mkdir(exist_ok=True) (root / 'images').mkdir(exist_ok=True) gt = [[n_samples, 'x', 'y', 'z', 'theta', 'phi', 'gamma']] x = y = z = theta = phi = gamma = 0 with open('data.vert') as f: vertex = f.read() with open('data.frag') as f: fragment = f.read() window = app.Window(width=imgsize, height=imgsize, visible=show, color=(0.0, 0.0, 0.0, 1.00)) framebuffer = np.zeros((window.height, window.width * 3), dtype=np.uint8) if shape == 'cube': V, I, _ = create_cube() cube = gloo.Program(vertex, fragment) cube.bind(V) cube["u_light_position"] = 3, 3, 3 cube["u_light_intensity"] = 1, 1, 1 cube["u_light_ambient"] = 0.2 elif shape == 'square': V, I, _ = create_square() cube = gloo.Program(vertex, fragment) cube.bind(V) cube["u_light_position"] = 3, 3, 3 cube["u_light_intensity"] = 0, 0, 0 cube["u_light_ambient"] = 1 elif shape in shapes: V, I, _ = load_ply(shape) cube = gloo.Program(vertex, fragment) cube.bind(V) cube["u_light_position"] = 3, 3, 3 cube["u_light_intensity"] = 1, 1, 1 cube["u_light_ambient"] = 0.2 # cube['u_texture'] = mono() cube['u_model'] = np.eye(4, dtype=np.float32) cube['u_view'] = glm.translation(0, 0, -7) if shape == 'square': min_xy, max_xy = -2, 2 min_z, max_z = -1, 3 elif shape == 'cube': min_xy, max_xy = -1.7, 1.7 min_z, max_z = -3, 1.8 elif shape in shapes: min_xy, max_xy = -1.7, 1.7 min_z, max_z = -3, 1.8 frame = 0 @window.event def on_draw(dt): nonlocal frame, x, y, z, theta, phi, gamma # Export screenshot gl.glReadPixels(0, 0, window.width, window.height, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, framebuffer) if frame > 2: # Skip empty zero frame if (frame) % 3 == 0: pbar.update() gt.append( [f'{(frame-3)//3:05d}.png', x, y, z, theta, phi, gamma]) png.from_array(framebuffer, 'RGB').save( root / 'images' / f'{(frame-3)//3:05d}.png') elif (frame) % 3 == 1: png.from_array(framebuffer, 'RGB').save( root / 'no_rotation' / f'{(frame-4)//3:05d}.png') elif (frame) % 3 == 2: png.from_array(framebuffer, 'RGB').save( root / 'no_translation' / f'{(frame-5)//3:05d}.png') if (frame - 1) % 3 == 0: theta = np.random.random_sample() * 360 x, y = np.random.random_sample(2) * (max_xy - min_xy) + min_xy if not fixed_z: z = np.random.random_sample() * (max_z - min_z) + min_z if shape == 'cube': phi = np.random.random_sample() * 180 if shape in shapes: phi = np.random.random_sample() * 180 gamma = np.random.random_sample() * 360 window.clear() # Fill cube gl.glDisable(gl.GL_BLEND) gl.glEnable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_POLYGON_OFFSET_FILL) cube['u_color'] = 1, 1, 1, 1 cube.draw(gl.GL_TRIANGLES, I) # Rotate cube view = cube['u_view'].reshape(4, 4) model = np.eye(4, dtype=np.float32) if (frame - 1) % 3 != 1: glm.rotate(model, theta, 0, 0, 1) glm.rotate(model, phi, 0, 1, 0) glm.rotate(model, gamma, 1, 0, 0) # Translate cube if (frame - 1) % 3 != 2: glm.translate(model, x, y, z) cube['u_model'] = model cube['u_normal'] = np.array(np.matrix(np.dot(view, model)).I.T) frame += 1 @window.event def on_resize(width, height): cube['u_projection'] = glm.perspective(45.0, width / float(height), 2.0, 100.0) @window.event def on_init(): gl.glEnable(gl.GL_DEPTH_TEST) gl.glPolygonOffset(1, 1) gl.glEnable(gl.GL_LINE_SMOOTH) app.run(framecount=n_samples * 3 + 2, framerate=0) gt = np.array(gt) np.savetxt(root / 'target.txt', gt, delimiter='\t', fmt='%s') pbar.close()
L = np.cumsum(np.sqrt(((P[1:] - P[:-1]) ** 2).sum(axis=-1))).reshape(n - 1, 1) UV[1:, :, 0] = L UV[..., 1] = 1, -1 if closed: V[0], V[-1] = V[-3], V[2] else: V[0], V[-1] = V[1], V[-2] return V_prev, V_curr, V_next, UV, L[-1] n = 2048 T = np.linspace(0, 20 * 2 * np.pi, n, dtype=np.float32) R = np.linspace(.1, np.pi - .1, n, dtype=np.float32) X = np.cos(T) * np.sin(R) Y = np.sin(T) * np.sin(R) Z = np.cos(R) P = np.dstack((X, Y, Z)).squeeze() V_prev, V_curr, V_next, UV, length = bake(P) spiral = gloo.Program(vertex, fragment) spiral["prev"], spiral["curr"], spiral["next"] = V_prev, V_curr, V_next spiral["uv"] = UV spiral["thickness"] = 5.0 spiral["antialias"] = 1.5 spiral["linelength"] = length spiral['model'] = np.eye(4, dtype=np.float32) spiral['view'] = glm.translation(0, 0, -5) phi, theta = 0, 0 app.run() # framerate=60, framecount=360)
def render_depth(shape, imgsize, R, t): root = Path(shape) root.mkdir(exist_ok=True) vertex = """ uniform mat4 u_model; // Model matrix uniform mat4 u_view; // View matrix uniform mat4 u_projection; // Projection matrix attribute vec4 a_color; // Vertex color attribute vec3 a_position; // Vertex position varying float v_eye_depth; void main() { gl_Position = u_projection * u_view * u_model * vec4(a_position,1.0); vec3 v_eye_pos = (u_view * u_model * vec4(a_position, 1.0)).xyz; // Vertex position in eye coords. // OpenGL Z axis goes out of the screen, so depths are negative v_eye_depth = -v_eye_pos.z; } """ # Depth fragment shader fragment = """ varying float v_eye_depth; void main() { gl_FragColor = vec4(v_eye_depth, v_eye_depth, v_eye_depth, 1.0); } """ window = app.Window(width=imgsize, height=imgsize, visible=False, color=(0.0, 0.0, 0.0, 1.00)) depthbuffer = np.zeros((window.height, window.width * 3), dtype=np.float32) if shape == 'cube': V, I, _ = create_cube() cube = gloo.Program(vertex, fragment) cube.bind(V) elif shape == 'square': V, I, _ = create_square() cube = gloo.Program(vertex, fragment) cube.bind(V) elif shape in shapes: V, I, _ = load_ply(shape) cube = gloo.Program(vertex, fragment) cube.bind(V) cube['u_model'] = np.eye(4, dtype=np.float32) cube['u_view'] = glm.translation(0, 0, -7) depth = None @window.event def on_draw(dt): nonlocal depth color_buf = np.zeros((imgsize, imgsize, 4), np.float32).view(gloo.TextureFloat2D) depth_buf = np.zeros((imgsize, imgsize), np.float32).view(gloo.DepthTexture) fbo = gloo.FrameBuffer(color=color_buf, depth=depth_buf) fbo.activate() window.clear() # Fill cube gl.glDisable(gl.GL_BLEND) gl.glEnable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_POLYGON_OFFSET_FILL) gl.glClearColor(0.0, 0.0, 0.0, 0.0) # Rotate cube model = np.eye(4, dtype=np.float32) # model = R_ @ model glm.rotate(model, R[0], 0, 0, 1) glm.rotate(model, R[1], 0, 1, 0) glm.rotate(model, R[2], 1, 0, 0) # Translate cube glm.translate(model, *t) cube['u_model'] = model # cube['u_normal'] = np.array(np.matrix(np.dot(view, model)).I.T) cube.draw(gl.GL_TRIANGLES, I) # depth = np.zeros((shape[0], shape[1], 4), dtype=np.float32) # gl.glReadPixels(0, 0, shape[1], shape[0], gl.GL_RGBA, gl.GL_FLOAT, depth) # depth.shape = shape[0], shape[1], 4 # depth = depth[::-1, :] # depth = depth[:, :, 0] # Depth is saved in the first channel # Export screenshot gl.glReadPixels(0, 0, window.width, window.height, gl.GL_RGB, gl.GL_FLOAT, depthbuffer) # png.from_array(np.floor((depthbuffer - 0) / depthbuffer.max() * 255).astype(np.uint8), # 'RGB').save('./images' + # f'depth{time.time()}.png') fbo.deactivate() fbo.delete() depth = depthbuffer.reshape((128, 128, 3))[::-1, :, 0] @window.event def on_resize(width, height): cube['u_projection'] = glm.perspective(45.0, width / float(height), .1, 100.0) @window.event def on_init(): gl.glEnable(gl.GL_DEPTH_TEST) gl.glPolygonOffset(1, 1) gl.glEnable(gl.GL_LINE_SMOOTH) app.run(framecount=1, framerate=0) window.close() return depth
#! /usr/bin/env python # -*- coding: utf-8 -*- # ----------------------------------------------------------------------------- # Copyright (c) 2014, Nicolas P. Rougier # Distributed under the (new) BSD License. See LICENSE.txt for more info. # ----------------------------------------------------------------------------- from glumpy import app window = app.Window() @window.event def on_draw(dt): window.clear() app.run(interactive=True) print("Try to type 'window.color = (1,1,1,1)' in the console")
def run(fps=60, duration=None): kwargs = {'framerate': fps} if duration is not None: kwargs['duration'] = duration app.run(**kwargs)
cube.draw(gl.GL_TRIANGLES, faces) # 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['model'] = model @window.event def on_resize(width, height): cube['projection'] = glm.perspective(45.0, width / float(height), 2.0, 100.0) vertices, faces = primitives.cube() cube = gloo.Program(vertex, fragment) cube.bind(vertices) view = np.eye(4, dtype=np.float32) glm.translate(view, 0, 0, -3) cube['view'] = view cube['model'] = np.eye(4, dtype=np.float32) cube['texture'] = data.checkerboard() phi, theta = 0, 0 duration = 5.0 framerate = 60 with record(window, "cube.mp4", fps=framerate): app.run(framerate=framerate, duration=duration)
def render(self, r, t, light_position, light_intensity, brightness_k=0, r_type='quat', K=None): ''' :param r: :param t: :param light_position: :param light_intensity: :param brightness_k: choose which brightness in __init__ :param r_type: :return: ''' if r_type == 'quat': R = quat2mat(r) elif r_type == 'mat': R = r self.brightness_k = brightness_k self.render_kernels[brightness_k]['u_view'] = self._get_view_mtx(R, t) self.render_kernels[brightness_k]['u_light_position'] = light_position self.render_kernels[brightness_k]['u_normal'] = np.array( np.matrix( np.dot( self.render_kernels[brightness_k]['u_view'].reshape(4, 4), self.render_kernels[brightness_k]['u_model'].reshape( 4, 4))).I.T) self.render_kernels[brightness_k][ 'u_light_intensity'] = light_intensity if K is not None: u_projection = self.my_compute_calib_proj(K, self.width, self.height, self.zNear, self.zFar) self.render_kernels[brightness_k]['u_projection'] = np.copy( u_projection) app.run(framecount=0) rgb_buffer = np.zeros((self.height, self.width, 4), dtype=np.float32) gl.glReadPixels(0, 0, self.width, self.height, gl.GL_RGBA, gl.GL_FLOAT, rgb_buffer) rgb_gl = np.copy(rgb_buffer) rgb_gl.shape = 480, 640, 4 rgb_gl = rgb_gl[::-1, :] rgb_gl = np.round(rgb_gl[:, :, :3] * 255).astype( np.uint8) # Convert to [0, 255] bgr_gl = rgb_gl[:, :, [2, 1, 0]] depth_buffer = np.zeros((self.height, self.width), dtype=np.float32) gl.glReadPixels(0, 0, self.width, self.height, gl.GL_DEPTH_COMPONENT, gl.GL_FLOAT, depth_buffer) depth_gl = np.copy(depth_buffer) depth_gl.shape = 480, 640 depth_gl = depth_gl[::-1, :] depth_bg = depth_gl == 1 depth_gl = 2 * self.zFar * self.zNear / (self.zFar + self.zNear - (self.zFar - self.zNear) * (2 * depth_gl - 1)) depth_gl[depth_bg] = 0 return bgr_gl, depth_gl
def run(self): app.run()
#! python # ----------------------------------------------------------------------------- # Copyright (c) 2009-2016 Nicolas P. Rougier. All rights reserved. # Distributed under the (new) BSD License. # ----------------------------------------------------------------------------- import numpy as np from glumpy import app, gl from glumpy.ext import png window = app.Window(color=(1,0,0,1)) framebuffer = np.zeros((window.height, window.width * 3), dtype=np.uint8) @window.event def on_draw(dt): window.clear() gl.glReadPixels(0, 0, window.width, window.height, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, framebuffer) png.from_array(framebuffer, 'RGB').save('screenshot'+ str(dt) +'.png') app.run(framecount=5)
duration -= dt else: writer.close() writer = None # 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['model'] = model @window.event def on_resize(width, height): cube['projection'] = glm.perspective(45.0, width / float(height), 2.0, 100.0) vertices, faces = primitives.cube() cube = gloo.Program(vertex, fragment) cube.bind(vertices) view = np.eye(4, dtype=np.float32) glm.translate(view, 0, 0, -3) cube['view'] = view cube['model'] = np.eye(4, dtype=np.float32) cube['texture'] = data.checkerboard() phi, theta = 0, 0 app.run(framerate=framerate)
print('Key released (symbol=%s, modifiers=%s)' % (symbol, modifiers)) @window.event def on_mouse_press(x, y, button): print('Mouse button pressed (x=%.1f, y=%.1f, button=%d)' % (x, y, button)) @window.event def on_mouse_release(x, y, button): print('Mouse button released (x=%.1f, y=%.1f, button=%d)' % (x, y, button)) @window.event def on_mouse_motion(x, y, dx, dy): print('Mouse motion (x=%.1f, y=%.1f, dx=%.1f, dy=%.1f)' % (x, y, dx, dy)) @window.event def on_mouse_drag(x, y, dx, dy, button): print('Mouse drag (x=%.1f, y=%.1f, dx=%.1f, dy=%.1f, button=%d)' % (x, y, dx, dy, button)) @window.event def on_mouse_scroll(x, y, dx, dy): print('Mouse scroll (x=%.1f, y=%.1f, dx=%.1f, dy=%.1f)' % (x, y, dx, dy)) app.run(framerate=10)
@window.event def on_character(character): print('Character entered (chracter: %s)'% character) @window.event def on_key_release(symbol, modifiers): print('Key released (symbol=%s, modifiers=%s)'% (symbol,modifiers)) @window.event def on_mouse_press(x, y, button): print('Mouse button pressed (x=%.1f, y=%.1f, button=%d)' % (x,y,button)) @window.event def on_mouse_release(x, y, button): print('Mouse button released (x=%.1f, y=%.1f, button=%d)' % (x,y,button)) @window.event def on_mouse_motion(x, y, dx, dy): pass #print('Mouse motion (x=%.1f, y=%.1f, dx=%.1f, dy=%.1f)' % (x,y,dx,dy)) @window.event def on_mouse_drag(x, y, dx, dy, button): print('Mouse drag (x=%.1f, y=%.1f, dx=%.1f, dy=%.1f, button=%d)' % (x,y,dx,dy,button)) @window.event def on_mouse_scroll(x, y, dx, dy): print('Mouse scroll (x=%.1f, y=%.1f, dx=%.1f, dy=%.1f)' % (x,y,dx,dy)) app.run(framerate=10)
def render(model, im_size, K, R, t, clip_near=100, clip_far=2000, texture=None, surf_color=None, bg_color=(0.0, 0.0, 0.0, 0.0), ambient_weight=0.5, shading='flat', mode='rgb+depth'): # Process input data #--------------------------------------------------------------------------- # Make sure vertices and faces are provided in the model assert({'pts', 'faces'}.issubset(set(model.keys()))) # Set texture / color of vertices if texture is not None: if texture.max() > 1.0: texture = texture.astype(np.float32) / 255.0 texture = np.flipud(texture) texture_uv = model['texture_uv'] colors = np.zeros((model['pts'].shape[0], 3), np.float32) else: texture_uv = np.zeros((model['pts'].shape[0], 2), np.float32) if not surf_color: if 'colors' in model.keys(): assert(model['pts'].shape[0] == model['colors'].shape[0]) colors = model['colors'] if colors.max() > 1.0: colors /= 255.0 # Color values are expected in range [0, 1] else: colors = np.ones((model['pts'].shape[0], 3), np.float32) * 0.5 else: colors = np.tile(list(surf_color) + [1.0], [model['pts'].shape[0], 1]) # Set the vertex data if mode == 'depth': vertices_type = [('a_position', np.float32, 3), ('a_color', np.float32, colors.shape[1])] vertices = np.array(list(zip(model['pts'], colors)), vertices_type) else: if shading == 'flat': vertices_type = [('a_position', np.float32, 3), ('a_color', np.float32, colors.shape[1]), ('a_texcoord', np.float32, 2)] vertices = np.array(list(zip(model['pts'], colors, texture_uv)), vertices_type) else: # shading == 'phong' vertices_type = [('a_position', np.float32, 3), ('a_normal', np.float32, 3), ('a_color', np.float32, colors.shape[1]), ('a_texcoord', np.float32, 2)] vertices = np.array(list(zip(model['pts'], model['normals'], colors, texture_uv)), vertices_type) # Rendering #--------------------------------------------------------------------------- render_rgb = mode in ['rgb', 'rgb+depth'] render_depth = mode in ['depth', 'rgb+depth'] # Model matrix mat_model = np.eye(4, dtype=np.float32) # From object space to world space # View matrix (transforming also the coordinate system from OpenCV to # OpenGL camera space) mat_view = np.eye(4, dtype=np.float32) # From world space to eye space mat_view[:3, :3], mat_view[:3, 3] = R, t.squeeze() yz_flip = np.eye(4, dtype=np.float32) yz_flip[1, 1], yz_flip[2, 2] = -1, -1 mat_view = yz_flip.dot(mat_view) # OpenCV to OpenGL camera system mat_view = mat_view.T # OpenGL expects column-wise matrix format # Projection matrix mat_proj = _compute_calib_proj(K, 0, 0, im_size[0], im_size[1], clip_near, clip_far) # Create buffers vertex_buffer = vertices.view(gloo.VertexBuffer) index_buffer = model['faces'].flatten().astype(np.uint32).view(gloo.IndexBuffer) # Create window # config = app.configuration.Configuration() # Number of samples used around the current pixel for multisample # anti-aliasing (max is 8) # config.samples = 8 # config.profile = "core" # window = app.Window(config=config, visible=False) window = app.Window(visible=False) global rgb, depth rgb = None depth = None @window.event def on_draw(dt): window.clear() shape = (im_size[1], im_size[0]) if render_rgb: # Render color image global rgb rgb = draw_color(shape, vertex_buffer, index_buffer, texture, mat_model, mat_view, mat_proj, ambient_weight, bg_color, shading) if render_depth: # Render depth image global depth depth = draw_depth(shape, vertex_buffer, index_buffer, mat_model, mat_view, mat_proj) app.run(framecount=0) # The on_draw function is called framecount+1 times window.close() # Set output #--------------------------------------------------------------------------- if mode == 'rgb': return rgb elif mode == 'depth': return depth elif mode == 'rgb+depth': return rgb, depth else: print('Error: Unknown rendering mode.') exit(-1)
UV += np.random.uniform(0.00, 0.01, (h, w, 4)) UV[:, :, 2] = UV[:, :, 0] UV[:, :, 3] = UV[:, :, 1] pingpong = 1 compute = gloo.Program(compute_vertex, compute_fragment, count=4) compute["params"] = P compute["texture"] = UV compute["texture"].interpolation = gl.GL_NEAREST compute["texture"].wrapping = gl.GL_CLAMP_TO_EDGE compute["position"] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)] compute["texcoord"] = [(0, 0), (0, 1), (1, 0), (1, 1)] compute['dt'] = dt compute['dx'] = 1.0 / w compute['dy'] = 1.0 / h compute['dd'] = dd compute['pingpong'] = pingpong render = gloo.Program(render_vertex, render_fragment, count=4) render["position"] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)] render["texcoord"] = [(0, 0), (0, 1), (1, 0), (1, 1)] render["texture"] = compute["texture"] render["texture"].interpolation = gl.GL_LINEAR render["texture"].wrapping = gl.GL_CLAMP_TO_EDGE render['pingpong'] = pingpong framebuffer = gloo.FrameBuffer(color=compute["texture"], depth=gloo.DepthBuffer(w, h)) app.run(framerate=0)
@window.event def on_mouse_scroll(x, y, dx, dy): global translate, scale _, _, w, h = gl.glGetIntegerv(gl.GL_VIEWPORT) y = h - y s = min(max(0.25, scale + 0.01 * dy * scale), 200) translate[0] = x - s * (x - translate[0]) / scale translate[1] = y - s * (y - translate[1]) / scale translate = [translate[0], translate[1]] scale = s program["u_translate"] = translate program["u_scale"] = scale program = gloo.Program(vertex, fragment, count=4) program["a_position"] = (-1, -1), (-1, +1), (+1, -1), (+1, +1) program["a_texcoord"] = (0, 0), (0, +1), (+1, 0), (+1, +1) program["u_grid_color"] = 0, 0, 0, 1 program["u_grid_thickness"] = 1 program["u_grid_antialias"] = 1 program["u_translate"] = 0, 0 program["u_scale"] = 1.0 program["u_size"] = 512, 512 translate = [0, 0] scale = 1 app.run()
#! /usr/bin/env python # -*- coding: utf-8 -*- # ----------------------------------------------------------------------------- # Copyright (c) 2014, Nicolas P. Rougier # Distributed under the (new) BSD License. See LICENSE.txt for more info. # ----------------------------------------------------------------------------- import numpy as np from glumpy import app, gl from glumpy.ext import png window = app.Window(color=(1, 0, 0, 1)) framebuffer = np.zeros((window.height, window.width * 3), dtype=np.uint8) @window.event def on_draw(dt): window.clear() gl.glReadPixels(0, 0, window.width, window.height, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, framebuffer) png.from_array(framebuffer, "RGB").save("screenshot.png") app.run(framecount=1)