annotate pytouhou/ui/renderer.pyx @ 383:0537af9125a7

Replace wildcard imports with normal ones.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 07 Sep 2012 13:05:19 +0200
parents 6702bc0215dc
children 690b5faaa0e6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
1 # -*- encoding: utf-8 -*-
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
2 ##
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com>
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
4 ##
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
5 ## This program is free software; you can redistribute it and/or modify
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
6 ## it under the terms of the GNU General Public License as published
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
7 ## by the Free Software Foundation; version 3 only.
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
8 ##
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
9 ## This program is distributed in the hope that it will be useful,
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
12 ## GNU General Public License for more details.
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
13 ##
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
14
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
15 from libc.stdlib cimport malloc, free
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
16 from libc.math cimport tan
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
17 from math import radians
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
18
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
19 import ctypes
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
20
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
21 from struct import pack
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
22
383
0537af9125a7 Replace wildcard imports with normal ones.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 371
diff changeset
23 from pyglet.gl import (glVertexPointer, glTexCoordPointer, glColorPointer,
0537af9125a7 Replace wildcard imports with normal ones.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 371
diff changeset
24 glBlendFunc, glBindTexture, glDrawElements,
0537af9125a7 Replace wildcard imports with normal ones.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 371
diff changeset
25 GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT, GL_INT, GL_FLOAT,
0537af9125a7 Replace wildcard imports with normal ones.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 371
diff changeset
26 GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE,
0537af9125a7 Replace wildcard imports with normal ones.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 371
diff changeset
27 GL_TEXTURE_2D, GL_TRIANGLES)
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
28
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
29 from .sprite cimport get_sprite_rendering_data
223
98c64ffcbdff Make pytouhou.ui.{background,texture} Cython modules as they are only used by Cython modules.
Thibaut Girka <thib@sitedethib.com>
parents: 222
diff changeset
30 from .texture cimport TextureManager
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
31 from pytouhou.utils.matrix cimport Matrix
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
32 from pytouhou.utils.vector import Vector, normalize, cross, dot
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
33
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
34
301
6f1ca1cb5238 Avoid segfaults in the unlikely case a huge number of sprites is rendered.
Thibaut Girka <thib@sitedethib.com>
parents: 245
diff changeset
35 MAX_ELEMENTS = 640*4*3
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
36
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
37
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
38 cdef class Renderer:
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
39 def __cinit__(self):
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
40 # Allocate buffers
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
41 self.vertex_buffer = <Vertex*> malloc(MAX_ELEMENTS * sizeof(Vertex))
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
42
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
43
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
44 def __dealloc__(self):
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
45 free(self.vertex_buffer)
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
46
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
47
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
48 def __init__(self, resource_loader):
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
49 self.texture_manager = TextureManager(resource_loader)
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
50
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
51
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
52 cpdef render_elements(self, elements):
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
53 cdef unsigned short nb_vertices = 0
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
54
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
55 indices_by_texture = {}
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
56
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
57 for element in elements:
301
6f1ca1cb5238 Avoid segfaults in the unlikely case a huge number of sprites is rendered.
Thibaut Girka <thib@sitedethib.com>
parents: 245
diff changeset
58 if nb_vertices >= MAX_ELEMENTS - 4:
6f1ca1cb5238 Avoid segfaults in the unlikely case a huge number of sprites is rendered.
Thibaut Girka <thib@sitedethib.com>
parents: 245
diff changeset
59 break
6f1ca1cb5238 Avoid segfaults in the unlikely case a huge number of sprites is rendered.
Thibaut Girka <thib@sitedethib.com>
parents: 245
diff changeset
60
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 301
diff changeset
61 sprite = element.sprite
245
d3ba32a9096e Implement ANM0 instruction 29 and fix 24
Thibaut Girka <thib@sitedethib.com>
parents: 223
diff changeset
62 if sprite and sprite.visible:
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
63 ox, oy = element.x, element.y
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
64 key, (vertices, uvs, colors) = get_sprite_rendering_data(sprite)
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
65 rec = indices_by_texture.setdefault(key, [])
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
66
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
67 # Pack data in buffer
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
68 (x1, y1, z1), (x2, y2, z2), (x3, y3, z3), (x4, y4, z4) = vertices
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
69 r1, g1, b1, a1, r2, g2, b2, a2, r3, g3, b3, a3, r4, g4, b4, a4 = colors
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
70 u1, v1, u2, v2, u3, v3, u4, v4 = uvs
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
71 self.vertex_buffer[nb_vertices] = Vertex(x1 + ox, y1 + oy, z1, u1, v1, r1, g1, b1, a1)
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
72 self.vertex_buffer[nb_vertices+1] = Vertex(x2 + ox, y2 + oy, z2, u2, v2, r2, g2, b2, a2)
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
73 self.vertex_buffer[nb_vertices+2] = Vertex(x3 + ox, y3 + oy, z3, u3, v3, r3, g3, b3, a3)
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
74 self.vertex_buffer[nb_vertices+3] = Vertex(x4 + ox, y4 + oy, z4, u4, v4, r4, g4, b4, a4)
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
75
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
76 # Add indices
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
77 index = nb_vertices
371
6702bc0215dc Replace GL_QUADS with GL_TRIANGLES, to be GLES compatible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
78 rec.extend((index, index + 1, index + 2, index + 2, index + 3, index))
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
79
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
80 nb_vertices += 4
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
81
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
82 for (texture_key, blendfunc), indices in indices_by_texture.items():
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
83 glVertexPointer(3, GL_INT, 24, <long> &self.vertex_buffer[0].x)
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
84 glTexCoordPointer(2, GL_FLOAT, 24, <long> &self.vertex_buffer[0].u)
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
85 glColorPointer(4, GL_UNSIGNED_BYTE, 24, <long> &self.vertex_buffer[0].r)
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
86
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
87 nb_indices = len(indices)
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
88 indices = pack(str(nb_indices) + 'H', *indices)
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
89 glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc])
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
90 glBindTexture(GL_TEXTURE_2D, self.texture_manager[texture_key].id)
371
6702bc0215dc Replace GL_QUADS with GL_TRIANGLES, to be GLES compatible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
91 glDrawElements(GL_TRIANGLES, nb_indices, GL_UNSIGNED_SHORT, indices)
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
92
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
93
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
94 cpdef ortho_2d(self, left, right, bottom, top):
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
95 mat = Matrix()
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
96 mat[0][0] = 2 / (right - left)
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
97 mat[1][1] = 2 / (top - bottom)
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
98 mat[2][2] = -1
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
99 mat[3][0] = -(right + left) / (right - left)
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
100 mat[3][1] = -(top + bottom) / (top - bottom)
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
101 return mat
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
102
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
103
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
104 cpdef look_at(self, eye, center, up):
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
105 eye = Vector(eye)
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
106 center = Vector(center)
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
107 up = Vector(up)
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
108
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
109 f = normalize(center - eye)
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
110 u = normalize(up)
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
111 s = normalize(cross(f, u))
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
112 u = cross(s, f)
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
113
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
114 return Matrix([[s[0], u[0], -f[0], 0],
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
115 [s[1], u[1], -f[1], 0],
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
116 [s[2], u[2], -f[2], 0],
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
117 [-dot(s, eye), -dot(u, eye), dot(f, eye), 1]])
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
118
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
119
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
120 cpdef perspective(self, fovy, aspect, z_near, z_far):
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
121 top = tan(radians(fovy / 2)) * z_near
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
122 bottom = -top
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
123 left = -top * aspect
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
124 right = top * aspect
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
125
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
126 mat = Matrix()
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
127 mat[0][0] = (2 * z_near) / (right - left)
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
128 mat[1][1] = (2 * z_near) / (top - bottom)
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
129 mat[2][2] = -(z_far + z_near) / (z_far - z_near)
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
130 mat[2][3] = -1
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
131 mat[3][2] = -(2 * z_far * z_near) / (z_far - z_near)
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
132 mat[3][3] = 0
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
133 return mat
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
134
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
135
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
136 cpdef setup_camera(self, dx, dy, dz):
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
137 # Some explanations on the magic constants:
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
138 # 192. = 384. / 2. = width / 2.
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
139 # 224. = 448. / 2. = height / 2.
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
140 # 835.979370 = 224./math.tan(math.radians(15)) = (height/2.)/math.tan(math.radians(fov/2))
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
141 # This is so that objects on the (O, x, y) plane use pixel coordinates
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
142 return self.look_at((192., 224., - 835.979370 * dz),
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
143 (192. + dx, 224. - dy, 0.), (0., -1., 0.))
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
144