"""This module initializes the main graphics application"""# Import Python modulesif__name__=='__main__':importmodernglasmglimportpygameaspgimportsys# Import application modulesfromcameraimportCamerafromlightimportLightfrommeshimportMeshfrommodelimport*fromsceneimportScenefromscene_rendererimportSceneRenderer
[docs]classGraphicsEngine():def__init__(self,win_size=(1600,900)):# Initialize PyGame modulespg.init()# Initialize window sizeself.WIN_SIZE=win_size# Set OpenGL attributes# NOTE: This application uses OpenGL 3.3 Corepg.display.gl_set_attribute(pg.GL_CONTEXT_MAJOR_VERSION,3)pg.display.gl_set_attribute(pg.GL_CONTEXT_MINOR_VERSION,3)pg.display.gl_set_attribute(pg.GL_CONTEXT_PROFILE_MASK,pg.GL_CONTEXT_PROFILE_CORE)# Create OpenGL contextpg.display.set_mode(self.WIN_SIZE,flags=pg.OPENGL|pg.DOUBLEBUF)# Mouse settingspg.event.set_grab(True)pg.mouse.set_visible(False)# Detect and use existing OpenGL context# NOTE: Contexts provides API functions such renderstates, buffers, etc.self.ctx=mgl.create_context()self.ctx.enable(flags=mgl.DEPTH_TEST|mgl.CULL_FACE)# Create an object to help track timeself.clock=pg.time.Clock()self.time=0self.delta_time=0# Create light data (positions, transformation)self.light=Light()# Create camera data (positions, transformations)self.camera=Camera(self)# Create mesh data (VBOs, VAOs, shader programs, textures)self.mesh=Mesh(self)# Accumulate required objects into a Sceneself.scene=Scene(self)# Render the accumulated objects from the Sceneself.scene_renderer=SceneRenderer(self)
[docs]defcheck_events(self):foreventinpg.event.get():ifevent.type==pg.QUITor(event.type==pg.KEYDOWNandevent.key==pg.K_ESCAPE):# Release everything from memoryself.mesh.destroy()self.scene_renderer.destroy()pg.quit()sys.exit()