def on_render(self, gl_area, gl_context): glClearColor(0.0, 0.0, 0.0, 0.0) # Set the background colour for the window -> Black glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) # Clear the window background colour to black by resetting the COLOR_BUFFER and clear the DEPTH_BUFFER eye = (0.0, 5, 18.0) # Eye coordinates (location of the camera) target = (0.0, 7.0, 0.0 ) # Target coordinates (where the camera is looking) up = (0.0, 1.0, 0.0) # A vector representing the 'up' direction. view_matrix = Matrix44.look_at(eye, target, up) # Calculate the view matrix glUniformMatrix4fv(self.__location_viewMatrix, 1, GL_FALSE, view_matrix) model_matrix = Matrix44.from_translation( [0.0, 0.0, 0.0]) * pyrr.matrix44.create_from_axis_rotation( (0.0, 1.0, 0.0), self.application_clock) * Matrix44.from_scale( [1.0, 1.0, 1.0]) glUniformMatrix4fv( self.__location_modelMatrix, 1, GL_FALSE, model_matrix ) # Update the value of the ModelViewPerspective matrix attribute variable in the vertex buffer glBindVertexArray(self.chibi[0]) glEnableVertexAttribArray(0) glEnableVertexAttribArray(1) glBindTexture(GL_TEXTURE_2D, self.chibi[2]) glDrawArrays(GL_TRIANGLES, 0, self.chibi[1]) self.queue_draw() # Schedules a redraw for Gtk.GLArea
def render(self, time: float, frame_time: float): self.ctx.clear(1.0, 1.0, 1.0) self.ctx.enable(moderngl.DEPTH_TEST) proj = Matrix44.perspective_projection(45.0, self.aspect_ratio, 0.1, 1000.0) lookat = Matrix44.look_at( (47.697, -8.147, 24.498), (0.0, 0.0, 8.0), (0.0, 0.0, 1.0), ) rotate = Matrix44.from_z_rotation(np.sin(time) * 0.5 + 0.2) self.use_texture.value = False self.light.value = (67.69, -8.14, 52.49) self.mvp.write((proj * lookat * rotate).astype('f4').tobytes()) self.color.value = (0.67, 0.49, 0.29) self.objects['ground'].render() self.color.value = (0.46, 0.67, 0.29) self.objects['grass'].render() self.color.value = (1.0, 1.0, 1.0) self.objects['billboard'].render() self.color.value = (0.2, 0.2, 0.2) self.objects['billboard-holder'].render() self.use_texture.value = True self.texture.use() self.objects['billboard-image'].render()
def render(self, time, frame_time): self.ctx.clear(0.2, 0.2, 0.2) self.ctx.enable(moderngl.DEPTH_TEST) self.fps = 1 / frame_time self.control() proj = Matrix44.perspective_projection(45.0, self.aspect_ratio, 0.1, 1000.0) lookat = Matrix44.look_at( (self.movX, self.movY, self.movZ), (200.0, 200.0, 0.0), (0.0, 0.0, 1.0), ) self.light.value = (100, 0, 200) self.texture.use(0) self.mvp_map.write((proj * lookat).astype('f4')) self.vao_map.render(moderngl.TRIANGLE_FAN) model_rot = Matrix44.from_z_rotation( 3.14 / 4) * Matrix44.from_x_rotation(-3.14 / 2) for x in range(int(self.positions.size / 3)): size = 1 + self.production[x] * (2.5 - 1) model_size = Matrix44.from_scale(np.array([size, size, size])) self.gradient.value = self.production[x] model = Matrix44.from_translation(np.array( self.positions[x])) * model_rot * model_size self.mvp.write((proj * lookat * model).astype('f4')) self.vao.render() self.render_ui()
def view(self): x, y, z = self.xyz lookat = Matrix44.look_at( ( x, y, z), (self.target.x, self.target.y, self.target.z), ( 0, 0, 1)) return lookat
def __init__(self, width=1280, height=720, name='OpenGL Window', cameraSpeed=0.1, mouseSpeed=0.3, invertMouse=False, resizable=False, vsync=False): super().__init__(width, height, name, resizable=resizable, vsync=vsync) self.cameraSpeed = cameraSpeed self.mouseSpeed = mouseSpeed self.invertMouse = invertMouse self.cameraPos = Vector3([0.0, 0.0, 0.0]) self.cameraTarget = Vector3([0.0, 0.0, -1.0]) self.cameraUp = Vector3([0.0, 1.0, 0.0]) self.worldUp = Vector3([0.0, 1.0, 0.0]) self.yaw = -90.0 self.pitch = 0.0 self.pressedKeys = [] self.set_mouse_visible(False) self.set_exclusive_mouse(True) # Matrices self.projection = Matrix44.perspective_projection( 45.0, width / height, 0.1, 1000.0) self.view = Matrix44.look_at(self.cameraPos, self.cameraTarget, self.cameraUp)
def render(self, time, frame_time): self.ctx.clear(1.0, 1.0, 1.0) self.ctx.enable(moderngl.DEPTH_TEST) proj = Matrix44.perspective_projection(45.0, self.aspect_ratio, 0.1, 100.0) camera_pos = np.array([ np.cos(self.angleY) * np.cos(self.angleX), np.sin(self.angleX), np.sin(self.angleY) ]) * self.distance lookat = Matrix44.look_at( tuple(camera_pos + self.origin), # eye tuple(self.origin), # target (0.0, 0.0, 1.0), # up ) self.light.value = (0, -3, 3) self.texture.use() for coord in self.coords: model = Matrix44.from_translation(coord) self.mvp.write((proj * lookat * model).astype('f4')) self.vao.render()
def __init__(self, **kwargs): super().__init__(**kwargs) self.camera = KeyboardCamera(self.wnd.keys, aspect_ratio=self.wnd.aspect_ratio) self.terrain_program = self.load_program('my_shader.glsl') self.ctx.front_face = 'cw' # self.ctx.wireframe = True self.translate = Matrix44.from_translation((0, 1., .25)) self.rotate = Matrix44.look_at((0., 1., .25), (0., 0., -1.), (0., 1., 0.)) self.projection = (self.camera.projection.matrix * (self.translate * self.rotate)).astype('f4') self.terrain_program['projection'].write(self.projection.tobytes()) terrain_resolution = 5 points = generate(terrain_resolution).astype('f4') self.buffer = self.ctx.buffer(points) indices = generate_index_buffer(terrain_resolution).astype('i4') print(indices) self.index_buffer = self.ctx.buffer(indices) self.vao_1 = VAO(name='vao_1') self.vao_1.buffer(self.buffer, '3f', ['in_position']) self.vao_1.index_buffer(self.index_buffer)
def on_render(self, gl_area, gl_context): glClearColor(0.0, 0.0, 0.0, 0.0) # Set the background colour for the window -> Black glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Clear the window background colour to black by resetting the COLOR_BUFFER and clear the DEPTH_BUFFER eye = (0.0, 5, 18.0) # Eye coordinates (location of the camera) target = (0.0, 7.0, 0.0) # Target coordinates (where the camera is looking) up = (0.0, 1.0, 0.0) # A vector representing the 'up' direction. view_matrix = Matrix44.look_at(eye, target, up) # Calculate the view matrix model_matrix = Matrix44.from_translation([0.0, 0.0, 0.0]) * pyrr.matrix44.create_from_axis_rotation((0.0, 1.0, 0.0), self.application_clock) * Matrix44.from_scale([1.0, 1.0, 1.0]) # self.camera.move() # for entity in list_of_entities_to_render: # self.renderer.processEntity(entity) # self.renderer.render(self.lights, self.camera) # self.guiRenderer.render(self.guis) glUseProgram(self.shader) # Tells OpenGL to use the shader program for rendering geometry glUniformMatrix4fv(self.viewMatrixLocationInShader, 1, GL_FALSE, view_matrix) glUniformMatrix4fv(self.modelMatrixLocationInShader, 1, GL_FALSE, model_matrix) glUniform3f(self.lightPositionLocationInShader, 0.0, 3.0, 10.0) glUniform3f(self.lightColourLocationInShader, 1.0, 1.0, 1.0) glUniform1f(self.useFakeLightingLocationInShader, 0.0) glBindVertexArray(self.vertex_array_object) # Binds the self.vertex_array_object to the OpenGL pipeline vertex target glDrawArrays(GL_TRIANGLES, 0, len(self.blenderModel.vertices)) glBindVertexArray(0) glUseProgram(0) # Tells OpenGL to use the shader program for rendering geometry self.queue_draw() # Schedules a redraw for Gtk.GLArea
def render(self, batch_size): warp = np.empty( (batch_size, self.height, self.width, 2), dtype=np.float32) for i in range(batch_size): translation = Matrix44.from_translation(( np.random.uniform(self.x_low, self.x_high), np.random.uniform(self.y_low, self.y_high), 0.0 )) rotation = Matrix44.from_matrix33( self.rand_rotation_matrix(self.deflection) ) view = Matrix44.look_at( (0.0, 0.0, np.random.uniform(self.close, self.far)), (0.0, 0.0, 0.0), (0.0, 1.0, 0.0), ) projection = Matrix44.perspective_projection( 45.0, self.width / self.height, 0.1, 1000.0 ) # TODO: translation or rotation first? transform = projection * view * translation * rotation self.fbo.use() self.fbo.clear() self.mvp.write(transform.astype('f4').tobytes()) self.vao.render() framebuffer = self.fbo.read(components=2, floats=True) warp[i] = np.frombuffer(framebuffer, dtype=np.float32).reshape( (self.height, self.width, 2))[::-1] return warp
def render(self, time: float, frame_time: float): self.ctx.clear(1.0, 1.0, 1.0) self.ctx.enable(moderngl.DEPTH_TEST | moderngl.CULL_FACE) proj = Matrix44.perspective_projection(45.0, self.aspect_ratio, 0.1, 1000.0) lookat = Matrix44.look_at( (47.697, -8.147, 24.498), (0.0, 0.0, 8.0), (0.0, 0.0, 1.0), ) rotate = Matrix44.from_z_rotation(np.sin(time) * 0.5 + 0.2) self.use_texture.value = False self.light.value = (67.69, -8.14, 52.49) self.mvp.write((proj * lookat * rotate).astype('f4').tobytes()) self.color.value = (0.67, 0.49, 0.29) self.vao_ground.render() self.color.value = (0.46, 0.67, 0.29) self.vao_grass.render() self.color.value = (1.0, 1.0, 1.0) self.vao_billboard.render() self.color.value = (0.2, 0.2, 0.2) self.vao_holder.render() self.use_texture.value = True self.texture.use() self.vao_image.render()
def render(ctx, prog, objects, fbo, q, x, img): print(q) m = q.rotation_matrix #q から 回転ベクトルだけ取り出して 変換かければ良さそう eye = np.array([47.697, -8.147, 24.498]) eyedir = np.array([-47.7, 8.15, -16.5]) up = np.array([0.0, 0.0, 1.0]) mvp = prog['Mvp'] proj = Matrix44.perspective_projection(58.0, float(W) / H, 0.1, 1000.0) lookat = Matrix44.look_at( eye, np.dot(m, eyedir) + eye, np.dot(m, up), ) print(m) mvp.write((proj * lookat).astype('f4').tobytes()) color = prog['Color'] fbo.clear(0.0, 0.0, 0.0, 0.0) color.value = (0.67, 0.49, 0.29) objects['ground'].render() color.value = (0.46, 0.67, 0.29) objects['grass'].render() myimg = np.asarray( Image.frombytes('RGBA', fbo.size, fbo.read(components=4))) obj_mask = myimg[:, :, 3] obj_img = myimg[:, :, 0:3] idx = (obj_mask != 0) img[idx] = obj_img[idx] cv2.imshow('RealSense', img)
def render(self): angle = self.wnd.time * 0.2 width, height = self.wnd.size self.ctx.viewport = self.wnd.viewport self.ctx.clear(1.0, 1.0, 1.0) self.ctx.enable(moderngl.DEPTH_TEST) camera_pos = (np.cos(angle) * 5.0, np.sin(angle) * 5.0, 2.0) proj = Matrix44.perspective_projection(45.0, width / height, 0.1, 1000.0) lookat = Matrix44.look_at( camera_pos, (0.0, 0.0, 0.5), (0.0, 0.0, 1.0), ) self.mvp.write((proj * lookat).astype('f4').tobytes()) self.light.value = camera_pos crate_z = np.sin(self.crate_a * self.wnd.time + self.crate_b) * 0.2 coordinates = np.dstack([self.crate_x, self.crate_y, crate_z]) self.vbo2.write(coordinates.astype('f4').tobytes()) self.vao.render(instances=1024)
def render(self): if self.current_pointcloud < len(self.pointclouds): self.handle_mouse() self.handle_keys() self.ctx.viewport = self.wnd.viewport self.ctx.clear(0.0, 0.0, 0.0) self.ctx.enable(mgl.BLEND) vertices = np.load(file=self.pointclouds[self.current_pointcloud]) self.vbo.write(vertices.astype('f4').tobytes()) model = Matrix44.from_scale((self.zoom, self.zoom, self.zoom)) model *= Matrix44.from_x_rotation(-self.theta[1]) model *= Matrix44.from_y_rotation(-self.theta[0]) view = Matrix44.look_at((0.0, 0.0, 1.0), (0.0, 0.0, 0.0), (0.0, 1.0, 0.0)) projection = Matrix44.perspective_projection( 45.0, self.wnd.ratio, 0.1, 100.0) self.mvp.write((projection * view * model).astype('f4').tobytes()) self.vao.render(mode=mgl.POINTS) self.sleep_to_target_fps(60) self.current_pointcloud += 1 else: if self.out_dir is None: self.current_pointcloud = 0 else: QtCore.QCoreApplication.instance().quit()
def __init__(self, ctx, width, height, Type, id): self.ctx = ctx self.type = Type self.ParentCube = id self.color = self.fromTypeToColor(self.type) self.width = width self.height = height self.x_rot = 0 self.y_rot = 0 self.x_last = 0 self.y_last = 0 self.x_cur = 0 self.y_cur = 0 self.isRotating = False self.orientation = pyrr.quaternion.create(0.0, 0.0, 0.0, 1.0) self.proj = Matrix44.perspective_projection(45.0, self.width / self.height, 0.1, 100.0) self.camera_pos = [0, 3, -8.0] self.vertexes = VERTICES[self.fromTypeTOIndex(Type)] self.rotation = Matrix44.identity() self.rotationLayer = Matrix44.identity() self.degree = 0 self.translation = Matrix44.identity() self.view = Matrix44.look_at( self.camera_pos, # position of camera in world coordinate (0.0, 0.0, 0.0), # target (0.0, 1.0, 0.0), ) self.prog = self.ctx.program( vertex_shader=''' #version 330 uniform mat4 Mvp; in vec3 in_vert; in vec3 in_color; out vec3 v_color; out vec4 Position; void main() { gl_Position = Mvp*vec4(in_vert, 1.0); v_color = in_color; Position=vec4(in_vert,1.0); } ''', fragment_shader=''' #version 330 in vec3 v_color; in vec4 Position; out vec4 f_color; void main() { f_color=vec4(v_color,1); } ''', )
def createViewMatrix(camera): position = camera.getPosition() # Eye coordinates (location of the camera) # front = camera.getFront() # target = position + front # Target coordinates (where the camera is looking) target = list(camera.getPlayer().getPosition()) target[1] = 2.5 up = (0.0, 1.0, 0.0) # A vector representing the 'up' direction. return Matrix44.look_at(position, target, up) # Calculate the view matrix
def render(self, camera, n_render_vert): # upright = [ 0.0, 1.0, 0.0 ]; upright = [0.0, 0.0, -1.0] # ModelNet10/40だとこれがupright cos = np.dot(camera, upright) / (np.linalg.norm(camera) * np.linalg.norm(upright)) if (np.abs(np.abs(cos) - 1.0) < 1e-6): # カメラ位置とuprightベクトルが平行の場合はuprightベクトルを変更 upright = [1.0, 0.0, 0.0] lookat = Matrix44.look_at( (camera[0], camera[1], camera[2]), # camera position (0.0, 0.0, 0.0), # camera direction (upright[0], upright[1], upright[2])) # upright vector mvp = self.projection * lookat # 法線レンダリング用回転行列 rot = lookat[0:3, 0:3] # 4x4のlookat行列から3x3の回転行列を抜粋 # 各シェーダプログラムに光源と投影行列を設定 self.prog_light.get('Light', 0).value = (camera[0], camera[1], camera[2]) # 光源位置 self.prog_light.get('Color', 0).value = (1.0, 1.0, 1.0, 0.25) # 光の色 self.prog_light.get('Mvp', 0).write(mvp.astype('f4').tobytes()) # 投影行列 ### self.prog_depth.get( 'Mvp', 0 ).write( mvp.astype('f4').tobytes() ); # 投影行列 ### self.prog_normal.get( 'Mvp', 0 ).write( mvp.astype('f4').tobytes() ); # 投影行列 ### self.prog_normal.get( 'Rot', 0 ).write( rot.astype('f4').tobytes() ); # lookat行列 self.fbo.use() self.ctx.enable(moderngl.DEPTH_TEST) # 照明付きレンダリング self.ctx.clear(1.0, 1.0, 1.0) self.vao_light.render(vertices=n_render_vert) data_light = self.fbo.read(components=1, alignment=1) # 深さ値レンダリング ### self.ctx.clear( 1.0, 1.0, 1.0 ); ### self.vao_depth.render( vertices = n_render_vert ); ### data_depth = self.fbo.read( components=1, alignment=1 ); # 法線マップレンダリング ### self.ctx.clear( 1.0, 1.0, 1.0 ); ### self.vao_normal.render( vertices = n_render_vert ); ### data_normal = self.fbo.read( components=3, alignment=1 ); ### return ( data_light, data_depth, data_normal ); return data_light
def render(self, time, frame_time): proj = Matrix44.perspective_projection(90.0, 1.0, 0.1, 1000.0) x, y, z = 0.0, 0.0, 1.0 lookats = [ Matrix44.look_at((x, y, z), (x - 1.0, y, z), (0.0, 1.0, 0.0)), Matrix44.look_at((x, y, z), (x + 1.0, y, z), (0.0, 1.0, 0.0)), Matrix44.look_at((x, y, z), (x, y - 1.0, z), (0.0, 0.0, 1.0)), Matrix44.look_at((x, y, z), (x, y + 1.0, z), (0.0, 0.0, -1.0)), Matrix44.look_at((x, y, z), (x, y, z + 1.0), (0.0, 1.0, 0.0)), Matrix44.look_at((x, y, z), (x, y, z - 1.0), (0.0, 1.0, 0.0)), ] self.prog2['Mvp'] = b''.join((proj * lookat).astype('f4').tobytes() for lookat in lookats) self.prog2['Light'] = (0.0, 0.0, 1.0) self.fbo1.clear(0, (1.0, 1.0, 1.0)) self.fbo1.clear(-1, 1.0) self.vao2.render() camera_pos = (np.cos(time) * 5.0, np.sin(time) * 5.0, 2.0) proj = Matrix44.perspective_projection(45.0, self.aspect_ratio, 0.1, 1000.0) lookat = Matrix44.look_at( camera_pos, (0.0, 0.0, 0.0), (0.0, 0.0, 1.0), ) self.prog1['Mvp'] = (proj * lookat).astype('f4').tobytes() self.prog1['Light'] = camera_pos self.ctx.screen.use() self.ctx.screen.clear(0, (1.0, 1.0, 1.0)) self.ctx.screen.clear(-1, 1.0) self.vao1.render()
def viewatrix(self): self.navigating = False direction = normalize([self.target - self.cameraPosition()])[0] right = np.cross(direction, [0., self.upsign, 0.]) up = np.cross(right, direction) eye = self.cameraPosition() return Matrix44.look_at(eye, self.target, up)
def render(self, time, frametime): self.ctx.enable_only(moderngl.DEPTH_TEST | moderngl.CULL_FACE) self.lightpos = Vector3((math.sin(time) * 20, 5, math.cos(time) * 20), dtype='f4') scene_pos = Vector3((0, -5, -32), dtype='f4') # --- PASS 1: Render shadow map self.offscreen.clear() self.offscreen.use() depth_projection = Matrix44.orthogonal_projection(-20, 20, -20, 20, -20, 40, dtype='f4') depth_view = Matrix44.look_at(self.lightpos, (0, 0, 0), (0, 1, 0), dtype='f4') depth_mvp = depth_projection * depth_view self.shadowmap_program['mvp'].write(depth_mvp) self.floor.render(self.shadowmap_program) self.wall.render(self.shadowmap_program) self.sphere.render(self.shadowmap_program) # --- PASS 2: Render scene to screen self.wnd.use() self.basic_light['m_proj'].write(self.camera.projection.matrix) self.basic_light['m_camera'].write(self.camera.matrix) self.basic_light['m_model'].write( Matrix44.from_translation(scene_pos, dtype='f4')) bias_matrix = Matrix44( [[0.5, 0.0, 0.0, 0.0], [0.0, 0.5, 0.0, 0.0], [0.0, 0.0, 0.5, 0.0], [0.5, 0.5, 0.5, 1.0]], dtype='f4', ) self.basic_light['m_shadow_bias'].write( matrix44.multiply(depth_mvp, bias_matrix)) self.basic_light['lightDir'].write(self.lightpos) self.offscreen_depth.use(location=0) self.floor.render(self.basic_light) self.wall.render(self.basic_light) self.sphere.render(self.basic_light) # Render the sun position self.sun_prog['m_proj'].write(self.camera.projection.matrix) self.sun_prog['m_camera'].write(self.camera.matrix) self.sun_prog['m_model'].write( Matrix44.from_translation(self.lightpos + scene_pos, dtype='f4')) self.sun.render(self.sun_prog) # --- PASS 3: Debug --- # self.ctx.enable_only(moderngl.NOTHING) self.offscreen_depth.use(location=0) self.offscreen_quad.render(self.raw_depth_prog)
def __init__(self, boundlesspath, basedir, ctx, args): # Path to a boundless installation self.boundlesspath = boundlesspath self.basedir = basedir self.args = args with open(self.boundlesspath + '/assets/archetypes/compiledcolorpalettelists.msgpack', 'rb') as palettefile: self.palette_json = utils.convert_msgpackfile(palettefile) self.lookat = Matrix44.look_at((10, 10, 0.0), (10, 10, -5), (0, 1, 0)) self.perspective = Matrix44.perspective_projection(FOV, 1.0, 0.01, 1000.0) self.projection_mat = self.perspective * self.lookat self.model_mat = Matrix44.identity() # Parsed shaders self.shaders = parse_shaders.get_shaders() with open(self.boundlesspath + "/assets/archetypes/compiledspecialmaterials.msgpack", 'rb') as specialsfile: self.specials_json = utils.convert_msgpackfile(specialsfile) self.specials_names = list(map(lambda a: a["name"], self.specials_json)) # A map of all the textures we use which are constant # Key is the uniform name, value is the texture object self.local_tex_path = os.path.join(self.basedir, 'assets/textures') self.const_tex = {} # Dynamic textures self.dyn_tex = {} self.ctx = ctx # Necessary to stop memory leaks self.ctx.gc_mode = "auto" # Note: this is the dimensions of the image. Certain items/blocks/props won't fill # this canvas. self.target_size = (int(args["resolution"]), int(args["resolution"])) self.render_size = ( int(self.target_size[0] // 0.9), int(self.target_size[1] // 0.9) ) if args["anti_alias"] else self.target_size # Initialise properly later, just allocating the field self.prog = {} self.cbo = self.ctx.renderbuffer(self.render_size) self.dbo = self.ctx.depth_texture(self.render_size, alignment=1) self.fbo = self.ctx.framebuffer(color_attachments=(self.cbo), depth_attachment=self.dbo) self.fbo.use() # Initialise all of the constant textures self.init_constant_tex2ds() self.init_constant_texcubes() self.buffer_cache = [] # Grab uniforms with open(os.path.join(self.basedir, 'assets/shader_dump.json')) as uniformsfile: self.uniforms_json = json.load(uniformsfile)
def __init__(self, **kwargs): super().__init__(**kwargs) # Create a custom attribute name spec # so attribute names are not forced to follow gltf standard attr_names = AttributeNames(position='in_vert', texcoord_0='in_tex', normal='in_norm') # Programs self.canvas_prog = self.load_program('mug_mockup/programs/canvas.glsl') self.sticker_prog = self.load_program('mug_mockup/programs/sticker.glsl') self.mug_prog = self.load_program('mug_mockup/programs/mug.glsl') # textures self.bg_texture = self.load_texture_2d('mug_mockup/textures/mug-background.jpg') self.sticker_texture = self.load_texture_2d('mug_mockup/textures/mug-pymet-logo.png') self.canvas_vao = geometry.quad_fs(attr_names=attr_names).instance(self.canvas_prog) obj = self.load_scene('mug_mockup/scenes/mug.obj', attr_names=attr_names) self.mug_vao = obj.root_nodes[0].mesh.vao.instance(self.mug_prog) # Create sticker geometry segs = 32 radius = 29.94 bottom = 6.601 top = 57.856 left = -163.12 * np.pi / 180.0 right = 11.25 * np.pi / 180.0 lin = np.linspace(left, right, segs) sticker_vertices = np.array([ np.repeat(np.cos(lin) * radius, 2), np.repeat(np.sin(lin) * radius, 2), np.tile([bottom, top], segs), np.repeat(np.cos(lin), 2), np.repeat(np.sin(lin), 2), np.tile([0.0, 0.0], segs), np.repeat(np.linspace(0.0, 1.0, segs), 2), np.tile([0.0, 1.0], segs), ]) self.sticker_vbo = self.ctx.buffer(sticker_vertices.T.astype('f4').tobytes()) self.sticker_vao = self.ctx.simple_vertex_array(self.sticker_prog, self.sticker_vbo, 'in_vert', 'in_norm', 'in_text') # Pre-fill uniforms. These currently do not change during rendering proj = Matrix44.perspective_projection(30.0, self.aspect_ratio, 1.0, 1000.0) lookat = Matrix44.look_at( (46.748, -280.619, 154.391), (-23.844, 2.698, 44.493), (0.0, 0.0, 1.0), ) mvp = (proj * lookat).astype('f4') light = (-143.438, -159.072, 213.268) self.mug_prog['Mvp'].write(mvp) self.mug_prog['Light'].value = light self.sticker_prog['Mvp'].write(mvp) self.sticker_prog['Light'].value = light
def update(dt): ctx.viewport = (0, 0, wnd.width, wnd.height) ctx.clear(0.9, 0.9, 0.9) ctx.enable(ModernGL.DEPTH_TEST) proj = Matrix44.perspective_projection(45.0, wnd.width / wnd.height, 0.1, 1000.0) lookat = Matrix44.look_at( (40.0, 30.0, 20.0), (0.0, 0.0, 0.0), (0.0, 0.0, 1.0),
def get_view_model(self): proj = Matrix44.perspective_projection(self.fov, self.width / self.height, self.near, self.far) lookat = Matrix44.look_at( self.eye, # eye / camera position self.lookat, # lookat self.up, # camera up vector ) return proj, lookat
def render(self, vertex_values, rotation_values, cam_pos=None): vertex_values, rotation_values = self.sanitize(vertex_values, rotation_values) faces = [f.copy() for f in self.faces_base] if self.scaled: scale_vertices(faces, vertex_values, iiis=self.iiis) vertex_buffers = [] for face in faces: face.calculate_normals() verts = np.dstack([ face.x(), face.y(), face.z(), face.nx(), face.ny(), face.nz() ]) vbo = self.ctx.buffer(verts.astype('f4').tobytes()) vao = self.ctx.simple_vertex_array(self.prog, vbo, 'in_vert', 'in_norm') vertex_buffers.append(vao) self.fbo.clear(0.0, 0.0, 0.0, 0.0) if cam_pos is None: eye = (2, 2, 1) else: eye = cam_pos lookat = Matrix44.look_at( eye, # eye / camera position (0.0, 0.0, 0.0), # lookat (0.0, 0.0, 1.0), # camera up vector ) rotate = np.array(Matrix44.from_eulers(rotation_values)) for key, val in self.misc.items(): self.prog[key].value = tuple( np.matmul(rotate[:3, :3], val).reshape(1, -1)[0]) self.prog['Lights'].value = tuple( np.matmul(rotate[:3, :3], self.base_light).reshape(1, -1)[0]) self.prog['Mvp'].write( (self.proj * lookat * rotate).astype('f4').tobytes()) for vb, color in zip(vertex_buffers, self.colors): self.prog['Color'].value = color vb.render(moderngl.TRIANGLES) return Image.frombytes('RGB', self.fbo.size, self.fbo.read(), 'raw', 'RGB', 0, -1)
def render(self): angle = self.wnd.time width, height = self.wnd.size self.ctx.viewport = self.wnd.viewport self.ctx.clear(1.0, 1.0, 1.0) self.ctx.enable(ModernGL.DEPTH_TEST) camera_pos = (np.cos(angle) * 20.0, np.sin(angle) * 20.0, 5.0) proj = Matrix44.perspective_projection(45.0, width / height, 0.1, 1000.0) lookat = Matrix44.look_at( camera_pos, (0.0, 0.0, 0.5), (0.0, 0.0, 1.0), ) self.mvp.write((proj * lookat).astype('f4').tobytes()) self.light.value = camera_pos # self.vbo2.write(Matrix33.from_z_rotation(self.wnd.time).astype('f4').tobytes(), offset=24) self.vbo2.write(b''.join( struct.pack( '15f', *car['color'], *car['pos'], 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, ) for car in cars)) self.vao.render(instances=len(cars)) self.vbo2.write(b''.join( struct.pack( '15f', 0.0, 0.0, 0.0, *car['pos'], 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.3, 0.6, 0.0, ) for car in cars)) self.vao.render(instances=len(cars))
def draw(self, *args): width, height = Window.size self.ctx.viewport = (0, 0, width, height) self.ctx.clear(0.9, 0.9, 0.9) self.ctx.enable(ModernGL.DEPTH_TEST) proj = Matrix44.perspective_projection(45.0, width / height, 0.1, 1000.0) lookat = Matrix44.look_at( (40.0, 30.0, 20.0), (0.0, 0.0, 0.0), (0.0, 0.0, 1.0),
def render(self): angle = self.wnd.time self.ctx.screen.viewport = self.wnd.viewport self.ctx.clear(1.0, 1.0, 1.0) with self.ctx.scope(mgl.DEPTH_TEST): camera_pos = (np.cos(angle) * 20.0, np.sin(angle) * 20.0, 5.0) proj = Matrix44.perspective_projection(45.0, self.wnd.ratio, 0.1, 1000.0) lookat = Matrix44.look_at( camera_pos, (0.0, 0.0, 0.5), (0.0, 0.0, 1.0), ) self.prog['Mvp'] = (proj * lookat).astype('f4').tobytes() self.prog['Light'] = camera_pos # self.vbo2.write(Matrix33.from_z_rotation(self.wnd.time).astype('f4').tobytes(), offset=24) self.vbo2.write(b''.join( struct.pack( '15f', *car['color'], *car['pos'], 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, ) for car in cars)) self.vao.render(instances=len(cars)) self.vbo2.write(b''.join( struct.pack( '15f', 0.0, 0.0, 0.0, *car['pos'], 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.3, 0.6, 0.0, ) for car in cars)) self.vao.render(instances=len(cars))
def __compute_view_matrix(self, drone_pose): # Camera view matrix return Matrix44.look_at( # eye: position of the camera in world coordinates drone_pose.translation, # target: position in world coordinates that the camera is looking at drone_pose.translation + (drone_pose.orientation * Vector3([1.0, 0.0, 0.0])), # up: up vector of the camera. drone_pose.orientation * Vector3([0.0, 0.0, 1.0]), )
def matrix(self) -> numpy.ndarray: """numpy.ndarray: The current view matrix for the camera""" return Matrix44.look_at( ( cos(self.angle_x) * sin(self.angle_y) * self.radius + self.target[0], cos(self.angle_y) * self.radius + self.target[1], sin(self.angle_x) * sin(self.angle_y) * self.radius + self.target[2], ), # camera (eye) position, calculated from angles and radius self.target, # what to look at self.up, # camera up direction (change for rolling the camera) dtype="f4", )
def render(self, time, frame_time): self.ctx.clear(1.0, 1.0, 1.0) self.ctx.enable(moderngl.DEPTH_TEST) proj = Matrix44.perspective_projection(45.0, self.aspect_ratio, 0.1, 1000.0) lookat = Matrix44.look_at( (40.0, 30.0, 30.0), (0.0, 0.0, 0.0), (0.0, 0.0, 1.0), ) self.mvp.write((proj * lookat).astype('f4').tobytes()) self.vao.render(moderngl.LINES)
def render(self): self.ctx.screen.viewport = self.wnd.viewport self.ctx.clear(1.0, 1.0, 1.0) with self.ctx.scope(mgl.DEPTH_TEST): proj = Matrix44.perspective_projection(45.0, self.wnd.ratio, 0.1, 1000.0) lookat = Matrix44.look_at( (40.0, 30.0, 30.0), (0.0, 0.0, 0.0), (0.0, 0.0, 1.0), ) self.prog['Mvp'] = (proj * lookat).astype('f4').tobytes() self.vao.render(mgl.LINES)
def update(self): """Handle input, update GLSL programs and render the map.""" # Update instantaneous FPS next_time = glfw.get_time() self.fps = 1 / (next_time - self.last_time) self.last_time = next_time # Character movements right, upward, forward = 0, 0, 0 if self.is_pressed(glfw.KEY_UP): forward += 1 if self.is_pressed(glfw.KEY_DOWN): forward -= 1 if self.is_pressed(glfw.KEY_LEFT): right -= 1 if self.is_pressed(glfw.KEY_RIGHT): right += 1 self.camera.move(right, upward, forward) # Renderings width, height = glfw.get_window_size(self.window) self.context.viewport = 0, 0, width, height self.context.clear(*color('Background')) visibility = self.visibility projection = Matrix44.perspective_projection(self.fov, width / height, 3E-3, visibility) view = Matrix44.look_at(self.pos, self.pos + self.forward, self.upward) vp = (view @ projection).astype(np.float32).tobytes() self.maprog['visibility'].write(visibility.tobytes()) self.maprog['camera'].write(self.pos.tobytes()) self.maprog['mvp'].write(vp) self.mapva.render(moderngl.TRIANGLES) self.prog['visibility'].write(visibility.tobytes()) self.prog['camera'].write(self.pos.tobytes()) self.prog['vp'].write(vp) self.lock.acquire(blocking=False) for pico in self.picos.values(): shards = {} for index, shard in pico.shards.items(): if not shard.power: continue shard.update(self.fps) self.render_shard(shard) shards[index] = shard pico.shards = shards if pico is not self.camera: self.render_pico(pico) self.lock.release() glfw.swap_buffers(self.window) # Resetting cursor position and event queues glfw.set_cursor_pos(self.window, width / 2, height / 2) glfw.poll_events()
def render(self, time: float, frame_time: float): camera_pos = (np.cos(time) * 300.0, np.sin(time) * 300.0, 120.0) proj = Matrix44.perspective_projection(45.0, self.aspect_ratio, 0.1, 1000.0) lookat = Matrix44.look_at( camera_pos, (0.0, 0.0, 50.0), (0.0, 0.0, 1.0), ) with self.scope: self.prog['Mvp'] = (proj * lookat).astype('f4').tobytes() self.prog['Eye'] = camera_pos self.ctx.clear(1.0, 1.0, 1.0) self.vao.render()
def render(self): width, height = self.wnd.size self.ctx.viewport = self.wnd.viewport self.ctx.clear(1.0, 1.0, 1.0) self.ctx.enable(moderngl.DEPTH_TEST) proj = Matrix44.perspective_projection(45.0, width / height, 0.1, 1000.0) lookat = Matrix44.look_at( (47.697, -8.147, 24.498), (0.0, 0.0, 8.0), (0.0, 0.0, 1.0), ) rotate = Matrix44.from_z_rotation(np.sin(self.wnd.time) * 0.5 + 0.2) for mode in ['render_to_texture', 'render_to_window']: if mode == 'render_to_texture': self.fbo.clear(1.0, 1.0, 1.0) self.fbo.use() else: self.ctx.screen.use() self.use_texture.value = False self.light.value = (67.69, -8.14, 52.49) self.mvp.write((proj * lookat * rotate).astype('f4').tobytes()) self.color.value = (0.67, 0.49, 0.29) self.objects['ground'].render() self.color.value = (0.46, 0.67, 0.29) self.objects['grass'].render() self.color.value = (1.0, 1.0, 1.0) self.objects['billboard'].render() self.color.value = (0.2, 0.2, 0.2) self.objects['billboard-holder'].render() self.use_texture.value = True if mode == 'render_to_texture': self.texture1.use() else: self.texture2.use() self.objects['billboard-image'].render()
def render(self, time, frame_time): angle = time * 0.2 self.ctx.clear(1.0, 1.0, 1.0) self.ctx.enable(moderngl.DEPTH_TEST) proj = Matrix44.perspective_projection(45.0, self.aspect_ratio, 0.1, 1000.0) lookat = Matrix44.look_at( (np.cos(angle), np.sin(angle), 0.8), (0.0, 0.0, 0.1), (0.0, 0.0, 1.0), ) self.mvp.write((proj * lookat).astype('f4').tobytes()) self.vao.render(moderngl.TRIANGLE_STRIP)
def render(self, time, frame_time): angle = time self.ctx.clear(1.0, 1.0, 1.0) camera_pos = (np.cos(angle) * 5.0, np.sin(angle) * 5.0, 2.0) proj = Matrix44.perspective_projection(45.0, self.aspect_ratio, 0.1, 1000.0) lookat = Matrix44.look_at( camera_pos, (0.0, 0.0, 0.0), (0.0, 0.0, 1.0), ) self.prog['Mvp'] = (proj * lookat).astype('f4').tobytes() self.prog['Light'] = camera_pos self.vao.render()
def render(self, time, frame_time): self.ctx.clear(1.0, 1.0, 1.0) self.ctx.enable(moderngl.DEPTH_TEST) proj = Matrix44.perspective_projection(45.0, self.aspect_ratio, 0.1, 1000.0) lookat = Matrix44.look_at( (-85, -180, 140), (0.0, 0.0, 65.0), (0.0, 0.0, 1.0), ) self.light.value = (-140.0, -300.0, 350.0) self.color.value = (1.0, 1.0, 1.0, 0.25) self.mvp.write((proj * lookat).astype('f4').tobytes()) self.texture.use() self.vao.render()
def render(self, time, frame_time): self.ctx.clear(1.0, 1.0, 1.0) # self.ctx.enable(mgl.DEPTH_TEST) proj = Matrix44.perspective_projection(45.0, self.aspect_ratio, 0.1, 1000.0) lookat = Matrix44.look_at( (47.697, -8.147, 24.498), (0.0, 0.0, 8.0), (0.0, 0.0, 1.0), ) rotate = Matrix44.from_z_rotation(np.sin(time) * 0.5 + 0.2) w, h = self.texture.size gw, gh = 16, 16 nx, ny, nz = int(w/gw), int(h/gh), 1 print('=' * 50) self.compute['time'] = time GL_WRITE_ONLY = 0x88B9 GL_R32F = 0x822E self.texture.bind_to_image(0,GL_WRITE_ONLY, GL_R32F) self.compute.run(nx, ny, nz) print('-' * 50) with self.scope: self.prog['UseTexture'] = False self.prog['Light'] = (67.69, -8.14, 52.49) self.prog['Mvp'] = (proj * lookat * rotate).astype('f4').tobytes() self.prog['Color'] = (0.67, 0.49, 0.29) self.objects['ground'].render() self.prog['Color'] = (0.46, 0.67, 0.29) self.objects['grass'].render() self.prog['Color'] = (1.0, 1.0, 1.0) self.objects['billboard'].render() self.prog['Color'] = (0.2, 0.2, 0.2) self.objects['billboard-holder'].render() self.prog['UseTexture'] = True self.objects['billboard-image'].render()
def render(self): angle = self.wnd.time self.ctx.screen.viewport = self.wnd.viewport camera_pos = (np.cos(angle) * 5.0, np.sin(angle) * 5.0, 2.0) proj = Matrix44.perspective_projection(45.0, self.wnd.ratio, 0.1, 1000.0) lookat = Matrix44.look_at( camera_pos, (0.0, 0.0, 0.5), (0.0, 0.0, 1.0), ) self.prog['Mvp'] = (proj * lookat).astype('f4').tobytes() self.prog['Light'] = camera_pos self.ctx.replay(self.bytecode)
def render(self, time, frame_time): self.ctx.clear(1.0, 1.0, 1.0) self.bg_texture.use() self.ctx.enable_only(moderngl.BLEND) self.canvas_vao.render(moderngl.TRIANGLE_STRIP) self.ctx.enable_only(moderngl.DEPTH_TEST) proj = Matrix44.perspective_projection(30.0, self.aspect_ratio, 1.0, 1000.0) lookat = Matrix44.look_at( (46.748, -280.619, 154.391), (-23.844, 2.698, 44.493), (0.0, 0.0, 1.0), ) self.mvp.write((proj * lookat).astype('f4').tobytes()) self.light.value = (-143.438, -159.072, 213.268) self.mug_texture.use() self.mug_vao.render() self.ctx.enable_only(moderngl.DEPTH_TEST | moderngl.BLEND) self.sticker_texture.use() self.sticker_vao.render(moderngl.TRIANGLE_STRIP)
def render(self, time, frame_time): angle = time * 0.2 self.ctx.clear(1.0, 1.0, 1.0) self.ctx.enable(moderngl.DEPTH_TEST) camera_pos = (np.cos(angle) * 5.0, np.sin(angle) * 5.0, 2.0) proj = Matrix44.perspective_projection(45.0, self.aspect_ratio, 0.1, 1000.0) lookat = Matrix44.look_at( camera_pos, (0.0, 0.0, 0.5), (0.0, 0.0, 1.0), ) self.mvp.write((proj * lookat).astype('f4').tobytes()) self.light.value = camera_pos crate_z = np.sin(self.crate_a * time + self.crate_b) * 0.2 coordinates = np.dstack([self.crate_x, self.crate_y, crate_z]) self.vbo2.write(coordinates.astype('f4').tobytes()) self.vao.render(instances=1024)
def render(self): angle = self.wnd.time self.ctx.screen.viewport = self.wnd.viewport self.ctx.clear(1.0, 1.0, 1.0) with self.ctx.scope(mgl.DEPTH_TEST): camera_pos = (np.cos(angle) * 20.0, np.sin(angle) * 20.0, 5.0) proj = Matrix44.perspective_projection(45.0, self.wnd.ratio, 0.1, 1000.0) lookat = Matrix44.look_at( camera_pos, (0.0, 0.0, 0.5), (0.0, 0.0, 1.0), ) self.prog['Mvp'] = (proj * lookat).astype('f4').tobytes() self.prog['Light'] = camera_pos # self.vbo2.write(Matrix33.from_z_rotation(self.wnd.time).astype('f4').tobytes(), offset=24) self.vbo2.write(b''.join(struct.pack( '15f', *car['color'], *car['pos'], 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, ) for car in cars)) self.vao.render(instances=len(cars)) self.vbo2.write(b''.join(struct.pack( '15f', 0.0, 0.0, 0.0, *car['pos'], 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.3, 0.6, 0.0, ) for car in cars)) self.vao.render(instances=len(cars))
def render(self): width, height = self.wnd.size self.ctx.screen.viewport = self.wnd.viewport self.ctx.clear(1.0, 1.0, 1.0) with self.ctx.scope(mgl.DEPTH_TEST): proj = Matrix44.perspective_projection(45.0, width / height, 0.1, 1000.0) lookat = Matrix44.look_at( (47.697, -8.147, 24.498), (0.0, 0.0, 8.0), (0.0, 0.0, 1.0), ) rotate = Matrix44.from_z_rotation(np.sin(self.wnd.time) * 0.5 + 0.2) self.prog['UseTexture'] = False self.prog['Light'] = (67.69, -8.14, 52.49) self.prog['Mvp'] = (proj * lookat * rotate).astype('f4').tobytes() self.prog['Color'] = (0.67, 0.49, 0.29) self.objects['ground'].render() self.prog['Color'] = (0.46, 0.67, 0.29) self.objects['grass'].render() self.prog['Color'] = (1.0, 1.0, 1.0) self.objects['billboard'].render() self.prog['Color'] = (0.2, 0.2, 0.2) self.objects['billboard-holder'].render() self.prog['UseTexture'] = True # self.texture.use() self.sampler.use() self.objects['billboard-image'].render()
def render(self, time, frame_time): angle = time self.ctx.clear(1.0, 1.0, 1.0) self.ctx.enable(moderngl.DEPTH_TEST) camera_pos = (np.cos(angle) * 20.0, np.sin(angle) * 20.0, 5.0) proj = Matrix44.perspective_projection(45.0, self.aspect_ratio, 0.1, 1000.0) lookat = Matrix44.look_at( camera_pos, (0.0, 0.0, 0.5), (0.0, 0.0, 1.0), ) self.mvp.write((proj * lookat).astype('f4').tobytes()) self.light.value = camera_pos # self.vbo2.write(Matrix33.from_z_rotation(self.wnd.time).astype('f4').tobytes(), offset=24) self.vbo2.write(b''.join(struct.pack( '15f', *car['color'], *car['pos'], 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, ) for car in cars)) self.vao.render(instances=len(cars)) self.vbo2.write(b''.join(struct.pack( '15f', 0.0, 0.0, 0.0, *car['pos'], 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.3, 0.6, 0.0, ) for car in cars)) self.vao.render(instances=len(cars))
def render(self, time, frame_time): self.ctx.clear(1.0, 1.0, 1.0) # self.ctx.enable(mgl.DEPTH_TEST) proj = Matrix44.perspective_projection(45.0, self.aspect_ratio, 0.1, 1000.0) lookat = Matrix44.look_at( (47.697, -8.147, 24.498), (0.0, 0.0, 8.0), (0.0, 0.0, 1.0), ) rotate = Matrix44.from_z_rotation(np.sin(time) * 0.5 + 0.2) self.fbo.clear(0, (1.0, 1.0, 1.0)) self.fbo.clear(-1, 1.0) for scope in [self.scope1, self.scope2]: with scope: self.prog['UseTexture'] = False self.prog['Light'] = (67.69, -8.14, 52.49) self.prog['Mvp'] = (proj * lookat * rotate).astype('f4').tobytes() self.prog['Color'] = (0.67, 0.49, 0.29) self.objects['ground'].render() self.prog['Color'] = (0.46, 0.67, 0.29) self.objects['grass'].render() self.prog['Color'] = (1.0, 1.0, 1.0) self.objects['billboard'].render() self.prog['Color'] = (0.2, 0.2, 0.2) self.objects['billboard-holder'].render() self.prog['UseTexture'] = True self.objects['billboard-image'].render()
def build_look_at(self): self._cameras_target = (self._camera_position + self._camera_front) self.mat_lookat = Matrix44.look_at( self._camera_position, self._cameras_target, self._camera_up)
'''), ]) mvp = prog.uniforms['Mvp'] grid = bytearray() for i in range(0, 32 + 1): grid += struct.pack('6f', i - 16.0, -16.0, 0.0, 0.0, 0.0, 0.0) grid += struct.pack('6f', i - 16.0, 16.0, 0.0, 0.0, 0.0, 0.0) grid += struct.pack('6f', -16.0, i - 16.0, 0.0, 0.0, 0.0, 0.0) grid += struct.pack('6f', 16.0, i - 16.0, 0.0, 0.0, 0.0, 0.0) vbo = ctx.buffer(grid) vao = ctx.simple_vertex_array(prog, vbo, ['in_vert', 'in_color']) while wnd.update(): ctx.viewport = wnd.viewport ctx.clear(0.9, 0.9, 0.9) ctx.enable(ModernGL.DEPTH_TEST) proj = Matrix44.perspective_projection(45.0, wnd.ratio, 0.1, 1000.0) lookat = Matrix44.look_at( (40.0, 30.0, 20.0), (0.0, 0.0, 0.0), (0.0, 0.0, 1.0), ) mvp.write((proj * lookat).astype('float32').tobytes()) vao.render(ModernGL.LINES)
def lookat(self) -> Matrix44: """Gets the lookat matrix from the posdir vectors.""" return Matrix44.look_at(self.pos, self.pos + self.dir, self.ure)