def __draw_object( self, obj ): ''' Rysuje obiekt przekazany jako argument. ''' glPushMatrix() # przygotuj mieszanie kolorów glEnable( GL_BLEND ) glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) # zbierz informacje o reprezentacji obiektu spriteName = obj.spriteName animName = obj.get_current_animation_name() frameNum = obj.get_current_frame_num() frame = self.__spriteManager.get_frame( spriteName, animName, frameNum ) (ww,wh) = self.__theApp.get_window_draw_dim() (tc,vs) = compute_tex_vertex_coords( obj, frame, ww, wh ) textureId = frame.textureId # narysuj tło pod sprite'em (do celów testowych) if( obj.display_pad() ): glDisable( GL_TEXTURE_2D ) glColor3f( .3, .3, .4 ) # ew. kolor może być skądś pobierany glBegin( GL_QUADS ) for v in vs: glVertex2f( *v ) glEnd() # narysuj sprite'a draw_textured_quad( tc, vs, textureId ) glColor3f( 1, 1, 1 ) glPopMatrix()
def draw(self): ''' Rysuje wszystkie obiekty w świecie. ''' glClearColor( 0.4, 0.4, 0.4, 0.5 ) glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) glMatrixMode( GL_MODELVIEW ) glLoadIdentity() # zmień viewport, będziemy renderować do tekstury if config.IS_RENDER_TO_TEXTURE: tw,th = const.renderTextureSize glViewport( 0, 0, tw, th ) glTranslatef( 0, 0, -10 ) # narysuj tło self.get_background_manager().draw_background() # narysuj scenerię glTranslatef( 0, 0, 1 ) for obj in filter(lambda o:isinstance(o,SceneryObject),self.__objects): self.__draw_object( obj ) # narysuj jednostki, etc. glTranslatef( 0, 0, 1 ) for obj in self.__objects: if not (isinstance(obj,SceneryObject) or isinstance(obj,GroundObject)): self.__draw_object( obj ) # narysuj podłoże glTranslatef( 0, 0, 1 ) for obj in filter(lambda o:isinstance(o,GroundObject) and o.position[0]<1.1,self.__objects): self.__draw_object( obj ) # zapisz bufor do tekstury if config.IS_RENDER_TO_TEXTURE: assert glIsTexture( self.__renderTexture.id ), "Próba narysowania czegoś, co nie jest teksturą" glBindTexture( GL_TEXTURE_2D, self.__renderTexture.id ) glCopyTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, 0, 0, tw, th, 0 ) # glCopyTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, 0, 0, tw, th ) # przywróć viewport, wyczyść bufor i narysuj czworokąt z zapisaną teksturą winSize = map( lambda x:int(x), self.__theApp.get_window_dim() ) glViewport( 0, 0, winSize[0], winSize[1] ) if config.IS_RENDER_TO_TEXTURE: glClearColor( 0.5, 0.5, 0.5, 0.5 ) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() glEnable( GL_TEXTURE_2D ) glColor3f( 1, 1, 1 ) # renderuj czworokąt na cały ekran coords = self.__theApp.get_window_coords() x0,x1,y0,y1 = coords[0], coords[1], coords[2], coords[3] tc = [ ( 0, 0), ( 1, 0), ( 1, 1), ( 0, 1) ] vs = [ ( x0, y0), ( x1, y0), ( x1, y1), ( x0, y1) ] draw_textured_quad( tc, vs, self.__renderTexture.id ) # zrzuć wszystko glFlush()
def draw_background(self): ''' Rysuje tło poziomu.''' # przelicz współrzędne tekstury i czworokąta z tłem coords = self.__window_coords # czworokąt zajmujący cały ekran x0, x1, y0, y1 = coords[0], coords[1], coords[2], coords[3] off = self.__backgroundOffset # przesunięcie tła (mapa stoi,poruszamy tłem) tc = [(0 + off, 0), (.5 + off, 0), (.5 + off, 1), (0 + off, 1)] vs = [(x0, y0), (x1, y0), (x1, y1), (x0, y1)] # jeżeli to konieczne to wczytaj teksturę z tłem if not self.__backgroundTexture: bgfilename = self.__levelManager.get_background_filenames()[0] spriteImagePath = os.path.join("..", "gfx", bgfilename) img = image.load(spriteImagePath) self.__backgroundTexture = img.get_texture() # narysuj czworokąt z tłem if self.__backgroundTexture: textureId = self.__backgroundTexture.id draw_textured_quad(tc, vs, textureId)
def draw_background( self ): ''' Rysuje tło poziomu.''' # przelicz współrzędne tekstury i czworokąta z tłem coords = self.__window_coords # czworokąt zajmujący cały ekran x0,x1,y0,y1 = coords[0], coords[1], coords[2], coords[3] off = self.__backgroundOffset # przesunięcie tła (mapa stoi,poruszamy tłem) tc = [ ( 0+off, 0), ( .5+off, 0), ( .5+off, 1), ( 0+off, 1) ] vs = [ ( x0, y0), ( x1, y0), ( x1, y1), ( x0, y1) ] # jeżeli to konieczne to wczytaj teksturę z tłem if not self.__backgroundTexture: bgfilename = self.__levelManager.get_background_filenames()[0] spriteImagePath = os.path.join( "..", "gfx", bgfilename ) img = image.load( spriteImagePath ) self.__backgroundTexture = img.get_texture() # narysuj czworokąt z tłem if self.__backgroundTexture: textureId = self.__backgroundTexture.id draw_textured_quad( tc, vs, textureId )