1import Ogre
2import OgreRTShader
3import OgreOverlay
4import OgreBites
5
6class SampleApp(OgreBites.ApplicationContext, OgreBites.InputListener):
7    def __init__(self):
8        OgreBites.ApplicationContext.__init__(self, "PySample")
9        OgreBites.InputListener.__init__(self)
10
11    def keyPressed(self, evt):
12        if evt.keysym.sym == OgreBites.SDLK_ESCAPE:
13            self.getRoot().queueEndRendering()
14
15        return True
16
17    def loadResources(self):
18        self.enableShaderCache()
19
20        # load essential resources for trays/ loading bar
21        Ogre.ResourceGroupManager.getSingleton().initialiseResourceGroup("Essential")
22        self.createDummyScene()
23        self.trays = OgreBites.TrayManager("Interface", self.getRenderWindow())
24        self.addInputListener(self.trays)
25
26        # show loading progress
27        self.trays.showLoadingBar(1, 0)
28        ret = OgreBites.ApplicationContext.loadResources(self)
29
30        # clean up
31        self.trays.hideLoadingBar()
32        self.destroyDummyScene()
33        return ret
34
35    def setup(self):
36        OgreBites.ApplicationContext.setup(self)
37        self.addInputListener(self)
38
39        root = self.getRoot()
40        scn_mgr = root.createSceneManager()
41
42        shadergen = OgreRTShader.ShaderGenerator.getSingleton()
43        shadergen.addSceneManager(scn_mgr)  # must be done before we do anything with the scene
44
45        # overlay/ trays
46        scn_mgr.addRenderQueueListener(self.getOverlaySystem())
47        self.trays.showFrameStats(OgreBites.TL_TOPRIGHT)
48        self.trays.refreshCursor()
49
50        # enable per pixel lighting
51        rs = shadergen.getRenderState(OgreRTShader.cvar.ShaderGenerator_DEFAULT_SCHEME_NAME)
52        rs.addTemplateSubRenderState(shadergen.createSubRenderState(OgreRTShader.cvar.PerPixelLighting_Type))
53
54        scn_mgr.setAmbientLight(Ogre.ColourValue(.1, .1, .1))
55
56        light = scn_mgr.createLight("MainLight")
57        lightnode = scn_mgr.getRootSceneNode().createChildSceneNode()
58        lightnode.setPosition(0, 10, 15)
59        lightnode.attachObject(light)
60
61        cam = scn_mgr.createCamera("myCam")
62        cam.setNearClipDistance(5)
63        cam.setAutoAspectRatio(True)
64        camnode = scn_mgr.getRootSceneNode().createChildSceneNode()
65        camnode.attachObject(cam)
66
67        self.camman = OgreBites.CameraMan(camnode)
68        self.camman.setStyle(OgreBites.CS_ORBIT)
69        self.camman.setYawPitchDist(Ogre.Radian(0), Ogre.Radian(0.3), 15)
70        self.addInputListener(self.camman)
71
72        # must keep a reference to ctrls so it does not get deleted
73        self.ctrls = OgreBites.AdvancedRenderControls(self.trays, cam)
74        self.addInputListener(self.ctrls)
75
76        vp = self.getRenderWindow().addViewport(cam)
77        vp.setBackgroundColour(Ogre.ColourValue(.3, .3, .3))
78
79        ent = scn_mgr.createEntity("Sinbad.mesh")
80        node = scn_mgr.getRootSceneNode().createChildSceneNode()
81        node.attachObject(ent)
82
83    def shutdown(self):
84        # manually destroy in reverse creation order
85        del app.ctrls
86        del app.trays
87
88if __name__ == "__main__":
89    app = SampleApp()
90    app.initApp()
91    app.getRoot().startRendering()
92    app.closeApp()
93