From deaa55d5358e044acd23df1aa9cd7f8885ea5c79 Mon Sep 17 00:00:00 2001 From: cecilkorik Date: Fri, 17 Jun 2011 19:54:49 -0600 Subject: [PATCH] working textures! completely overhauled models framework... now loads Model, Mesh, Material, and Texture (plus TextureFile) fixed a number of bugs/inflexibilities in gamedata xml parser refactored the various modules to use "init" functions to better control when they get loaded (ie, before or after OpenGL init) which it turns out is *very important*, because if the shaders and textures get loaded before OpenGL does, they don't work. go figure. --- asset.py | 6 +- data/xml/def/models.xml | 34 ++-- data/xml/def/textures.xml | 11 +- data/xml/models.xml | 105 +----------- data/xml/textures.xml | 12 +- enums.py | 20 ++- files.py | 9 +- gamedata.py | 85 ++++++---- models.py | 344 ++++++++++++++++++++++++++++++++------ roc.py | 72 +------- shader.py | 8 +- sprite.py | 69 -------- texture.py | 21 --- video.py | 3 +- 14 files changed, 440 insertions(+), 359 deletions(-) delete mode 100755 sprite.py delete mode 100755 texture.py diff --git a/asset.py b/asset.py index a65eb41..b1c8071 100755 --- a/asset.py +++ b/asset.py @@ -10,4 +10,8 @@ class asset_manager(object): return go -mgr = asset_manager() \ No newline at end of file +def init(): + global mgr + mgr = asset_manager() + +mgr = None diff --git a/data/xml/def/models.xml b/data/xml/def/models.xml index 42935d0..fd39876 100755 --- a/data/xml/def/models.xml +++ b/data/xml/def/models.xml @@ -1,23 +1,37 @@ + + + + + + - - - - - + + + + + + + + + + - - + + + + + + + + - - - diff --git a/data/xml/def/textures.xml b/data/xml/def/textures.xml index 8ce4ec1..f2bcfcf 100755 --- a/data/xml/def/textures.xml +++ b/data/xml/def/textures.xml @@ -3,9 +3,16 @@ - - + + + + + + + + + diff --git a/data/xml/models.xml b/data/xml/models.xml index 3d49092..4c21b0d 100755 --- a/data/xml/models.xml +++ b/data/xml/models.xml @@ -1,101 +1,14 @@ - + + + + + + + - star_sprite_washout - - 1.0,1.0,0.5,1.0 - - - star_sprite_main - - 1.0,1.0,0.7,1.0 - - - star_sprite_inner - - 1.0,1.0,1.0,1.0 - - - - - 1.0 - - - - - kestrel - kestrel - - - - - thor - - - - 0.5 - - - - - - navbuoy - - - - 1.0 - - - - - - - navbuoy - - - - 2.5 - - - - - - planet1 - 100.0 - - - - - soft_particle - - - - - - - hard_particle - - - - - - - medium_particle - - - - - - - kestrel - kestrel - - - - - planet1 - planet1_earthy - 1000.0 + mesh_sprite + mat_test diff --git a/data/xml/textures.xml b/data/xml/textures.xml index b21e498..884cee2 100755 --- a/data/xml/textures.xml +++ b/data/xml/textures.xml @@ -1,16 +1,20 @@ tex/black.png + + tex/white.png + tex/nm_flat.png + - + - + tex/test1.png @@ -21,7 +25,7 @@ tex/plasma2.png - - + + \ No newline at end of file diff --git a/enums.py b/enums.py index e00ecb3..0a101cd 100755 --- a/enums.py +++ b/enums.py @@ -1,16 +1,32 @@ class enum(object): - pass + def __len__(self): + return len([x for x in dir(self) if x[0] != '_']) + + def __iter__(self): + for name in sequence(self): + yield getattr(self, name) + +def index(enumobj, name): + return getattr(enumobj, name) def reverse(enumobj, value): for name in dir(enumobj): if name[0] == '_': continue - if value == eval("enumobj.%s" % (name,)): + if value == getattr(enumobj, name): return name return None +def sequence(enumobj): + retlist = [] + for name in dir(enumobj): + if name[0] == '_': + continue + retlist.append((getattr(enumobj, name), name)) + + return [x[1] for x in sorted(retlist)] # render types (for model.renderable_layer) rt = enum() diff --git a/files.py b/files.py index ceb6549..30ec322 100755 --- a/files.py +++ b/files.py @@ -24,5 +24,12 @@ class filemanager(object): def png(self, *args): filename = self.path(*args) return pygame.image.load(filename) + + def canonize_path(self, path): + return path.lower() -mgr = filemanager() +def init(): + global mgr + mgr = filemanager() + +mgr = None diff --git a/gamedata.py b/gamedata.py index b086eb0..f3f886c 100755 --- a/gamedata.py +++ b/gamedata.py @@ -24,6 +24,8 @@ class GameDataTagDef(object): class GameDataNode(object): def __init__(self): self.dict = {} + self.missing = False + def __setitem__(self, name, val): self.dict[name] = val def __getitem__(self, name): @@ -97,20 +99,24 @@ class XMLGameDataReader(object): def create_attribute_def(self, tag): return self.create_datatag_def(tag) - def construct_node(self, bin, xml): + def construct_node(self, bin, xml, allow_missing=False): node = GameDataNode() + if isinstance(xml, MissingNode): + node.missing = True value = None if bin.tag == 'datatag': df = self.create_datatag_def(bin) if df.type: value = xml.text - if type(xml) == MissingNode and df.opt: + if df.type == 'bool': + # if tag exists, since it is a bool, that means it's True + value = not node.missing + elif (node.missing or value == None) and df.opt: value = df.default - elif type(xml) == MissingNode: + elif (node.missing or value == None) and not allow_missing: raise ValueError, "Missing value for mandatory tag %s" % (bin.get('name'),) - elif df.type == 'bool': - # tag exists, and since it is a bool, that means it's True - value = True + elif (node.missing or value == None): + value = None else: value = self.value_as_type(value, df.type) @@ -121,8 +127,10 @@ class XMLGameDataReader(object): value = xml.get(bin.get('name')) if value == None and df.opt: value = df.default - elif not value: + elif not value and not allow_missing: raise ValueError, "Missing value for mandatory tag %s" % (bin.get('name'),) + elif not value: + value = None else: value = self.value_as_type(value, df.type) @@ -133,7 +141,7 @@ class XMLGameDataReader(object): return node - def construct_recurse(self, bin, xml): + def construct_recurse(self, bin, xml, allow_missing=False): xmldict = {} tagdict = {} attrdict = {} @@ -141,7 +149,7 @@ class XMLGameDataReader(object): if bin.tag == 'xml_binary_packing': node = GameDataNode() else: - node = self.construct_node(bin, xml) + node = self.construct_node(bin, xml, allow_missing) for child in bin.getchildren(): if child.tag == 'datatag': @@ -152,38 +160,41 @@ class XMLGameDataReader(object): raise ValueError xmlattrdict = {} - for k, v in xml.items(): - if not k in attrdict: - raise ValueError, "Key %s not a valid attribute: %s" % (k, attrdict.keys()) - continue - binchild = attrdict[k] - xmlchild = xml - xmlattrdict[k] = 0 - subnode = self.construct_node(binchild, xml) - if subnode._def.multi: - node.add_multi(k, subnode) - else: - node.add_single(k, subnode) - xmltagdict = {} - for child in xml.getchildren(): - if not child.tag in tagdict: - raise ValueError - continue - binchild = tagdict[child.tag] - xmlchild = child - xmltagdict[child.tag] = 0 - subnode = self.construct_recurse(binchild, xmlchild) - if subnode._def.multi: - node.add_multi(child.tag, subnode) - else: - node.add_single(child.tag, subnode) - + + if not isinstance(xml, MissingNode): + for k, v in xml.items(): + if not k in attrdict: + raise ValueError, "Key %s not a valid attribute: %s" % (k, attrdict.keys()) + continue + binchild = attrdict[k] + xmlchild = xml + xmlattrdict[k] = 0 + subnode = self.construct_node(binchild, xml, allow_missing) + if subnode._def.multi: + node.add_multi(k, subnode) + else: + node.add_single(k, subnode) + + for child in xml.getchildren(): + if not child.tag in tagdict: + raise ValueError + continue + binchild = tagdict[child.tag] + xmlchild = child + xmltagdict[child.tag] = 0 + subnode = self.construct_recurse(binchild, xmlchild, allow_missing) + if subnode._def.multi: + node.add_multi(child.tag, subnode) + else: + node.add_single(child.tag, subnode) + + missing = MissingNode() for k in tagdict.keys(): if not k in xmltagdict: # Missing datatag - subnode = self.construct_node(tagdict[k], missing) + subnode = self.construct_recurse(tagdict[k], missing, isinstance(xml, MissingNode)) if not subnode._def.multi: node.add_single(k, subnode) else: @@ -192,7 +203,7 @@ class XMLGameDataReader(object): for k in attrdict.keys(): if not k in xmlattrdict: # Missing attribute - subnode = self.construct_node(attrdict[k], missing) + subnode = self.construct_node(attrdict[k], missing, isinstance(xml, MissingNode)) if not subnode._def.multi: node.add_single(k, subnode) else: diff --git a/models.py b/models.py index bb04bf1..58f6a1b 100755 --- a/models.py +++ b/models.py @@ -1,4 +1,9 @@ import enums +import files +import gamedata +import pygame +from OpenGL.GL import * +from py3dutil import vect class Vertex(object): __slots__ = [ @@ -7,39 +12,99 @@ class Vertex(object): 't', # texture coordinates 'c', # color ] + + def __init__(self, v, n, t, c): + self.v = v + self.n = n + self.t = t + self.c = c class Model_Manager(object): def __init__(self): - self.load_textypes() - self.load_textures() - self.load_materials() - self.load_models() - - def load_textypes(): - self.textypes = {} - self.textypes[enums.tt.diffuse] = TextureType(GL_TEXTURE0, GL_TEXTURE_2D, GL_CLAMP, GL_CLAMP, GL_NEAREST, GL_NEAREST) - self.textypes[enums.tt.dark] = TextureType(GL_TEXTURE1, GL_TEXTURE_2D) - self.textypes[enums.tt.specular] = TextureType(GL_TEXTURE2, GL_TEXTURE_2D) - self.textypes[enums.tt.normal] = TextureType(GL_TEXTURE3, GL_TEXTURE_2D) + pass + + def create(self, mname): + return self.models[mname].instance() + def get_textype(self, textype): if isinstance(textype, TextureType): return textype return self.textypes[textype] - - def load_textures(self): - gamedata.get('textures') - - def load_textures(self): - gamedata.get('materials') - - def load_textures(self): - gamedata.get('models') + + def load(self): + self.load_textypes() + self.load_textures() + self.load_materials() + self.load_models() -mgr = Model_Manager() + def load_textypes(self): + self.textypes = {} + self.textypes[enums.tt.diffuse] = TextureType(GL_TEXTURE0, GL_TEXTURE_2D, GL_CLAMP, GL_CLAMP, GL_NEAREST, GL_NEAREST) + self.textypes[enums.tt.emissive] = TextureType(GL_TEXTURE1, GL_TEXTURE_2D) + self.textypes[enums.tt.specular] = TextureType(GL_TEXTURE2, GL_TEXTURE_2D) + self.textypes[enums.tt.normal] = TextureType(GL_TEXTURE3, GL_TEXTURE_2D) + + for tt in self.textypes.values(): + tt.initialize() + + def load_textures(self): + texdata = gamedata.get('textures') + self.texture_files = {} + self.texture_ids = {} + self.textures = {} + self.materials = {} + for texd in texdata.texture: + fp = files.mgr.canonize_path(texd['file']) + if not fp in self.texture_files: + tf = TextureFile() + tf.load(fp) + self.texture_files[fp] = tf + self.texture_ids[tf.id] = tf + else: + tf = self.texture_files[fp] + to = Texture() + to.load(texd, tf) + self.textures[to.id] = to + + for matd in texdata.material: + mo = Material() + mo.load(matd) + self.materials[mo.id] = mo + + + def load_meshes(self): + "ignored!!! meshes are contained in models.xml for now" + return + gamedata.get('meshes') + + def load_materials(self): + "ignored!!! materials are contained in textures.xml for now" + return + gamedata.get('materials') + + + def load_models(self): + mdldata = gamedata.get('models') + self.meshes = {} + self.models = {} + for meshdata in mdldata.mesh: + if meshdata['sprite']: + mesh = SpriteMesh.singleton() + elif meshdata['centered_sprite']: + mesh = SpriteMeshCentered.singleton() + else: + mesh = Mesh.factory(meshdata) + mgr.meshes[meshdata['id']] = mesh + + for modeldata in mdldata.model: + model = Model() + model.load(modeldata) + mgr.models[modeldata['id']] = model + class TextureType(object): - def __init__(self, texunit, texdim, wrap_s=GL_CLAMP, wrap_t=GL_CLAMP, mag=GL_LINEAR, min=GL_LINEAR) + def __init__(self, texunit, texdim, wrap_s=GL_CLAMP, wrap_t=GL_CLAMP, mag=GL_LINEAR, min=GL_LINEAR): self.texunit = texunit self.texdim = texdim self.wrap_s = wrap_s @@ -64,32 +129,126 @@ class TextureType(object): class Material(object): - def __init__(self): - pass - def attach_texture(self, textype, texid): - textype = mgr.get_textype(textype) - textype.activate() + self.id = None + self.textures = {} + self.texcoords = self.texcoords_notimpl + self.texcoords_uniform = self.texcoords_notimpl + + def texcoords_notimpl(self, u, v): + raise NotImplementedError("No textures were associated with this material.") + + def bind(self): + for tt in enums.tt: + if tt in self.textures: + tto = mgr.get_textype(tt) + tto.initialize() + tex = self.textures[tt] + tex.bind() + + def load(self, data): + self.id = data['id'] + prevtex = None + for tex in data.texture: + try: + texobj = mgr.textures[tex['ref']] + except KeyError: + raise KeyError("""Material "%s" references invalid %s texture "%s".""" % (self.id, tex['type'], tex['ref'])) + + typeidx = enums.index(enums.tt, tex['type']) + self.textures[typeidx] = texobj + + if texobj.uniform: + if self.texcoords_uniform == self.texcoords_notimpl: + self.texcoords_uniform = texobj.texcoords + else: + if prevtex: + assert texobj.texcoords(0.0, 0.0) == prevtex.texcoords(0.0, 0.0) and texobj.texcoords(1.0, 1.0) == prevtex.texcoords(1.0, 1.0) + if self.texcoords == self.texcoords_notimpl: + self.texcoords = texobj.texcoords + prevtex = texobj + + print self.textures - self.texid class Texture(object): def __init__(self): - pass - def load(self, file): - img = files.mgr.png(file) - texid, = glGenTextures(1) + self.id = None + self.texid = None + self.uniform = False + self.x = None + self.y = None + self.h = None + self.w = None + + + def texcoords_subset(self, u, v): + tf = mgr.texture_ids[self.texid] + dest_x = float(self.x) / float(tf.w) + dest_w = (float(self.x + self.w) / float(tf.w)) - dest_x + dest_y = float(self.y) / float(tf.h) + dest_h = (float(self.y + self.h) / float(tf.h)) - dest_y + return (dest_x + (u * dest_w), dest_y + (v * dest_h)) + + def texcoords_direct(self, u, v): + return (u, v) + + def load(self, data, tf): + self.id = data['id'] + self.texid = tf.id + self.uniform = data['uniform'] + c = data.coords + if c['x'] != -1 and c['y'] != -1 and c['h'] != -1 and c['w'] != -1: + self.x = c['x'] + self.y = c['y'] + self.w = c['w'] + self.h = c['h'] + assert self.x < tf.w and self.w < tf.w and self.y < tf.h and self.h < tf.h + self.texcoords = self.texcoords_subset + elif c['x1'] != -1 and c['y1'] != -1 and c['x2'] != -1 and c['y2'] != -1: + self.x = c['x1'] + self.y = c['y1'] + self.w = c['x2'] - self.x + self.h = c['y2'] - self.y + assert self.x < tf.w and self.w < tf.w and self.y < tf.h and self.h < tf.h + self.texcoords = self.texcoords_subset + else: + self.x = 0 + self.y = 0 + self.w = tf.w + self.h = tf.h + self.texcoords = self.texcoords_direct + + def bind(self): + glBindTexture(GL_TEXTURE_2D, self.texid) + +class TextureFile(object): + def __init__(self): + self.id = None + self.filename = None + self.h = None + self.w = None + + def load(self, filename): + self.filename = filename + img = files.mgr.png(filename) + texid = glGenTextures(1) + print "Generated texture id %s" % (texid,) self.id = texid imgdata = pygame.image.tostring(img, "RGBA") imgr = img.get_rect() + self.h = imgr.h + self.w = imgr.w - glBindTexture(textype.get_dimension(), texid) - glTexImage2D(textype.get_dimension(), 0, GL_RGBA8, imgr.w, imgr.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, imgdata) - glBindTexture(textype.get_dimension(), 0) + dimension = GL_TEXTURE_2D + glBindTexture(dimension, texid) + glTexImage2D(dimension, 0, GL_RGBA8, imgr.w, imgr.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, imgdata) + glBindTexture(dimension, 0) -class Model(object): + +class Mesh(object): def __init__(self): self.vertexes = [] @@ -101,26 +260,105 @@ class Model(object): def unset_rotation(self): glPopMatrix() - def bind_textures(self): - glBindTexture(GL_TEXTURE_2D - def unbind_textures(self): - - def draw(self): - self.bind_textures() + def draw(self, texcoord): glBegin(GL_TRIANGLES) for v in self.vertexes: - self.draw_vertex(v) - glEnd(GL_TRIANGLES) - self.unbind_textures() + self.draw_vertex(v, texcoord) + glEnd() - def draw_vertex(self, v): + def draw_vertex(self, v, texcoord): glColor3f(v.c[0], v.c[1], v.c[2]) glNormal3f(v.n.x, v.n.y, v.n.z) - glTexCoord2f(v.t[0], v.t[1]) + glTexCoord2f(*texcoord(v.t[0], v.t[1])) glVertex3f(v.v.x, v.v.y, v.v.z) + - + +class SpriteMesh(Mesh): + _SINGLETON = None + + def __init__(self): + Mesh.__init__(self) + self.vertexes = [ + Vertex(vect(0.0, 0.0, 0.0), vect(0.0, 0.0, 1.0), (0.0, 0.0), (1.0, 1.0, 1.0)), + Vertex(vect(0.0, 1.0, 0.0), vect(0.0, 0.0, 1.0), (0.0, 1.0), (1.0, 1.0, 1.0)), + Vertex(vect(1.0, 0.0, 0.0), vect(0.0, 0.0, 1.0), (1.0, 0.0), (1.0, 1.0, 1.0)), + Vertex(vect(1.0, 0.0, 0.0), vect(0.0, 0.0, 1.0), (1.0, 0.0), (1.0, 1.0, 1.0)), + Vertex(vect(0.0, 1.0, 0.0), vect(0.0, 0.0, 1.0), (0.0, 1.0), (1.0, 1.0, 1.0)), + Vertex(vect(1.0, 1.0, 0.0), vect(0.0, 0.0, 1.0), (1.0, 1.0), (1.0, 1.0, 1.0)), + ] + + @classmethod + def return_singleton(cls): + return cls._SINGLETON + + @classmethod + def singleton(cls): + cls._SINGLETON = cls() + cls.singleton = cls.return_singleton + return cls._SINGLETON + + +class SpriteMeshCentered(SpriteMesh): + def __init__(self): + SpriteMesh.__init__(self) + self.vertexes = [ + Vertex(vect(-0.5, -0.5, 0.0), vect(0.0, 0.0, 1.0), (0.0, 0.0), (1.0, 1.0, 1.0)), + Vertex(vect( 0.5, -0.5, 0.0), vect(0.0, 0.0, 1.0), (1.0, 0.0), (1.0, 1.0, 1.0)), + Vertex(vect(-0.5, 0.5, 0.0), vect(0.0, 0.0, 1.0), (0.0, 1.0), (1.0, 1.0, 1.0)), + Vertex(vect( 0.5, -0.5, 0.0), vect(0.0, 0.0, 1.0), (1.0, 0.0), (1.0, 1.0, 1.0)), + Vertex(vect(-0.5, 0.5, 0.0), vect(0.0, 0.0, 1.0), (0.0, 1.0), (1.0, 1.0, 1.0)), + Vertex(vect( 0.5, 0.5, 0.0), vect(0.0, 0.0, 1.0), (1.0, 1.0), (1.0, 1.0, 1.0)), + ] + + +class Model(object): + def __init__(self): + self.layers = [] + + def load(self, data): + tmplayers = [] + for rend in data.render: + layernum = rend['layer'] + rl = RenderLayer(layernum) + rl.load(rend) + tmplayers.append((layernum, rl)) + for ln, rl in sorted(tmplayers): + self.layers.append(rl) + + def instance(self): + return self + + def render(self): + for layer in self.layers: + layer.render() + +class RenderLayer(object): + def __init__(self, layernum): + self.id = layernum + self.mesh = None + self.material = None + self.color = [1.0, 1.0, 1.0, 1.0] + self.scale = [1.0, 1.0, 1.0] + + def load(self, layerdata): + meshdata = layerdata.mesh + matdata = layerdata.material + self.mesh = mgr.meshes[meshdata['ref']] + self.material = mgr.materials[matdata['ref']] + self.color = [matdata.color['r'], matdata.color['g'], matdata.color['b'], matdata.color['a']] + self.scale = [meshdata.scale['x'], meshdata.scale['y'], meshdata.scale['z']] + self.scale = [x * meshdata['scale'] for x in self.scale] + + def render(self): + self.material.bind() + glPushMatrix() + glTranslatef(500.0, 300.0, 0.0) + glScalef(*self.scale) + self.mesh.draw(self.material.texcoords) + glPopMatrix() + class Sprite(object): def __init__(self): @@ -143,4 +381,12 @@ class Sprite(object): self.corners = vl self.vertexes = [tl, bl, tr, bl, tr, br] - def set_texture( \ No newline at end of file + def set_texture(self): + pass + +def init(): + global mgr + mgr = Model_Manager() + mgr.load() + +mgr = None diff --git a/roc.py b/roc.py index f713ce4..ce6a3c6 100755 --- a/roc.py +++ b/roc.py @@ -1,11 +1,9 @@ import pygame -from pygame.locals import * -import gamedata -import gametimer +import files import video import shader -import inputs -from OpenGL.GL import * +import models +import roc_main def init2d(): @@ -16,6 +14,8 @@ def init3d(): def init(videoinit): pygame.init() + files.init() + shader.init() size = width, height = (1600,1200) size = width, height = (1024,768) @@ -23,64 +23,8 @@ def init(videoinit): video.set_res(size) videoinit() video.enable_vsync() - -def test_frame(): - from PIL import Image - - # pink triangle - glBegin(GL_TRIANGLE_STRIP) - glNormal3f(0.0, 0.0, 1.0) - glColor4f(1.0, 0.0, 0.5, 1.0) - glVertex3f( 20.0, 50.0, 1.0) - glVertex3f( 20.0, 600.0, 1.0) - glVertex3f( 400.0, 50.0, 1.0) - glEnd() - - # yellow square - glBegin(GL_TRIANGLE_STRIP) - glNormal3f(0.0, 0.0, 1.0) - glColor4f(1.0, 1.0, 0.0, 1.0) - glVertex3f( 150.0, 20.0, -50.0) - glVertex3f( 150.0, 400.0, -50.0) - glVertex3f( 360.0, 20.0, -50.0) - glVertex3f( 360.0, 400.0, -50.0) - glEnd() - - # texture test - glBegin(GL_TRIANGLE_STRIP) - glNormal3f(0.0, 0.0, 1.0) - glColor4f(1.0, 1.0, 1.0, 1.0) - glVertex3f( 500.0, 300.0, 5.0) - glVertex3f( 500.0, 500.0, 5.0) - glVertex3f( 700.0, 300.0, 5.0) - glVertex3f( 700.0, 500.0, 5.0) - glEnd() - -def main(): - gametimer.start_loop() - while True: - events = pygame.event.get() - gametimer.next_frame() - pygame.event.pump() - for ev in events: - if ev.type == QUIT: - inputs.add_command('exit') - break - elif ev.type == KEYUP: - inputs.keyup(ev.key) - elif ev.type == KEYDOWN: - inputs.keydown(ev.key) - #elif ev.type == VIDEOEXPOSE: - # video.force_redraw() - elif ev.type in (MOUSEBUTTONDOWN, MOUSEBUTTONUP): - pass - - if 'exit' in inputs.commands: - break - - video.predraw() + models.init() - test_frame() - - video.next_frame() +def main(): + roc_main.mainloop() diff --git a/shader.py b/shader.py index 56443a3..a9f69ba 100755 --- a/shader.py +++ b/shader.py @@ -211,5 +211,9 @@ class shader_manager(object): def load_shaders(self, shaders): for shader in shaders.shader: self.initshadersources.append((shader['id'], shader['vertex'], shader['fragment'])) - -mgr = shader_manager() + +def init(): + global mgr + mgr = shader_manager() + +mgr = None diff --git a/sprite.py b/sprite.py deleted file mode 100755 index 1f36a08..0000000 --- a/sprite.py +++ /dev/null @@ -1,69 +0,0 @@ -import files -import os -from OpenGL.GL import * -from py3ds.example.gltexture import Texture - -class renderable_sprite(object): - """ - Internal class to create a textured quad for a sprite to live on. - Multiple sprites can use the same sprite_tex if the graphic they want to - display is the same. - """ - def __init__(self): - self.gltex = None - self.gllist = None - self.texfile = None - self.scale = 1.0 - def load_name(self, name): - self.texfile = files.mgr.open(os.path.join('sprite', name + '.png')) - def set_file(self, fileobj): - self.texfile = fileobj - def update(self): - pass - def render(self): - if self.gltex == None: - self.gltex = Texture(self.texfile) - self.texfile.close() - self.texfile = None - if self.gllist == None: - glPushMatrix() - #glLoadIdentity() - dl = glGenLists(1) - if dl == 0: - raise GLError, "cannot allocate display list" - glNewList(dl, GL_COMPILE) - glEnable(GL_BLEND) - glEnable(GL_DEPTH_TEST) - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) - self.gltex.enable() - self.gltex.bind() - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE) - glBegin(GL_TRIANGLE_STRIP) - glNormal3f(1.0, 0.0, 0.0) - glTexCoord2f(1.0, 1.0); glVertex3f( 0.0, 1.0, 1.0) - glTexCoord2f(0.0, 1.0); glVertex3f( 0.0, 1.0, -1.0) - glTexCoord2f(1.0, 0.0); glVertex3f(-0.0, -1.0, 1.0) - glTexCoord2f(0.0, 0.0); glVertex3f(-0.0, -1.0, -1.0) - glEnd() - - self.gltex.disable() - glDisable(GL_BLEND) - glEndList() - self.gllist = dl - glPopMatrix() - glCallList(self.gllist) - - -class sprite_manager(object): - def __init__(self): - self.sprite_cache = {} - def get(self, name): - if name in self.sprite_cache: - return self.sprite_cache[name] - - s = renderable_sprite() - s.load_name(name) - self.sprite_cache[name] = s - return s - -mgr = sprite_manager() \ No newline at end of file diff --git a/texture.py b/texture.py deleted file mode 100755 index 57d9eab..0000000 --- a/texture.py +++ /dev/null @@ -1,21 +0,0 @@ - -class texture_wrapper(object): - def __init__(self): - pass - - def load_name(self, name): - pass - -class texture_manager(object): - def __init__(self): - self.texture_cache = {} - def get(self, name): - if name in self.texture_cache: - return self.texture_cache[name] - - t = texture_wrapper() - t.load_name(name) - self.texture_cache[name] = t - return t - -mgr = texture_manager() \ No newline at end of file diff --git a/video.py b/video.py index 58eb880..4e6c9e8 100755 --- a/video.py +++ b/video.py @@ -70,7 +70,8 @@ def init2d(): glDepthFunc(GL_GEQUAL) glEnable(GL_BLEND) glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) - + glLightModelfv(GL_LIGHT_MODEL_AMBIENT, (0.3, 0.3, 0.3, 1.0)) + shader.mgr.load() shader.mgr.init_gl() shader.mgr.select("ffp")