def test_translation( self ): root = SceneNode( '/root' ) child = SceneNode( '/child' ) root.add_child( child ) # # Initial state # test_translation( self, root.transform, [0.0, 0.0, 0.0] ) test_translation( self, root.world_transform, [0.0, 0.0, 0.0] ) test_translation( self, child.transform, [0.0, 0.0, 0.0] ) test_translation( self, child.world_transform, [0.0, 0.0, 0.0] ) # # Root Translate += 1.0, 1.0, 1.0 # root.transform.translation += [1.0, 1.0, 1.0] test_translation( self, root.transform, [1.0, 1.0, 1.0] ) test_translation( self, root.world_transform, [1.0, 1.0, 1.0] ) test_translation( self, child.transform, [0.0, 0.0, 0.0] ) test_translation( self, child.world_transform, [1.0, 1.0, 1.0] ) # # Child Translate -= 1.0, 1.0, 1.0 # child.transform.translation -= [1.0, 1.0, 1.0] test_translation( self, root.transform, [1.0, 1.0, 1.0] ) test_translation( self, root.world_transform, [1.0, 1.0, 1.0] ) test_translation( self, child.transform, [-1.0,-1.0,-1.0] ) test_translation( self, child.world_transform, [0.0, 0.0, 0.0] )
def test_child( self ): root = SceneNode( '/root' ) child = SceneNode( '/child' ) root.add_child( child ) test_initial_state( self, root ) test_initial_state( self, child )
def create_cubes(): # create a grid of cubes self.grid_root = SceneNode( 'grid_root' ) self.scene_root.add_child( self.grid_root ) self.grid_root.transform.scale = [2.0, 2.0, 2.0] # create a number of cubes # the grid will extend from -5 to +5 x,z = numpy.mgrid[ -5:5:11j, -5:5:11j ] x = x.flatten() z = z.flatten() positions = numpy.vstack( (x, numpy.zeros( x.shape ), z ) ) positions = positions.T # set the distance of the cubes # cube is -1 -> 1 # so distance is 2 positions *= 2.5 # store a list of renderables self.renderables = [] for position in positions: node = SceneNode( 'node-%s' % position ) node.transform.inertial.translation = position self.grid_root.add_child( node ) self.renderables.append( node ) # create a range of colours from 0.1 -> 0.5 self.cube_colours = numpy.linspace( 0.1, 0.5, len(positions) ) # make them consistent for RGBA self.cube_colours = self.cube_colours.repeat( 4 ) self.cube_colours.shape = (-1, 4) # replace the Blue and Alpha value self.cube_colours[:,2] = 0.5 self.cube_colours[:,3] = 0.5
def setup_scene(self): # create an fps display self.fps_display = pyglet.clock.ClockDisplay() # create a list of renderables self.renderables = [] # create a scene self.scene_node = SceneNode('root') self.grid_node = SceneNode('grid') self.scene_node.add_child(self.grid_node) self.grid_render_node = RenderCallbackNode('mesh', grid.initialise_grid, grid.render_grid) self.grid_node.add_child(self.grid_render_node) # add to our list of renderables self.renderables.append(self.grid_render_node) # move the grid backward so we can see it # and move it down so we start above it self.grid_node.transform.inertial.translate([0.0, 0.0, -80.0]) # create a camera and a view matrix self.view_matrix = ProjectionViewMatrix(self.viewport.aspect_ratio, fov=45.0, near_clip=1.0, far_clip=200.0) # create a camera self.camera = CameraNode('camera', self.view_matrix) self.scene_node.add_child(self.camera) # move the camera up so it starts above the grid self.camera.transform.inertial.translate([0.0, 20.0, 0.0]) # assign a camera controller # we'll use the 6 degrees of freedom # camera for this one self.camera_controller = SixDOF_Controller(self.camera.transform)
def setup_scene(self): """Creates the scene to be rendered. Creates our camera, scene graph, """ # don't call 'SimpleApplication's setup_scene CoreApplication.setup_scene(self) # setup our GL state # enable z buffer glEnable(GL_DEPTH_TEST) # enable back face culling glEnable(GL_CULL_FACE) glCullFace(GL_BACK) # load our texture # use the PIL decoder as the pyglet one is broken # and loads most images as greyscale path = os.path.join(os.path.dirname(__file__), '../../data/md2/sydney.bmp') image = Image.open(path) self.texture = Texture2D(GL_TEXTURE_2D) self.texture.bind() self.texture.set_min_mag_filter(min=GL_LINEAR, mag=GL_LINEAR) # load the image from PIL # MD2 textures are inverted pygly.pil_texture.set_pil_image(self.texture, image, flip=False) self.texture.unbind() # create a grid of cubes self.grid_root = SceneNode('grid_root') self.scene_node.add_child(self.grid_root) # store a list of renderables path = os.path.join(os.path.dirname(__file__), '../../data/md2/sydney.md2') self.mesh_node = RenderCallbackNode('mesh', None, self.render_node) # rotate the mesh to face the camera self.mesh_node.transform.object.rotate_y(math.pi) self.mesh_node.mesh = MD2_Mesh(path) self.mesh_node.mesh.load() # attach to our scene graph self.grid_root.add_child(self.mesh_node) # scale the node #self.mesh_node.transform.scale = 0.2 # store current animation self.animation_number = 0 self.set_animation(self.animation_number)
def test_scale( self ): root = SceneNode( '/root' ) child = SceneNode( '/child' ) root.add_child( child ) # # Initial state # test_scale( self, root.transform, [1.0, 1.0, 1.0] ) test_scale( self, root.world_transform, [1.0, 1.0, 1.0] ) test_scale( self, child.transform, [1.0, 1.0, 1.0] ) test_scale( self, child.world_transform, [1.0, 1.0, 1.0] ) # # Root Scale = 2.0 # root.transform.scale = [2.0, 2.0, 2.0] test_scale( self, root.transform, [2.0, 2.0, 2.0] ) test_scale( self, root.world_transform, [2.0, 2.0, 2.0] ) test_scale( self, child.transform, [1.0, 1.0, 1.0] ) test_scale( self, child.world_transform, [2.0, 2.0, 2.0] ) # # Child World Scale = 2.0 # child.world_transform.scale = [1.0, 1.0, 1.0] test_scale( self, root.transform, [2.0, 2.0, 2.0] ) test_scale( self, root.world_transform, [2.0, 2.0, 2.0] ) test_scale( self, child.transform, [0.5, 0.5, 0.5] ) test_scale( self, child.world_transform, [1.0, 1.0, 1.0] ) # # Root Scale = 1.0 # root.transform.scale = [1.0, 1.0, 1.0] test_scale( self, root.transform, [1.0, 1.0, 1.0] ) test_scale( self, root.world_transform, [1.0, 1.0, 1.0] ) test_scale( self, child.transform, [0.5, 0.5, 0.5] ) test_scale( self, child.world_transform, [0.5, 0.5, 0.5] )
def setup_scene(self): # create an fps display self.fps_display = pyglet.clock.ClockDisplay() # store a list of our renderables self.renderables = [] # create a scene self.scene_node = SceneNode('root') self.mesh_node = SceneNode('obj') self.scene_node.add_child(self.mesh_node) # create a mesh object and render node self.mesh = OBJ_Mesh('examples/data/obj/cessna.obj') self.mesh_render_node = RenderCallbackNode('mesh', self.initialise_mesh, self.render_mesh) self.mesh_node.add_child(self.mesh_render_node) # add to our list of renderables self.renderables.append(self.mesh_render_node) # create a camera and a view matrix self.view_matrix = ProjectionViewMatrix(self.viewport.aspect_ratio, fov=45.0, near_clip=1.0, far_clip=200.0) # create a camera self.camera = CameraNode('camera', self.view_matrix) self.scene_node.add_child(self.camera) # move the camera so we can see the model self.camera.transform.object.translate([0.0, 20.0, 30.0]) # rotate the camera so it is pointing down self.camera.transform.object.rotate_x(-math.pi / 4.0)
def setup_scene(): # create a scene # we'll create the scene as a tree # to demonstrate the depth-first iteration # technique we will use to render it self.scene_root = SceneNode( 'root' ) # the letter indicates the tier the node # is on, a = tier 1, b = tier 2, etc. self.a1 = SceneNode( 'a1' ) self.b1 = SceneNode( 'b1' ) self.b2 = SceneNode( 'b2' ) self.c1 = SceneNode( 'c1' ) self.c2 = SceneNode( 'c2' ) self.c3 = SceneNode( 'c3' ) # the tree looks as follows # / c1 # / b1 - c2 # root - a1 # \ b2 - c3 self.scene_root.add_child( self.a1 ) self.a1.add_child( self.b1 ) self.a1.add_child( self.b2 ) self.b1.add_child( self.c1 ) self.b1.add_child( self.c2 ) self.b2.add_child( self.c3 ) # if we set the nodes local scale (transform) # it will be affected by the parent's scale. # by setting the world scale (world_transform) # we are ignoring the parent's scale. # re-attaching the node would invalidate this. self.a1.world_transform.scale = [2.0, 2.0, 2.0] self.b1.world_transform.scale = [1.0, 1.0, 1.0] self.b2.world_transform.scale = [1.0, 1.0, 1.0] self.c1.world_transform.scale = [0.8, 0.8, 0.8] self.c2.world_transform.scale = [0.8, 0.8, 0.8] self.c3.world_transform.scale = [0.8, 0.8, 0.8] # move our scene nodes # leave a1 at the centre self.b1.transform.object.translate( [10.0, 0.0, 0.0 ] ) self.b2.transform.object.translate([-10.0, 0.0, 0.0 ] ) self.c1.transform.object.translate( [ 5.0, 0.0, 0.0 ] ) self.c2.transform.object.translate( [-5.0, 0.0, 0.0 ] ) self.c3.transform.object.translate( [ 5.0, 0.0, 0.0 ] ) # rotate the our b nodes so they tilting forward self.b1.transform.object.rotate_x( math.pi / 4.0 ) self.b2.transform.object.rotate_x( math.pi / 4.0 )
def test_translation_with_rotation( self ): root = SceneNode( '/root' ) child = SceneNode( '/child' ) root.add_child( child ) # # Rotate 180 deg (1 * pi) about the Y axis (yaw) # root.transform.object.rotate_y( math.pi ) identity = matrix44.identity() root_matrix = matrix44.create_from_y_rotation( math.pi ) # # Translate the child node # child.transform.translation = [1.0, 1.0, 1.0] test_translation( self, root.transform, [0.0, 0.0, 0.0] ) test_translation( self, root.world_transform, [0.0, 0.0, 0.0] ) test_translation( self, child.transform, [1.0, 1.0, 1.0] ) # Y does not invert test_translation( self, child.world_transform, [-1.0, 1.0,-1.0] )
def setup_camera(): # add another node that our camera will be under # we can rotate this node to show how the cameras work self.camera2_parent = SceneNode('camera2_parent') self.scene_root.add_child(self.camera2_parent) # create our camera node self.camera2 = CameraNode('camera2', pyrr.matrix44.create_identity()) self.camera2_parent.add_child(self.camera2) # place the camera at the same position as the previous one #self.camera2.transform.translation = self.camera.transform.translation #self.camera2.transform.orientation = self.camera.transform.orientation #self.camera2.transform.scale = self.camera.transform.scale self.camera2.transform.object.translate([0.0, 0.0, 35.0])
def setup_scene(self): """Creates the scene to be rendered. Creates our camera, scene graph, """ # don't call 'SimpleApplication's setup_scene CoreApplication.setup_scene(self) # setup our GL state # enable z buffer glEnable(GL_DEPTH_TEST) # enable back face culling glEnable(GL_CULL_FACE) glCullFace(GL_BACK) # create a grid of cubes self.grid_root = SceneNode('grid_root') self.scene_node.add_child(self.grid_root) # store a list of renderables path = os.path.join(os.path.dirname(__file__), '../data/obj/capsule.obj') self.mesh_node = RenderCallbackNode('mesh', None, self.render_node) # rotate the mesh to face the camera self.mesh_node.transform.object.rotate_y(math.pi) self.mesh_node.mesh = OBJ_Mesh(path) self.mesh_node.mesh.load() # attach to our scene graph self.grid_root.add_child(self.mesh_node) # scale the node self.mesh_node.transform.scale = 1.0 # create a list of groups to render # by default, render all groups # this may be in-efficient if data is contained in # multiple groups self.groups = self.mesh_node.mesh.data.meshes.keys()
def setup_scene(): # create a scene # we'll create the scene as a tree # to demonstrate the depth-first iteration # technique we will use to render it self.scene_root = SceneNode('root')
def test_rotation( self ): """ Rotation and Inheritance """ # we'll add a child to a root node # we'll move the child # rotate the root # and check the child is where it should be # the child should be moved somewhere that will # make it easy to check root = SceneNode( '/root' ) child = SceneNode( '/child' ) root.add_child( child ) # # Rotate 180 deg (1 * pi) about the Y axis (yaw) # root.transform.object.rotate_y( math.pi ) identity = matrix44.identity() root_matrix = matrix44.create_from_y_rotation( math.pi ) # root object test_axis( self, root.transform.object, root_matrix ) test_axis( self, root.transform.inertial, identity ) test_axis( self, root.world_transform.object, root_matrix ) test_axis( self, root.world_transform.inertial, identity ) child_matrix = matrix44.identity() test_axis( self, child.transform.object, child_matrix ) test_axis( self, child.transform.inertial, identity ) test_axis( self, child.world_transform.object, root_matrix ) test_axis( self, child.world_transform.inertial, identity ) # check the node matrix matches what we're seeing in # the transform axis values self.assertTrue( numpy.allclose( root.transform.matrix, root_matrix ), "Root Local Matrix incorrect" ) self.assertTrue( numpy.allclose( root.world_transform.matrix, root_matrix ), "Root RootMatrix incorrect" ) self.assertTrue( numpy.allclose( child.transform.matrix, identity ), "Child Local Matrix incorrect" ) self.assertTrue( numpy.allclose( child.world_transform.matrix, root_matrix ), "Child RootMatrix incorrect" ) # # Rotate 180 deg (1 * pi) about the X axis (pitch) # # rotate 180 deg / 1pi about the x axis (pitch) child.transform.object.rotate_x( math.pi ) child_matrix = matrix44.multiply( matrix44.create_from_x_rotation( math.pi ), child_matrix ) child_world = matrix44.multiply( child_matrix, root_matrix ) # root object test_axis( self, root.transform.object, root_matrix ) test_axis( self, root.transform.inertial, identity ) test_axis( self, root.world_transform.object, root_matrix ) test_axis( self, root.world_transform.inertial, identity ) test_axis( self, child.transform.object, child_matrix ) test_axis( self, child.transform.inertial, identity ) test_axis( self, child.world_transform.object, child_world ) test_axis( self, child.world_transform.inertial, identity ) # check the node matrix matches what we're seeing in # the transform axis values self.assertTrue( numpy.allclose( root.transform.matrix, root_matrix ), "Root Local Matrix incorrect" ) self.assertTrue( numpy.allclose( root.world_transform.matrix, root_matrix ), "Root RootMatrix incorrect" ) self.assertTrue( numpy.allclose( child.transform.matrix, child_matrix ), "Child Local Matrix incorrect" ) self.assertTrue( numpy.allclose( child.world_transform.matrix, child_world ), "Child RootMatrix incorrect" )
def setup_scene( self ): """Creates the scene to be rendered. Creates our camera, scene graph, """ # don't call 'SimpleApplication's setup_scene CoreApplication.setup_scene( self ) # setup our GL state # enable z buffer glEnable( GL_DEPTH_TEST ) # enable back face culling #glEnable( GL_CULL_FACE ) glDisable( GL_CULL_FACE ) #glCullFace( GL_BACK ) # create a grid of cubes self.grid_root = SceneNode( 'grid_root' ) self.scene_node.add_child( self.grid_root ) self.mesh_node = RenderCallbackNode( 'mesh', None, self.render_node ) # rotate the mesh to face the camera self.mesh_node.transform.object.rotate_x( -math.pi * 0.5 ) # store a list of renderables mesh_path = os.path.join( os.path.dirname( __file__ ), '../data/md5/boblampclean.md5mesh' #'../data/md5/md5/cyberdemon/cyberdemon.md5mesh' ) anim_path = os.path.join( os.path.dirname( __file__ ), '../data/md5/boblampclean.md5anim' #'../data/md5/md5/cyberdemon/idle.md5anim' ) # load our md5 data self.md5mesh = MD5_Mesh() self.md5mesh.load( mesh_path ) self.md5anim = MD5_Anim() self.md5anim.load( anim_path ) # load the gl mesh self.mesh_node.mesh = Mesh( self.md5mesh ) # load our bindpose self.mesh_node.baseframe = BaseFrameSkeleton( self.md5mesh ) # load some test skeletons self.mesh_node.anim = Animation( self.md5anim ) # set to base frame self.mesh_node.mesh.set_skeleton( self.mesh_node.baseframe ) # load a skeleton renderer self.mesh_node.skeleton = SkeletonRenderer() #self.mesh_node.skeleton.set_skeleton( self.mesh_node.baseframe ) self.mesh_node.skeleton.set_skeleton( self.mesh_node.anim.skeleton( 0 ) ) # create a list of frames self.frames = [ skeleton for skeleton in self.mesh_node.anim ] #self.frames = [] self.frames.append( self.mesh_node.baseframe ) self.frame = 0 self.time_accumulator = 0.0 self.time_per_frame = 1.0 / self.mesh_node.anim.frame_rate # attach to our scene graph self.grid_root.add_child( self.mesh_node ) # scale the node self.mesh_node.transform.scale = 0.5
def test_initial_axis( self ): root = SceneNode( '/root' ) test_initial_state( self, root )
def test_tree( self ): root = SceneNode( 'root' ) child1_1 = SceneNode( 'child1_1' ) child1_2 = SceneNode( 'child1_2' ) child2_1 = SceneNode( 'child2_1' ) child2_2 = SceneNode( 'child2_2' ) root.add_child( child1_1 ) root.add_child( child1_2 ) child1_1.add_child( child2_1 ) child1_2.add_child( child2_2 ) self.assertTrue( child1_1.parent is root, "Parent not set correctly" ) self.assertTrue( child1_1 in root.children, "Child not in parents child list" ) self.assertTrue( child1_2.parent is root, "Parent not set correctly" ) self.assertTrue( child1_2 in root.children, "Child not in parents child list" ) self.assertTrue( child2_1.parent is child1_1, "Parent not set correctly" ) self.assertTrue( child2_1 in child1_1.children, "Child not in parents child list" ) self.assertTrue( child2_2.parent is child1_2, "Parent not set correctly" ) self.assertTrue( child2_2 in child1_2.children, "Child not in parents child list" ) child1_2.remove_child( child2_2 ) child1_1.add_child( child2_2 ) self.assertTrue( child2_2.parent is child1_1, "Parent not set correctly" ) self.assertTrue( child2_2 in child1_1.children, "Child not in parents child list" ) self.assertFalse( child2_2 in child1_2.children, "Child not removed from parents child list" )
def setup_scene(self): """Creates the scene to be rendered. Creates our camera, scene graph, """ # don't call 'SimpleApplication's setup_scene CoreApplication.setup_scene(self) # setup our GL state # enable z buffer glEnable(GL_DEPTH_TEST) # enable back face culling glEnable(GL_CULL_FACE) glCullFace(GL_BACK) # load our texture # use the PIL decoder as the pyglet one is broken # and loads most images as greyscale path = os.path.join(os.path.dirname(__file__), '../../data/md2/sydney.bmp') image = Image.open(path) self.texture = Texture2D(GL_TEXTURE_2D) self.texture.bind() self.texture.set_min_mag_filter(min=GL_LINEAR, mag=GL_LINEAR) # load the image from PIL # MD2 textures are inverted pygly.pil_texture.set_pil_image(self.texture, image, flip=False) self.texture.unbind() # create a grid of cubes self.grid_root = SceneNode('grid_root') self.scene_node.add_child(self.grid_root) # create a number of cubes # the grid will extend from -5 to +5 x, z = numpy.mgrid[-5:5:11j, -5:5:11j] x = x.flatten() z = z.flatten() positions = numpy.vstack((x, numpy.zeros(x.shape), z)) positions = positions.T # set the distance between the models positions *= 4.5 # store a list of renderables self.renderables = [] path = os.path.join(os.path.dirname(__file__), '../../data/md2/sydney.md2') for position in positions: node = RenderCallbackNode('node-%s' % position, None, self.render_node) node.mesh = MD2_Mesh(path) node.mesh.load() # attach to our scene graph self.grid_root.add_child(node) self.renderables.append(node) # move and scale the node node.transform.inertial.translation = position node.transform.scale = 0.2 # create a range of animation times # 0.0 <= x < num_frames self.frames = numpy.linspace(0.0, float( self.renderables[0].mesh.num_frames), len(positions), endpoint=False) # create an array that will store our frame rates self.frame_rate = numpy.zeros(len(self.renderables), dtype=numpy.float) self.animation = ''