コード例 #1
0
ファイル: view_mesh.py プロジェクト: ratcave/ratcave_utils
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()
コード例 #2
0
ファイル: shadow_demo.py プロジェクト: AtiMotors/ratcave
monkey = obj_reader.get_mesh("Monkey")
monkey.uniforms['flat_shading'] = False

monkey.position.xyz = 0, 0, -4
monkey.scale.xyz = .25
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]:
コード例 #3
0
ファイル: renderer.py プロジェクト: Fabalinks/Beacon_task
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()
コード例 #4
0
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()
コード例 #5
0
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(
    'calibration_assets/projector.pkl'
)  # settign the pickle filled of the projector, which gives us the coordinates of where the projector is
projector.projection = rc.PerspectiveProjection(fov_y=41.5, aspect=1.777777778)
light = rc.Light(position=projector.position)

## Make Virtual Scene ##
virtual_arena = arena_reader.get_mesh('Arena')
wall = arena_reader.get_mesh("Plane")
wall.parent = virtual_arena
rat_camera = rc.Camera(projection=rc.PerspectiveProjection(aspect=1,
                                                           fov_y=90,
                                                           z_near=.001),
                       position=rat_rb.position
                       )  # settign the camera to be on top of the rats head
virtual_scene = rc.Scene(
    meshes=[wall, virtual_arena],
    light=light,
    camera=rat_camera,
    bgColor=(0, 0, 255)
コード例 #6
0
	GROWING = 1
	SHRINKING = 2
	PLASMA = 3


# pyglet inits
window = pyglet.window.Window(width=1280, height=960, screen=0)
keys = key.KeyStateHandler()
window.push_handlers(keys)

# ratcave inits
default = rc.WavefrontReader(rc.resources.obj_primitives)
scene = rc.Scene(
	meshes=[],
	bgColor=(45 / 255, 45 / 255, 45 / 255),
	light=rc.Light(position=(0, 10_000, 0)),
	camera=rc.Camera(position=(0, 7, 7), rotation=(-45, 0, 0)))


class Planet:
	def __init__(self, diameter, pos=(0, 0, 0), day=24, period=None, name="", children=None, schedule=False):
		"""
		:param diameter:
			radius of planet (0-1, not to scale)
		:param pos:
			distance from the sun in 3 axis (AU)
		:param period:
			orbital period (days to encircle the parent-celestial-object), 365.2 in earth's case
		:param day:
			hours in this object's day
		"""