def __make_arrow(self, color): arrow_top = self.obj_reader.get_mesh("Cone") arrow_bottom = self.obj_reader.get_mesh("Cylinder") arrow_bottom.scale.xyz = 0.6, 0.5, 0.6 arrow_top.scale.xyz = 1.0, 0.5, 1.0 arrow_bottom.position.xyz = 0, 0.5, 0 arrow_bottom.uniforms['diffuse'] = color arrow_top.position.xyz = 0, 0.75, 0 arrow_top.uniforms['diffuse'] = color arrow = rc.EmptyEntity() arrow.add_children(arrow_top, arrow_bottom) return arrow
def __make_axis(self): axis = rc.EmptyEntity(name='arrow') x_axis_arrow = self.__make_arrow(color=(1, 0, 0)) y_axis_arrow = self.__make_arrow(color=(0, 1, 0)) z_axis_arrow = self.__make_arrow(color=(0, 0, 1)) x_axis_arrow.scale.xyz = 0.2, 2.0, 0.2 y_axis_arrow.scale.xyz = 0.2, 2.0, 0.2 z_axis_arrow.scale.xyz = 0.2, 2.0, 0.2 x_axis_arrow.rotation.xyz = 0., 0., 0. y_axis_arrow.rotation.xyz = 0., 0., -90. z_axis_arrow.rotation.xyz = -90., 0., 0. axis.add_children(x_axis_arrow, y_axis_arrow, z_axis_arrow) return axis
def __make_quadrotor(self): toruses = [ self.obj_reader.get_mesh("Torus", position=(0, 0.5, 0), scale=.3), self.obj_reader.get_mesh("Torus", position=(0, -0.5, 0), scale=.3), self.obj_reader.get_mesh("Torus", position=(0.5, 0, 0), scale=.3), self.obj_reader.get_mesh("Torus", position=(-0.5, 0, 0), scale=.3) ] quad = rc.EmptyEntity(name='quadrotor') for torus in toruses: torus.uniforms['diffuse'] = (0.0, 0.0, 0.0) quad.add_children(torus) quad.scale = .5 return quad
def __make_arrow(self, color): arrow_top = self.obj_reader.get_mesh("Cone") arrow_bottom = self.obj_reader.get_mesh("Cylinder") arrow_bottom.scale.xyz = 0.3, 0.25, 0.3 arrow_top.scale.xyz = 0.5, 0.25, 0.5 arrow_bottom.position.xyz = 0, 0.25, 0 arrow_bottom.uniforms['diffuse'] = color arrow_bottom.uniforms['flat_shading'] = False arrow_top.position.xyz = 0, 0.375, 0 arrow_top.uniforms['diffuse'] = color arrow_top.uniforms['flat_shading'] = False arrow = rc.EmptyEntity() arrow.add_children(arrow_top, arrow_bottom) return arrow
def __init_window(self, width, height, name): self.window = pyglet.window.Window(width=width, height=height, caption=name) self.world = rc.EmptyEntity(name='world') self.world.position.xyz = 0, 0, -2 self.world.scale = 0.5 self.grid = self.__make_grid() self.scene = rc.Scene(meshes=self.world) self.scene.camera.position.xyz = 0, 0, 2 self.world.rotation.xyz = 45, 45, 0 self.__reset_drawing() #This is called on the draw event @self.window.event def on_draw(): with rc.default_shader: self.scene.draw() [lbl.draw() for lbl in self.labels] self.__reset_drawing() #This is called during a mouse drag event @self.window.event def on_mouse_drag(x, y, dx, dy, buttons, modifiers): rot_speed = 1.0 self.world.rotation.y += dx * rot_speed self.world.rotation.x -= dy * rot_speed #Register keyboard handler keys = key.KeyStateHandler() self.window.push_handlers(keys) def on_key_press(dt): camera_speed = 3 if keys[key.W]: self.scene.camera.position.z -= camera_speed * dt if keys[key.S]: self.scene.camera.position.z += camera_speed * dt if keys[key.R]: self.scene.camera.position.xyz = 0, 0, 0 self.world.rotation.xyz = 0, 0, 0 #Processes key presses on a clock schedule pyglet.clock.schedule(on_key_press)
def view_arenafit(motive_filename, projector_filename, arena_filename, screen): # """Displays mesh in .obj file. Useful for checking that files are rendering properly.""" reader = rc.WavefrontReader(arena_filename) arena = reader.get_mesh('Arena', mean_center=True) arena.rotation = arena.rotation.to_quaternion() print('Arena Loaded. Position: {}, Rotation: {}'.format(arena.position, arena.rotation)) camera = rc.Camera.from_pickle(projector_filename) camera.projection.fov_y = 39 light = rc.Light(position=(camera.position.xyz)) root = rc.EmptyEntity() root.add_child(arena) sphere = rc.WavefrontReader(rc.resources.obj_primitives).get_mesh('Sphere', scale=.05) root.add_child(sphere) scene = rc.Scene(meshes=root, 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()[screen] window = pyglet.window.Window(fullscreen=True, screen=screen) label = pyglet.text.Label() fps_display = FPSDisplay(window) shader = rc.Shader.from_file(*rc.resources.genShader) @window.event def on_draw(): with shader: scene.draw() # label.draw() # window.clear() fps_display.draw() @window.event def on_resize(width, height): camera.projection.aspect = float(width) / height @window.event def on_key_release(sym, mod): if sym == key.UP: scene.camera.projection.fov_y += .5 elif sym == key.DOWN: scene.camera.projection.fov_y -= .5 import motive motive.initialize() motive.load_project(motive_filename.encode()) motive.update() rb = motive.get_rigid_bodies()['Arena'] # for el in range(3): # rb.reset_orientation() def update_arena_position(dt): motive.update() arena.position.xyz = rb.location arena.rotation.xyzw = rb.rotation_quats arena.update() sphere.position.xyz = rb.location label.text = "aspect={}, fov_y={}, ({:2f}, {:2f}, {:2f}), ({:2f}, {:2f}, {:2f})".format(scene.camera.projection.aspect, scene.camera.projection.fov_y, *(arena.position.xyz + rb.location)) pyglet.clock.schedule(update_arena_position) pyglet.app.run()
# Insert filename into WavefrontReader. obj_filename = rc.resources.obj_primitives obj_reader = rc.WavefrontReader(obj_filename) # Create Meshes sun = obj_reader.get_mesh("Sphere", name='sun') merkury = obj_reader.get_mesh("Sphere", scale=.1, name='merkury') venus = obj_reader.get_mesh("Sphere", scale=.2, name='venus') earth = obj_reader.get_mesh("Sphere", scale=.2, name='earth') mars = obj_reader.get_mesh("Sphere", scale=.2, name='mars') jupyter = obj_reader.get_mesh("Sphere", scale=.4, name='jupyter') moon = obj_reader.get_mesh("Sphere", scale=.5, name='moon') # Create Empty Entities empty_merkury = rc.EmptyEntity(name='sun_merkury') empty_venus = rc.EmptyEntity(name='sun_venus') empty_earth = rc.EmptyEntity(name='sun_earth') empty_mars = rc.EmptyEntity(name='sun_mars') empty_jupyter = rc.EmptyEntity(name='sun_jupyter') # Define Relationships sun.add_children(empty_merkury, empty_earth, empty_venus, empty_mars, empty_jupyter) empty_merkury.add_child(merkury) empty_venus.add_child(venus) empty_earth.add_child(earth) empty_mars.add_child(mars) empty_jupyter.add_child(jupyter)
def __make_vector(self): vector = self.__make_arrow((0, 0, 0)) vector.scale.xyz = 0.2, 2.0, 0.2 vec = rc.EmptyEntity(name='vector') vec.add_children(vector) return vec
monkey2.uniforms['flat_shading'] = False monkey2.uniforms['spec_weight'] = 0 monkey3 = obj_reader.get_mesh("Monkey", position=(0, 0, -3), name='specular') monkey3.uniforms['flat_shading'] = False monkey3.uniforms['spec_weight'] = 30 monkey4 = obj_reader.get_mesh("Monkey", position=(0, 0, -3), name='specular') monkey4.uniforms['flat_shading'] = False monkey4.uniforms['spec_weight'] = 30 monkey5 = obj_reader.get_mesh("Monkey", position=(0, 0, -3), name='specular') monkey5.uniforms['flat_shading'] = False monkey5.uniforms['spec_weight'] = 30 meshes = cycle([monkey, monkey2, monkey3, monkey4, monkey5, rc.EmptyEntity()]) mesh = [next(meshes)] img = rc.Texture.from_image(rc.resources.img_colorgrid) def change_mesh(dt): mesh[0] = next(meshes) pyglet.clock.schedule_interval(change_mesh, 4) @window.event def on_draw(): rc.clear_color(*plane.uniforms['diffuse'])