import pygame from OpenGL.GL import * from OpenGL.GLU import * def createTexture(w, h): texture = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, texture) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP) glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL) return texture def createEmptyTexture(w, h): texture = createTexture(w, h) data = [0, 0, 0, 0] * w * h glTexImage2D(GL_TEXTURE_2D, 0, 4, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, data) return texture def createTextureFromFile(filename): surf = pygame.image.load(filename) w, h = surf.get_width(), surf.get_height() data = pygame.image.tostring(surf, "RGBA") texture = createTexture(w, h) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data) return texture def drawTexture(x, y, z, w, h, texture): glBindTexture(GL_TEXTURE_2D, texture) glBegin(GL_QUADS) glColor4f(1.0, 1.0, 1.0, 0.5) glTexCoord2f(0.0, 0.0) glVertex3d(x, y, z) glTexCoord2f(1.0, 0.0) glVertex3d(x + w, y, z) glTexCoord2f(1.0, 1.0) glVertex3d(x + w, y + h, z) glTexCoord2f(0.0, 1.0) glVertex3d(x, y + h, z) glEnd() def initialize(width, height): pygame.init() pygame.display.init() resize(width, height) def resize(w, h, fullscreen = False): if h == 0: h = 1 screen = pygame.display.set_mode((w, h), pygame.OPENGL | pygame.DOUBLEBUF) glViewport(0, 0, w, h) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(45.0, float(w)/float(h), 0.1, 100.0) glMatrixMode(GL_MODELVIEW) glEnable(GL_TEXTURE_2D) glClearColor(0.0, 0.0, 0.0, 0.0) glClearDepth(1.0) glDepthFunc(GL_LESS) glDisable(GL_DEPTH_TEST) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glShadeModel(GL_SMOOTH) if __name__ == "__main__": initialize(640, 480) texture = createTextureFromFile("deceptacon-logo.png") while True: glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() glTranslate(0.0, 0.0, -5.0) drawTexture(-1.0, -1.0, 0.0, 2.0, 2.0, texture) pygame.display.flip() for event in pygame.event.get(): if event.type == pygame.QUIT: raise Exception("quit") elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: raise Exception("quit")