Exemple #1
0
    def __init__(self):
        super(Application, self).__init__()

        # setup our opengl requirements
        config = pyglet.gl.Config(depth_size=16, double_buffer=True)

        # create our window
        self.window = pyglet.window.Window(fullscreen=False,
                                           width=1024,
                                           height=768,
                                           resizable=True,
                                           vsync=False,
                                           config=config)

        # create a viewport
        self.viewport = RatioViewport(self.window, [[0.0, 0.0], [1.0, 1.0]])

        # setup our scene
        self.setup_scene()

        # listen for on_draw events
        self.window.push_handlers(on_draw=self.on_draw)

        # setup our update loop the app
        # we'll render at 60 fps
        frequency = 60.0
        self.update_delta = 1.0 / frequency

        # over-ride the frequency and render at full speed
        self.update_delta = -1

        # use a pyglet callback for our render loop
        pyglet.clock.schedule_interval(self.step, self.update_delta)
Exemple #2
0
    def __init__( self ):
        super( Application, self ).__init__()
        
        # setup our opengl requirements
        config = pyglet.gl.Config(
            depth_size = 16,
            double_buffer = True
            )

        # create our window
        self.window = pyglet.window.Window(
            fullscreen = False,
            width = 1024,
            height = 768,
            resizable = True,
            vsync = False,
            config = config
            )

        # create a viewport
        self.viewport = RatioViewport(
            self.window,
            [ [0.0, 0.0], [1.0, 1.0] ]
            )

        # create our input devices
        self.keyboard = Keyboard( self.window )
        self.mouse = Mouse( self.window )

        # setup our scene
        self.setup_scene()

        # setup our text
        self.setup_text()

        # listen for on_draw events
        self.window.push_handlers(
            on_draw = self.on_draw
            )
        
        # setup our update loop the app
        # we'll render at 60 fps
        frequency = 60.0
        self.update_delta = 1.0 / frequency

        # over-ride the frequency and render at full speed
        self.update_delta = -1

        # use a pyglet callback for our render loop
        pyglet.clock.schedule_interval(
            self.step,
            self.update_delta
            )
Exemple #3
0
    def setup_window(self, caption):
        # setup our opengl requirements
        config = pyglet.gl.Config(depth_size=16, double_buffer=True, major_version=2, minor_version=1)

        # create our window
        self.window = pyglet.window.Window(
            fullscreen=False, width=1024, height=768, caption=caption, resizable=True, config=config
        )

        # listen for on_draw events
        self.window.push_handlers(on_draw=self.on_draw)

        # create a viewport
        self.viewport = RatioViewport(self.window, [[0.0, 0.0], [1.0, 1.0]])
Exemple #4
0
class Application(object):
    def __init__(self):
        super(Application, self).__init__()

        # setup our opengl requirements
        config = pyglet.gl.Config(depth_size=16, double_buffer=True)

        # create our window
        self.window = pyglet.window.Window(fullscreen=False,
                                           width=1024,
                                           height=768,
                                           resizable=True,
                                           vsync=False,
                                           config=config)

        # create a viewport
        self.viewport = RatioViewport(self.window, [[0.0, 0.0], [1.0, 1.0]])

        # setup our scene
        self.setup_scene()

        # listen for on_draw events
        self.window.push_handlers(on_draw=self.on_draw)

        # setup our update loop the app
        # we'll render at 60 fps
        frequency = 60.0
        self.update_delta = 1.0 / frequency

        # over-ride the frequency and render at full speed
        self.update_delta = -1

        # use a pyglet callback for our render loop
        pyglet.clock.schedule_interval(self.step, self.update_delta)

    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 run(self):
        pyglet.app.run()

    def step(self, dt):
        # rotate the mesh about it's own vertical axis
        self.mesh_node.transform.object.rotate_y(dt)

        # manually dispatch the on_draw event
        # as we patched it out of the idle loop
        self.window.dispatch_event('on_draw')

        # display the frame buffer
        self.window.flip()

    def on_draw(self):
        # render the scene
        self.render()

        # render the fps
        self.fps_display.draw()

    def initialise_mesh(self):
        # load the obj mesh from the file
        self.mesh.load()

    def render_mesh(self):
        # render the mesh
        self.mesh.render()

    def set_gl_state(self):
        # enable z buffer
        glEnable(GL_DEPTH_TEST)

        # enable smooth shading
        glShadeModel(GL_SMOOTH)

        # rescale only normals for lighting
        glEnable(GL_RESCALE_NORMAL)

        # enable scissoring for viewports
        glEnable(GL_SCISSOR_TEST)

        # enable back face culling
        glEnable(GL_CULL_FACE)
        glCullFace(GL_BACK)

        # setup lighting for our viewport
        glEnable(GL_LIGHTING)

        # set an ambient light level
        glAmbient = glLightModelfv(GL_LIGHT_MODEL_AMBIENT,
                                   (GLfloat * 4)(*[0.8, 0.8, 0.8, 1.0]))

        # create a light
        glEnable(GL_LIGHT0)
        glLightfv(GL_LIGHT0, GL_POSITION,
                  (GLfloat * 4)(*[0.0, 20.0, 30.0, 1.0]))

    def render(self):
        #
        # setup
        #

        # set our window
        self.window.switch_to()

        # activate our viewport
        self.viewport.switch_to()

        # scissor to our viewport
        self.viewport.scissor_to_viewport()

        # setup our viewport properties
        glPushAttrib(GL_ALL_ATTRIB_BITS)
        self.set_gl_state()

        # update the view matrix aspect ratio
        self.camera.view_matrix.aspect_ratio = self.viewport.aspect_ratio

        # apply our view matrix and camera translation
        self.camera.view_matrix.push_view_matrix()
        self.camera.push_model_view()

        #
        # render
        #

        # clear our frame buffer and depth buffer
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        for renderable in self.renderables:
            renderable.render()

        #
        # tear down
        #

        # pop our view matrix and camera translation
        self.camera.pop_model_view()
        self.camera.view_matrix.pop_view_matrix()

        # pop our viewport attributes
        glPopAttrib()

        #
        # reset state
        #

        # set our viewport to the entire window
        pygly.gl.set_scissor(pygly.window.create_rectangle(self.window))
        pygly.gl.set_viewport(pygly.window.create_rectangle(self.window))
Exemple #5
0
class BaseApplication(object):
    def __init__(self, caption=None):
        super(BaseApplication, self).__init__()

        self.setup_window(caption)
        self.setup_input()
        self.setup_ui()
        self.setup_scene_root()
        self.setup_camera()
        self.setup_scene()
        self.setup_events()

    def setup_window(self, caption):
        # setup our opengl requirements
        config = pyglet.gl.Config(depth_size=16, double_buffer=True, major_version=2, minor_version=1)

        # create our window
        self.window = pyglet.window.Window(
            fullscreen=False, width=1024, height=768, caption=caption, resizable=True, config=config
        )

        # listen for on_draw events
        self.window.push_handlers(on_draw=self.on_draw)

        # create a viewport
        self.viewport = RatioViewport(self.window, [[0.0, 0.0], [1.0, 1.0]])

    def setup_input(self):
        # create our keyboard device
        self.keyboard = Keyboard(self.window)

        # register for keypresses
        self.keyboard.digital.push_handlers(on_digital_input=self.on_key_event)

        # create our mouse device
        self.mouse = Mouse(self.window)

    def setup_events(self):
        # setup our update loop the app
        # we'll render at 60 fps
        frequency = 60.0
        self.update_delta = 1.0 / frequency

        # use a pyglet callback for our render loop
        pyglet.clock.schedule_interval(self.step, self.update_delta)

    def setup_ui(self):
        # create an fps display
        self.fps_display = pyglet.clock.ClockDisplay()

    def setup_scene_root(self):
        # create a list of renderables
        self.renderables = []

        # create a scene
        self.scene_node = SceneNode("root")

    def setup_camera(self):
        # 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)

    def setup_scene(self):
        # CREATE SCENE HERE
        pass

    def run(self):
        pyglet.app.run()

    def step(self, dt):
        # update our mouse
        self.update_mouse(dt)

        # update the scene here
        self.update_scene(dt)

        # manually dispatch the on_draw event
        # as we patched it out of the idle loop
        self.window.dispatch_event("on_draw")

        # display the frame buffer
        self.window.flip()

    def update_mouse(self, dt):
        # USE MOUSE VALUES HERE
        pass

        # reset the relative position of the mouse
        self.mouse.clear_delta()

    def on_key_event(self, digital, event, key):
        # HANDLE KEYBOARD INPUT HERE
        pass

    def update_scene(self, dt):
        # UPDATE SCENE HERE
        pass

    def on_draw(self):
        # render the scene
        self.render()

        # render the fps
        self.fps_display.draw()

    def render(self):
        #
        # setup
        #

        # set our window
        self.window.switch_to()

        # activate our viewport
        self.viewport.switch_to()

        # scissor to our viewport
        self.viewport.scissor_to_viewport()

        # setup our viewport properties
        glPushAttrib(GL_ALL_ATTRIB_BITS)

        # update the view matrix aspect ratio
        self.camera.view_matrix.aspect_ratio = self.viewport.aspect_ratio

        # apply our view matrix and camera translation
        self.camera.view_matrix.push_view_matrix()
        self.camera.push_model_view()

        #
        # render
        #

        # clear our frame buffer and depth buffer
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        self.render_3d()

        #
        # tear down
        #

        # pop our view matrix and camera translation
        self.camera.pop_model_view()
        self.camera.view_matrix.pop_view_matrix()

        # pop our viewport attributes
        glPopAttrib()

        #
        # reset state
        #

        # set our viewport to the entire window
        pygly.gl.set_scissor(pygly.window.create_rectangle(self.window))
        pygly.gl.set_viewport(pygly.window.create_rectangle(self.window))

        #
        # render 2d
        #

        self.render_2d()

    def render_2d(self):
        # render the fps
        self.fps_display.draw()
Exemple #6
0
class Application(object):
    def __init__(self):
        super(Application, self).__init__()

        # setup our opengl requirements
        config = pyglet.gl.Config(depth_size=16, double_buffer=True)

        # create our window
        self.window = pyglet.window.Window(fullscreen=False,
                                           width=1024,
                                           height=768,
                                           resizable=True,
                                           vsync=False,
                                           config=config)

        # create a viewport
        self.viewport = RatioViewport(self.window, [[0.0, 0.0], [1.0, 1.0]])

        # create our input devices
        self.keyboard = Keyboard(self.window)
        self.mouse = Mouse(self.window)

        # setup our scene
        self.setup_scene()

        # setup our text
        self.setup_text()

        # listen for on_draw events
        self.window.push_handlers(on_draw=self.on_draw)

        # setup our update loop the app
        # we'll render at 60 fps
        frequency = 60.0
        self.update_delta = 1.0 / frequency

        # over-ride the frequency and render at full speed
        self.update_delta = -1

        # use a pyglet callback for our render loop
        pyglet.clock.schedule_interval(self.step, self.update_delta)

    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_text(self):
        self.help_label = pyglet.text.HTMLLabel(
            """
<b>6-DOF Camera demo</b>
<ul>
<li>Mouse: look around</li>
<li>W,A,S,D: move around</li>
<li>Space: move up</li>
<li>Shift: move down</li>
</ul>
""",
            multiline=True,
            x=0,
            y=50,
            width=500,
            anchor_x='left',
            anchor_y='bottom',
        )
        self.help_label.color = (255, 255, 255, 255)

    def run(self):
        pyglet.app.run()

    def step(self, dt):
        # update the Camera
        camera_speed = 40.0

        # handle input
        # this looks complex, but all we're doing
        # is checking for WASD / Arrows
        # and then sending forward, backward, etc
        # to the camera controller with an amount that
        # is scaled by the current time delta
        if self.keyboard[self.keyboard.keys.W] or self.keyboard[
                self.keyboard.keys.UP]:
            # move forward
            self.camera_controller.translate_forward(camera_speed * dt)
        if self.keyboard[self.keyboard.keys.S] or self.keyboard[
                self.keyboard.keys.DOWN]:
            # move backward
            self.camera_controller.translate_backward(camera_speed * dt)
        if self.keyboard[self.keyboard.keys.D] or self.keyboard[
                self.keyboard.keys.RIGHT]:
            # move right
            self.camera_controller.translate_right(camera_speed * dt)
        if self.keyboard[self.keyboard.keys.A] or self.keyboard[
                self.keyboard.keys.LEFT]:
            # move right
            self.camera_controller.translate_left(camera_speed * dt)
        if self.keyboard[self.keyboard.keys.SPACE]:
            # move up
            self.camera_controller.translate_up(camera_speed * dt)
        if self.keyboard[self.keyboard.keys.LSHIFT]:
            # move up
            self.camera_controller.translate_down(camera_speed * dt)

        # handle camera rotation
        # get the relative movement of the mouse
        # since the last frame
        mouse_relative = self.mouse.relative_position

        # the base movement speed we use for
        # scaling with the mouse movements
        # this value just feels about right
        mouse_speed = 0.006

        # scale the mouse movement by the relative value
        # DON'T multiply by the time delta here
        # think about it, it's not what you want!
        frame_pitch = math.pi * mouse_speed * mouse_relative[1]
        frame_yaw = -math.pi * mouse_speed * mouse_relative[0]

        # check for mouse inverts, for us freaks...
        # WE HAVE RIGHTS TOO!
        invert_y = True
        if invert_y == True:
            frame_pitch = -frame_pitch

        # pass the mouse movement to the camera controller
        self.camera_controller.orient(pitch=frame_pitch, yaw=frame_yaw)

        # reset our mouse relative position
        # we should do this each time we take a reading
        # or the delta will continue to accumulate
        self.mouse.clear_delta()

        # manually dispatch the on_draw event
        # as we patched it out of the idle loop
        self.window.dispatch_event('on_draw')

        # display the frame buffer
        self.window.flip()

    def on_draw(self):
        # render the scene
        self.render()

        # render our help text
        self.help_label.draw()

        # render the fps
        self.fps_display.draw()

    def render(self):
        #
        # setup
        #

        # activate the window
        self.window.switch_to()

        # activate our viewport
        self.viewport.switch_to()

        # setup our viewport properties
        self.viewport.push_viewport_attributes()

        # update the view matrix aspect ratio
        self.camera.view_matrix.aspect_ratio = self.viewport.aspect_ratio

        # apply our view matrix and camera translation
        self.camera.view_matrix.push_view_matrix()
        self.camera.push_model_view()

        #
        # render
        #

        # clear our frame buffer and depth buffer
        pygly.gl.set_scissor(self.viewport.rect)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        # render our grid
        for renderable in self.renderables:
            renderable.render()

        #
        # tear down
        #

        # pop our view matrix and camera translation
        self.camera.pop_model_view()
        self.camera.view_matrix.pop_view_matrix()

        # reset our gl state
        self.viewport.pop_viewport_attributes()

        #
        # reset state
        #

        # set our viewport to the entire window
        pygly.gl.set_scissor(pygly.window.create_rectangle(self.window))
        pygly.gl.set_viewport(pygly.window.create_rectangle(self.window))
Exemple #7
0
class Application( object ):
    
    def __init__( self ):
        super( Application, self ).__init__()
        
        # setup our opengl requirements
        config = pyglet.gl.Config(
            depth_size = 16,
            double_buffer = True
            )

        # create our window
        self.window = pyglet.window.Window(
            fullscreen = False,
            width = 1024,
            height = 768,
            resizable = True,
            vsync = False,
            config = config
            )

        # create a viewport
        self.viewport = RatioViewport(
            self.window,
            [ [0.0, 0.0], [1.0, 1.0] ]
            )

        # create our input devices
        self.keyboard = Keyboard( self.window )
        self.mouse = Mouse( self.window )

        # setup our scene
        self.setup_scene()

        # setup our text
        self.setup_text()

        # listen for on_draw events
        self.window.push_handlers(
            on_draw = self.on_draw
            )
        
        # setup our update loop the app
        # we'll render at 60 fps
        frequency = 60.0
        self.update_delta = 1.0 / frequency

        # over-ride the frequency and render at full speed
        self.update_delta = -1

        # use a pyglet callback for our render loop
        pyglet.clock.schedule_interval(
            self.step,
            self.update_delta
            )

    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_text( self ):
        self.help_label = pyglet.text.HTMLLabel(
"""
<b>6-DOF Camera demo</b>
<ul>
<li>Mouse: look around</li>
<li>W,A,S,D: move around</li>
<li>Space: move up</li>
<li>Shift: move down</li>
</ul>
""",
            multiline = True,
            x = 0,
            y = 50,
            width = 500,
            anchor_x = 'left',
            anchor_y = 'bottom',
            )
        self.help_label.color = (255,255,255,255)
        
    def run( self ):
        pyglet.app.run()
    
    def step( self, dt ):
        # update the Camera
        camera_speed = 40.0
        
        # handle input
        # this looks complex, but all we're doing
        # is checking for WASD / Arrows
        # and then sending forward, backward, etc
        # to the camera controller with an amount that
        # is scaled by the current time delta
        if self.keyboard[ self.keyboard.keys.W ] or self.keyboard[ self.keyboard.keys.UP ]:
            # move forward
            self.camera_controller.translate_forward( camera_speed * dt )
        if self.keyboard[ self.keyboard.keys.S ] or self.keyboard[ self.keyboard.keys.DOWN ]:
            # move backward
            self.camera_controller.translate_backward( camera_speed * dt )
        if self.keyboard[ self.keyboard.keys.D ] or self.keyboard[ self.keyboard.keys.RIGHT ]:
            # move right
            self.camera_controller.translate_right( camera_speed * dt )
        if self.keyboard[ self.keyboard.keys.A ] or self.keyboard[ self.keyboard.keys.LEFT ]:
            # move right
            self.camera_controller.translate_left( camera_speed * dt )
        if self.keyboard[ self.keyboard.keys.SPACE ]:
            # move up
            self.camera_controller.translate_up( camera_speed * dt )
        if self.keyboard[ self.keyboard.keys.LSHIFT ]:
            # move up
            self.camera_controller.translate_down( camera_speed * dt )
        
        # handle camera rotation
        # get the relative movement of the mouse
        # since the last frame
        mouse_relative = self.mouse.relative_position

        # the base movement speed we use for
        # scaling with the mouse movements
        # this value just feels about right
        mouse_speed = 0.006
        
        # scale the mouse movement by the relative value
        # DON'T multiply by the time delta here
        # think about it, it's not what you want!
        frame_pitch = math.pi * mouse_speed * mouse_relative[ 1 ]
        frame_yaw = -math.pi * mouse_speed * mouse_relative[ 0 ]
        
        # check for mouse inverts, for us freaks...
        # WE HAVE RIGHTS TOO!
        invert_y = True
        if invert_y == True:
            frame_pitch = -frame_pitch
        
        # pass the mouse movement to the camera controller
        self.camera_controller.orient( pitch = frame_pitch, yaw = frame_yaw )
        
        # reset our mouse relative position
        # we should do this each time we take a reading
        # or the delta will continue to accumulate
        self.mouse.clear_delta()

        # manually dispatch the on_draw event
        # as we patched it out of the idle loop
        self.window.dispatch_event( 'on_draw' )
        
        # display the frame buffer
        self.window.flip()

    def on_draw( self ):
        # render the scene
        self.render()

        # render our help text
        self.help_label.draw()

        # render the fps
        self.fps_display.draw()

    def render( self ):
        #
        # setup
        #

        # activate the window
        self.window.switch_to()

        # activate our viewport
        self.viewport.switch_to()

        # setup our viewport properties
        self.viewport.push_viewport_attributes()

        # update the view matrix aspect ratio
        self.camera.view_matrix.aspect_ratio = self.viewport.aspect_ratio

        # apply our view matrix and camera translation
        self.camera.view_matrix.push_view_matrix()
        self.camera.push_model_view()

        #
        # render
        #

        # clear our frame buffer and depth buffer
        pygly.gl.set_scissor( self.viewport.rect )
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )

        # render our grid
        for renderable in self.renderables:
            renderable.render()

        #
        # tear down
        #

        # pop our view matrix and camera translation
        self.camera.pop_model_view()
        self.camera.view_matrix.pop_view_matrix()

        # reset our gl state
        self.viewport.pop_viewport_attributes()

        #
        # reset state
        #

        # set our viewport to the entire window
        pygly.gl.set_scissor(
            pygly.window.create_rectangle( self.window )
            )
        pygly.gl.set_viewport(
            pygly.window.create_rectangle( self.window )
            )
Exemple #8
0
class Application( object ):
    
    def __init__( self ):
        super( Application, self ).__init__()
        
        # setup our opengl requirements
        config = pyglet.gl.Config(
            depth_size = 16,
            double_buffer = True
            )

        # create our window
        self.window = pyglet.window.Window(
            fullscreen = False,
            width = 1024,
            height = 768,
            resizable = True,
            vsync = False,
            config = config
            )

        # create a viewport
        self.viewport = RatioViewport(
            self.window,
            [ [0.0, 0.0], [1.0, 1.0] ]
            )
        
        # setup our scene
        self.setup_scene()

        # listen for on_draw events
        self.window.push_handlers(
            on_draw = self.on_draw
            )
        
        # setup our update loop the app
        # we'll render at 60 fps
        frequency = 60.0
        self.update_delta = 1.0 / frequency

        # over-ride the frequency and render at full speed
        self.update_delta = -1

        # use a pyglet callback for our render loop
        pyglet.clock.schedule_interval(
            self.step,
            self.update_delta
            )

    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 run( self ):
        pyglet.app.run()

    def step( self, dt ):
        # rotate the mesh about it's own vertical axis
        self.mesh_node.transform.object.rotate_y( dt )

        # manually dispatch the on_draw event
        # as we patched it out of the idle loop
        self.window.dispatch_event( 'on_draw' )
        
        # display the frame buffer
        self.window.flip()

    def on_draw( self ):
        # render the scene
        self.render()

        # render the fps
        self.fps_display.draw()

    def initialise_mesh( self ):
        # load the obj mesh from the file
        self.mesh.load()

    def render_mesh( self ):
        # render the mesh
        self.mesh.render()

    def set_gl_state( self ):
        # enable z buffer
        glEnable( GL_DEPTH_TEST )

        # enable smooth shading
        glShadeModel( GL_SMOOTH )

        # rescale only normals for lighting
        glEnable( GL_RESCALE_NORMAL )

        # enable scissoring for viewports
        glEnable( GL_SCISSOR_TEST )

        # enable back face culling
        glEnable( GL_CULL_FACE )
        glCullFace( GL_BACK )

        # setup lighting for our viewport
        glEnable( GL_LIGHTING )

        # set an ambient light level
        glAmbient = glLightModelfv(
            GL_LIGHT_MODEL_AMBIENT,
            (GLfloat * 4)( *[ 0.8, 0.8, 0.8, 1.0 ] )
            )

        # create a light
        glEnable( GL_LIGHT0 )
        glLightfv(
            GL_LIGHT0,
            GL_POSITION,
            (GLfloat * 4)( *[0.0, 20.0, 30.0, 1.0] )
            )
    
    def render( self ):
        #
        # setup
        #

        # set our window
        self.window.switch_to()

        # activate our viewport
        self.viewport.switch_to()

        # scissor to our viewport
        self.viewport.scissor_to_viewport()

        # setup our viewport properties
        glPushAttrib( GL_ALL_ATTRIB_BITS )
        self.set_gl_state()

        # update the view matrix aspect ratio
        self.camera.view_matrix.aspect_ratio = self.viewport.aspect_ratio

        # apply our view matrix and camera translation
        self.camera.view_matrix.push_view_matrix()
        self.camera.push_model_view()

        #
        # render
        #

        # clear our frame buffer and depth buffer
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )

        for renderable in self.renderables:
            renderable.render()

        #
        # tear down
        #

        # pop our view matrix and camera translation
        self.camera.pop_model_view()
        self.camera.view_matrix.pop_view_matrix()

        # pop our viewport attributes
        glPopAttrib()

        #
        # reset state
        #

        # set our viewport to the entire window
        pygly.gl.set_scissor(
            pygly.window.create_rectangle( self.window )
            )
        pygly.gl.set_viewport(
            pygly.window.create_rectangle( self.window )
            )