texture rendering fixes
font rendering fixes FPS counter added particle demo added
This commit is contained in:
parent
ab901dc58f
commit
5f3e46d7bb
15 changed files with 447 additions and 73 deletions
|
@ -11,4 +11,30 @@
|
|||
<material><ref>mat_test</ref></material>
|
||||
</render>
|
||||
</model>
|
||||
<model id="m_star">
|
||||
<render layer="0">
|
||||
<mesh><ref>mesh_sprite</ref><scale x="200" y="200"/></mesh>
|
||||
<material><ref>mat_star</ref></material>
|
||||
</render>
|
||||
</model>
|
||||
<model id="m_star_tiny">
|
||||
<render layer="0">
|
||||
<mesh><ref>mesh_centered_sprite</ref><scale x="40" y="40"/></mesh>
|
||||
<material><ref>mat_star</ref></material>
|
||||
</render>
|
||||
</model>
|
||||
<model id="m_particle">
|
||||
<render layer="0">
|
||||
<mesh><ref>mesh_centered_sprite</ref><scale x="16" y="16"/></mesh>
|
||||
<material><ref>mat_particle</ref></material>
|
||||
</render>
|
||||
</model>
|
||||
<model id="m_dralthi">
|
||||
<render layer="0">
|
||||
<mesh><ref>mesh_centered_sprite</ref><scale x="50" y="50"/></mesh>
|
||||
<material><ref>mat_dralthi</ref></material>
|
||||
</render>
|
||||
</model>
|
||||
|
||||
|
||||
</models>
|
||||
|
|
|
@ -19,6 +19,16 @@
|
|||
<texture id="t_test">
|
||||
<file>tex/test1.png</file>
|
||||
</texture>
|
||||
<texture id="t_particle">
|
||||
<file>tex/whitelight_static.png</file>
|
||||
</texture>
|
||||
<texture id="t_star">
|
||||
<file>tex/star_sprite_main.png</file>
|
||||
</texture>
|
||||
<texture id="t_dralthi">
|
||||
<file>tex/dralthi.png</file>
|
||||
</texture>
|
||||
|
||||
<texture id="t_test_plasma">
|
||||
<file>tex/plasma1.png</file>
|
||||
</texture>
|
||||
|
@ -28,4 +38,13 @@
|
|||
<material id="mat_test">
|
||||
<texture ref="t_test"/>
|
||||
</material>
|
||||
<material id="mat_star">
|
||||
<texture ref="t_star"/>
|
||||
</material>
|
||||
<material id="mat_particle">
|
||||
<texture ref="t_particle"/>
|
||||
</material>
|
||||
<material id="mat_dralthi">
|
||||
<texture ref="t_dralthi"/>
|
||||
</material>
|
||||
</textures>
|
78
fonts.py
78
fonts.py
|
@ -11,9 +11,11 @@ class TextureFile(object):
|
|||
|
||||
def load(self, filename):
|
||||
self.filename = filename
|
||||
img = files.mgr.png(filename)
|
||||
img = pygame.image.load(filename)
|
||||
img.convert_alpha()
|
||||
glActiveTexture(GL_TEXTURE0)
|
||||
texid = glGenTextures(1)
|
||||
print "Generated texture id %s" % (texid,)
|
||||
#print "Generated font texture id %s" % (texid,)
|
||||
|
||||
self.id = texid
|
||||
|
||||
|
@ -25,7 +27,10 @@ class TextureFile(object):
|
|||
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)
|
||||
glTexParameteri(dimension, GL_TEXTURE_WRAP_S, GL_CLAMP)
|
||||
glTexParameteri(dimension, GL_TEXTURE_WRAP_T, GL_CLAMP)
|
||||
glTexParameteri(dimension, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
|
||||
glTexParameteri(dimension, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
|
||||
|
||||
class TexFont(object):
|
||||
def __init__(self):
|
||||
|
@ -41,6 +46,7 @@ class TexFont(object):
|
|||
self.gltex = None
|
||||
self.monospace = False
|
||||
self.charsizes = []
|
||||
self.colors = (1.0, 1.0, 1.0, 1.0)
|
||||
|
||||
@staticmethod
|
||||
def new(fontdir, fontname):
|
||||
|
@ -49,7 +55,52 @@ class TexFont(object):
|
|||
tf.load_texture(os.path.join(fontdir, "%s.png" % (fontname,)))
|
||||
return tf
|
||||
|
||||
def render
|
||||
def render(self, text, color=None):
|
||||
if color is None:
|
||||
color = self.colors
|
||||
#print "Binding texture %s" % (self.gltex.id,)
|
||||
glActiveTexture(GL_TEXTURE0)
|
||||
dimension = GL_TEXTURE_2D
|
||||
glBindTexture(GL_TEXTURE_2D, self.gltex.id)
|
||||
glTexParameteri(dimension, GL_TEXTURE_WRAP_S, GL_CLAMP)
|
||||
glTexParameteri(dimension, GL_TEXTURE_WRAP_T, GL_CLAMP)
|
||||
glTexParameteri(dimension, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
|
||||
glTexParameteri(dimension, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
|
||||
|
||||
glBegin(GL_TRIANGLES)
|
||||
glNormal3f(0.0, 0.0, 1.0)
|
||||
glColor4f(*color)
|
||||
#print "Rendering font"
|
||||
offset = 0.0
|
||||
for i, letter in enumerate(text):
|
||||
offset = self.render_letter(letter, offset)
|
||||
|
||||
glEnd()
|
||||
|
||||
|
||||
def render_letter(self, letter, offset):
|
||||
charidx = self.mapchar(letter)
|
||||
x1, y1, x2, y2 = self.get_coordinates(charidx)
|
||||
cx, cy = self.charsize(charidx)
|
||||
|
||||
#print "Printing letter %s(%s) from %s, %s to %s, %s using texcoords %s, %s, %s, %s" % (letter, charidx, offset, 0.0, offset + cx, cy, x1*128.0, y1*128.0, x2*128.0, y2*128.0)
|
||||
glTexCoord2f(x1, y1); glVertex3f( offset + 0.0, 0.0, 0.0)
|
||||
glTexCoord2f(x1, y2); glVertex3f( offset + 0.0, cy, 0.0)
|
||||
glTexCoord2f(x2, y1); glVertex3f( offset + cx, 0.0, 0.0)
|
||||
glTexCoord2f(x2, y1); glVertex3f( offset + cx, 0.0, 0.0)
|
||||
glTexCoord2f(x1, y2); glVertex3f( offset + 0.0, cy, 0.0)
|
||||
glTexCoord2f(x2, y2); glVertex3f( offset + cx, cy, 0.0)
|
||||
glTexCoord2f(0.0, 0.0)
|
||||
|
||||
return offset + cx
|
||||
|
||||
def charsize(self, idx):
|
||||
if self.monospace:
|
||||
return (self.charwidth, self.charheight)
|
||||
else:
|
||||
return self.charsizes[idx]
|
||||
|
||||
|
||||
|
||||
def mapchar(self, char):
|
||||
"""
|
||||
|
@ -67,9 +118,9 @@ class TexFont(object):
|
|||
char = ord(char)
|
||||
|
||||
if char >= self.start and char < self.end:
|
||||
return self.end - self.start - 1
|
||||
else:
|
||||
return char - self.start
|
||||
else:
|
||||
return self.end - self.start - 1
|
||||
|
||||
|
||||
def get_coordinates(self, idx):
|
||||
|
@ -81,15 +132,15 @@ class TexFont(object):
|
|||
if idx < 0 or idx >= (self.end - self.start):
|
||||
idx = (self.end - self.start - 1)
|
||||
|
||||
xp1 = (idx % self.cols) * self.charwidth
|
||||
yp1 = (idx // self.cols) * self.charheight
|
||||
xp1 = (idx % self.cols) * (self.charwidth + 1)
|
||||
yp1 = (idx // self.cols) * (self.charheight + 1)
|
||||
|
||||
if self.monospace:
|
||||
xp2 = xp + self.charwidth
|
||||
yp2 = yp + self.charheight
|
||||
xp2 = xp1 + self.charwidth + 1
|
||||
yp2 = yp1 + self.charheight + 1
|
||||
else:
|
||||
xp2 = xp + self.charsizes[idx][0]
|
||||
yp2 = yp + self.charsizes[idx][1]
|
||||
xp2 = xp1 + self.charsizes[idx][0] + 1
|
||||
yp2 = yp1 + self.charsizes[idx][1] + 1
|
||||
|
||||
xtc1 = (float(xp1) + 0.0) / self.texwidth
|
||||
ytc1 = (float(yp1) + 0.0) / self.texheight
|
||||
|
@ -120,6 +171,9 @@ class TexFont(object):
|
|||
self.cols, self.rows = nextline(dd)
|
||||
self.texwidth, self.texheight = nextline(dd)
|
||||
|
||||
self.charwidth -= 1
|
||||
self.charheight -= 1
|
||||
|
||||
assert dd.readline() == "charsizes\n"
|
||||
|
||||
self.charsizes = [None] * (self.end - self.start)
|
||||
|
|
78
gametimer.py
78
gametimer.py
|
@ -1,10 +1,12 @@
|
|||
import pygame
|
||||
import collections
|
||||
|
||||
def start_loop():
|
||||
global g_timer, g_elapsed
|
||||
g_elapsed = 0
|
||||
g_elapsed_sec = 0.0
|
||||
g_timer = pygame.time.get_ticks()
|
||||
reset_fps_count()
|
||||
|
||||
def next_frame(skipping=False):
|
||||
global g_timer, g_elapsed, g_elapsed_sec
|
||||
|
@ -17,6 +19,30 @@ def next_frame(skipping=False):
|
|||
g_timer = newticks
|
||||
g_elapsed_sec = float(g_elapsed) / 1000.0
|
||||
|
||||
if g_elapsed != 0:
|
||||
update_fps_count(g_elapsed)
|
||||
|
||||
|
||||
def reset_fps_count():
|
||||
global g_framelist, update_fps_count
|
||||
g_framelist = collections.deque()
|
||||
update_fps_count = update_fps_count_empty
|
||||
|
||||
def update_fps_count_empty(elapsed):
|
||||
global g_framelist, update_fps_count
|
||||
g_framelist.append(elapsed)
|
||||
if len(g_framelist) >= 25:
|
||||
update_fps_count = update_fps_count_full
|
||||
|
||||
def update_fps_count_full(elapsed):
|
||||
global g_framelist
|
||||
g_framelist.popleft()
|
||||
g_framelist.append(elapsed)
|
||||
|
||||
def update_fps_count(elapsed):
|
||||
"this is replaced with either the _empty or _full variant, depending on the situation"
|
||||
pass
|
||||
|
||||
def elapsed():
|
||||
"""
|
||||
get the amount of time in milliseconds passed since the last frame was displayed
|
||||
|
@ -33,9 +59,61 @@ def num_frames(delay, offset=0):
|
|||
"""
|
||||
if you want something to occur every "delay" milliseconds,
|
||||
this will return the number of times you should make it happen
|
||||
in this particular frame (can be 0)
|
||||
"""
|
||||
global g_elapsed, g_timer
|
||||
return int((g_timer - offset) / delay) - int((g_timer - g_elapsed - offset) / delay)
|
||||
|
||||
def loop_frames(delay, offset=0):
|
||||
return xrange(num_frames(delay, offset))
|
||||
|
||||
def get_timer():
|
||||
return g_timer
|
||||
|
||||
def average(d):
|
||||
#spf = (float(sum(g_framelist)) / (len(g_framelist) * 1000.0))
|
||||
if len(d) == 0:
|
||||
return 0.0
|
||||
|
||||
smooth = 0.85
|
||||
#v2 = float(d[-1])
|
||||
#for i in xrange(len(d) - 2, 0, -1):
|
||||
# pass
|
||||
|
||||
v2 = float(d[0])
|
||||
for i in xrange(1, len(d)):
|
||||
v1 = float(d[i])
|
||||
v2 = (smooth * v2) + ((1.0 - smooth) * v1)
|
||||
|
||||
return v2
|
||||
|
||||
|
||||
def get_fps():
|
||||
global g_framelist
|
||||
#return ",".join([str(x) for x in sorted(g_framelist)])
|
||||
if len(g_framelist) == 0.0:
|
||||
return 0.0
|
||||
spf = average(g_framelist) / 1000.0
|
||||
if spf == 0.0:
|
||||
return 0.0
|
||||
#print "%s < %s" % (1.0 / spf, g_framelist)
|
||||
return 1.0 / spf
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
140
models.py
140
models.py
|
@ -1,9 +1,13 @@
|
|||
import os
|
||||
import glob
|
||||
import enums
|
||||
import files
|
||||
import gamedata
|
||||
import pygame
|
||||
from OpenGL.GL import *
|
||||
from py3dutil import vect
|
||||
import fonts
|
||||
import collections
|
||||
|
||||
class Vertex(object):
|
||||
__slots__ = [
|
||||
|
@ -21,7 +25,8 @@ class Vertex(object):
|
|||
|
||||
class Model_Manager(object):
|
||||
def __init__(self):
|
||||
pass
|
||||
self.centered_sprite_mesh = None
|
||||
self.sprite_mesh = None
|
||||
|
||||
def create(self, mname):
|
||||
return self.models[mname].instance()
|
||||
|
@ -37,10 +42,11 @@ class Model_Manager(object):
|
|||
self.load_textures()
|
||||
self.load_materials()
|
||||
self.load_models()
|
||||
self.load_fonts()
|
||||
|
||||
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.diffuse] = TextureType(GL_TEXTURE0, GL_TEXTURE_2D, GL_CLAMP, GL_CLAMP, GL_LINEAR, GL_LINEAR)
|
||||
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)
|
||||
|
@ -91,9 +97,12 @@ class Model_Manager(object):
|
|||
self.models = {}
|
||||
for meshdata in mdldata.mesh:
|
||||
if meshdata['sprite']:
|
||||
mesh = SpriteMesh.singleton()
|
||||
self.sprite_mesh = SpriteMesh()
|
||||
mesh = self.sprite_mesh
|
||||
elif meshdata['centered_sprite']:
|
||||
mesh = SpriteMeshCentered.singleton()
|
||||
if self.centered_sprite_mesh == None:
|
||||
self.centered_sprite_mesh = SpriteMeshCentered()
|
||||
mesh = self.centered_sprite_mesh
|
||||
else:
|
||||
mesh = Mesh.factory(meshdata)
|
||||
mgr.meshes[meshdata['id']] = mesh
|
||||
|
@ -103,6 +112,18 @@ class Model_Manager(object):
|
|||
model.load(modeldata)
|
||||
mgr.models[modeldata['id']] = model
|
||||
|
||||
def load_fonts(self):
|
||||
self.fontlib = {}
|
||||
fontdir = os.path.join(files.get_basedir(), 'data', 'font')
|
||||
for fontfile in glob.glob(os.path.join(fontdir, '*.tfd')):
|
||||
fontname = os.path.splitext(os.path.split(fontfile)[1])[0]
|
||||
fontobj = fonts.TexFont.new(fontdir, fontname)
|
||||
self.fontlib[fontname] = fontobj
|
||||
print "Loaded font %s" % (fontname,)
|
||||
|
||||
def get_font(self, name):
|
||||
return self.fontlib[name]
|
||||
|
||||
class TextureType(object):
|
||||
def __init__(self, texunit, texdim, wrap_s=GL_CLAMP, wrap_t=GL_CLAMP, mag=GL_LINEAR, min=GL_LINEAR):
|
||||
self.texunit = texunit
|
||||
|
@ -113,11 +134,13 @@ class TextureType(object):
|
|||
self.min = min
|
||||
|
||||
def initialize(self):
|
||||
glActiveTexture(self.texunit)
|
||||
glTexParameterf(self.texdim, GL_TEXTURE_WRAP_S, self.wrap_s)
|
||||
glTexParameterf(self.texdim, GL_TEXTURE_WRAP_T, self.wrap_t)
|
||||
glTexParameterf(self.texdim, GL_TEXTURE_MAG_FILTER, self.mag)
|
||||
glTexParameterf(self.texdim, GL_TEXTURE_MIN_FILTER, self.min)
|
||||
return
|
||||
|
||||
def apply_parameters(self):
|
||||
glTexParameteri(self.texdim, GL_TEXTURE_WRAP_S, self.wrap_s)
|
||||
glTexParameteri(self.texdim, GL_TEXTURE_WRAP_T, self.wrap_t)
|
||||
glTexParameteri(self.texdim, GL_TEXTURE_MAG_FILTER, self.mag)
|
||||
glTexParameteri(self.texdim, GL_TEXTURE_MIN_FILTER, self.min)
|
||||
|
||||
def get_dimension(self):
|
||||
return self.texdim
|
||||
|
@ -175,8 +198,10 @@ class Texture(object):
|
|||
self.texcoords = self.texcoords_direct
|
||||
#self.texcoords = self.texcoords_subset
|
||||
|
||||
def bind(self):
|
||||
def bind(self, textype):
|
||||
textype.activate()
|
||||
glBindTexture(GL_TEXTURE_2D, self.texid)
|
||||
textype.apply_parameters()
|
||||
|
||||
class TextureFile(object):
|
||||
def __init__(self):
|
||||
|
@ -189,7 +214,7 @@ class TextureFile(object):
|
|||
self.filename = filename
|
||||
img = files.mgr.png(filename)
|
||||
texid = glGenTextures(1)
|
||||
print "Generated texture id %s" % (texid,)
|
||||
print "Generated texture id %s from %s" % (texid, self.filename)
|
||||
|
||||
self.id = texid
|
||||
|
||||
|
@ -201,7 +226,9 @@ class TextureFile(object):
|
|||
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)
|
||||
|
||||
|
||||
#glBindTexture(dimension, 0)
|
||||
|
||||
|
||||
|
||||
|
@ -219,9 +246,10 @@ class Material(object):
|
|||
for tt in enums.tt:
|
||||
if tt in self.textures:
|
||||
tto = mgr.get_textype(tt)
|
||||
tto.initialize()
|
||||
tto.activate()
|
||||
tex = self.textures[tt]
|
||||
tex.bind()
|
||||
#print "Binding texture %s" % (tex.texid,)
|
||||
tex.bind(tto)
|
||||
|
||||
def load(self, data):
|
||||
self.id = data['id']
|
||||
|
@ -245,7 +273,6 @@ class Material(object):
|
|||
self.texcoords = texobj.texcoords
|
||||
prevtex = texobj
|
||||
|
||||
print self.textures
|
||||
|
||||
|
||||
|
||||
|
@ -267,6 +294,7 @@ class Mesh(object):
|
|||
glEnd()
|
||||
|
||||
def draw_vertex(self, v, texcoord):
|
||||
if v.c != None:
|
||||
glColor3f(v.c[0], v.c[1], v.c[2])
|
||||
glNormal3f(v.n.x, v.n.y, v.n.z)
|
||||
glTexCoord2f(*texcoord(v.t[0], v.t[1]))
|
||||
|
@ -275,40 +303,35 @@ class Mesh(object):
|
|||
|
||||
|
||||
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)),
|
||||
Vertex(vect(0.0, 0.0, 0.0), vect(0.0, 0.0, 1.0), (0.0, 0.0), None),
|
||||
Vertex(vect(0.0, 1.0, 0.0), vect(0.0, 0.0, 1.0), (0.0, 1.0), None),
|
||||
Vertex(vect(1.0, 0.0, 0.0), vect(0.0, 0.0, 1.0), (1.0, 0.0), None),
|
||||
Vertex(vect(1.0, 0.0, 0.0), vect(0.0, 0.0, 1.0), (1.0, 0.0), None),
|
||||
Vertex(vect(0.0, 1.0, 0.0), vect(0.0, 0.0, 1.0), (0.0, 1.0), None),
|
||||
Vertex(vect(1.0, 1.0, 0.0), vect(0.0, 0.0, 1.0), (1.0, 1.0), None),
|
||||
]
|
||||
|
||||
@classmethod
|
||||
def return_singleton(cls):
|
||||
return cls._SINGLETON
|
||||
|
||||
@classmethod
|
||||
def singleton(cls):
|
||||
cls._SINGLETON = cls()
|
||||
cls.singleton = cls.return_singleton
|
||||
return cls._SINGLETON
|
||||
if cls.__SINGLETON == None:
|
||||
cls.__SINGLETON = cls()
|
||||
|
||||
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)),
|
||||
Vertex(vect(-0.5, -0.5, 0.0), vect(0.0, 0.0, 1.0), (0.0, 0.0), None),
|
||||
Vertex(vect(-0.5, 0.5, 0.0), vect(0.0, 0.0, 1.0), (0.0, 1.0), None),
|
||||
Vertex(vect( 0.5, -0.5, 0.0), vect(0.0, 0.0, 1.0), (1.0, 0.0), None),
|
||||
Vertex(vect( 0.5, -0.5, 0.0), vect(0.0, 0.0, 1.0), (1.0, 0.0), None),
|
||||
Vertex(vect(-0.5, 0.5, 0.0), vect(0.0, 0.0, 1.0), (0.0, 1.0), None),
|
||||
Vertex(vect( 0.5, 0.5, 0.0), vect(0.0, 0.0, 1.0), (1.0, 1.0), None),
|
||||
]
|
||||
|
||||
class RenderLayer(object):
|
||||
|
@ -316,7 +339,7 @@ class RenderLayer(object):
|
|||
self.id = layernum
|
||||
self.mesh = None
|
||||
self.material = None
|
||||
self.color = [1.0, 1.0, 1.0, 1.0]
|
||||
self.color = None
|
||||
self.scale = [1.0, 1.0, 1.0]
|
||||
self.displaylist = None
|
||||
self.anim = "idle"
|
||||
|
@ -334,6 +357,8 @@ class RenderLayer(object):
|
|||
self.material.bind()
|
||||
glPushMatrix()
|
||||
glScalef(*self.scale)
|
||||
if self.color != None:
|
||||
glColor4f(*self.color)
|
||||
|
||||
if self.displaylist == None:
|
||||
self.displaylist = glGenLists(1)
|
||||
|
@ -341,9 +366,17 @@ class RenderLayer(object):
|
|||
self.mesh.draw(self.material.texcoords)
|
||||
glEndList()
|
||||
|
||||
#print "Drawing mesh %s" % ( self.displaylist, )
|
||||
glCallList(self.displaylist)
|
||||
glPopMatrix()
|
||||
|
||||
class ParticleLayer(RenderLayer):
|
||||
def __init__(self, layernum):
|
||||
RenderLayer.__init__(self, layernum)
|
||||
self.particles = collections.deque()
|
||||
|
||||
def render(self):
|
||||
pass
|
||||
|
||||
|
||||
class Model(object):
|
||||
|
@ -368,6 +401,41 @@ class Model(object):
|
|||
for layer in self.layers:
|
||||
layer.render()
|
||||
|
||||
|
||||
class TextModel(Model):
|
||||
def __init__(self, fontname, text, color=None, scale=None):
|
||||
self.displaylist = None
|
||||
self.fontname = fontname
|
||||
self.text = text
|
||||
self.color = color
|
||||
self.scale = scale
|
||||
if self.scale == None:
|
||||
self.scale = (1.0, 1.0, 1.0)
|
||||
|
||||
def load(self, data):
|
||||
pass
|
||||
|
||||
def instance(self):
|
||||
return self
|
||||
|
||||
def render(self):
|
||||
glPushMatrix()
|
||||
glScalef(*self.scale)
|
||||
if self.color != None:
|
||||
glColor4f(*self.color)
|
||||
|
||||
if self.displaylist == None:
|
||||
self.displaylist = glGenLists(1)
|
||||
glNewList(self.displaylist, GL_COMPILE)
|
||||
font = mgr.get_font(self.fontname)
|
||||
font.render(self.text, self.color)
|
||||
glEndList()
|
||||
|
||||
#print "Drawing mesh %s" % ( self.displaylist, )
|
||||
glCallList(self.displaylist)
|
||||
glPopMatrix()
|
||||
|
||||
|
||||
def init():
|
||||
global mgr
|
||||
mgr = Model_Manager()
|
||||
|
|
133
roc_test.py
133
roc_test.py
|
@ -3,14 +3,80 @@ import pygame
|
|||
from OpenGL.GL import *
|
||||
from py3dutil import vect, quat
|
||||
import models
|
||||
from platform_win32 import *
|
||||
import fonts
|
||||
import gametimer
|
||||
from platform import *
|
||||
import pipeline
|
||||
import time
|
||||
import collections
|
||||
import random
|
||||
import math
|
||||
|
||||
class partycle(object):
|
||||
def __init__(self, x, y, xd, yd, rot, rotd, color):
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.xd = xd
|
||||
self.yd = yd
|
||||
self.rot = rot
|
||||
self.rotd = rotd
|
||||
self.color = color
|
||||
|
||||
@classmethod
|
||||
def random(cls):
|
||||
x, y = [((random.random() * 2.0) - 1.0) * 10.0 for _ in xrange(2)]
|
||||
xd, yd = [((random.random() * 2.0) - 1.0) * 4.0 for _ in xrange(2)]
|
||||
hue = random.random()
|
||||
m1 = 1.0
|
||||
h1 = hue * 6.0
|
||||
s1 = 1.0 - abs((h1 % 2.0) - 1.0)
|
||||
if h1 < 1.0:
|
||||
r, g, b = 1.0, s1, 0.0
|
||||
elif h1 < 2.0:
|
||||
r, g, b = s1, 1.0, 0.0
|
||||
elif h1 < 3.0:
|
||||
r, g, b = 0.0, 1.0, s1
|
||||
elif h1 < 4.0:
|
||||
r, g, b = 0.0, s1, 1.0
|
||||
elif h1 < 5.0:
|
||||
r, g, b = s1, 0.0, 1.0
|
||||
elif h1 < 6.0:
|
||||
r, g, b = 1.0, 0.0, s1
|
||||
|
||||
rot = random.random() * 90.0
|
||||
rotd = ((random.random() * 2.0) - 1.0) * 3.0
|
||||
rv = cls(x, y, xd, yd, rot, rotd, (r, g, b, 1.0))
|
||||
|
||||
return rv
|
||||
|
||||
def movestep(self):
|
||||
self.x += self.xd
|
||||
self.y += self.yd
|
||||
self.xd *= 0.985
|
||||
self.yd *= 0.985
|
||||
self.rot += self.rotd
|
||||
|
||||
class test_universe(roc.base_universe):
|
||||
def __init__(self):
|
||||
self.font = models.mgr.fontlib["micross20"]
|
||||
self.text1 = models.TextModel("micross20", "#$% 0123 ,,,, Hello world!\xa8\xa8\xa8F", (1.0, 1.0, 0.5, 1.0))
|
||||
self.string = ""
|
||||
self.move = 199.0
|
||||
self.moveinc = 1.0
|
||||
self.particles = collections.deque()
|
||||
self.particles.append(partycle.random())
|
||||
|
||||
|
||||
|
||||
def frame(self, events):
|
||||
#print "*** Starting frame"
|
||||
# pink triangle
|
||||
glDisable(GL_BLEND)
|
||||
glDisable(GL_TEXTURE_2D)
|
||||
glBegin(GL_TRIANGLE_STRIP)
|
||||
glNormal3f(0.0, 0.0, 1.0)
|
||||
glColor4f(1.0, 0.0, 0.5, 1.0)
|
||||
glTexCoord2f(0.0, 0.0)
|
||||
glColor4f(1.0, 0.0, 0.5, 0.0)
|
||||
glVertex3f( 20.0, 50.0, 1.0)
|
||||
glVertex3f( 20.0, 600.0, 1.0)
|
||||
glVertex3f( 400.0, 50.0, 1.0)
|
||||
|
@ -19,12 +85,16 @@ class test_universe(roc.base_universe):
|
|||
# yellow square
|
||||
glBegin(GL_TRIANGLE_STRIP)
|
||||
glNormal3f(0.0, 0.0, 1.0)
|
||||
glColor4f(0.0, 0.2, 0.5, 1.0)
|
||||
glTexCoord2f(0.0, 0.0)
|
||||
glColor4f(0.0, 0.2, 0.5, 0.8)
|
||||
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()
|
||||
glEnable(GL_BLEND)
|
||||
glEnable(GL_TEXTURE_2D)
|
||||
glColor4f(1.0, 1.0, 1.0, 1.0)
|
||||
|
||||
# texture test
|
||||
mdl = models.mgr.create("m_test")
|
||||
|
@ -35,6 +105,63 @@ class test_universe(roc.base_universe):
|
|||
|
||||
|
||||
|
||||
|
||||
# font test
|
||||
glPushMatrix()
|
||||
glTranslate(200.0, 200.0, 5.0)
|
||||
self.text1.render()
|
||||
glPopMatrix()
|
||||
|
||||
for x in xrange(gametimer.num_frames(10)):
|
||||
self.move = self.move + self.moveinc
|
||||
if self.move > 300.0:
|
||||
self.moveinc = -1.0
|
||||
elif self.move < 200.0:
|
||||
self.moveinc = 1.0
|
||||
for party in self.particles:
|
||||
party.movestep()
|
||||
if self.moveinc > 0.0:
|
||||
party = partycle.random()
|
||||
self.particles.append(party)
|
||||
#print party.color
|
||||
else:
|
||||
self.particles.popleft()
|
||||
|
||||
|
||||
glPushMatrix()
|
||||
glTranslate(self.move, 250.0, 5.0)
|
||||
|
||||
|
||||
"""
|
||||
if len(self.string) > 61:
|
||||
self.string = ""
|
||||
|
||||
for x in xrange(gametimer.num_frames(1000)):
|
||||
self.string = self.string + str(len(self.string) % 10)
|
||||
"""
|
||||
for x in xrange(gametimer.num_frames(100)):
|
||||
self.string = str(round(gametimer.get_fps(), 1))
|
||||
#print len(gametimer.g_framelist)
|
||||
|
||||
|
||||
self.font.render(self.string)
|
||||
glPopMatrix()
|
||||
|
||||
# texture test
|
||||
mdl2 = models.mgr.create("m_dralthi")
|
||||
mdl2.layers[0].color = None
|
||||
#print mdl2.layers[0].mesh
|
||||
for party in self.particles:
|
||||
glPushMatrix()
|
||||
glTranslate(600.0 + party.x, 350.0 + party.y, 6.0)
|
||||
glRotate(party.rot, 0.0, 0.0, 1.0)
|
||||
#glColor4f(*party.color)
|
||||
mdl2.render()
|
||||
glPopMatrix()
|
||||
|
||||
|
||||
|
||||
|
||||
roc.init2d()
|
||||
roc.set_universe(test_universe())
|
||||
roc.main()
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
fontheader
|
||||
32,128
|
||||
14,9
|
||||
8,13
|
||||
9,14
|
||||
13,8
|
||||
128,128
|
||||
charsizes
|
||||
8,13
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
fontheader
|
||||
32,128
|
||||
25,21
|
||||
9,11
|
||||
21,25
|
||||
11,9
|
||||
256,256
|
||||
charsizes
|
||||
5,24
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
fontheader
|
||||
32,128
|
||||
49,43
|
||||
10,11
|
||||
43,49
|
||||
11,10
|
||||
512,512
|
||||
charsizes
|
||||
11,48
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
fontheader
|
||||
32,128
|
||||
11,7
|
||||
8,13
|
||||
7,11
|
||||
13,8
|
||||
128,128
|
||||
charsizes
|
||||
6,10
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
fontheader
|
||||
32,128
|
||||
12,8
|
||||
8,12
|
||||
12,8
|
||||
128,128
|
||||
charsizes
|
||||
7,11
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
fontheader
|
||||
32,128
|
||||
11,7
|
||||
8,13
|
||||
7,11
|
||||
13,8
|
||||
128,128
|
||||
charsizes
|
||||
6,10
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
fontheader
|
||||
32,128
|
||||
16,9
|
||||
8,14
|
||||
9,16
|
||||
14,8
|
||||
128,128
|
||||
charsizes
|
||||
8,15
|
||||
|
|
|
@ -81,8 +81,8 @@ pygame.image.save(tex, "%s.png" % (fontname,))
|
|||
datf = open("%s.tfd" % (fontname,), "w")
|
||||
datf.write("fontheader\n")
|
||||
datf.write("%s,%s\n" % (start, end))
|
||||
datf.write("%s,%s\n" % (maxheight, maxwidth))
|
||||
datf.write("%s,%s\n" % (ratioheight, ratiowidth))
|
||||
datf.write("%s,%s\n" % (maxwidth, maxheight))
|
||||
datf.write("%s,%s\n" % (ratiowidth, ratioheight))
|
||||
datf.write("%s,%s\n" % (texsize, texsize))
|
||||
datf.write("charsizes\n")
|
||||
for i, sz in enumerate(imsizes):
|
||||
|
|
6
video.py
6
video.py
|
@ -72,7 +72,7 @@ def init2d():
|
|||
#glRotatef(120.0, 1.0, 0.0, 0.0)
|
||||
|
||||
|
||||
glClearColor(0.0, 0.0, 0.0, 0.0)
|
||||
glClearColor(0.0, 0.0, 0.0, 0.5)
|
||||
glClearDepth(-256.0)
|
||||
|
||||
glShadeModel(GL_SMOOTH)
|
||||
|
@ -81,6 +81,8 @@ def init2d():
|
|||
glEnable(GL_TEXTURE_2D)
|
||||
glDepthFunc(GL_GEQUAL)
|
||||
glEnable(GL_BLEND)
|
||||
glDisable(GL_ALPHA_TEST)
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
|
||||
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
|
||||
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, (0.3, 0.3, 0.3, 1.0))
|
||||
|
||||
|
@ -100,7 +102,7 @@ def init3d():
|
|||
glLoadIdentity()
|
||||
|
||||
glShadeModel(GL_SMOOTH)
|
||||
glClearColor(0.0, 0.0, 0.0, 0.0)
|
||||
glClearColor(0.0, 0.0, 0.0, 1.0)
|
||||
glClearDepth(1.0)
|
||||
glEnable(GL_DEPTH_TEST)
|
||||
glEnable(GL_CULL_FACE)
|
||||
|
|
Loading…
Add table
Reference in a new issue