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 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, *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)
monkey.point_size = .1 plane = obj_reader.get_mesh('Plane') plane.position.xyz = 0, 0, -5 plane.rotation.x = 0 plane.scale.xyz = 8 plane.uniforms['spec_weight'] = 0 fps_display = pyglet.window.FPSDisplay(window) light = rc.Light() light.projection.fov_y = 75. light.position.z = 3 light.time = 0. fbo_shadow = rc.FBO(texture=rc.DepthTexture(width=2048, height=2048)) plane.textures.append(fbo_shadow.texture) plane.textures.append(rc.Texture.from_image(rc.resources.img_colorgrid)) monkey.textures.append(fbo_shadow.texture) @window.event def on_draw(): window.clear() with ExitStack() as stack: for shader in [rc.resources.shadow_shader, rc.default_shader]: with shader, rc.default_states, light, rc.default_camera: if shader == rc.resources.shadow_shader: stack.enter_context(fbo_shadow) window.clear() else:
if IS_MOVING: for sphere in spheres: sphere.rot_velocity = 45 * np.random.random() # Debug Text label_fps = pyglet.text.Label('FPS', font_size=20) # Create Scenes and FBOs to render onto proj_scene = rc.Scene(meshes=spheres) proj_scene.camera.aspect = 1. proj_scene.camera.fov_y = 90. god_scene = rc.Scene(meshes=[player, screen]) cubetexture = rc.TextureCube(width=CUBEMAP_TEXTURE_SIZE, height=CUBEMAP_TEXTURE_SIZE) screen.texture = cubetexture fbo_cube = rc.FBO(cubetexture) aaShader = rc.resources.aaShader aa_texture = rc.Texture() fbo_aa = rc.FBO(aa_texture) fullscreen_quad = rc.resources.gen_fullscreen_quad() fullscreen_quad.texture = aa_texture # Run Display and Update Loops @window.event def on_resize(width, height): # proj_scene.camera.aspect = width / float(height) god_scene.camera.aspect = width / float(height) fullscreen_quad.uniforms['frameBufSize'] = float(width), float(height)
# Assemble the Projected Scene monkey = obj_reader.get_mesh("Monkey", position=(0, 0, -1), scale=0.8) screen = obj_reader.get_mesh("Plane", position=(0, 0, 1), rotation=(1.5, 180, 0)) projected_scene = rc.Scene(meshes=[monkey, screen, sphere, cube], bgColor=(1., .5, 1.)) projected_scene.light.position = virtual_scene.light.position projected_scene.camera = rc.Camera(position=(0, 4, 0), rotation=(-90, 0, 0)) projected_scene.camera.projection.z_far = 6 # Create Framebuffer and Textures cube_texture = rc.texture.TextureCube( width=1024, height=1024) # this is the actual cube texture cube_fbo = rc.FBO(texture=cube_texture) screen.textures.append(cube_texture) clock = 0. def update(dt): global clock clock += dt monkey.position.x = math.sin(1.3 * clock) virtual_scene.camera.position.xyz = monkey.position.xyz screen.uniforms['playerPos'] = virtual_scene.camera.position.xyz pyglet.clock.schedule(update)
def main(): #gettign positions of rigib bodies in real time client = NatClient() arena_rb = client.rigid_bodies['Arena'] rat_rb = client.rigid_bodies['Rat'] window = pyglet.window.Window(resizable=True, fullscreen=True, screen=get_screen(1)) # Opening the basic pyglet window # Load Arena remove_image_lines_from_mtl('assets/3D/grass_scene.mtl') arena_filename = 'assets/3D/grass_scene.obj'# we are taking an arena which has been opened in blender and rendered to 3D after scanning it does not have flipped normals arena_reader = rc.WavefrontReader(arena_filename) # loading the mesh of the arena thought a wavefrontreader arena = arena_reader.get_mesh("Arena", position=arena_rb.position) # making the wafrotn into mesh so we can extrude texture ont top of it. arena.uniforms['diffuse'] = 1., 1., 1. # addign a white diffuse material to the arena arena.rotation = arena.rotation.to_quaternion() # we also need to get arena's rotation not just xyz so it can be tracked and moved if it gets bumped # Load the projector as a Ratcave camera, set light to its position projector = rc.Camera.from_pickle('assets/3D/projector.pkl') # settign the pickle filled of the projector, which gives us the coordinates of where the projector is projector.position.x += .004 projector.projection = rc.PerspectiveProjection(fov_y =40.5, aspect=1.777777778) light = rc.Light(position=projector.position) ## Make Virtual Scene ## fields = [] for x, z in itertools.product([-.8, 0, .8], [-1.6, 0, 1.6]): field = load_textured_mesh(arena_reader, 'grass', 'grass.png') field.position.x += x field.position.z += z fields.append(field) ground = load_textured_mesh(arena_reader, 'Ground', 'dirt.png') sky = load_textured_mesh(arena_reader, 'Sky', 'sky.png') snake = load_textured_mesh(arena_reader, 'Snake', 'snake.png') rat_camera = rc.Camera(projection=rc.PerspectiveProjection(aspect=1, fov_y=90, z_near=.001, z_far=10), position=rat_rb.position) # settign the camera to be on top of the rats head meshes = [ground, sky, snake] + fields for mesh in meshes: mesh.uniforms['diffuse'] = 1., 1., 1. mesh.uniforms['flat_shading'] = False mesh.parent = arena virtual_scene = rc.Scene(meshes=meshes, light=light, camera=rat_camera, bgColor=(0, 0, 255)) # seetign aset virtual scene to be projected as the mesh of the arena virtual_scene.gl_states.states = virtual_scene.gl_states.states[:-1] ## Make Cubemapping work on arena cube_texture = rc.TextureCube(width=4096, height=4096) # usign cube mapping to import eh image on the texture of the arena framebuffer = rc.FBO(texture=cube_texture) ## creating a fr`amebuffer as the texture - in tut 4 it was the blue screen arena.textures.append(cube_texture) # Stereo vr_camgroup = rc.StereoCameraGroup(distance=.05) vr_camgroup.rotation = vr_camgroup.rotation.to_quaternion() # updating the posiotn of the arena in xyz and also in rotational perspective def update(dt): """main update function: put any movement or tracking steps in here, because it will be run constantly!""" vr_camgroup.position, vr_camgroup.rotation.xyzw = rat_rb.position, rat_rb.quaternion # setting the actual osiont of the rat camera to vbe of the rat position arena.uniforms['playerPos'] = rat_rb.position arena.position, arena.rotation.xyzw = arena_rb.position, arena_rb.quaternion arena.position.y -= .02 pyglet.clock.schedule(update) # making it so that the app updates in real time @window.event def on_draw(): ## Render virtual scene onto cube texture with framebuffer: with cube_shader: for mask, camside in zip([(True, False, False, True), (False, True, True, True)], [vr_camgroup.left, vr_camgroup.right]): gl.glColorMask(*mask) virtual_scene.camera.position.xyz = camside.position_global virtual_scene.draw360_to_texture(cube_texture) ## Render real scene onto screen gl.glColorMask(True, True, True, True) window.clear() with cube_shader: # usign cube shader to create the actuall 6 sided virtual cube which gets upated with position and angle of the camera/viewer rc.clear_color(255, 0, 0) # why is it here 39? e with projector, light: arena.draw() # actually run everything. 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.arena = rc.WavefrontReader(arena_objfile).get_mesh( cfg.ARENA_MESH_NAME) self.arena.uniforms['diffuse'] = cfg.ARENA_LIGHTING_DIFFUSE self.arena.uniforms['flat_shading'] = cfg.ARENA_LIGHTING_FLAT_SHADING self.arena.rotation = self.arena.rotation.to_quaternion() self.arena_rb = motive.rigid_bodies[cfg.ARENA_MOTIVE_NAME] self.arena.position.xyz = self.arena_rb.position self.arena.rotation.wxyz = self.arena_rb.quaternion beamer = rc.Camera.from_pickle(projector_file) beamer.projection.aspect = 1.77778 beamer.projection.fov_y = 41.5 self.active_scene = rc.Scene(meshes=[self.arena], bgColor=(.6, 0, 0), camera=beamer) self.active_scene.gl_states.states = self.active_scene.gl_states.states[: -1] self.active_scene.light.position.xyz = beamer.position.xyz self.orig_texture = None self.shader = rc.resources.cube_shader self._vr_scenes = set() self.current_vr_scene = None self.rodent_rb = motive.rigid_bodies[cfg.RODENT_MOTIVE_NAME] 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: raise NotImplementedError( "Haven't gotten fps_mode to work properly yet.") self.fps_mode = fps_mode pyglet.clock.schedule(self.update)
import ratcave as rc win = pyglet.window.Window(resizable=True) reader = rc.WavefrontReader(rc.resources.obj_primitives) stars = reader.get_mesh('Torus', drawmode=rc.POINTS) stars.point_size = 3 stars.uniforms['diffuse'] = 1., 1., 1. stars.position.xyz = 0, 0, -2 stars.scale.xyz = 1.2 monkey = reader.get_mesh('Monkey', position=(0, 0.1, -2)) monkey.uniforms['flat_shading'] = True monkey.uniforms['diffuse'] = 1., 1., 1. fbo = rc.FBO(texture=rc.Texture(width=win.width, height=win.height)) def update(dt): stars.rotation.z += 10. * dt pyglet.clock.schedule(update) @win.event def on_draw(): with rc.default_shader: with fbo: pyglet.gl.glClearColor(0, 0, 0, 1)
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()
) # seetign aset virtual scene to be projected as the mesh of the arena virtual_scene.gl_states.states = virtual_scene.gl_states.states[:-1] checkerboard_texture = rc.Texture.from_image( rc.resources.img_uvgrid) # or 'assets/wood.jpg' wall.textures.append(checkerboard_texture) virtual_arena.textures.append(checkerboard_texture) virtual_arena.uniforms['flat_shading'] = True wall.uniforms['flat_shading'] = True ## Make Cubemapping work on arena cube_texture = rc.TextureCube( width=4096, height=4096 ) # usign cube mapping to import eh image on the texture of the arena framebuffer = rc.FBO( texture=cube_texture ) ## creating a fr`amebuffer as the texture - in tut 4 it was the blue screen arena.textures.append(cube_texture) # updating the posiotn of the arena in xyz and also in rotational perspective def update(dt): """main update function: put any movement or tracking steps in here, because it will be run constantly!""" rat_camera.position = rat_rb.position # setting the actual osiont of the rat camera to vbe of the rat position arena.uniforms['playerPos'] = rat_rb.position arena.position, arena.rotation.xyzw = arena_rb.position, arena_rb.quaternion virtual_arena.position, virtual_arena.rotation = arena.position, arena.rotation pyglet.clock.schedule(update) # making it so that the app updates in real time