class Graphics: wireframe = False vertices = [] reDraw = False toggleDrawAxes = False spectator = False numberOfVertices = 0 def __init__(self, main): from font import Font global g_fVBOObjects g_fVBOObjects = [] self.main = main self.g_nFrames = 0 self.fps = 0 init_opengl(main) self.font = Font() def addSurface(self, Mesh, Obj, Texture): g_pMesh = CMesh() vertices, vnormals, f, self.vertexCount = \ loadObj(Obj) g_pMesh.textureId, textureWidthRatio, textureHeightRatio = \ loadTexture(Texture) xMax = xMin = vertices[0][0] zMax = zMin = vertices[0][2] for i in vertices: if i[0] < xMin: xMin = i[0] elif i[0] > xMax: xMax = i[0] if i[2] < zMin: zMin = i[2] elif i[2] > zMax: zMax = i[2] sizeX = xMax - xMin sizeY = zMax - zMin texCoords = Numeric.zeros ((self.vertexCount, 2), 'f') nIndex = 0 for i in vertices: self.vertices.append( CVector3(i[0], i[1], i[2]) ) self.numberOfVertices += 1 texCoords[nIndex, 0] = (i[0]-xMin) / sizeX * textureWidthRatio texCoords[nIndex, 1] = (i[2]-zMin) / sizeY * textureHeightRatio nIndex += 1 self.verticesId, self.vnormalsId, self.texCoordsId = \ createVBO(Obj, vertices, vnormals, texCoords) g_pMesh.verticesId = self.verticesId g_pMesh.vnormalsId = self.vnormalsId g_pMesh.texCoordsId = self.texCoordsId g_pMesh.vertexCount = self.vertexCount g_fVBOObjects.append(g_pMesh) def loadStaticObject(self, x, y, z, model, texture): g_pMesh = CMesh() vertices, vnormals, f, vertexCount = \ loadObj(model) for v in vertices: # transform v[0] += x v[1] += y v[2] += z g_pMesh.textureId, textureWidthRatio, textureHeightRatio = \ loadTexture(texture) xMax = xMin = vertices[0][0] zMax = zMin = vertices[0][2] for i in vertices: if i[0] < xMin: xMin = i[0] elif i[0] > xMax: xMax = i[0] if i[2] < zMin: zMin = i[2] elif i[2] > zMax: zMax = i[2] sizeX = xMax - xMin sizeY = zMax - zMin texCoords = Numeric.zeros ((vertexCount, 2), 'f') nIndex = 0 for i in vertices: self.vertices.append( CVector3(i[0], i[1], i[2]) ) self.numberOfVertices += 1 texCoords[nIndex, 0] = (i[0]-xMin) / sizeX * textureWidthRatio texCoords[nIndex, 1] = (i[2]-zMin) / sizeY * textureHeightRatio nIndex += 1 verticesId, vnormalsId, texCoordsId = \ createVBO(model, vertices, vnormals, texCoords) g_pMesh.verticesId = verticesId g_pMesh.vnormalsId = vnormalsId g_pMesh.texCoordsId = texCoordsId g_pMesh.vertexCount = vertexCount g_fVBOObjects.append(g_pMesh) def initGL(self): from skydome import Skydome if not glInitVertexBufferObjectARB(): sys.stderr.write("ERROR: Vertex buffer objects is not supported\n") #Global.quit = 1 return glClearColor( 0.0, 0.0, 0.0, 0.0) glClearDepth(1.0) glDepthFunc(GL_LEQUAL) glEnable(GL_DEPTH_TEST) glShadeModel(GL_SMOOTH) glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) glViewport(0, 0, self.main.config.getint('Resolution', 'Width'), self.main.config.getint('Resolution', 'Height')) glMatrixMode(GL_PROJECTION) #glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0) glLoadIdentity() gluPerspective(60.0, self.main.config.getfloat('Resolution','Width') / self.main.config.getfloat('Resolution', 'Height'), 0.1, 5000.0) glMatrixMode(GL_MODELVIEW) #Lighting diffuseMaterial = (0.5, 0.5, 0.0, 1.0) mat_specular = (1.0, 1.0, 1.0, 1.0) light_position = (150.0, 0.0, 75.0, 1.0) glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, diffuseMaterial) glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseMaterial) glLightfv(GL_LIGHT1, GL_POSITION, light_position) glEnable(GL_LIGHTING) glDisable(GL_LIGHT0) glEnable(GL_LIGHT1) ########### glEnable(GL_NORMALIZE) self.skydome = Skydome() if not self.main.args['disableWater']: self.water = Water() def printFPS(self): self.fps = self.g_nFrames pygame.display.set_caption("FarornasGrotta - %d FPS" % (self.fps)) self.g_nFrames = 0 def drawAxes(self): """ Draws x, y and z axes """ light = glIsEnabled(GL_LIGHTING) if light: glDisable(GL_LIGHTING) glColor3f(1.0, 0.0, 0.0) glBegin(GL_LINES) glVertex3f(-1000.0, 0.0, 0.0) glVertex3f( 1000.0, 0.0, 0.0) glEnd() glColor3f(0.0, 1.0, 0.0) glBegin(GL_LINES) glVertex3f(0.0, -1000.0, 0.0) glVertex3f(0.0, 1000.0, 0.0) glEnd() glColor3f(0.0, 0.0, 1.0) glBegin(GL_LINES) glVertex3f(0.0, 0.0, -1000.0) glVertex3f(0.0, 0.0, 1000.0) glEnd() if light: glEnable(GL_LIGHTING) def drawPlayerList(self): glPushMatrix() glDisable(GL_LIGHTING) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) glLoadIdentity() glTranslated(-320.0, -240.0, -410.0) glColor3f(1.0, 1.0, 1.0) row = 3 self.font.glPrint(80.0, 480 - 30.0 * row, "Name") self.font.glPrint(420.0, 480 - 30.0 * row, "Frags") self.font.glPrint(500.0, 480 - 30.0 * row, "Deaths") row += 1 i = 0 for obj in self.main.physics.objects: if obj.data.type != "player1": continue if obj.data.id == self.main.player.data.id: glColor3f(0.0, 1.0, 0.0) else: glColor3f(1.0, 1.0, 1.0) self.font.glPrint(80.0, 480.0 - 30.0*row, "%s" % obj.data.name) self.font.glPrint(420.0, 480.0 - 30.0*row, "%5d" % obj.data.frags) self.font.glPrint(500.0, 480.0 - 30.0*row, "%6d" % obj.data.deaths) i += 1 row += 1 glEnable(GL_LIGHTING) glPopMatrix() def drawFPS(self): glPushMatrix() glDisable(GL_LIGHTING) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) glLoadIdentity() glTranslated(-320.0, -240.0, -410.0) glColor3f(1.0, 1.0, 1.0) self.font.glPrint(540.0, 440.0, "FPS: %d" % (self.fps)) glEnable(GL_LIGHTING) glPopMatrix() def draw(self, objects): global g_fVBOObjects #if self.reDraw: #self.main.octree.g_EndNodeCount = 0 #self.main.octree.debug.Clear() #self.main.octree.DestroyOctree() #self.main.octree.GetSceneDimensions(self.vertices, self.numberOfVertices) #self.main.octree.CreateNode(self.vertices, self.numberOfVertices, # self.main.octree.GetCenter(), self.main.octree.GetWidth()) #self.reDraw = False glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) glLoadIdentity() if self.toggleDrawAxes: self.drawAxes() glClearColor(0.4, 0.4, 0.4, 0.0) if self.wireframe: glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) else: glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) glLoadIdentity() glRotatef(self.main.input.xrot, 1.0, 0.0, 0.0) glRotatef(self.main.input.yrot, 0.0, 1.0, 0.0) # SkyDome self.skydome.draw() if self.spectator: glTranslated(-self.main.input.xpos, -self.main.input.ypos, -self.main.input.zpos) else: glTranslated( -self.main.player.data.position[0]-0.2*math.sin( math.radians(self.main.player.data.orientation[1]) ), -self.main.player.data.position[1]-2.2, -self.main.player.data.position[2]+0.2*math.cos( math.radians(self.main.player.data.orientation[1]-180) ) ) self.g_nFrames += 1 if self.toggleDrawAxes: self.drawAxes() # Water if not self.main.args['disableWater']: self.water.draw() glColor3f(1.0, 1.0, 1.0) #glClearColor(0.0, 0.0, 0.6, 0.5) glFogi(GL_FOG_MODE, GL_LINEAR) glFogfv(GL_FOG_COLOR, (0.4, 0.4, 0.4, 0.0)) glFogf(GL_FOG_DENSITY, 0.1) glHint(GL_FOG_HINT, GL_DONT_CARE) glFogf(GL_FOG_START, 1.0) glFogf(GL_FOG_END, 110.0) #glEnable(GL_FOG) glEnableClientState(GL_VERTEX_ARRAY) glEnableClientState(GL_TEXTURE_COORD_ARRAY) glEnableClientState(GL_NORMAL_ARRAY) glPushMatrix() glColor3f(1.0, 1.0, 1.0) #glScalef(10.0, 10.0, 10.0) for VBOobject in g_fVBOObjects: #glEnable(GL_BLEND) #glBlendFunc(GL_ONE, GL_ONE) glBindTexture(GL_TEXTURE_2D, VBOobject.textureId) glEnable(GL_TEXTURE_2D) drawVBO(VBOobject.verticesId, VBOobject.vnormalsId, VBOobject.vertexCount, VBOobject.texCoordsId) glDisable(GL_TEXTURE_2D) #glDisable(GL_BLEND) glPopMatrix() glDisableClientState(GL_VERTEX_ARRAY) glDisableClientState(GL_TEXTURE_COORD_ARRAY) glDisableClientState(GL_NORMAL_ARRAY) glDisable(GL_FOG) if self.main.physics.octree.debugLines: # Turn OFF lighting so the debug lines are bright yellow glDisable(GL_LIGHTING) # Start rendering lines glBegin(GL_LINES) # Turn the lines yellow glColor3ub(255, 255, 0) self.main.physics.octree.debug.debugLines = [] for obj in objects: self.main.physics.octree.debug.addDebugRectangle( obj.data.position, obj.data.width, obj.data.height, obj.data.depth) # Go through the whole list of lines stored in the vector debugLines for line in self.main.physics.octree.debug.debugLines: # Pass in the current point to be rendered as part of a line glVertex3f(line[0], line[1], line[2]) # Stop rendering lines glEnd() # If we have lighting turned on, turn the lights back on glEnable(GL_LIGHTING) for obj in objects: obj.draw() if self.main.input.keys["KEY_TAB"] == 1: self.drawPlayerList() self.drawFPS() glFlush() pygame.display.flip() err = glGetError() if err: print "OpenGL Error:",err,"(",gluErrorString(err),")"
class Scene : def __init__( self , fov , ratio , near , far , skybox_img , duck_img ) : self.fov = fov self.far = far self.near = near self.ratio = ratio self.last_time = timer() self.water = Water( 128 ) self.box = Skybox( skybox_img ) self.duck = Mesh( 'data/duck.gpt' , duck_img , 'shad/anisotropic' ) self.path = BSpline( (-1,1) , (-1,1) ) self.light = np.array( (0,2,0) ) self.water.drop_rnd() def gfx_init( self ) : self.camera = Camera( ( 0 , 5 , 0 ) , ( 1 , 1 , 0 ) , ( 1 , 0 , 0 ) ) self._update_proj() self.water.gfx_init() self.box .gfx_init() self.duck .gfx_init() def draw( self ) : self.time = timer() dt = self.time - self.last_time glMatrixMode(GL_MODELVIEW) glLoadIdentity() self.camera.look() self.box.draw() self.path.next( dt ) self.water.drop( *((self.path.value+1.0)*self.water.n/2.0) , force = np.linalg.norm(self.path.tangent)*25 ) self.water.step( dt * .5 ) self.water.draw( self.box.texture , self.camera.matrix ) self.duck.draw( self.path.value , self.path.tangent , self.light ) self.last_time = self.time def set_fov( self , fov ) : self.fov = fov self._update_proj() def set_near( self , near ) : self.near = near self._update_proj() def set_ratio( self , ratio ) : self.ratio = ratio self._update_proj() def set_screen_size( self , w , h ) : self.width = w self.height = h self.set_ratio( float(w)/float(h) ) def set_fov( self , fov ) : self.fov = fov self._update_proj() def set_near( self , near ) : self.near = near self._update_proj() def set_ratio( self , ratio ) : self.ratio = ratio self._update_proj() def set_screen_size( self , w , h ) : self.width = w self.height = h self.set_ratio( float(w)/float(h) ) def mouse_move( self , df ) : self.camera.rot( *map( lambda x : -x*.2 , df ) ) def key_pressed( self , mv ) : self.camera.move( *map( lambda x : x*.05 , mv ) ) def _update_proj( self ) : glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective( self.fov , self.ratio , self.near , self.far ) glMatrixMode(GL_MODELVIEW)
class Scene : def __init__( self , fovy , ratio , near , far ) : self.fovy = fovy self.near = near self.far = far self.ratio = ratio self.camera = None self.water = Water() self.water.set_borders( (-10,10,-10,10,-10,10) ) # self.water.set_borders( (-100,100,-10,1000,-100,100) ) self.mode = 0 def mkpln( s , p , r , a , c ) : p = Plane((s,s),np.dot(tr.translation_matrix(p),tr.rotation_matrix(r*m.pi/180.0,a))) p.c = c return p # self.borders = [ # mkpln(200,(-100,0,0),-90,(0,0,1),(.4,1,0)) , # mkpln(200,( 100,0,0), 90,(0,0,1),(.4,1,0)) , # mkpln(200,(0,0,-100), 90,(1,0,0),(.4,1,0)) , # mkpln(200,(0,0, 100),-90,(1,0,0),(.4,1,0)) , # mkpln(200,(0,-10,0), 0,(1,0,0),(.4,1,0)) ] self.borders = [ mkpln(20,(-10,0,0),-90,(0,0,1),(.4,1,0)) , mkpln(20,( 10,0,0), 90,(0,0,1),(.4,1,0)) , mkpln(20,(0,0,-10), 90,(1,0,0),(.4,1,0)) , mkpln(20,(0,0, 10),-90,(1,0,0),(.4,1,0)) , mkpln(20,(0,-10,0), 0,(1,0,0),(.4,1,0)) , mkpln(20,(0, 10,0),180,(1,0,0),(.4,1,0)) ] self.x = 0.0 self.last_time = timer() self.plane_alpha = 65.0 / 180.0 * m.pi self.lpos = [ 1 ,-1 , 0 ] def gfx_init( self ) : self.camera = Camera( ( 0 , 20 ,50 ) , ( 0 , 0 , 0 ) , ( 0 , 1 , 0 ) ) self.water.gfx_init() self._update_proj() glEnable( GL_DEPTH_TEST ) glEnable( GL_NORMALIZE ) glEnable( GL_CULL_FACE ) glEnable( GL_COLOR_MATERIAL ) glColorMaterial( GL_FRONT , GL_AMBIENT_AND_DIFFUSE ) def draw( self ) : self.time = timer() dt = self.time - self.last_time self._step( dt ) glMatrixMode(GL_MODELVIEW) glLoadIdentity() self.camera.look() self.lpos = [ 3,2,1 ] self._set_lights() self._draw_scene() self.x+=dt*.3 self.last_time = self.time def _step( self , dt ) : self.water.wave( dt ) def _draw_scene( self ) : glTranslatef( -1.5 , - 1.5 , -1.5 ) glCullFace( GL_BACK ) self.water.draw() glCullFace( GL_FRONT ) for b in self.borders : glColor3f( *b.c ) b.draw() def _update_proj( self ) : glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective( self.fovy , self.ratio , self.near , self.far ) glMatrixMode(GL_MODELVIEW) def _set_lights( self ) : glEnable(GL_LIGHTING); glLightfv(GL_LIGHT0, GL_AMBIENT, [ 0.2 , 0.2 , 0.2 ] ); glLightfv(GL_LIGHT0, GL_DIFFUSE, [ 0.5 , 0.5 , 0.5 ] ); glLightfv(GL_LIGHT0, GL_SPECULAR,[ 0.0 , 0.0 , 0.0 ] ); glLightfv(GL_LIGHT0, GL_POSITION, self.lpos ); glEnable(GL_LIGHT0); def set_fov( self , fov ) : self.fov = fov self._update_proj() def set_near( self , near ) : self.near = near self._update_proj() def set_ratio( self , ratio ) : self.ratio = ratio self._update_proj() def set_screen_size( self , w , h ) : self.width = w self.height = h self.set_ratio( float(w)/float(h) ) def mouse_move( self , df , buts ) : if 3 in buts and buts[3] : self.camera.rot( *map( lambda x : -x*.2 , df ) ) def key_pressed( self , mv ) : self.camera.move( *map( lambda x : x*.25 , mv ) ) def toggle_control( self ) : self.mode = not self.mode def set_model( self , mtype ) : if mtype == 0 : self.water.set( Water.SPH_T ) elif mtype == 1 : self.water.set( Water.LBM_T )