Used context managers to control GL attributes and matrices. No more stray GL_BLEND, much faster!

This commit is contained in:
Quantum 2014-02-02 16:47:05 -05:00
parent 44889fbf11
commit 83d8baeb8e
2 changed files with 268 additions and 282 deletions

View file

@ -365,7 +365,7 @@ class Applet(pyglet.window.Window):
progress_bar(10, self.height - 240, self.width - 20, 50, progress)
self._info_label.draw()
def on_draw(self, glMatrix=GLfloat * 16):
def on_draw(self, glMatrixBuffer=GLfloat * 16):
if not self.loaded:
return self.draw_loading()
@ -392,15 +392,14 @@ class Applet(pyglet.window.Window):
x, y, z = entity.location
pitch, yaw, roll = entity.rotation
glPushMatrix()
with glMatrix(), glRestore(GL_CURRENT_BIT):
glTranslatef(x, y, z)
glRotatef(pitch, 1, 0, 0)
glRotatef(yaw, 0, 1, 0)
glRotatef(roll, 0, 0, 1)
glPushAttrib(GL_CURRENT_BIT)
glCallList(entity.id)
if self.debug:
glPushMatrix()
with glMatrix(), glRestore(GL_ENABLE_BIT | GL_POLYGON_BIT | GL_LINE_BIT):
glLineWidth(0.25)
glPolygonOffset(1, 1)
glDisable(GL_LIGHTING)
@ -408,36 +407,29 @@ class Applet(pyglet.window.Window):
glColor3f(0, 1, 0)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
glCallList(entity.id)
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
glEnable(GL_LIGHTING)
glEnable(GL_TEXTURE_2D)
glPopMatrix()
glPopAttrib()
glPopMatrix()
has_corona = hasattr(entity, 'corona') and entity.corona
has_atmosphere = hasattr(entity, 'atmosphere') and entity.atmosphere
if self.atmosphere and (has_corona or has_atmosphere):
glPushMatrix()
with glMatrix(), glRestore(GL_ENABLE_BIT):
x0, y0, z0 = entity.location
glTranslatef(x0, y0, z0)
matrix = glMatrix()
matrix = glMatrixBuffer()
glGetFloatv(GL_MODELVIEW_MATRIX, matrix)
matrix[0: 3] = [1, 0, 0]
matrix[4: 7] = [0, 1, 0]
matrix[8:11] = [0, 0, 1]
glLoadMatrixf(matrix)
glEnable(GL_BLEND)
if has_atmosphere:
glCallList(entity.atmosphere)
if has_corona:
x, y, z = c.direction()
glTranslatef(-x, -y, -z)
glCallList(entity.corona)
glPopMatrix()
if self.cloud and hasattr(entity, 'cloudmap') and entity.cloudmap:
glPushMatrix()
with glMatrix(), glRestore(GL_ENABLE_BIT):
glEnable(GL_BLEND)
glEnable(GL_ALPHA_TEST)
glTranslatef(*entity.location)
@ -446,25 +438,21 @@ class Applet(pyglet.window.Window):
glRotatef(yaw, 0, 1, 0)
glRotatef(roll, 0, 0, 1)
glCallList(entity.cloudmap)
glDisable(GL_ALPHA_TEST)
glDisable(GL_BLEND)
glPopMatrix()
if self.orbit and hasattr(entity, 'get_orbit') and hasattr(entity, 'parent'):
parent = entity.parent
distance = get_distance(parent)
if distance < parent.orbit_show:
glPushMatrix()
with glMatrix(), glRestore(GL_ENABLE_BIT | GL_LINE_BIT | GL_CURRENT_BIT):
glTranslatef(*entity.parent.location)
glDisable(GL_LIGHTING)
glPushAttrib(GL_LINE_BIT | GL_CURRENT_BIT)
glColor4f(1, 1, 1, 1 if distance < parent.orbit_opaque else
solid = distance < parent.orbit_opaque
glColor4f(1, 1, 1, 1 if solid else
(1 - (distance - parent.orbit_opaque) / parent.orbit_blend))
if not solid:
glEnable(GL_BLEND)
glLineWidth(1)
glCallList(entity.get_orbit())
glPopAttrib()
glEnable(GL_LIGHTING)
glPopMatrix()
glColor4f(1, 1, 1, 1)
glDisable(GL_TEXTURE_2D)
@ -473,7 +461,6 @@ class Applet(pyglet.window.Window):
if self.info:
ortho(width, height)
if self.info_precise:
info = ('%d FPS @ (x=%.2f, y=%.2f, z=%.2f) @ %s, %s/s\n'
'Direction(pitch=%.2f, yaw=%.2f, roll=%.2f)\nTick: %d' %
@ -484,15 +471,10 @@ class Applet(pyglet.window.Window):
(pyglet.clock.get_fps(), c.x, c.y, c.z, self.speed, self.get_time_per_second()))
self.label.text = info
self.label.draw()
glPushAttrib(GL_CURRENT_BIT | GL_LINE_BIT)
with glRestore(GL_CURRENT_BIT | GL_LINE_BIT):
glLineWidth(2)
cx, cy = width / 2, height / 2
glColor4f(0, 1, 0, 1)
circle(10, 20, (cx, cy))
glPopAttrib()
frustrum()

View file

@ -5,7 +5,7 @@ from random import random, gauss, choice
TWOPI = pi * 2
__all__ = ['compile', 'ortho', 'frustrum', 'crosshair', 'circle', 'disk', 'sphere', 'colourball', 'torus', 'belt',
'flare', 'normal_sphere', 'glSection', 'progress_bar']
'flare', 'normal_sphere', 'glSection', 'glMatrix', 'glRestore', 'progress_bar']
class glSection(object):
@ -19,6 +19,25 @@ class glSection(object):
glEnd()
class glRestore(object):
def __init__(self, flags):
self.flags = flags
def __enter__(self):
glPushAttrib(self.flags)
def __exit__(self, exc_type, exc_val, exc_tb):
glPopAttrib()
class glMatrix(object):
def __enter__(self):
glPushMatrix()
def __exit__(self, exc_type, exc_val, exc_tb):
glPopMatrix()
def compile(pointer, *args, **kwargs):
display = glGenLists(1)
glNewList(display, GL_COMPILE)
@ -49,47 +68,43 @@ def frustrum():
def crosshair(size, (cx, cy)):
glBegin(GL_LINES)
with glSection(GL_LINES):
glVertex2f(cx - size, cy)
glVertex2f(cx + size, cy)
glVertex2f(cx, cy - size)
glVertex2f(cx, cy + size)
glEnd()
def circle(r, seg, (cx, cy)):
glBegin(GL_LINE_LOOP)
with glSection(GL_LINE_LOOP):
for i in xrange(seg):
theta = TWOPI * i / seg
glVertex2f(cx + cos(theta) * r, cy + sin(theta) * r)
glEnd()
def disk(rinner, router, segs, tex):
with glRestore(GL_ENABLE_BIT):
glEnable(GL_TEXTURE_2D)
glEnable(GL_BLEND)
glDisable(GL_LIGHTING)
glBindTexture(GL_TEXTURE_2D, tex)
res = segs * 5
glBegin(GL_TRIANGLE_STRIP)
texture = 0
with glSection(GL_TRIANGLE_STRIP):
factor = TWOPI / res
theta = 0
for n in xrange(res + 1):
theta += factor
x = cos(theta)
y = sin(theta)
glTexCoord2f(0, texture)
glTexCoord2f(0, 0)
glVertex2f(rinner * x, rinner * y)
glTexCoord2f(1, texture)
glTexCoord2f(1, 0)
glVertex2f(router * x, router * y)
texture ^= 1
glEnd()
glEnable(GL_LIGHTING)
glDisable(GL_TEXTURE_2D)
def flare(rinner, router, res, prob, tex):
with glRestore(GL_ENABLE_BIT):
glEnable(GL_TEXTURE_2D)
glDisable(GL_LIGHTING)
glBindTexture(GL_TEXTURE_2D, tex)
@ -98,7 +113,7 @@ def flare(rinner, router, res, prob, tex):
last_theta = 0
factor = TWOPI / res
rdelta = (router - rinner)
glBegin(GL_QUADS)
with glSection(GL_QUADS):
for i in xrange(res + 1):
theta = last_theta + factor
x = cos(theta)
@ -120,15 +135,13 @@ def flare(rinner, router, res, prob, tex):
last_theta = theta
last_x = x
last_y = y
glEnd()
glEnable(GL_LIGHTING)
glDisable(GL_TEXTURE_2D)
def sphere(r, lats, longs, tex, lighting=True, fv4=GLfloat * 4):
"""
Sphere function from the OpenGL red book.
"""
with glRestore(GL_ENABLE_BIT | GL_TEXTURE_BIT):
sphere = gluNewQuadric()
gluQuadricDrawStyle(sphere, GLU_FILL)
gluQuadricTexture(sphere, True)
@ -147,10 +160,6 @@ def sphere(r, lats, longs, tex, lighting=True, fv4=GLfloat * 4):
gluSphere(sphere, r, lats, longs)
glBindTexture(GL_TEXTURE_2D, 0)
glDisable(GL_TEXTURE_2D)
glEnable(GL_LIGHTING)
glEnable(GL_BLEND)
gluDeleteQuadric(sphere)
@ -158,6 +167,7 @@ def colourball(r, lats, longs, colour, fv4=GLfloat * 4):
"""
Sphere function from the OpenGL red book.
"""
with glRestore(GL_ENABLE_BIT):
sphere = gluNewQuadric()
glDisable(GL_BLEND)
@ -186,6 +196,7 @@ except ImportError:
width, height = normal_map.size
gray_scale = len(normal[0, 0]) == 1
with glRestore(GL_ENABLE_BIT | GL_TEXTURE_BIT):
glEnable(GL_TEXTURE_2D)
if lighting:
glDisable(GL_BLEND)
@ -198,7 +209,7 @@ except ImportError:
twopi_divide = TWOPI / divide
pi_divide = pi / divide
glBegin(GL_TRIANGLE_STRIP)
with glSection(GL_TRIANGLE_STRIP):
for j in xrange(divide + 1):
phi1 = j * twopi_divide
phi2 = (j + 1) * twopi_divide
@ -235,11 +246,6 @@ except ImportError:
glNormal3f(nx, ny, nz)
glTexCoord2f(s, 1 - t)
glVertex3f(r * dx, r * dy, r * dz)
glEnd()
glDisable(GL_TEXTURE_2D)
glEnable(GL_LIGHTING)
glEnable(GL_BLEND)
def belt(radius, cross, object, count):
@ -248,14 +254,13 @@ def belt(radius, cross, object, count):
r = gauss(radius, cross)
x, y, z = cos(theta) * r, gauss(0, cross), sin(theta) * r
glPushMatrix()
with glMatrix():
glTranslatef(x, y, z)
scale = gauss(1, 0.5)
if scale < 0:
scale = 1
glScalef(scale, scale, scale)
glCallList(choice(object))
glPopMatrix()
try:
@ -265,7 +270,7 @@ except ImportError:
"""
Torus function from the OpenGL red book.
"""
glPushAttrib(GL_CURRENT_BIT)
with glRestore(GL_CURRENT_BIT):
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, fv4(*material))
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, fv4(1, 1, 1, 1))
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess)
@ -285,8 +290,7 @@ except ImportError:
x1 = cos(a1)
y1 = sin(a1)
glBegin(GL_TRIANGLE_STRIP)
with glSection(GL_TRIANGLE_STRIP):
for j in xrange(n_minor + 1):
b = j * minor_s
c = cos(b)
@ -299,11 +303,11 @@ except ImportError:
glNormal3f(*n(x1 * c, y1 * c, z / minor_radius))
glVertex3f(x1 * r, y1 * r, z)
glEnd()
glPopAttrib()
def progress_bar(x, y, width, height, filled):
with glRestore(GL_ENABLE_BIT):
glDisable(GL_TEXTURE_2D)
glDisable(GL_BLEND)
x1 = x
x2 = x + width
y1 = y