From 517a7584cad7867c7be29cf2e7f567fe8afa5e7c Mon Sep 17 00:00:00 2001 From: Quantum Date: Sat, 25 Aug 2018 17:47:19 -0400 Subject: [PATCH] Get rid of glMatrix since we handle all matrices manually --- punyverse/entity.py | 29 +++++++++++++---------------- punyverse/glgeom.py | 38 +++++++++----------------------------- 2 files changed, 22 insertions(+), 45 deletions(-) diff --git a/punyverse/entity.py b/punyverse/entity.py index 0b340df..fad0845 100644 --- a/punyverse/entity.py +++ b/punyverse/entity.py @@ -5,7 +5,7 @@ from pyglet.gl import * # noinspection PyUnresolvedReferences from six.moves import range -from punyverse.glgeom import compile, glMatrix, glRestore, belt, Sphere, Disk, OrbitVBO, Matrix4f +from punyverse.glgeom import compile, glRestore, belt, Sphere, Disk, OrbitVBO, Matrix4f from punyverse.model import load_model, WavefrontVBO from punyverse.orbit import KeplerOrbit from punyverse.texture import get_best_texture, load_clouds @@ -53,9 +53,8 @@ class Asteroid(Entity): self.rotation = rx + 1, ry + 1, rz + 1 def draw(self, options): - with glMatrix(): - glLoadMatrixf(self.mv_matrix.as_gl()) - self.model.draw() + glLoadMatrixf(self.mv_matrix.as_gl()) + self.model.draw() class AsteroidManager(object): @@ -105,9 +104,8 @@ class Belt(Entity): self.rotation = pitch, self.world.tick * self.rotation_angle % 360, roll def draw(self, options): - with glMatrix(), glRestore(GL_CURRENT_BIT): - glLoadMatrixf(self.mv_matrix.as_gl()) - glCallList(self.belt_id) + glLoadMatrixf(self.mv_matrix.as_gl()) + glCallList(self.belt_id) class Sky(Entity): @@ -126,7 +124,7 @@ class Sky(Entity): def draw(self, options): cam = self.world.cam - with glMatrix(), glRestore(GL_TEXTURE_BIT | GL_ENABLE_BIT): + with glRestore(GL_TEXTURE_BIT | GL_ENABLE_BIT): matrix = self.world.view_matrix() * Matrix4f.from_angles((-cam.x, -cam.y, -cam.z), self.rotation) glLoadMatrixf(matrix.as_gl()) glEnable(GL_CULL_FACE) @@ -225,7 +223,7 @@ class Body(Entity): return self.orbit_vbo def _draw_orbits(self, distance): - with glMatrix(), glRestore(GL_ENABLE_BIT | GL_LINE_BIT | GL_CURRENT_BIT): + with glRestore(GL_ENABLE_BIT | GL_LINE_BIT | GL_CURRENT_BIT): glLoadMatrixf(self.parent.orbit_matrix.as_gl()) glDisable(GL_LIGHTING) @@ -299,7 +297,7 @@ class SphericalBody(Body): self.ring = Disk(distance, distance + size, 30) def _draw_sphere(self, fv4=GLfloat * 4): - with glMatrix(), glRestore(GL_LIGHTING_BIT | GL_ENABLE_BIT | GL_TEXTURE_BIT): + with glRestore(GL_LIGHTING_BIT | GL_ENABLE_BIT | GL_TEXTURE_BIT): glLoadMatrixf(self.mv_matrix.as_gl()) glEnable(GL_CULL_FACE) glCullFace(GL_BACK) @@ -318,7 +316,7 @@ class SphericalBody(Body): self.sphere.draw() def _draw_atmosphere(self): - with glMatrix(), glRestore(GL_ENABLE_BIT | GL_CURRENT_BIT | GL_TEXTURE_BIT): + with glRestore(GL_ENABLE_BIT | GL_CURRENT_BIT | GL_TEXTURE_BIT): mv = self.mv_matrix.matrix matrix = Matrix4f([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, mv[12], mv[13], mv[14], 1]) glLoadMatrixf(matrix.as_gl()) @@ -332,7 +330,7 @@ class SphericalBody(Body): self.atmosphere.draw() def _draw_clouds(self): - with glMatrix(), glRestore(GL_ENABLE_BIT | GL_TEXTURE_BIT): + with glRestore(GL_ENABLE_BIT | GL_TEXTURE_BIT): glLoadMatrixf(self.mv_matrix.as_gl()) glEnable(GL_BLEND) glEnable(GL_ALPHA_TEST) @@ -345,7 +343,7 @@ class SphericalBody(Body): self.clouds.draw() def _draw_rings(self): - with glMatrix(), glRestore(GL_ENABLE_BIT | GL_TEXTURE_BIT): + with glRestore(GL_ENABLE_BIT | GL_TEXTURE_BIT): glLoadMatrixf(self.mv_matrix.as_gl()) glDisable(GL_LIGHTING) glEnable(GL_TEXTURE_2D) @@ -383,6 +381,5 @@ class ModelBody(Body): info.get('sz', scale)) def _draw(self, options): - with glMatrix(): - glLoadMatrixf(self.mv_matrix.as_gl()) - self.vbo.draw() + glLoadMatrixf(self.mv_matrix.as_gl()) + self.vbo.draw() diff --git a/punyverse/glgeom.py b/punyverse/glgeom.py index 127e9fd..be5104f 100644 --- a/punyverse/glgeom.py +++ b/punyverse/glgeom.py @@ -10,7 +10,7 @@ from six.moves import range TWOPI = pi * 2 __all__ = ['compile', 'ortho', 'frustrum', 'crosshair', 'circle', 'Sphere', 'belt', - 'glSection', 'glMatrix', 'glRestore', 'progress_bar'] + 'glSection', 'glRestore', 'progress_bar'] class glContext(object): @@ -58,27 +58,6 @@ class glRestoreClient(object): glPopClientAttrib() -class glMatrix(object): - def __init__(self, location=None, rotation=None): - self.location = location - self.rotation = rotation - - def __enter__(self): - glPushMatrix() - - if self.location: - glTranslatef(*self.location) - - if self.rotation: - pitch, yaw, roll = self.rotation - glRotatef(pitch, 1, 0, 0) - glRotatef(yaw, 0, 1, 0) - glRotatef(roll, 0, 0, 1) - - def __exit__(self, exc_type, exc_val, exc_tb): - glPopMatrix() - - def array_to_ctypes(arr): return cast(arr.buffer_info()[0], POINTER({ 'f': c_float, @@ -295,13 +274,14 @@ def belt(radius, cross, object, count): r = gauss(radius, cross) x, y, z = cos(theta) * r, gauss(0, cross), sin(theta) * r - with glMatrix(): - glTranslatef(x, y, z) - scale = gauss(1, 0.5) - if scale < 0: - scale = 1 - glScalef(scale, scale, scale) - choice(object).draw() + glPushMatrix() + glTranslatef(x, y, z) + scale = gauss(1, 0.5) + if scale < 0: + scale = 1 + glScalef(scale, scale, scale) + choice(object).draw() + glPopMatrix() def progress_bar(x, y, width, height, filled):