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))
def on_draw(dt): global alpha window.clear() gl.glEnable(gl.GL_BLEND) gl.glDisable(gl.GL_DEPTH_TEST) for i in range(len(image_info)): img = image_info[i] if img is not None: modelmat = np.eye(4, dtype=np.float32) if i > 0: alpha = min(1.0, alpha + dt * 0.4) print('current alpha: {}'.format(alpha)) program['alpha'] = alpha # min(0.6, alpha) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) # target position. front_center = face_rect_center(img['face_rect']) back_center = face_rect_center(image_info[i - 1]['face_rect']) translate = back_center - front_center # print('translation: {}, front center: {}, back center: {}'.format(translate, front_center, back_center)) translate = mix(np.zeros(2, dtype=np.float32), translate, alpha) T2 = glm.translation(translate[0], translate[1], 0.0) T1 = np.matrix( glm.translation(front_center[0], front_center[1], 0.0)) # target scale. front_sz = face_rect_size(img['face_rect']) back_sz = face_rect_size(image_info[i - 1]['face_rect']) scale = mix(1.0, back_sz / front_sz, alpha) S = np.eye(4, dtype=np.float32) S = glm.scale(S, scale, scale, 1.0) modelmat = T1.I * S * T1 * T2 else: program['alpha'] = 1.0 - alpha program['u_model'] = modelmat program['position'] = img['image_rect'] program['tex'] = img['image'].view(gloo.Texture2D) program['u_lineflag'] = 0.0 program.draw(gl.GL_TRIANGLE_STRIP) program['position'] = img['face_rect'] program['u_lineflag'] = 1.0 program['u_linecolor'] = [1.0, 0.0, 0.0] program.draw(gl.GL_LINE_LOOP)
def update(self, dt=None): if not self['visible']: return if 'physics' in self.entity.components: pos = self.entity.physics.position self['program_shader']['u_model'] = glm.translation( pos[0], pos[1], pos[2]) self['program_shader'].draw(self['draw_type'], self['index_buffer'])
def set_camera_position(self, x, y, z): self._camera_view = glm.translation(x, y, z) self._camera_pos = x, y, z for model_info in self._model_infos: program = model_info.program program['u_view'] = self._camera_view program['u_view_pos'] = self._camera_pos
def criar_obj(v_list,i_list): global vertex_list,index_list,obj vertex_list = np.zeros(len(v_list),[("a_position", np.float32, 3)]) vertex_list['a_position'] = v_list vertex_list= vertex_list.view(gloo.VertexBuffer) index_list = np.array(i_list,dtype=np.uint32) index_list=index_list.view(gloo.IndexBuffer) obj= gloo.Program(vertex, fragment) obj.bind(vertex_list) obj['u_view'] = glm.translation(0, 0, -5)
def __init__(self, hand: Hand, direction): self.should_apply_force = False self.fist_threshold = 1 self.palm_open_count_max = 1 self.palm_open_count = 0 self.hand = hand # store a reference to the hand self.base_left = np.array([-0.8, 0.6, .6]) # left palm base position self.base_right = np.array([0.8, 0, 0]) # rhgt palm base position self.debug_cube = HollowCube(glm.translation(0, -2, -10), np.eye(4, dtype=np.float32)) self.cube_scale = 2 self.direction = direction
def on_draw(dt): window.clear() gl.glDisable(gl.GL_BLEND) gl.glEnable(gl.GL_DEPTH_TEST) program.draw(gl.GL_TRIANGLES, indices) #model = np.eye(4, dtype=np.float32) #program['u_model'] = model #program['u_model'] = glm.xrotate(glm.translation(0, 0, posx), angleR) #program['u_model'] = np.eye(4) program['u_model'] = glm.zrotate(np.eye(4), angleR).dot(glm.translation(0, 0, posx))
def get_key_point_transform(self, position, caller): """ From position to transformation matrix Apply corresponding scale first :param position: len 3 np.array of the new positions to be updated :param caller: caller name, defined in self.component_names """ if caller == "arm": return glm.translation(*position) else: return glm.translate( glm.scale(np.eye(4, dtype=np.float32), self.finger_scale, self.finger_scale, self.finger_scale), *position)
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))
def __init__(self, data=None, name='ProgramSV3D', point_size=1): data = data.view(gloo.VertexBuffer) program = gloo.Program(vertexPoint, fragmentSelect) program.bind(data) program['color'] = (1, 0, 0, 1) program['color_sel'] = 1 program['u_model'] = np.eye(4, dtype=np.float32) program['u_view'] = glm.translation(0, 0, -50) program['a_pointSize'] = point_size self.name = name self.program = program self.draw_mode = gl.GL_POINTS self.u_model, self.u_view, self.u_projection = np.eye(4, dtype=np.float32), np.eye(4, dtype=np.float32), np.eye( 4, dtype=np.float32) self.lat, self.lon, self.yaw = 0, 0, 0
def make_dice(dice, image_count): global cube_prog, indices, phi, theta, dice_val, count count = image_count dice_val = dice vertices, indices = cube() cube_prog = gloo.Program(vertex, fragment) cube_prog.bind(vertices) cube_prog["u_light_position"] = (.5, .5, .5) cube_prog["u_light_intensity"] = 3, 3, 3 #cube['u_texture'] = data.get("crate.png") t = cv2.imread("base_images/dice_" + str(dice) + "_rgb.png", cv2.IMREAD_UNCHANGED) cube_prog['u_texture'] = t cube_prog['u_model'] = np.eye(4, dtype=np.float32) cube_prog['u_view'] = glm.translation(0, 0, -5) phi, theta = 400, 300 app.run()
def __init__(self, data=None, name='ProgramPlane', face=None): data = data.view(gloo.VertexBuffer) #program = gloo.Program(vertexPoint, fragmentSelect) program = gloo.Program(vertexPoint, fragmentAlpha) program.bind(data) #program['color'] = (1, 0, 0, 1) #program['color_sel'] = 1 program['alpha'] = 1.0 program['u_model'] = np.eye(4, dtype=np.float32) program['u_view'] = glm.translation(0, 0, -50) self.name = name self.program = program self.draw_mode = gl.GL_TRIANGLES self.u_model, self.u_view, self.u_projection = np.eye(4, dtype=np.float32), np.eye(4, dtype=np.float32), np.eye( 4, dtype=np.float32) self.lat, self.lon, self.yaw = 0, 0, 0 self.face = face.view(gloo.IndexBuffer)
def update(self): if self.mesh != self.internal_mesh: self.load_mesh(mesh_dict[self.mesh]) self.internal_mesh = self.mesh # The model is rotated because the texturing is the wrong way around self.shader["model"] = self.model_matrix self.shader["view"] = glm.translation(0, 0, self.view_distance) # Scale is increased to fill the whole screen self.shader["scale"] = 1.0 self.shader["tex"] = to_tex_format(self.texture) width, height = self.window_size self.shader["projection"] = glm.perspective( # 45.0, 1.0, 2.0, 100.0 45.0, width / float(height), 2.0, 100.0, )
def bind_obj(self, obj): # make obj vertex = """ uniform vec4 ucolor; uniform mat4 model; uniform mat4 view; uniform mat4 projection; attribute vec3 position; attribute vec4 color; varying vec4 v_color; void main() { v_color = ucolor * color; gl_Position = projection * view * model * vec4(position,1.0); } """ fragment = """ varying vec4 v_color; void main() { gl_FragColor = v_color; } """ VertexBuffer = obj.VertexBuffer outline = obj.outline point_idx = obj.point_idx program = gloo.Program(vertex, fragment) program.bind(VertexBuffer) program['model'] = np.eye(4, dtype=np.float32) program['view'] = glm.translation(0, 0, -5) VertexBuffer.activate() VertexBuffer.deactivate() self.RegisteredBuffer = gw.make_RegisteredBuffer(VertexBuffer) self.program = program self.outline = outline self.point_idx = point_idx
def draw(self, r: gloo.Program, dt: float): super(GG2Level, self).draw(r, dt) r[TEXTURE_MATRIX] = np.matmul(self._tex_mat, glm.translation(0, 0.5, 0)) r.bind(self._bg.buffer) r.draw(self._bg.primitive, self._bg.indices)
def opengl(): # colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (0, 0, 0)] 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; varying vec3 v_color; void main() { v_color = u_color; gl_Position = projection * view * model * vec4(position,1.0); } """ fragment = """ varying vec3 v_color; void main() { gl_FragColor = vec4(v_color, 1.0); } """ vertex2 = """ attribute vec3 position; attribute vec2 texcoord; varying vec2 v_texcoord; void main() { gl_Position = vec4(position,1.0); v_texcoord = texcoord; } """ fragment2 = """ uniform sampler2D texture; varying vec2 v_texcoord; void main() { gl_FragColor = texture2D(texture, v_texcoord); } """ vertex3 = """ attribute vec3 last_position; attribute float side; uniform mat4 projection; // Projection matrix uniform mat4 inv_transform; attribute vec3 position; // Vertex position attribute vec3 u_color; varying vec3 v_color; void main() { vec3 line_vec; line_vec = position - last_position; vec3 normal; // normal = vec3(0.0, line_vec.y, -line_vec.y*line_vec.y/line_vec.z); normal = vec3(-line_vec.y, line_vec.x, 0.0); normal = normal/(sqrt(normal.x*normal.x + normal.y*normal.y))*0.8; vec3 new_position; new_position = position + normal; vec3 new_line_vec; new_line_vec = new_position - last_position; float multiplier; multiplier = 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; } } new_position = position + normal*multiplier; new_position = (inv_transform * vec4(new_position, 1)).xyz; 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.y, -new_position.z, -new_position.x, 1.0); v_color = u_color; gl_Position = projection * pos * vec4(0.0, 0.0, 0.0, 1.0); } """ 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) bezier_I = list() for i in range(4 * 25 * 2 - 2): bezier_I += [i, i + 1, i + 2] bezier_I = np.array(bezier_I, dtype=np.uint32) bezier_I.view(gloo.IndexBuffer) 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) 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 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) path["position"] = np.zeros((4 * 25 * 2, 3)) path["last_position"] = np.zeros((4 * 25 * 2, 3)) path["side"] = np.zeros(4 * 25 * 2) path["u_color"] = 0, 0.5, 0 window = app.Window(width=img_width, height=img_height, color=(1, 1, 1, 1)) @window.event def on_draw(dt): global phi, theta window.clear() lock.acquire() try: gl.glDisable(gl.GL_DEPTH_TEST) # try: # quad['texture'] = cam_img_texture # except: # quad['texture'] = cv.imread("/home/rahul/Pictures/GitKraken_001.png")[..., ::-1] quad.draw(gl.GL_TRIANGLE_STRIP) gl.glEnable(gl.GL_DEPTH_TEST) # Filled cube path["u_color"] = 0, 1, 1 path.draw(gl.GL_TRIANGLE_STRIP) gl.glDepthMask(gl.GL_FALSE) path["u_color"] = 0, 0, 0 gl.glLineWidth(7.5) path.draw(gl.GL_LINES, bline_I) gl.glLineWidth(1.0) gl.glDepthMask(gl.GL_TRUE) 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 obj.draw(gl.GL_TRIANGLES, I) gl.glDepthMask(gl.GL_FALSE) obj["u_color"] = 0, 0, 0 obj.draw(gl.GL_LINES, O) gl.glDepthMask(gl.GL_TRUE) obj["model"] = model # cube.draw(gl.GL_TRIANGLES, I) # Make cube rotate theta += 2.0 # degrees phi += 2.0 # degrees finally: lock.release() @window.event def on_resize(width, height): 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 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 # print(glm.perspective(45.0, width / float(height), 2.0, 100.0)) @window.event def on_init(): gl.glEnable(gl.GL_DEPTH_TEST) app.run(framerate=30)
def callback_camera(self, camera_feed): # global variables that are described as follows # path - glumpy object for augmented reality path in OpenGL # lock - global lock as described above # quad - glumpy object for background texture given by camera topic global path, lock, quad lock.acquire() try: # try to update background texture or return to ros loop if fails quad["texture"] = self.bridge.imgmsg_to_cv2(camera_feed) except: return finally: lock.release() # coords_3d is a numpy arraythat is stores 3d_ccordinates of all objects and is later extrapolated to a curve that # represents the path coords_3d = np.zeros((len(bag), 3)) for i in range(len(bag)): try: # get the transform of teh objects w.r.t the drone camera and update coords_3d with the coordinates trans = self.tfBuffer.lookup_transform("camera_base_link", "object%s" % str(i), rospy.Time()) coords_3d[i] = [ trans.transform.translation.x, trans.transform.translation.y, trans.transform.translation.z, ] except ( tf2_ros.LookupException, tf2_ros.ConnectivityException, tf2_ros.ExtrapolationException, ): return lock.acquire() try: # Define the view matrix of the objects w.r.t the drone camera bag[i]["view"] = glm.translation( -trans.transform.translation.y, 0.0, -trans.transform.translation.x) except: return finally: lock.release() # Interpolate the coordinates in coords 3d to define the curve tck, u_ = interpolate.splprep(coords_3d.T, s=0.0) bez_x, bez_y, bez_z = np.array( interpolate.splev(np.linspace(0, 1, 25 * 4), tck)) # Specific index in position defines the current point on the curve # Specific index in position defines the previous point on the curve # Specific index in side represents left or right, namely -1 or +1 position = list() last_position = list() side = list() # Initialise for first position with corresponding last position as itself because it has no predecessor position.append([-bez_y[1], 0.8, -bez_x[1]]) position.append([-bez_y[1], 0.8, -bez_x[1]]) last_position.append([-bez_y[1], 0.8, -bez_x[1]]) last_position.append([-bez_y[1], 0.8, -bez_x[1]]) side += [-1.0, 1.0] # For each point on the curve append itself and its predecssor twice, once for each side for i in range(1, len(bez_x)): position.append([-bez_y[i], 0.8, -bez_x[i]]) position.append([-bez_y[i], 0.8, -bez_x[i]]) last_position.append([-bez_y[i - 1], 0.8, -bez_x[i - 1]]) last_position.append([-bez_y[i - 1], 0.8, -bez_x[i - 1]]) side += [-1.0, 1.0] # update the position, last_position and side list to the path object in glumpy lock.acquire() try: path["position"] = position path["last_position"] = last_position path["side"] = side except: return finally: lock.release()
gl.glDisable(gl.GL_BLEND) gl.glEnable(gl.GL_DEPTH_TEST) cube.draw(gl.GL_TRIANGLES, indices) # Rotate cube 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.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) vertices, indices = cube() cube = gloo.Program(vertex, fragment) cube.bind(vertices) cube['u_texture'] = data.get("crate.png") cube['u_model'] = np.eye(4, dtype=np.float32) cube['u_view'] = glm.translation(0, 0, -5) phi, theta = 40, 30 app.run()
fragment = """ varying vec4 v_color; // Interpolated fragment color (in) void main() { gl_FragColor = v_color; } """ # ----------------------------------------------------------------------------- # OpenGL # ----------------------------------------------------------------------------- program = gloo.Program(vertex, fragment) program.bind(V) program['u_model'] = mats[0] program['u_view'] = glm.translation(0, 0, -1200) window = app.Window(width=1200, height=1200, color=(1, 1, 1, 1)) k = 0 @window.event def on_draw(dt): # index global k V["a_position"][:-8, :] = deforms[k] program['u_model'] = mats[k] k = (k + 1) % n_deforms window.clear() # mesh
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()
@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) V = np.zeros(8, [("a_position", np.float32, 3)]) V["a_position"] = [[1, 1, 1], [-1, 1, 1], [-1, -1, 1], [1, -1, 1], [1, -1, -1], [1, 1, -1], [-1, 1, -1], [-1, -1, -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) cube = gloo.Program(vertex, fragment) cube.bind(V) cube['u_model'] = np.eye(4, dtype=np.float32) cube['u_view'] = glm.translation(0, 0, -10) phi, theta = 40, 30 app.run()
def __init__(self): # ! Actual init definitions, want more bones? Add them here # the names of the fingers, with order # Note: for both fingers and arms, the joints all starts from your heart and goes to your tips, for example, with arm, the first element is the cloest to your heart, which is the elbow, then wrist, then plam self.finger_names = ["thumb", "index", "middle", "ring", "pinky"] self.arm_names = ["arm"] # all components of a hand, including arm, fingers self.component_names = self.arm_names + self.finger_names # Leap Motion subscription of arm keypoints self.arm_pos_names = ["elbow", "wrist", "palmPosition"] # Leap Motion subscription of finger keypoints # metacarpal, proximal, middle, distal self.finger_pos_names = [ "carpPosition", "mcpPosition", "pipPosition", "dipPosition", "btipPosition" ] # ! Derived information from the above definitions self.name_to_pos_names = { **{name: self.arm_pos_names for name in self.arm_names}, **{name: self.finger_pos_names for name in self.finger_names} } # number of key points of the fingers self.finger_key_pt_count = len(self.finger_pos_names) * len( self.finger_names) # number of key points of the arm self.arm_key_pt_count = len(self.arm_pos_names) # mapper from all finger names and "arm" to their index in the position list self.name_to_index = {} arm_name_count = len(self.arm_pos_names) finger_name_count = len(self.finger_pos_names) index = 0 self.name_to_index["arm"] = [index, index + arm_name_count] index += arm_name_count for name in self.finger_names: self.name_to_index[name] = [index, index + finger_name_count] index += finger_name_count # ! OpenGL controls # global camera view transformation self.u_view = glm.translation(0, -2, -10) # global finger key point scale relative to arm self.finger_scale = 0.5 # global bones scale relative to finger self.bone_scale = 0.67 # show_type: 1: only joints, 2: joints + bones, 0: bones self.show_type = 0 # OpenGL objects # actual OpenGL object wrapper of all key points, reused self.key_point = HollowCube(self.u_view, np.eye(4, dtype=np.float32)) self.bone = HollowCube(self.u_view, np.eye(4, dtype=np.float32)) # ! Actual bare metal data # keypoint position list, queried every frame update for new keypoint position # websockt process should update this list instead of the raw OpenGL obj self.pos = np.array([ np.zeros(3, np.float32) for _ in range(self.finger_key_pt_count + self.arm_key_pt_count) ]) # Extra information to be remembered in the history # The normal vector of the palm self.palm_normal = np.zeros(3, np.float32) # timestamp self.timestamp = 256101634501 # currently not used in parsing # ! History list # empty gesture history, updated withe new infromation from the above mentioned data self.history = []
def on_mouse_scroll(x, y, dx, dy): global zoom zoom += dy*20 program_ptCloud['u_view'] = glm.translation(0, 0, zoom)
def callback_camera(self, camera_feed): global bag, path, lock, quad, position, last_position, side lock.acquire() try: quad["texture"] = self.bridge.imgmsg_to_cv2(camera_feed)[..., ::-1] except: return finally: lock.release() transforms = np.zeros((len(bag), 3)) trans_glob = self.tfBuffer.lookup_transform("camera", "world", rospy.Time()) t = trans_glob.transform.translation r = trans_glob.transform.rotation matrix = self.transformer.fromTranslationRotation((t.x, t.y, t.z), (r.x, r.y, r.z, r.w)) for i in range(len(bag)): try: trans = self.tfBuffer.lookup_transform("world", "object%s" % str(i), rospy.Time()) transforms[i] = [ trans.transform.translation.x, trans.transform.translation.y, trans.transform.translation.z, ] except ( tf2_ros.LookupException, tf2_ros.ConnectivityException, tf2_ros.ExtrapolationException, ): return lock.acquire() try: cam = self.tfBuffer.lookup_transform("camera", "object%s" % str(i), rospy.Time()) # values = matrix.dot(np.array([cam.transform.translation.x, # cam.transform.translation.y, # cam.transform.translation.z, 1])) bag[i]["view"] = glm.translation( -cam.transform.translation.y, -cam.transform.translation.z, -cam.transform.translation.x, ) except: return finally: lock.release() tck, _ = interpolate.splprep(transforms.T, s=0.0) bez_x, bez_y, bez_z = np.array( interpolate.splev(np.linspace(0, 1, 25 * 4), tck)) position[0] = [bez_x[1], bez_y[1], bez_z[1] - 0.8] position[1] = [bez_x[1], bez_y[1], bez_z[1] - 0.8] last_position[0] = [bez_x[1], bez_y[1], bez_z[1] - 0.8] last_position[1] = [bez_x[1], bez_y[1], bez_z[1] - 0.8] side[:2] = [-1.0, 1.0] # remember to chaneg values for i in range(1, len(bez_x)): position[2 * i] = [bez_x[i], bez_y[i], bez_z[i] - 0.8] position[2 * i + 1] = [bez_x[i], bez_y[i], bez_z[i] - 0.8] last_position[2 * i] = [ bez_x[i - 1], bez_y[i - 1], bez_z[i - 1] - 0.8 ] last_position[2 * i + 1] = [ bez_x[i - 1], bez_y[i - 1], bez_z[i - 1] - 0.8 ] side[2 * i:2 * (i + 1)] = [-1.0, 1.0] lock.acquire() try: path["inv_transform"] = matrix.T path["position"] = position path["last_position"] = last_position path["side"] = side except: return finally: lock.release()
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(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()
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
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) @window.event def on_init(): gl.glEnable(gl.GL_DEPTH_TEST) gl.glPolygonOffset(1, 1) gl.glEnable(gl.GL_LINE_SMOOTH) gl.glLineWidth(0.75) vertices, faces, outline = colorcube() cube = gloo.Program(vertex, fragment) cube.bind(vertices) cube['model'] = np.eye(4, dtype=np.float32) cube['view'] = glm.translation(0, 0, -5) phi, theta = 0, 0 app.run()
@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() tic = timeit.default_timer() #sv3D.CreatePtCloud2(sphericalRay) toc = timeit.default_timer() #data_ptCloud_streetView3D = sv3D.data_ptCLoud data_ptCloud_streetView3D = data program_ptCloud = gloo.Program(shader.vertex, shader.fragment) data_ptCloud_streetView3D = data_ptCloud_streetView3D.view(gloo.VertexBuffer) program_ptCloud.bind(data_ptCloud_streetView3D) #program_ptCloud['a_position'] = data_ptCloud_streetView3D['a_position'] #program_ptCloud['a_color'] = data_ptCloud_streetView3D['a_color'] program_ptCloud['color'] = 1, 0, 0, 1 program_ptCloud['color_sel'] = 1 program_ptCloud['u_model'] = np.eye(4, dtype=np.float32) program_ptCloud['u_view'] = glm.translation(0, 0, -200) phi, theta, size, zoom = 0, 0, 1, -200 tx, ty = 0, 0 widowSetting()
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)
@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() tic=timeit.default_timer() #sv3D.CreatePtCloud2(sphericalRay) toc=timeit.default_timer() #data_ptCloud_streetView3D = sv3D.data_ptCLoud data_ptCloud_streetView3D = data program_ptCloud = gloo.Program(shader.vertex, shader.fragment) data_ptCloud_streetView3D = data_ptCloud_streetView3D.view(gloo.VertexBuffer); program_ptCloud.bind(data_ptCloud_streetView3D) #program_ptCloud['a_position'] = data_ptCloud_streetView3D['a_position'] #program_ptCloud['a_color'] = data_ptCloud_streetView3D['a_color'] program_ptCloud['color'] = 1,0,0,1 program_ptCloud['color_sel'] = 1 program_ptCloud['u_model'] = np.eye(4, dtype=np.float32) program_ptCloud['u_view'] = glm.translation(0, 0, -200) phi, theta, size, zoom = 0, 0, 1, -200 tx, ty = 0, 0 widowSetting();
gl.glDepthMask(gl.GL_TRUE) # 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) @window.event def on_init(): gl.glEnable(gl.GL_DEPTH_TEST) gl.glPolygonOffset(1, 1) gl.glEnable(gl.GL_LINE_SMOOTH) gl.glLineWidth(0.75) vertices, faces, outline = colorcube() cube = gloo.Program(vertex, fragment) cube.bind(vertices) cube['model'] = np.eye(4, dtype=np.float32) cube['view'] = glm.translation(0, 0, -15) phi, theta = 0, 0 app.run()
def draw(vertices, indexes, color=None) -> None: vertex = """ attribute vec3 position; attribute vec4 color; uniform mat4 model; uniform mat4 view; uniform mat4 projection; varying vec4 v_color; void main() { gl_Position = projection * view * model * vec4(position, 1.0); v_color = color; } """ fragment = """ varying vec4 v_color; void main() { gl_FragColor = v_color; } """ window = app.Window(color=(0, 0, 0, 1)) phi = 0.0 theta = 0.0 omega = 0.0 dx = 0.0 dy = 0.0 dz = 0.0 scale = 1.0 d = 2 blue = [0.0, 0.0, 1.0, 1.0] red = [1.0, 0.0, 0.0, 1.0] if not color: color = [red for _ in range(len(vertices))] @window.event def on_init(): gl.glEnable(gl.GL_DEPTH_TEST) gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_LINE) @window.event def on_resize(width, height): cube['projection'] = glm.perspective(45.0, width / float(height), 2.0, 100.0) @window.event def on_draw(dt): nonlocal phi, theta, omega, dx, dy, dz, scale, d model = np.eye(4, 4, dtype=np.float32) glm.xrotate(model, omega) glm.yrotate(model, phi) glm.zrotate(model, theta) glm.scale(model, scale, scale, scale) glm.translate(model, dx, dy, dz) cube['model'] = model cube['color'] = color window.clear() cube.draw(gl.GL_TRIANGLES, I) @window.event def on_key_press(symbol, modifiers): nonlocal phi, theta, omega, dx, dy, dz, scale, d if symbol == 45 and modifiers == 1: # shift - scale -= 0.1 elif symbol == 61 and modifiers == 1: # shit + scale += 0.1 elif symbol == 81: # Q theta += 10.0 elif symbol == 69: # E theta -= 10.0 elif symbol == 87: # W dy += 0.1 elif symbol == 65: # A dx -= 0.1 elif symbol == 83: # S dy -= 0.1 elif symbol == 68: # D dx += 0.1 elif symbol == 90: # Z phi += 10.0 elif symbol == 67: # C phi -= 10.0 elif symbol == 73: # I omega += 10.0 elif symbol == 80: # P omega -= 10.0 elif symbol == 76: # L d += 0.1 elif symbol == 75: # K d -= 0.1 elif symbol == 32: # space phi = 0.0 theta = 0.0 omega = 0.0 dx = 0.0 dy = 0.0 dz = 0.0 scale = 1.0 d = 2 color[color.index(blue)] = red elif symbol == 71: # G if blue not in color: color[0] = blue else: color.insert(0, color.pop()) print(color.index(blue)) V = np.zeros(len(vertices), [('position', np.float32, 3)]) V['position'] = vertices V = V.view(gloo.VertexBuffer) I = np.array(indexes, dtype=np.uint32) I = I.view(gloo.IndexBuffer) cube = gloo.Program(vertex, fragment) cube['view'] = glm.translation(0, 0, -5) cube.bind(V) app.run(framerate=60)
V_prev, V_curr, V_next = V[:-2], V[1:-1], V[2:] V_curr[..., 0] = P[:, np.newaxis, 0] V_curr[..., 1] = P[:, np.newaxis, 1] V_curr[..., 2] = P[:, np.newaxis, 2] 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] def update(program, points): print(points.shape) V_prev, V_curr, V_next, UV, length = bake(points) program["prev"], program["curr"], program["next"] = V_prev, V_curr, V_next program["uv"] = UV program["linelength"] = length spiral = gloo.Program(vertex, fragment) print(P.shape) update(spiral, P) spiral["thickness"] = 5.0 spiral["antialias"] = 1.5 spiral['model'] = np.eye(4, dtype=np.float32) spiral['view'] = glm.translation(0, 0, -5) app.run() # framerate=60, framecount=360)
def plot(self, cmap=None, color_function=None): """ Method used to plot a surface. """ print("Plotting object of dimension: {}".format( self.decomposition.dimension)) data = self.decomposition tuples = data.surface.surface_sampler_data points = extract_points(data) triangle = [] for i in range(len(tuples)): for tri in tuples[i]: x_coordinate = int(tri[0]) y_coordinate = int(tri[1]) z_coordinate = int(tri[2]) point = [x_coordinate, y_coordinate, z_coordinate] triangle.append(point) triangle = np.asarray(triangle) # ----------------------------------------------------------------------------- # # Vertex and fragment are OpenGL code # ----------------------------------------------------------------------------- # vertex = """ attribute vec4 a_color; // Vertex Color uniform mat4 u_model; // Model matrix uniform mat4 u_view; // View matrix uniform mat4 u_projection; // Projection matrix attribute vec3 position; // Vertex position varying vec4 v_color; // Interpolated fragment color (out) void main() { v_color = a_color; gl_Position = <transform>; } """ fragment = """ varying vec4 v_color; // Interpolated fragment color (in) void main() { gl_FragColor = v_color; } """ window = app.Window(width=2048, height=2048, color=(0.30, 0.30, 0.35, 1.00)) verts = np.zeros(len(points), [("position", np.float32, 3), ("a_color", np.float32, 4)]) verts["position"] = points verts["a_color"] = make_colors(verts["position"], cmap, color_function) verts = verts.view(gloo.VertexBuffer) indeces = np.array(triangle).astype(np.uint32) indeces = indeces.view(gloo.IndexBuffer) surface = gloo.Program(vertex, fragment) surface.bind(verts) surface['u_model'] = np.eye(4, dtype=np.float32) surface['u_view'] = glm.translation(0, 0, -5) surface['transform'] = Trackball(Position("position"), znear=0) window.attach(surface['transform']) @window.event def on_draw(draw_triangles): window.clear() surface.draw(gl.GL_TRIANGLES, indeces) # surface.draw(gl.GL_LINES, indeces) @window.event def on_init(): gl.glEnable(gl.GL_DEPTH_TEST) gl.glPolygonOffset(1, 1) gl.glEnable(gl.GL_LINE_SMOOTH) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) app.run()