PyOgre Tutorial3        

CellShading Demo with PyOgre (example can be found in pyogre example folder)

Pyogre3.JPG

# This code is in the Public Domain
 import pyogre.ogre as ogre
 import SampleFramework as sf  
 
 # Custom parameter bindings
 CUSTOM_SHININESS = 1
 CUSTOM_DIFFUSE = 2
 CUSTOM_SPECULAR = 3
 
 class CelShadingApplication(sf.Application):
    def _createScene(self):
        sceneManager = self.sceneManager
 
        capabilities = ogre.Root.getSingleton().renderSystem.capabilities
        if not capabilities.hasCapability(ogre.RSC_VERTEX_PROGRAM) or not capabilities.hasCapability(ogre.RSC_FRAGMENT_PROGRAM):
            raise ogre.Exception(111, 'Your card does not support vertex and fragment programs, so cannot run this demo. Sorry!', 'celshadingdemo.py')
        
        # Accept default settings: point light, white diffuse, just set position
        light = sceneManager.createLight('MainLight')
        self.rotationNode = sceneManager.rootSceneNode.createChildSceneNode()
        self.rotationNode.createChildSceneNode((20,40,50)).attachObject(light)
 
        # create head entity
        entity = sceneManager.createEntity('head', 'ogrehead.mesh')
        self.camera.position = (20, 0, 100)
        self.camera.lookAt(0, 0, 0)
 
        # eyes
        sub = entity.getSubEntity(0)
        sub.materialName = 'Examples/CelShading'
        sub.setCustomParameter(CUSTOM_SHININESS, (35.0, 0.0, 0.0, 0.0))
        sub.setCustomParameter(CUSTOM_DIFFUSE, (1.0, 0.3, 0.3, 1.0))
        sub.setCustomParameter(CUSTOM_SPECULAR, (1.0, 0.6, 0.6, 1.0))
 
        # skin
        sub = entity.getSubEntity(1)
        sub.materialName = 'Examples/CelShading'
        sub.setCustomParameter(CUSTOM_SHININESS, (10.0, 0.0, 0.0, 0.0))
        sub.setCustomParameter(CUSTOM_DIFFUSE, (0.0, 0.5, 0.0, 1.0))
        sub.setCustomParameter(CUSTOM_SPECULAR, (0.3, 0.5, 0.3, 1.0))
 
        # earring
        sub = entity.getSubEntity(2)
        sub.materialName = 'Examples/CelShading'
        sub.setCustomParameter(CUSTOM_SHININESS, (25.0, 0.0, 0.0, 0.0))
        sub.setCustomParameter(CUSTOM_DIFFUSE, (1.0, 1.0, 0.0, 1.0))
        sub.setCustomParameter(CUSTOM_SPECULAR, (1.0, 1.0, 0.7, 1.0))
 
        # teeth
        sub = entity.getSubEntity(3)
        sub.materialName = 'Examples/CelShading'
        sub.setCustomParameter(CUSTOM_SHININESS, (20.0, 0.0, 0.0, 0.0))
        sub.setCustomParameter(CUSTOM_DIFFUSE, (1.0, 1.0, 0.7, 1.0))
        sub.setCustomParameter(CUSTOM_SPECULAR, (1.0, 1.0, 1.0, 1.0))
 
        sceneManager.rootSceneNode.createChildSceneNode().attachObject(entity)
        self.renderWindow.getViewport(0).backgroundColour = (1, 1, 1)
 
    def _createFrameListener(self):
        self.frameListener = CelShadingListener(self.renderWindow, self.camera, self.rotationNode)
        self.root.addFrameListener(self.frameListener)
 
 class CelShadingListener(sf.FrameListener):
    def __init__(self, renderWindow, camera, rotationNode):
        sf.FrameListener.__init__(self, renderWindow, camera)
        self.rotationNode = rotationNode
 
    def frameStarted(self, frameEvent):
        self.rotationNode.yaw(ogre.Radian(ogre.Degree(frameEvent.timeSinceLastFrame * 30)))
        return sf.FrameListener.frameStarted(self, frameEvent)
 
 if __name__ == '__main__':
    application = CelShadingApplication()
    application.go()