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)
# 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) @window.event def on_draw():
import numpy as np import pyglet import ratcave as rc window = pyglet.window.Window() cube = rc.Mesh.from_primitive('Cube', position=(0, 0, -3), rotation=(45, 45, 0)) arr = np.random.randint(0, 255, size=(128, 128, 4)) arr = np.zeros_like(arr) # + 255 arr[:, :, 0] = 255 tex2 = rc.Texture(values=arr) tex2.values = np.random.randint(0, 255, size=(128, 128, 4)) cube.textures.append(tex2) @window.event def on_draw(): window.clear() with rc.default_shader, rc.default_camera, rc.default_states: cube.draw() def randomize_texture(dt): tex2.values = np.random.randint(0, 255, size=(128, 128, 4))
def debug_view(vertices, texcoord, image=None, window_size=(800, 600)): # creates the window and sets its properties width, height = window_size window = pyglet.window.Window(width=width, height=height, caption='Debug Viewer', resizable=False) num_verts = 3 * vertices.shape[0] model = ratcave.Mesh(arrays=(vertices.reshape(num_verts, 3), texcoord.reshape(num_verts, 2))) model.position.xyz = 0, 0, -10 if image is not None: image = image.transpose(PIL.Image.FLIP_TOP_BOTTOM) imgdata = pyglet.image.ImageData(image.width, image.height, 'RGBA', image.tobytes()) mipmap = False tex = imgdata.get_mipmapped_texture( ) if mipmap else imgdata.get_texture() pyglet.gl.glBindTexture(pyglet.gl.GL_TEXTURE_2D, 0) model.textures.append( ratcave.Texture(id=tex.id, data=tex, mipmap=mipmap)) scene = ratcave.Scene(meshes=[model]) scene.camera.projection = ratcave.PerspectiveProjection(60.0, width / float(height), z_far=100.0) def update(dt): pass pyglet.clock.schedule(update) shader = ratcave.Shader(vert=vert_shader, frag=frag_shader) @window.event def on_resize(width, height): # TODO update scene.camera.projection.viewport scene.camera.projection.aspect = width / float(height) return pyglet.event.EVENT_HANDLED @window.event def on_draw(): with shader: scene.draw() @window.event def on_mouse_scroll(x, y, scroll_x, scroll_y): # scroll the MOUSE WHEEL to zoom scene.camera.position.z -= scroll_y / 10.0 @window.event def on_mouse_drag(x, y, dx, dy, button, modifiers): # press the LEFT MOUSE BUTTON to rotate if button == pyglet.window.mouse.LEFT: model.rotation.y += dx / 5.0 model.rotation.x -= dy / 5.0 # press the LEFT and RIGHT MOUSE BUTTONS simultaneously to pan if button == pyglet.window.mouse.LEFT | pyglet.window.mouse.RIGHT: scene.camera.position.x -= dx / 100.0 scene.camera.position.y -= dy / 100.0 # starts the application pyglet.app.run()
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()