diff pytouhou/ui/renderer.pyx @ 370:74471afbac37

Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 27 Jul 2012 18:43:48 +0200
parents f3099ebf4f61
children 6702bc0215dc
line wrap: on
line diff
--- a/pytouhou/ui/renderer.pyx
+++ b/pytouhou/ui/renderer.pyx
@@ -13,6 +13,8 @@
 ##
 
 from libc.stdlib cimport malloc, free
+from libc.math cimport tan
+from math import radians
 
 import ctypes
 
@@ -22,6 +24,8 @@ from pyglet.gl import *
 
 from .sprite cimport get_sprite_rendering_data
 from .texture cimport TextureManager
+from pytouhou.utils.matrix cimport Matrix
+from pytouhou.utils.vector import Vector, normalize, cross, dot
 
 
 MAX_ELEMENTS = 640*4*3
@@ -83,14 +87,54 @@ cdef class Renderer:
             glDrawElements(GL_QUADS, nb_indices, GL_UNSIGNED_SHORT, indices)
 
 
+    cpdef ortho_2d(self, left, right, bottom, top):
+        mat = Matrix()
+        mat[0][0] = 2 / (right - left)
+        mat[1][1] = 2 / (top - bottom)
+        mat[2][2] = -1
+        mat[3][0] = -(right + left) / (right - left)
+        mat[3][1] = -(top + bottom) / (top - bottom)
+        return mat
+
+
+    cpdef look_at(self, eye, center, up):
+        eye = Vector(eye)
+        center = Vector(center)
+        up = Vector(up)
+
+        f = normalize(center - eye)
+        u = normalize(up)
+        s = normalize(cross(f, u))
+        u = cross(s, f)
+
+        return Matrix([[s[0], u[0], -f[0], 0],
+                       [s[1], u[1], -f[1], 0],
+                       [s[2], u[2], -f[2], 0],
+                       [-dot(s, eye), -dot(u, eye), dot(f, eye), 1]])
+
+
+    cpdef perspective(self, fovy, aspect, z_near, z_far):
+        top = tan(radians(fovy / 2)) * z_near
+        bottom = -top
+        left = -top * aspect
+        right = top * aspect
+
+        mat = Matrix()
+        mat[0][0] = (2 * z_near) / (right - left)
+        mat[1][1] = (2 * z_near) / (top - bottom)
+        mat[2][2] = -(z_far + z_near) / (z_far - z_near)
+        mat[2][3] = -1
+        mat[3][2] = -(2 * z_far * z_near) / (z_far - z_near)
+        mat[3][3] = 0
+        return mat
+
+
     cpdef setup_camera(self, dx, dy, dz):
-            glMatrixMode(GL_MODELVIEW)
-            glLoadIdentity()
-            # Some explanations on the magic constants:
-            # 192. = 384. / 2. = width / 2.
-            # 224. = 448. / 2. = height / 2.
-            # 835.979370 = 224./math.tan(math.radians(15)) = (height/2.)/math.tan(math.radians(fov/2))
-            # This is so that objects on the (O, x, y) plane use pixel coordinates
-            gluLookAt(192., 224., - 835.979370 * dz,
-                      192. + dx, 224. - dy, 0., 0., -1., 0.)
+        # Some explanations on the magic constants:
+        # 192. = 384. / 2. = width / 2.
+        # 224. = 448. / 2. = height / 2.
+        # 835.979370 = 224./math.tan(math.radians(15)) = (height/2.)/math.tan(math.radians(fov/2))
+        # This is so that objects on the (O, x, y) plane use pixel coordinates
+        return self.look_at((192., 224., - 835.979370 * dz),
+                            (192. + dx, 224. - dy, 0.), (0., -1., 0.))