#!/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. # import sys import OpenGL import math from PyOSG import osg from PyOSG import osgDB from PyOSG import osgUtil from terrain import getDatabaseCenterRadius radius = 2.0 dbcenter = [0,0,0] dbradius = 0 def conv(a, mat, b ): t = [0,0,0] for i in range(3): t[i] = (a[0] * mat(0,i)) +(a[1] * mat(1,i)) + (a[2] * mat(2,i)) + mat(3,i) b[0] = t[0] b[1] = t[1] b[2] = t[2] def makeTank(): geode = osg.Geode() global dbradius, dbcenter dbradius, dbcenter = getDatabaseCenterRadius() mat = osg.Matrix() data = ( 0.05, 0, 0, 0, 0, 0.05, 0, 0, 0, 0, 0.05, 0, 1.5999 - 0.3, 3.1474, dbcenter[2] + 0.6542 - 0.09, 1 ) mat.set(data) # 42 required for sides, 22 for the top. vc = osg.Vec3Array(42+22) tc = osg.Vec2Array(42+22) gset = osg.Geometry() gset.setVertexArray( vc ) gset.setTexCoordArray( 0, tc ) c = 0 for i in range(0,361,18): theta = osg.DegreesToRadians(i) s = i/90.0 t = 1.0 x = radius * math.cos( theta ) y = radius * math.sin( theta ) z = 1.0 vc[c][0] = x vc[c][1] = y vc[c][2] = z tc[c][0] = s tc[c][1] = t c += 1 t = 0.0 z = 0.0 vc[c][0] = x vc[c][1] = y vc[c][2] = z tc[c][0] = s tc[c][1] = t c += 1 gset.addPrimitiveSet( osg.DrawArrays(osg.PrimitiveSet.TRIANGLE_STRIP,0,c) ) # create the top of the tank. prev_c = c vc[c][0] = 0.0 vc[c][1] = 0.0 vc[c][2] = 1.0 tc[c][0] = 0.0 tc[c][1] = 0.0 c += 1 for i in range(0,361,18): theta = osg.DegreesToRadians(i) # s = (float)i/360.0 # t = 1.0 s = math.cos( theta ) t = math.sin( theta ) x = radius * math.cos( theta ) y = radius * math.sin( theta ) z = 1.0 vc[c][0] = x vc[c][1] = y vc[c][2] = z tc[c][0] = s tc[c][1] = t c += 1 for i in range(c): conv( vc[i], mat, vc[i] ) gset.addPrimitiveSet(osg.DrawArrays(osg.PrimitiveSet.TRIANGLE_FAN,prev_c,c-prev_c)) tex = osg.Texture2D() tex.setWrap( osg.Texture.WRAP_S, osg.Texture.REPEAT ) tex.setWrap( osg.Texture.WRAP_T, osg.Texture.REPEAT ) tex.setImage(osgDB.readImageFile("Images/tank.rgb")) dstate = osg.StateSet() dstate.setTextureAttributeAndModes(0, tex, osg.StateAttribute.ON ) dstate.setTextureAttribute(0, osg.TexEnv() ) gset.setStateSet( dstate ) geode.addDrawable( gset ) return geode if __name__ == "__main__": tank = makeTank() from PyOSG import * import sys viewer = osgProducer.Viewer() viewer.setUpViewer(osgProducer.Viewer.STANDARD_SETTINGS) viewer.setSceneData(tank) viewer.realize() while not viewer.done(): viewer.sync() viewer.update() viewer.frame() viewer.sync() sys.exit(0)