|
| 1 | +import math |
| 2 | +import time |
| 3 | +import random |
| 4 | + |
| 5 | +from pyglet.gl import * |
| 6 | +import pyglet |
| 7 | +import numpy |
| 8 | + |
| 9 | +import pygly.window |
| 10 | +import pygly.gl |
| 11 | +from pygly.ratio_viewport import RatioViewport |
| 12 | +from pygly.projection_view_matrix import ProjectionViewMatrix |
| 13 | +from pygly.scene_node import SceneNode |
| 14 | +from pygly.camera_node import CameraNode |
| 15 | +from pygly.render_callback_node import RenderCallbackNode |
| 16 | +from pygly.input.keyboard import Keyboard |
| 17 | +from pygly.input.mouse import Mouse |
| 18 | + |
| 19 | +import pyrr |
| 20 | + |
| 21 | +# over-ride the default pyglet idle loop |
| 22 | +import pygly.monkey_patch |
| 23 | +pygly.monkey_patch.patch_idle_loop() |
| 24 | + |
| 25 | + |
| 26 | +class BaseApplication( object ): |
| 27 | + |
| 28 | + def __init__( self ): |
| 29 | + super( BaseApplication, self ).__init__() |
| 30 | + |
| 31 | + self.setup() |
| 32 | + |
| 33 | + def setup( self ): |
| 34 | + self.setup_window() |
| 35 | + self.setup_input() |
| 36 | + self.setup_ui() |
| 37 | + self.setup_scene_root() |
| 38 | + self.setup_camera() |
| 39 | + self.setup_scene() |
| 40 | + self.setup_events() |
| 41 | + |
| 42 | + def setup_window( self ): |
| 43 | + # setup our opengl requirements |
| 44 | + config = pyglet.gl.Config( |
| 45 | + depth_size = 16, |
| 46 | + double_buffer = True |
| 47 | + ) |
| 48 | + |
| 49 | + # create our window |
| 50 | + self.window = pyglet.window.Window( |
| 51 | + fullscreen = False, |
| 52 | + width = 1024, |
| 53 | + height = 768, |
| 54 | + resizable = True, |
| 55 | + config = config |
| 56 | + ) |
| 57 | + |
| 58 | + # listen for on_draw events |
| 59 | + self.window.push_handlers( |
| 60 | + on_draw = self.on_draw |
| 61 | + ) |
| 62 | + |
| 63 | + # create a viewport |
| 64 | + self.viewport = RatioViewport( |
| 65 | + self.window, |
| 66 | + [ [0.0, 0.0], [1.0, 1.0] ] |
| 67 | + ) |
| 68 | + |
| 69 | + def setup_input( self ): |
| 70 | + # create our keyboard device |
| 71 | + self.keyboard = Keyboard( self.window ) |
| 72 | + |
| 73 | + # register for keypresses |
| 74 | + self.keyboard.digital.push_handlers( |
| 75 | + on_digital_input = self.on_key_event |
| 76 | + ) |
| 77 | + |
| 78 | + # create our mouse device |
| 79 | + self.mouse = Mouse( self.window ) |
| 80 | + |
| 81 | + def setup_events( self ): |
| 82 | + # setup our update loop the app |
| 83 | + # we'll render at 60 fps |
| 84 | + frequency = 60.0 |
| 85 | + self.update_delta = 1.0 / frequency |
| 86 | + |
| 87 | + # use a pyglet callback for our render loop |
| 88 | + pyglet.clock.schedule_interval( |
| 89 | + self.step, |
| 90 | + self.update_delta |
| 91 | + ) |
| 92 | + |
| 93 | + def setup_ui( self ): |
| 94 | + # create an fps display |
| 95 | + self.fps_display = pyglet.clock.ClockDisplay() |
| 96 | + |
| 97 | + def setup_scene_root( self ): |
| 98 | + # create a list of renderables |
| 99 | + self.renderables = [] |
| 100 | + |
| 101 | + # create a scene |
| 102 | + self.scene_node = SceneNode( 'root' ) |
| 103 | + |
| 104 | + def setup_camera( self ): |
| 105 | + # create a camera and a view matrix |
| 106 | + self.view_matrix = ProjectionViewMatrix( |
| 107 | + self.viewport.aspect_ratio, |
| 108 | + fov = 45.0, |
| 109 | + near_clip = 1.0, |
| 110 | + far_clip = 200.0 |
| 111 | + ) |
| 112 | + # create a camera |
| 113 | + self.camera = CameraNode( |
| 114 | + 'camera', |
| 115 | + self.view_matrix |
| 116 | + ) |
| 117 | + self.scene_node.add_child( self.camera ) |
| 118 | + |
| 119 | + def setup_scene( self ): |
| 120 | + # CREATE SCENE HERE |
| 121 | + pass |
| 122 | + |
| 123 | + def run( self ): |
| 124 | + pyglet.app.run() |
| 125 | + |
| 126 | + def step( self, dt ): |
| 127 | + # update our mouse |
| 128 | + self.update_mouse( dt ) |
| 129 | + |
| 130 | + # update the scene here |
| 131 | + self.update_scene( dt ) |
| 132 | + |
| 133 | + # manually dispatch the on_draw event |
| 134 | + # as we patched it out of the idle loop |
| 135 | + self.window.dispatch_event( 'on_draw' ) |
| 136 | + |
| 137 | + # display the frame buffer |
| 138 | + self.window.flip() |
| 139 | + |
| 140 | + def update_mouse( self, dt ): |
| 141 | + # USE MOUSE VALUES HERE |
| 142 | + pass |
| 143 | + |
| 144 | + # reset the relative position of the mouse |
| 145 | + self.mouse.clear_delta() |
| 146 | + |
| 147 | + def on_key_event( self, digital, event, key ): |
| 148 | + # HANDLE KEYBOARD INPUT HERE |
| 149 | + pass |
| 150 | + |
| 151 | + def update_scene( self, dt ): |
| 152 | + # UPDATE SCENE HERE |
| 153 | + pass |
| 154 | + |
| 155 | + def on_draw( self ): |
| 156 | + # render the scene |
| 157 | + self.render() |
| 158 | + |
| 159 | + # render the fps |
| 160 | + self.fps_display.draw() |
| 161 | + |
| 162 | + def render( self ): |
| 163 | + # |
| 164 | + # setup |
| 165 | + # |
| 166 | + |
| 167 | + # set our window |
| 168 | + self.window.switch_to() |
| 169 | + |
| 170 | + # activate our viewport |
| 171 | + self.viewport.switch_to() |
| 172 | + |
| 173 | + # scissor to our viewport |
| 174 | + self.viewport.scissor_to_viewport() |
| 175 | + |
| 176 | + # setup our viewport properties |
| 177 | + glPushAttrib( GL_ALL_ATTRIB_BITS ) |
| 178 | + |
| 179 | + # update the view matrix aspect ratio |
| 180 | + self.camera.view_matrix.aspect_ratio = self.viewport.aspect_ratio |
| 181 | + |
| 182 | + # apply our view matrix and camera translation |
| 183 | + self.camera.view_matrix.push_view_matrix() |
| 184 | + self.camera.push_model_view() |
| 185 | + |
| 186 | + # |
| 187 | + # render |
| 188 | + # |
| 189 | + |
| 190 | + # clear our frame buffer and depth buffer |
| 191 | + glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) |
| 192 | + |
| 193 | + self.render_3d() |
| 194 | + |
| 195 | + # |
| 196 | + # tear down |
| 197 | + # |
| 198 | + |
| 199 | + # pop our view matrix and camera translation |
| 200 | + self.camera.pop_model_view() |
| 201 | + self.camera.view_matrix.pop_view_matrix() |
| 202 | + |
| 203 | + # pop our viewport attributes |
| 204 | + glPopAttrib() |
| 205 | + |
| 206 | + # |
| 207 | + # reset state |
| 208 | + # |
| 209 | + |
| 210 | + # set our viewport to the entire window |
| 211 | + pygly.gl.set_scissor( |
| 212 | + pygly.window.create_rectangle( self.window ) |
| 213 | + ) |
| 214 | + pygly.gl.set_viewport( |
| 215 | + pygly.window.create_rectangle( self.window ) |
| 216 | + ) |
| 217 | + |
| 218 | + # |
| 219 | + # render 2d |
| 220 | + # |
| 221 | + |
| 222 | + self.render_2d() |
| 223 | + |
| 224 | + def render_2d( self ): |
| 225 | + # render the fps |
| 226 | + self.fps_display.draw() |
| 227 | + |
0 commit comments