reorganized models.py
started adding universe basics started adding gameobj basics
This commit is contained in:
parent
46e7856e19
commit
49f4256439
6 changed files with 169 additions and 102 deletions
77
gameobj.py
77
gameobj.py
|
@ -1,5 +1,82 @@
|
||||||
|
|
||||||
class gameobj(object):
|
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):
|
def __init__(self):
|
||||||
pass
|
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
|
||||||
|
|
||||||
|
|
145
models.py
145
models.py
|
@ -126,51 +126,6 @@ class TextureType(object):
|
||||||
glActiveTexture(self.texunit)
|
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):
|
class Texture(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.id = None
|
self.id = None
|
||||||
|
@ -218,7 +173,7 @@ class Texture(object):
|
||||||
self.w = tf.w
|
self.w = tf.w
|
||||||
self.h = tf.h
|
self.h = tf.h
|
||||||
self.texcoords = self.texcoords_direct
|
self.texcoords = self.texcoords_direct
|
||||||
self.texcoords = self.texcoords_subset
|
#self.texcoords = self.texcoords_subset
|
||||||
|
|
||||||
def bind(self):
|
def bind(self):
|
||||||
glBindTexture(GL_TEXTURE_2D, self.texid)
|
glBindTexture(GL_TEXTURE_2D, self.texid)
|
||||||
|
@ -249,6 +204,51 @@ class TextureFile(object):
|
||||||
glBindTexture(dimension, 0)
|
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):
|
class Mesh(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.vertexes = []
|
self.vertexes = []
|
||||||
|
@ -312,6 +312,33 @@ class SpriteMeshCentered(SpriteMesh):
|
||||||
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)),
|
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):
|
class Model(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
@ -335,32 +362,6 @@ class Model(object):
|
||||||
for layer in self.layers:
|
for layer in self.layers:
|
||||||
layer.render()
|
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():
|
def init():
|
||||||
global mgr
|
global mgr
|
||||||
mgr = Model_Manager()
|
mgr = Model_Manager()
|
||||||
|
|
|
@ -109,7 +109,11 @@ class pipeline_manager(object):
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
mgr = pipeline_manager()
|
def init():
|
||||||
|
global mgr
|
||||||
|
mgr = pipeline_manager()
|
||||||
|
|
||||||
|
mgr = None
|
||||||
|
|
||||||
def register_ship(obj):
|
def register_ship(obj):
|
||||||
render_pipes = ('model',)
|
render_pipes = ('model',)
|
||||||
|
|
4
roc.py
4
roc.py
|
@ -5,7 +5,6 @@ import shader
|
||||||
import models
|
import models
|
||||||
import roc_main
|
import roc_main
|
||||||
|
|
||||||
|
|
||||||
def init2d():
|
def init2d():
|
||||||
init(videoinit=video.init2d)
|
init(videoinit=video.init2d)
|
||||||
|
|
||||||
|
@ -26,5 +25,8 @@ def init(videoinit):
|
||||||
|
|
||||||
models.init()
|
models.init()
|
||||||
|
|
||||||
|
def set_universe(new_universe):
|
||||||
|
roc_main.set_universe(new_universe)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
roc_main.mainloop()
|
roc_main.mainloop()
|
||||||
|
|
31
roc_main.py
31
roc_main.py
|
@ -6,35 +6,18 @@ import video
|
||||||
import shader
|
import shader
|
||||||
import inputs
|
import inputs
|
||||||
import models
|
import models
|
||||||
|
import universe
|
||||||
from OpenGL.GL import *
|
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():
|
def mainloop():
|
||||||
|
global universe
|
||||||
|
|
||||||
gametimer.start_loop()
|
gametimer.start_loop()
|
||||||
while True:
|
while True:
|
||||||
|
@ -59,6 +42,6 @@ def mainloop():
|
||||||
|
|
||||||
video.predraw()
|
video.predraw()
|
||||||
|
|
||||||
test_frame()
|
universe.frame()
|
||||||
|
|
||||||
video.next_frame()
|
video.next_frame()
|
||||||
|
|
2
video.py
2
video.py
|
@ -54,7 +54,7 @@ def init2d():
|
||||||
glViewport(0, 0, width, height)
|
glViewport(0, 0, width, height)
|
||||||
glMatrixMode(GL_PROJECTION)
|
glMatrixMode(GL_PROJECTION)
|
||||||
glLoadIdentity()
|
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)
|
glMatrixMode(GL_MODELVIEW)
|
||||||
glLoadIdentity()
|
glLoadIdentity()
|
||||||
#glRotatef(120.0, 1.0, 0.0, 0.0)
|
#glRotatef(120.0, 1.0, 0.0, 0.0)
|
||||||
|
|
Loading…
Add table
Reference in a new issue