def setup_deferred_rendering(): """Return (Shader, FBO, QuadMesh) for deferred rendering.""" fbo = rc.FBO(rc.Texture(width=4096, height=4096, mipmap=True)) quad = rc.gen_fullscreen_quad("DeferredQuad") quad.texture = fbo.texture shader = rc.Shader.from_file(*rc.resources.deferredShader) return RenderCollection(shader=shader, fbo=fbo, quad=quad)
def view_mesh(body, obj_filename): # """Displays mesh in .obj file. Useful for checking that files are rendering properly.""" reader = rc.WavefrontReader(obj_filename) mesh = reader.get_mesh(body, position=(0, 0, -1)) print(mesh.vertices.shape) mesh.scale.x = .2 / np.ptp(mesh.vertices, axis=0).max() camera = rc.Camera(projection=rc.PerspectiveProjection(fov_y=20)) light = rc.Light(position=(camera.position.xyz)) scene = rc.Scene(meshes=[mesh], camera=camera, light=light, bgColor=(.2, .4, .2)) scene.gl_states = scene.gl_states[:-1] display = pyglet.window.get_platform().get_default_display() screen = display.get_screens()[0] window = pyglet.window.Window(fullscreen=True, screen=screen) fbo = rc.FBO(rc.Texture(width=4096, height=4096)) quad = rc.gen_fullscreen_quad() quad.texture = fbo.texture label = pyglet.text.Label() @window.event def on_draw(): with rc.resources.genShader, fbo: scene.draw() with rc.resources.deferredShader: quad.draw() verts_mean = np.ptp(mesh.vertices, axis=0) label.text = 'Name: {}\nRotation: {}\nSize: {} x {} x {}'.format( mesh.name, mesh.rotation, verts_mean[0], verts_mean[1], verts_mean[2]) label.draw() @window.event def on_resize(width, height): camera.projection.aspect = float(width) / height @window.event def on_mouse_motion(x, y, dx, dy): x, y = x / float(window.width) - .5, y / float(window.height) - .5 mesh.rotation.x = -360 * y mesh.rotation.y = 360 * x pyglet.app.run()
def __init__(self, arena_objfile, projector_file, fullscreen=True, screen=1, antialiasing=True, vsync=False, fps_mode=False, *args, **kwargs): """ A Pyglet Window that sets up the arena, beamer, and virtual scenes, along with motive update and draw functions for updating and rendering a virtual reality environment in a ratCAVE setup. """ self.__display = pyglet.window.get_platform().get_default_display() self.__screen = self.__display.get_screens()[screen] super(self.__class__, self).__init__(fullscreen=fullscreen, screen=self.__screen, vsync=vsync, *args, **kwargs) self.cube_fbo = rc.FBO(texture=rc.TextureCube(width=4096, height=4096)) self.active_scene, self.arena, self.arena_rb = utils.load_projected_scene( arena_file=arena_objfile, projector_file=projector_file, motive_client=motive) self.orig_texture = None self.shader = rc.Shader.from_file(*rc.resources.genShader) self._vr_scenes = set() self.current_vr_scene = None self.rat_rb = motive.rigid_bodies['Rat'] self.antialiasing = antialiasing self.fbo_aa = rc.FBO(rc.Texture(width=4096, height=4096, mipmap=True)) self.aa_quad = rc.gen_fullscreen_quad() self.aa_quad.texture = self.fbo_aa.texture self.shader_deferred = rc.Shader.from_file( *rc.resources.deferredShader) if fps_mode: pass # raise NotImplementedError("Haven't gotten fps_mode to work properly yet.") self.fps_mode = fps_mode pyglet.clock.schedule(self.update)
def __init__(self, *args, **kwargs): super(RotationWindow, self).__init__(*args, **kwargs) pyglet.clock.schedule(lambda dt: None) reader = rc.WavefrontReader(rc.resources.obj_primitives) self.mesh = reader.get_mesh('Monkey', scale=.05, position=(0, 0, 0)) self.mesh.rotation = self.mesh.rotation.to_quaternion() self.scene = rc.Scene(meshes=[self.mesh], bgColor=(0., 0., 0.)) self.fbo = rc.FBO(rc.Texture(width=4096, height=4096)) self.quad = rc.gen_fullscreen_quad() self.quad.texture = self.fbo.texture self.label = pyglet.text.Label() self.shader3d = rc.Shader.from_file(*rc.resources.genShader) self.shaderAA = rc.Shader.from_file(*rc.resources.deferredShader)
def setup_grey3x_rendering(update_projector=False): """ Return (Shader, FBO, QuadMesh) for rendering in Propixx's 'GREY3X' mode. If desired, can also apply the settings to the beamer to set it to this mode (caution: won't change back afterward). """ if update_projector: beamer = propixx.PROPixx() beamer.setDlpSequencerProgram('GREY3X') vert_shader = open(rc.resources.deferredShader.vert).read() frag_shader = """ #version 400 #extension GL_ARB_texture_rectangle : enable uniform sampler2D TextureMap; in vec2 texCoord; out vec3 color; void main( void ) { if (gl_FragCoord.x * 3 + 1 < 1920){ color = vec3(texture2D(TextureMap, vec2(floor(gl_FragCoord.x * 3. + 0.0) / 1920.0, gl_FragCoord.y / 1080.0)).r, texture2D(TextureMap, vec2(floor(gl_FragCoord.x * 3. + 1.0) / 1920.0, gl_FragCoord.y / 1080.0)).r, texture2D(TextureMap, vec2(floor(gl_FragCoord.x * 3. + 2.0) / 1920.0, gl_FragCoord.y / 1080.0)).r ); } else { color = vec3(.2, 0., 0.); } return; } """ shader = rc.Shader(vert=vert_shader, frag=frag_shader) quad = rc.gen_fullscreen_quad('Grey3xQuad') return RenderCollection(shader=shader, fbo=None, quad=quad)
def vr_demo(motive_filename, projector_filename, arena_filename, body, screen): motive.initialize() motive_filename = motive_filename.encode() motive.load_project(motive_filename) body = motive.get_rigid_bodies()[body] for el in range(3): body.reset_orientation() motive.update() arena_body = motive.get_rigid_bodies()['Arena'] # Load projector's Camera object, created from the calib_projector ratcave_utils CLI tool. display = pyglet.window.get_platform().get_default_display() screen = display.get_screens()[screen] window = pyglet.window.Window(fullscreen=False, screen=screen, vsync=False) fbo = rc.FBO(rc.TextureCube(width=4096, height=4096)) shader3d = rc.Shader.from_file(*rc.resources.genShader) reader = rc.WavefrontReader(rc.resources.obj_primitives) mesh = reader.get_mesh('Monkey', scale=.022, position=(0, 0, .3)) mesh.position.y = .6 mesh.uniforms['ambient'] = .15, .15, .15 # mesh.rotation = mesh.rotation.to_quaternion() arena = rc.WavefrontReader(arena_filename.encode()).get_mesh('Arena') arena.rotation = arena.rotation.to_quaternion() arena.texture = fbo.texture floor = reader.get_mesh('Plane', scale=50, position=(0, 0, 0), mean_center=True) # floor.scale.x = 20 floor.rotation.x = -90 floor.texture = fbo.texture # floor.rotation.x = 180 vr_scene = rc.Scene(meshes=[mesh], bgColor=(0., .3, 0)) vr_scene.camera.projection.fov_y = 90 vr_scene.camera.projection.aspect = 1. vr_scene.camera.projection.z_near = .005 vr_scene.camera.projection.z_far = 2. fbo_aa = rc.FBO(rc.Texture(width=4096, height=4096, mipmap=True)) quad = rc.gen_fullscreen_quad() quad.texture = fbo_aa.texture shader_deferred = rc.Shader.from_file(*rc.resources.deferredShader) scene = rc.Scene(meshes=[arena, floor], bgColor=(0., 0., .3)) camera = rc.Camera.from_pickle(projector_filename.encode()) camera.projection.fov_y = 39 scene.camera = camera scene.light.position.xyz = camera.position.xyz vr_scene.light.position.xyz = camera.position.xyz @window.event def on_draw(): with shader3d: with fbo: vr_scene.camera.projection.match_aspect_to_viewport() vr_scene.draw360_to_texture(fbo.texture) scene.camera.projection.match_aspect_to_viewport() with fbo_aa: scene.draw() with shader_deferred: quad.draw() def update_body(dt, body): motive.update() mesh.position.xyz = arena_body.location mesh.position.y -= .07 # mesh.rotation.xyzw = arena_body.rotation_quats # mesh.rotation.y += 10 * dt arena.position.xyz = arena_body.location arena.rotation.xyzw = arena_body.rotation_quats vr_scene.camera.position.xyz = body.location scene.camera.uniforms['playerPos'] = body.location pyglet.clock.schedule(update_body, body) pyglet.app.run()