from PyOSG import * from OpenGL.GL import * from terrain_coords import vertex import math def createSpotLightImage(centerColour, backgroudColour, size, power): image = osg.Image() image.allocateImage(size,size,1, GL_RGBA,GL_UNSIGNED_BYTE) mid = (float(size)-1)*0.5 div = 2.0/float(size) for r in range(size): # ptr = image.data(0,r,0) : FIXME for c in range(size): dx = (float(c) - mid)*div dy = (float(r) - mid)*div i = pow(1.0-math.sqrt(dx*dx+dy*dy),power) if i<0.0: i=0.0 color = centerColour*i+backgroudColour*(1.0-i) image.setPixel(c, r, 0, color) #*ptr++ = (unsigned char)((color[0])*255.0) #*ptr++ = (unsigned char)((color[1])*255.0) #*ptr++ = (unsigned char)((color[2])*255.0) #*ptr++ = (unsigned char)((color[3])*255.0) return image #return osgDB.readImageFile("spot.dds") def createSpotLightDecoratorState(lightNum, textureUnit): stateset = osg.StateSet() stateset.setMode(GL_LIGHT0+lightNum, osg.StateAttribute.ON) centerColour = osg.Vec4(1.0,1.0,1.0,1.0) ambientColour = osg.Vec4(0.05,0.05,0.05,1.0) # set up spot light texture texture = osg.Texture2D() texture.setImage(createSpotLightImage(centerColour, ambientColour, 64, 1.0)) texture.setBorderColor(osg.Vec4(ambientColour)) texture.setWrap(osg.Texture.WRAP_S,osg.Texture.CLAMP_TO_BORDER) texture.setWrap(osg.Texture.WRAP_T,osg.Texture.CLAMP_TO_BORDER) texture.setWrap(osg.Texture.WRAP_R,osg.Texture.CLAMP_TO_BORDER) stateset.setTextureAttributeAndModes(textureUnit, texture, osg.StateAttribute.ON) # set up tex gens stateset.setTextureMode(textureUnit, GL_TEXTURE_GEN_S, osg.StateAttribute.ON) stateset.setTextureMode(textureUnit, GL_TEXTURE_GEN_T, osg.StateAttribute.ON) stateset.setTextureMode(textureUnit, GL_TEXTURE_GEN_R, osg.StateAttribute.ON) stateset.setTextureMode(textureUnit, GL_TEXTURE_GEN_Q, osg.StateAttribute.ON) return stateset def createSpotLightNode(position, direction, angle, lightNum, textureUnit): group = osg.Group() # create light source. lightsource = osg.LightSource() light = lightsource.getLight() light.setLightNum(lightNum) light.setPosition(osg.Vec4(position,1.0)) light.setAmbient(osg.Vec4(0.00,0.00,0.05,1.0)) light.setDiffuse(osg.Vec4(1.0,1.0,1.0,1.0)) group.addChild(lightsource) # create tex gen. up = osg.Vec3(0.0,0.0,1.0) up = (direction ^ up) ^ direction up.normalize() texgenNode = osg.TexGenNode() texgenNode.setTextureUnit(textureUnit) texgen = texgenNode.getTexGen() texgen.setMode(osg.TexGen.EYE_LINEAR) texgen.setPlanesFromMatrix(osg.Matrix.lookAt(position, position+direction, up)* osg.Matrix.perspective(angle,1.0,0.1,100)) group.addChild(texgenNode) return group def createAnimationPath(center, radius, looptime): # set up the animation path animationPath = osg.AnimationPath() animationPath.setLoopMode(osg.AnimationPath.LOOP) numSamples = 40 yaw = 0.0 yaw_delta = 2.0*osg.PI/(numSamples-1.0) roll = osg.inDegrees(30.0) time=0.0 time_delta = looptime/float(numSamples) for i in range(numSamples): position = osg.Vec3(center+osg.Vec3(math.sin(yaw)*radius,math.cos(yaw)*radius,0.0)) rotation = osg.Quat(roll,osg.Vec3(0.0,1.0,0.0))*osg.Quat(-(yaw+osg.inDegrees(90.0)),osg.Vec3(0.0,0.0,1.0)) animationPath.insert(time,osg.AnimationPath.ControlPoint(position,rotation)) yaw += yaw_delta time += time_delta return animationPath def createBase(center, radius): geode = osg.Geode() # set up the texture of the base. stateset = osg.StateSet() image = osgDB.readImageFile("Images/lz.rgb") if image: texture = osg.Texture2D() texture.setImage(image) stateset.setTextureAttributeAndModes(0,texture,osg.StateAttribute.ON) geode.setStateSet( stateset ) grid = osg.HeightField() grid.allocate(38,39) grid.setOrigin(center+osg.Vec3(-radius,-radius,0.0)) grid.setXInterval(radius*2.0/(float)(38-1)) grid.setYInterval(radius*2.0/(float)(39-1)) minHeight = 1e38 maxHeight = -1e38 for r in range(39): for c in range(38): h = vertex[r+c*39][2] if h>maxHeight: maxHeight=h if h