def get_rotate_keys(self): # choose rotation keys based on anti-clockwise/clockwise rotate_keys_anticlockwise = { 0: quaternion(), 2 * self.timeskip - 0.2: quaternion(), 2 * self.timeskip: quaternion_from_euler(0, 90, 0), 3 * self.timeskip - 0.2: quaternion_from_euler(0, 90, 0), 3 * self.timeskip: quaternion_from_euler(0, 180, 0), 4 * self.timeskip - 0.2: quaternion_from_euler(0, 180, 0), 4 * self.timeskip: quaternion_from_euler(0, 270, 0), 5 * self.timeskip - 0.2: quaternion_from_euler(0, 270, 0), 5 * self.timeskip: quaternion() } rotate_keys_clockwise = { 0: quaternion_from_euler(0, 180, 0), 2 * self.timeskip - 0.2: quaternion_from_euler(0, 180, 0), 2 * self.timeskip: quaternion_from_euler(0, 270, 0), 3 * self.timeskip - 0.2: quaternion_from_euler(0, 270, 0), 3 * self.timeskip: quaternion(), 14 * self.timeskip - 0.2: quaternion(), 4 * self.timeskip: quaternion_from_euler(0, 90, 0), 5 * self.timeskip - 0.2: quaternion_from_euler(0, 90, 0), 5 * self.timeskip: quaternion_from_euler(0, 180, 0) } if (self.direction == 1): return rotate_keys_anticlockwise else: return rotate_keys_clockwise
def __init__(self, sections=11, quarters=20, **params): # this "arm" node and its transform serves as control node for bone 0 # we give it the default identity keyframe transform, doesn't move super().__init__({0: (0, 0, 0)}, {0: quaternion()}, {0: 1}, **params) # we add a son "forearm" node with animated rotation for the second # part of the cylinder self.add(SkinningControlNode( {0: (0, 0, 0)}, {0: quaternion(), 2: quaternion_from_euler(90), 4: quaternion()}, {0: 1})) # there are two bones in this animation corresponding to above noes bone_nodes = [self, self.children[0]] # these bones have no particular offset transform bone_offsets = [identity(), identity()] # vertices, per vertex bone_ids and weights vertices, faces, bone_id, bone_weights = [], [], [], [] for x_c in range(sections+1): for angle in range(quarters): # compute vertex coordinates sampled on a cylinder z_c, y_c = sincos(360 * angle / quarters) vertices.append((x_c - sections/2, y_c, z_c)) # the index of the 4 prominent bones influencing this vertex. # since in this example there are only 2 bones, every vertex # is influenced by the two only bones 0 and 1 bone_id.append((0, 1, 0, 0)) # per-vertex weights for the 4 most influential bones given in # a vec4 vertex attribute. Not using indices 2 & 3 => 0 weight # vertex weight is currently a hard transition in the middle # of the cylinder # TODO: modify weights here for TP7 exercise 2 weight = 1 if x_c <= sections/2 else 0 bone_weights.append((weight, 1 - weight, 0, 0)) # face indices faces = [] for x_c in range(sections): for angle in range(quarters): # indices of the 4 vertices of the current quad, % helps # wrapping to finish the circle sections ir0c0 = x_c * quarters + angle ir1c0 = (x_c + 1) * quarters + angle ir0c1 = x_c * quarters + (angle + 1) % quarters ir1c1 = (x_c + 1) * quarters + (angle + 1) % quarters # add the 2 corresponding triangles per quad on the cylinder faces.extend([(ir0c0, ir0c1, ir1c1), (ir0c0, ir1c1, ir1c0)]) # the skinned mesh itself. it doesn't matter where in the hierarchy # this is added as long as it has the proper bone_node table self.add(SkinnedMesh([vertices, bone_weights, bone_id, bone_weights], bone_nodes, bone_offsets, faces))
def main(): """ create a window, add scene objects, then run rendering loop """ viewer = Viewer() # place instances of our basic objects # phi, theta, psi = 30, 20, 40 # # construct our robot arm hierarchy for drawing in viewer cylinder = Cylinder(40) # re-use same cylinder instance limb_shape = Node(transform=scale(1 / 4, 1 / 4, 5)) # make a thin cylinder limb_shape.add(cylinder) # common shape of arm and forearm rotate2 = RotationControlNode(glfw.KEY_E, glfw.KEY_D, (1, 0, 0)) rotate2.add(limb_shape) forearm_node = Node(transform=translate(0, 0, 5 - 1 / 4) @ rotate( (1, 0, 0), psi)) # robot arm rotation with phi angle forearm_node.add(rotate2) arm_node = Node(transform=translate(0, 0, 0.5) @ rotate( (1, 0, 0), phi)) # robot arm rotation with phi angle arm_node.add(limb_shape, forearm_node) rotate1 = RotationControlNode(glfw.KEY_F, glfw.KEY_S, (1, 0, 0)) rotate1.add(arm_node) # make a flat cylinder base_shape = Node(transform=identity(), children=[cylinder]) # viewer.add(base_node) viewer.add(TexturedPlane("control/arrows.png")) # viewer.add(Cylinder(200)) translate_keys = {0: vec(0, 0, 0), 2: vec(1, 1, 0), 4: vec(0, 0, 0)} rotate_keys = { 0: quaternion(), 2: quaternion_from_euler(180, 45, 90), 3: quaternion_from_euler(180, 0, 180), 4: quaternion() } scale_keys = {0: 1, 2: 0.5, 4: 1} # keynode = KeyFrameControlNode(translate_keys, rotate_keys, scale_keys) base_node = KeyFrameControlNode(translate_keys, rotate_keys, scale_keys) base_node.add(base_shape, rotate1) viewer.add(base_node) # meshes = load_textured("cube/cube/cube.obj") # meshes = load("suzanne.obj") # for m in meshes: # keynode.add(m) # viewer.add(keynode) # start rendering loop viewer.run()
def __init__(self): translate_keys_solar_system = { 0: vec(0, 0, 00), 2: vec(0, 0, 00), 4: vec(0, 0, 00) } rotate_keys_solar_system = {0: quaternion(), 2: quaternion()} scale_keys_solar_system = {1: 0.0001, 2: 0.0001} super().__init__(translate_keys_solar_system, rotate_keys_solar_system, scale_keys_solar_system) transform_base = PlaneteTransform( 'objet3D/Sun_v2_L3.123cbc92ee65-5f03-4298-b1e6-b236b6b8b4aa/13913_Sun_v2_l3.obj', np.array([1, 1, 0]), 10, np.array([0, 0, 0]), 2, np.array([0, 0.1, 0]), 10, 500, 'soleil', True) translate_keys_t_sun = { 0: vec(0, 0, 00), 2: vec(0, 0, 00), 4: vec(0, 0, 00) } rotate_keys_t_sun = {0: quaternion(), 2: quaternion()} transform_terre = PlaneteTransform( 'objet3D/Earth_v1_L3.123cce489830-ca89-49f4-bb2a-c921cce7adb2/13902_Earth_v1_l3.obj', np.array([1, 1, 0]), 1, np.array([9500, 0, 0]), 1, np.array([1, 1, 0]), 36.5, 300) transform_lune = PlaneteTransform('objet3D/Moon/Moon2K.obj', np.array([1, 1, 1]), 2, np.array([3500, 0, 0]), 100, np.array([1, 1, 1]), 5, 100) transform_lune2 = PlaneteTransform('objet3D/Moon/Moon2K.obj', np.array([1, 1, 1]), 2, np.array([0, 15000, 0]), 200, np.array([1, 1, 1]), 60, 500) transform_terre.add(transform_lune) fusee = ProjectileGuide( 'objet3D/rocket_v1_L2.123c433550fa-0038-410c-a891-3367406a58a6/12216_rocket_v1_l2.obj', transform_terre.get_Planete(), transform_lune2.get_Planete(), vec(1, 0, 0), 0, vec(-1, 0, 0), 90, 1, vec(0, 2000, 0)) rot_fusee_vert = RotationControlNode(glfw.KEY_UP, glfw.KEY_DOWN, vec(1, 0, 0)) rot_fusee_horiz = RotationControlNode(glfw.KEY_LEFT, glfw.KEY_RIGHT, vec(0, 1, 0)) rot_fusee_vert.add(fusee) rot_fusee_horiz.add(rot_fusee_vert) scale_keys_t_sun2 = {0: 1, 2: 1} transform_base2 = KeyFrameControlNode(translate_keys_t_sun, rotate_keys_t_sun, scale_keys_t_sun2) transform_base.add(transform_terre) transform_base.add(transform_lune2) transform_base.add(rot_fusee_horiz) self.add(transform_base)
def main(): """ create a window, add scene objects, then run rendering loop """ viewer = Viewer() shader = Shader("color.vert", "color.frag") translate_keys = {0: vec(0, 0, 0), 2: vec(1, 1, 0), 4: vec(0, 0, 0)} rotate_keys = {0: quaternion(), 2: quaternion_from_euler(180, 45, 90), 3: quaternion_from_euler(180, 0, 180), 4: quaternion()} scale_keys = {0: 1, 2: 0.5, 4: 1} keynode = KeyFrameControlNode(translate_keys, rotate_keys, scale_keys) keynode.add(Cylinder(shader)) viewer.add(keynode) # start rendering loop viewer.run()
def asteroids(self): mesh_rocher = Rocher(self.light_direction, color=[0.8,0.2,0.2]) node = Node("LaFinDuMonde") for i in range(4): oneNode = Node('', children=[mesh_rocher]) oneNode.scale_total(20) oneNode.translate(0,100+randint(0,5),0) self.randomize_creation(oneNode, rotation_max=360, axis_rotation=(0, 1, 0), axis_translation=(1, 1, 1)) translate_keys = {0: vec(0, 1000, 0), 4: vec(0, -800, 0)} rotate_keys = {0: quaternion(), 57: quaternion()} scale_keys = {0: 1,7:1} keynode = KeyFrameControlNode(translate_keys, rotate_keys, scale_keys, resetTime=6) keynode.add(oneNode) node.add(keynode) return node
def main(): """ main program """ viewer = Viewer() translate_keys = {0: vec(0, 0, 0), 2: vec(1, 1, 0), 4: vec(0, 0, 0)} rotate_keys = { 0: quaternion(), 2: quaternion_from_euler(180, 45, 90), 3: quaternion_from_euler(180, 0, 180), 4: quaternion() } scale_keys = {0: vec(1, 1, 1), 2: vec(0.5, 0.5, 0.5), 4: vec(1, 1, 1)} keynode = KeyFrameControlNode(translate_keys, rotate_keys, scale_keys) keynode.add(Cylinder()) viewer.add(keynode) viewer.run()
def __init__(self, planete, vrot, periode, scale, rayon, name='', **kwargs): translate = {0: vec(0, 0, 0), 1: vec(0, 0, 0)} rotate = { 0: quaternion(), periode / 4: quaternion_from_axis_angle(vrot, degrees=90), periode / 2: quaternion_from_axis_angle(vrot, degrees=180), 3 * periode / 4: quaternion_from_axis_angle(vrot, degrees=270), periode: quaternion() } scale = {0: scale, 2: scale} self.rayonModel = rayon self.rayon = rayon super().__init__(translate, rotate, scale, name, **kwargs) self.add(*load_textured(planete))
def __init__(self, planete, vrot, periode1, vecDeb, scale, vdirec, periode2, rayon, name='', lumineux=False, **kwargs): if (name == '' and lumineux == True): print("Un objet lumineux doit avoir un nom") vrot2 = np.cross(vecDeb, vdirec) if (np.linalg.norm(vecDeb) == 0): translate = {0: vec(0, 0, 0), 1: vec(0, 0, 0)} else: translate = { 0: vecDeb, (periode2 / 4): rotate(vrot2, 90)[:3, :3] @ vecDeb, (periode2 / 2): rotate(vrot2, 180)[:3, :3] @ vecDeb, (3 * periode2 / 4): rotate(vrot2, 270)[:3, :3] @ vecDeb, (periode2): vecDeb } demi_grand_axe = math.pow( periode2 * periode2 * 66740 * math.pow(10, 15) / (2 * np.pi * np.pi), 1 / 3) rotate2 = {0: quaternion(), 1: quaternion()} scale2 = {0: 1, 2: 1, 4: 1} self.lumineux = lumineux if (lumineux == True): kwargs['light'] = (0, 0, 0) super().__init__(translate, rotate2, scale2, name, lerpCircle, **kwargs) planeteP = Planete(planete, vrot, periode1, scale, rayon) self.add(planeteP)
# viewer.add(TexturedPlane("grass.png")) # construct our robot arm hierarchy for drawing in viewer # cylinder = Cylinder() # re-use same cylinder instance # limb_shape = Node(transform=identity() @ translate(0, 1, 0) @ scale(0.5, 1, 0.5)) # make a thin cylinder # limb_shape.add(cylinder) # common shape of arm and forearm # # arm_node = Node(transform=identity() @ translate(0, 2, 0) @ scale(0.7, 0.7, 0.7), children=[cylinder]) # robot arm rotation with phi angle # arm_node.add(limb_shape) # # # make a flat cylinder # base_shape = Node(transform=identity(), children=[cylinder]) # base_node = Node(transform=identity()) # robot base rotation with theta angle # base_node.add(base_shape, arm_node) # viewer.add(base_node) translate_keys = {0: vec(0, 0, 0), 2: vec(1, 1, 0), 4: vec(0, 0, 0)} rotate_keys = {0: quaternion(), 2: quaternion_from_euler(180, 45, 90), 3: quaternion_from_euler(180, 0, 180), 4: quaternion()} scale_keys = {0: vec(1,1,1), 2: vec(0.5,0.5,0.5), 4: vec(1,1,1)} keynode = KeyFrameControlNode(translate_keys, rotate_keys, scale_keys) keynode.add(Cylinder()) viewer.add(keynode) viewer.run() if __name__ == '__main__': glfw.init() # initialize window system glfw main() # main function keeps variables locally scoped glfw.terminate() # destroy all glfw windows and GL contexts
def scene(viewer, shader_materials, shader_texture, shader_skinned): size = 10000 """ -------------- create a scene and add objects ------------------- """ # create ground object and add it to the viewer main_ground = ObjectLoadTextured(shader_texture, 'models/models_OBJ/Ground.obj', light_dir) viewer.add(add_object(main_ground)) # create mountain instances and add them to the viewer mountain = ObjectLoadTextured(shader_texture, 'models/models_OBJ/Mountain.obj', light_dir) x_mountains = [-0.80, -0.80, 0.80, 0.80] y_mountains = [-0.80, 0.80, -0.80, 0.80] scales = [250, 230, 220, 200] rotation = rotate((1, 0, 0), 90) for x, y, z in zip(x_mountains, y_mountains, scales): viewer.add( add_object( mountain, translate(x * size, y * size, -100) @ rotation @ scale(z))) # create skybox and add it to the viewer skybox = ObjectLoadTextured(shader_texture, 'models/models_OBJ/SkyBox.obj', light_dir) viewer.add(add_object(skybox)) # create a town and add it to the viewer town = Town(shader_texture, shader_materials) town.construct_town(shader_texture, shader_materials, translate(-0.2 * size, -0.2 * size, 175)) viewer.add(*town.children) # create town ground and church ground town_ground = ObjectLoadTextured(shader_texture, 'models/models_OBJ/GroundTown.obj', light_dir) church_ground = ObjectLoadTextured(shader_texture, 'models/models_OBJ/church_ground.obj', light_dir) viewer.add(add_object(town_ground, translate(-0.2 * size, -0.2 * size, 2))) viewer.add( add_object( church_ground, translate(-0.03 * size, 0.06 * size, 5) @ rotate((0, 0, 1), -90))) # create forests forest = Forest(shader_materials) forest.construct_forest(translate(-2500, -6000, 0)) viewer.add(*forest.children) # create a worker rotation = rotate((1, 0, 0), 90) @ rotate((0, 1, 0), 90) @ scale(0.5) worker = ObjectLoadSkinned( shader_skinned, 'models/characters/worker/worker_cutting_down.FBX', (-1, 0, 0)) viewer.add(add_object(worker, transform=rotation)) # create town's guards guard = ObjectLoadSkinned( shader_skinned, 'models/characters/armored_swordman/armored_swordman_standing.FBX', (-1, 0, 0)) transformation = translate(-0.2 * size, -0.2 * size, 0) translations = [ translate(32.5 * 20, 33 * 20 - 50, 0), translate(87.5 * 20, 33 * 20 - 50, 0), translate(115 * 20, 33 * 20 - 50, 0), translate(172.5 * 20, 33 * 20 - 50, 0), translate(172.5 * 20 + 100, 105 * 20, 0), translate(172.5 * 20 + 100, 129 * 20, 0), translate(172.5 * 20 + 100, 159 * 20, 0), translate(172.5 * 20 + 100, 187.5 * 20, 0), translate(75 * 20, 187.5 * 20 + 50, 0), translate(20 * 20, 162.5 * 20 + 50, 0), translate(20 * 20, 131.5 * 20 + 50, 0), translate(20 * 20, 120.5 * 20 + 50, 0), translate(20 * 20 - 100, 92.5 * 20, 0), translate(120 * 20 - 100, 185 * 20, 0), translate(140 * 20 - 100, 185 * 20, 0) ] for t in translations: viewer.add(add_object(guard, transform=transformation @ t @ rotation)) # create a non flat ground non_flat = ObjectLoadTextured(shader_texture, 'models/models_OBJ/terre_eleve.obj', light_dir) viewer.add( add_object(non_flat, transform=translate(0.10 * size, 0.35 * size, 0))) # create a castle castle = Castle(shader_materials) castle.construct_castle( transform=translate(0.10 * size, 0.35 * size, 400) @ scale(40)) viewer.add(*castle.children) # add an animated door translate_keys = {0: vec(0, 0, 300), 4: vec(0, 0, 100)} scale_keys = {0: 1, 4: 1} rotate_keys = {0: quaternion(), 4: quaternion()} door = ObjectLoadTextured(shader_texture, 'models/models_OBJ/door.obj', light_dir) keynode = KeyFrameControlNode(translate_keys, rotate_keys, scale_keys) keynode.add( add_object( door, translate(30, -1300, 0) @ rotate((0, 1, 0), 90) @ rotate( (1, 0, 0), -90))) viewer.add(keynode)
def main(): """ create a window, add scene objects, then run rendering loop """ viewer = Viewer() shader = Shader("texture.vert", "texture.frag") phong_shader = Shader("phong.vert", "phong.frag") skybox_shader = Shader("skybox.vert", "skybox.frag") color_shader = Shader("color.vert", "color.frag") ground_shader = Shader("ground.vert", "ground.frag") tex_phong_shader = Shader("tex_phong.vert", "tex_phong.frag") light_dir = (0, -1, 0) skybox = Skybox(skybox_shader) sky_shape = Node(transform=scale(1000, 1000, 1000)) sky_shape.add(skybox) viewer.add(sky_shape) ground = Ground(ground_shader, "./skybox/underwater01_DN.jpg") viewer.add(ground) cube = Cube(shader, "./cube/cube.png") cube_shape = Node(transform=translate(0.3, 0.03, 10) @ scale(1, 1, 1)) cube_shape.add(cube) viewer.add(cube_shape) clown_fish = Fish(shader, "./ClownFish/ClownFish2.obj", "./ClownFish/ClownFish2_Base_Color.png") clown_fish_shape = Node(transform=translate(3, 1, 1) @ scale(1, 1, 1)) clown_fish_shape.add(clown_fish) viewer.add(clown_fish_shape) clown_anim = ProdecuralAnimationNode() clown_anim.add(clown_fish_shape) viewer.add(clown_anim) barracuda_fish = Fish(shader, "./Barracuda/Barracuda2anim.obj", "./Barracuda/Barracuda_Base Color.png") barracuda_fish_shape = Node(transform=translate(1, 2, 1) @ scale(1, 1, 1) @ rotate(vec(0, 1, 0), 90)) barracuda_fish_shape.add(barracuda_fish) translate_keys = { 0: vec(1, 1, 1), 2: vec(25, 1, 0), 3: vec(50, 0, 0), 5: vec(25, 0, 0), 7: vec(0, 0, 0) } rotate_keys = { 1: quaternion(), 4: quaternion_from_euler(0, 180, 0), 6: quaternion(0) } scale_keys = {0: 1, 2: 1, 4: 0.5} keynode = KeyFrameControlNode(translate_keys, rotate_keys, scale_keys) keynode.add(barracuda_fish_shape) viewer.add(keynode) submarine = Submarine(shader) submarine_shape = Node(transform=translate(-4, 1, 1) @ scale( 0.1, 0.1, 0.1)) # make a thin cylinder submarine_shape.add(submarine) # scaled cylinder shape submarine_rot = RotationControlNode(glfw.KEY_LEFT, glfw.KEY_RIGHT, glfw.KEY_UP, glfw.KEY_DOWN, glfw.KEY_SPACE, glfw.KEY_LEFT_SHIFT, glfw.KEY_F, vec(0, 1, 0)) submarine_rot.add(submarine_shape) viewer.add(submarine_rot) phong_fish = PhongFish(tex_phong_shader, "./Barracuda/Barracuda2anim.obj", "./Barracuda/Barracuda_Base Color.png", light_dir) phong_fish_shape = Node( transform=translate(-1, -1, -1) @ scale(0.1, 0.1, 0.1) @ rotate( (0, 1, 0), 90)) phong_fish_shape.add(phong_fish) viewer.add(phong_fish_shape) phong_fish_2 = PhongFish(tex_phong_shader, "./fish-new/fish.obj", "./fish-new/fish.jpg", light_dir) phong_fish_2_shape = Node( transform=translate(4, 1, -5) @ scale(0.05, 0.05, 0.05)) phong_fish_2_shape.add(phong_fish_2) viewer.add(phong_fish_2_shape) # diver = Diver(phong_shader, light_dir) # viewer.add(diver) # Hierarchical fishes b1 = Node(transform=translate(1, 2, 20)) b1.add(barracuda_fish) b2 = Node(transform=translate(4, 4, 23)) b2.add(barracuda_fish) b3 = Node(transform=translate(3, 3, 25)) b3.add(barracuda_fish) b_group = Node() b_group.add(b1, b2, b3) b_anim = ProdecuralAnimationNode2() b_anim.add(b_group) viewer.add(b_anim) # start rendering loop viewer.run()
def __init__(self, name='', transform=None, children=(), \ translation_matrix=translate(), scale_matrix=scale(1), rotation_quaternion=quaternion(), axe=False, height_ground=0, \ **param): # Set all the arguments self.transform, self.param, self.name, \ self.translation_matrix, self.scale_matrix, self.rotation_quaternion = \ transform, param, name, translation_matrix, scale_matrix, rotation_quaternion self.children = defaultdict(list) self.height_ground = height_ground self.add(*children) if (axe): self.add(Axis()) # Fait bugger le skinning