diff --git a/gameobj.py b/gameobj.py index c57501b..0f9adaf 100755 --- a/gameobj.py +++ b/gameobj.py @@ -1,5 +1,82 @@ class gameobj(object): + role_factory = {} + + @staticmethod + def create_role_factory(role, factory): + gameobj.role_factory[role] = factory + + def __init__(self, self.pos): + self.pos = pos + self.roles = [] + self.effects = [] + + def add_role(self, role): + assert not type(role) in [type(x) for x in self.roles] + for i, r in enumerate(self.roles): + if role is r: + break + if role.order < r.order: + self.roles.insert(i, role) + break + + def remove_role(self, role): + for + + def ai(self): + pass + + def handle_input(self): + pass + + def move(self): + pass + + def animate(self): + pass + + def collide(self): + pass + + def prerender(self): + pass + + +gameobj.create_role_factory('ai', ai_role) + +class role(object): + order = 1000 def __init__(self): pass +class ai_role(role): + order = 200 + pass + +class visible_role(role): + order = 10000 + pass + +class corporeal_role(role): + order = 500 + pass + +class animated_role(role): + order = 400 + pass + +class movable_role(role): + order = 300 + pass + +class player_role(role): + order = 100 + pass + +class light_role(role): + order = 9000 + pass + +class _role(role): + pass + diff --git a/models.py b/models.py index 25f64eb..a6904ea 100755 --- a/models.py +++ b/models.py @@ -126,51 +126,6 @@ class TextureType(object): glActiveTexture(self.texunit) - - -class Material(object): - def __init__(self): - 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 - - class Texture(object): def __init__(self): self.id = None @@ -218,7 +173,7 @@ class Texture(object): self.w = tf.w self.h = tf.h self.texcoords = self.texcoords_direct - self.texcoords = self.texcoords_subset + #self.texcoords = self.texcoords_subset def bind(self): glBindTexture(GL_TEXTURE_2D, self.texid) @@ -248,6 +203,51 @@ class TextureFile(object): glTexImage2D(dimension, 0, GL_RGBA8, imgr.w, imgr.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, imgdata) glBindTexture(dimension, 0) + + +class Material(object): + def __init__(self): + 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 + + class Mesh(object): def __init__(self): @@ -311,7 +311,34 @@ class SpriteMeshCentered(SpriteMesh): 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 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] + self.anim = "idle" + + 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.5, 300.5, 0.0) + glScalef(*self.scale) + self.mesh.draw(self.material.texcoords) + glPopMatrix() + + class Model(object): def __init__(self): @@ -334,33 +361,7 @@ class Model(object): 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() - - + def init(): global mgr mgr = Model_Manager() diff --git a/pipeline.py b/pipeline.py index d4e860c..40eb2c9 100755 --- a/pipeline.py +++ b/pipeline.py @@ -109,7 +109,11 @@ class pipeline_manager(object): -mgr = pipeline_manager() +def init(): + global mgr + mgr = pipeline_manager() + +mgr = None def register_ship(obj): render_pipes = ('model',) diff --git a/roc.py b/roc.py index ce6a3c6..cdb5751 100755 --- a/roc.py +++ b/roc.py @@ -5,7 +5,6 @@ import shader import models import roc_main - def init2d(): init(videoinit=video.init2d) @@ -25,6 +24,9 @@ def init(videoinit): video.enable_vsync() models.init() + +def set_universe(new_universe): + roc_main.set_universe(new_universe) def main(): roc_main.mainloop() diff --git a/roc_main.py b/roc_main.py index 72b1b27..98e4aba 100755 --- a/roc_main.py +++ b/roc_main.py @@ -6,35 +6,18 @@ import video import shader import inputs import models +import universe from OpenGL.GL import * -def test_frame(): - # 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 - mdl = models.mgr.create("m_test") - mdl.render() - +def set_universe(new_universe): + global universe + universe = new_universe + +universe = universe.test_universe() def mainloop(): + global universe gametimer.start_loop() while True: @@ -59,6 +42,6 @@ def mainloop(): video.predraw() - test_frame() + universe.frame() video.next_frame() diff --git a/video.py b/video.py index 4e6c9e8..37d2f9e 100755 --- a/video.py +++ b/video.py @@ -54,7 +54,7 @@ def init2d(): glViewport(0, 0, width, height) glMatrixMode(GL_PROJECTION) glLoadIdentity() - glOrtho(-0.5, float(width)-0.5, float(height)-0.5, -0.5, 256.0, -256.0) + glOrtho(0.0, float(width), float(height), 0.0, 256.0, -256.0) glMatrixMode(GL_MODELVIEW) glLoadIdentity() #glRotatef(120.0, 1.0, 0.0, 0.0)