#!/bin/env python # Copyright (C) 2002-2003 Gideon May (gideon@computer.org) # # Permission to copy, use, sell and distribute this software is granted # provided this copyright notice appears in all copies. # Permission to modify the code and to distribute modified code is granted # provided this copyright notice appears in all copies, and a notice # that the code was modified is included with the copyright notice. # # This software is provided "as is" without express or implied warranty, # and with no claim as to its suitability for any purpose. # -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield # # This application is open source and may be redistributed and/or modified # freely and without restriction, both in commericial and non commericial applications, # as long as this copyright notice is maintained. # # This application is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # from PyOSG import osg from PyOSG import osgUtil import math import OpenGL from OpenGL.GL import * # TODO: Make it up to date with the osg demo keep = [] class MyTexGen(osg.TexGenDer): def __init__(self): osg.TexGenDer.__init__(self) # XXX Dirty trick to prevent the garbage collector from # deleting the instance of MyTexGen. # Need to do this since the MyTexGen instance is passed to osg # and osg is unable to increase the reference of the instance object # Thus the moment the object goes out of scope, it is flagged for deletion, # UNLESS it is self referencing (circular reference)! self._self = self self._matrix = osg.Matrix() def setMatrix(self, matrix): self._matrix = matrix def apply(self, state): glPushMatrix() glLoadMatrixf(self._matrix.asList()) osg.TexGenDer.apply(self, state) glPopMatrix() class CreateShadowTextureCullCallback(osg.NodeCallback): def __init__(self, shadower, position, ambientLightColor, textureUnit): osg.NodeCallback.__init__(self) self._self = self self._shadower = shadower self._position = position self._ambientLightColor = ambientLightColor self._unit = textureUnit self._shadowState = osg.StateSet() self._texture = osg.Texture2D() self._texture.setFilter(osg.Texture2D.MIN_FILTER,osg.Texture2D.LINEAR) self._texture.setFilter(osg.Texture2D.MAG_FILTER,osg.Texture2D.LINEAR) self._texture.setWrap(osg.Texture2D.WRAP_S,osg.Texture2D.CLAMP_TO_BORDER) self._texture.setWrap(osg.Texture2D.WRAP_T,osg.Texture2D.CLAMP_TO_BORDER) self._texture.setBorderColor(osg.Vec4(1.0,1.0,1.0,1.0)) def apply(self, node, nv): cullVisitor = osgUtil.asCullVisitor(nv) if cullVisitor and self._texture and self._shadower: self.doPreRender(node,cullVisitor, nv) else: # must traverse the shadower self.traverse(node, nv) def doPreRender(self, node, cv, nv): global keep bs = self._shadower.getBound() if not bs: print "bb invalid", self._shadower return # create the render to texture stage. rtts = osgUtil.RenderToTextureStage() #-- keep.append(rtts) # set up lighting. # currently ignore lights in the scene graph itself.. # will do later. previous_stage = cv.getCurrentRenderBin().getStage() # set up the background color and clear mask. rtts.setClearColor(osg.Vec4(1.0,1.0,1.0,1.0)) rtts.setClearMask(previous_stage.getClearMask()) # set up to charge the same RenderStageLighting is the parent previous stage. rtts.setRenderStageLighting(previous_stage.getRenderStageLighting()) # record the render bin, to be restored after creation # of the render to text previousRenderBin = cv.getCurrentRenderBin() # set the current renderbin to be the newly created stage. cv.setCurrentRenderBin(rtts) centerDistance = (self._position-bs.center()).length() znear = centerDistance-bs.radius() zfar = centerDistance+bs.radius() zNearRatio = 0.001 if znear