comparison pytouhou/opengl/texture.py @ 119:fad7b44cebf2

Switch from pygame + PyOpenGL to pyglet
author Thibaut Girka <thib@sitedethib.com>
date Wed, 07 Sep 2011 18:12:24 +0200
parents ac2e5e1c2c3c
children 7769ce7be03c
comparison
equal deleted inserted replaced
118:c596a1a69402 119:fad7b44cebf2
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of 10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details. 12 ## GNU General Public License for more details.
13 ## 13 ##
14 14
15 import pygame 15 import pyglet
16 from pyglet.gl import *
16 import os 17 import os
17 from io import BytesIO
18
19 import OpenGL
20 OpenGL.FORWARD_COMPATIBLE_ONLY = True
21 from OpenGL.GL import *
22 from OpenGL.GLU import *
23 18
24 19
25 class TextureManager(object): 20 class TextureManager(object):
26 def __init__(self, loader=None): 21 def __init__(self, loader=None):
27 self.loader = loader 22 self.loader = loader
45 40
46 41
47 def load_texture(self, key): 42 def load_texture(self, key):
48 first_name, secondary_name = key 43 first_name, secondary_name = key
49 44
50 image_file = self.loader.get_file(os.path.basename(first_name)) 45 image_file = pyglet.image.load(first_name, file=self.loader.get_file(os.path.basename(first_name)))
51 textureSurface = pygame.image.load(image_file).convert_alpha()
52 46
53 if secondary_name: 47 if secondary_name:
54 alpha_image_file = self.loader.get_file(os.path.basename(secondary_name)) 48 alpha_file = pyglet.image.load(secondary_name, file=self.loader.get_file(os.path.basename(secondary_name)))
55 alphaSurface = pygame.image.load(alpha_image_file) 49 assert (image_file.width, image_file.height) == (alpha_file.width, image_file.height)
56 assert textureSurface.get_size() == alphaSurface.get_size()
57 for x in range(alphaSurface.get_width()):
58 for y in range(alphaSurface.get_height()):
59 r, g, b, a = textureSurface.get_at((x, y))
60 color2 = alphaSurface.get_at((x, y))
61 textureSurface.set_at((x, y), (r, g, b, color2[0]))
62 50
63 textureData = pygame.image.tostring(textureSurface, 'RGBA', 1) 51 data = image_file.get_data('RGB', image_file.width * 3)
52 alpha_data = alpha_file.get_data('RGB', image_file.width * 3)
53 image_file = pyglet.image.ImageData(image_file.width, image_file.height, 'RGBA', b''.join(data[i*3:i*3+3] + alpha_data[i*3] for i in range(image_file.width * image_file.height)))
64 54
65 width = textureSurface.get_width() 55 #TODO
66 height = textureSurface.get_height()
67 56
68 texture = glGenTextures(1) 57 texture = image_file.get_texture()
69 glBindTexture(GL_TEXTURE_2D, texture)
70 58
71 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, 59 glTexParameteri(texture.target, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
72 GL_UNSIGNED_BYTE, textureData) 60 glTexParameteri(texture.target, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
73
74 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
75 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
76 61
77 return texture 62 return texture
78 63