annotate pytouhou/ui/renderer.pyx @ 398:8d252cdb495f

Move the background rendering code to pytouhou.ui.renderer.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 12 Feb 2013 18:20:49 +0100
parents c5ba11ede097
children 1c773544eaeb
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
384
690b5faaa0e6 Make rendering of multiple-sprites elements work like single-sprites.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 383
diff changeset
18 from itertools import chain
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
19
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
20 import ctypes
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
21
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
22 from struct import pack
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
23
383
0537af9125a7 Replace wildcard imports with normal ones.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 371
diff changeset
24 from pyglet.gl import (glVertexPointer, glTexCoordPointer, glColorPointer,
394
346614f788f1 Replace gl{Vertex,TexCoord,Color}Pointer with the more modern glVertexAttribPointer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 393
diff changeset
25 glVertexAttribPointer, glEnableVertexAttribArray,
383
0537af9125a7 Replace wildcard imports with normal ones.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 371
diff changeset
26 glBlendFunc, glBindTexture, glDrawElements,
396
34a91f918e7c Use Buffer Objects instead of host pointers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 395
diff changeset
27 glBindBuffer, glBufferData, GL_ARRAY_BUFFER,
34a91f918e7c Use Buffer Objects instead of host pointers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 395
diff changeset
28 GL_DYNAMIC_DRAW, GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT,
34a91f918e7c Use Buffer Objects instead of host pointers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 395
diff changeset
29 GL_INT, GL_FLOAT, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
398
8d252cdb495f Move the background rendering code to pytouhou.ui.renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 397
diff changeset
30 GL_ONE, GL_TEXTURE_2D, GL_TRIANGLES,
8d252cdb495f Move the background rendering code to pytouhou.ui.renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 397
diff changeset
31 glEnable, glDisable, GL_DEPTH_TEST, glDrawArrays, GL_QUADS)
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
32
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
33 from .sprite cimport get_sprite_rendering_data
398
8d252cdb495f Move the background rendering code to pytouhou.ui.renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 397
diff changeset
34 from .background import get_background_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
35 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
36 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
37 from pytouhou.utils.vector import Vector, normalize, cross, dot
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
38
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
39
301
6f1ca1cb5238 Avoid segfaults in the unlikely case a huge number of sprites is rendered.
Thibaut Girka <thib@sitedethib.com>
parents: 245
diff changeset
40 MAX_ELEMENTS = 640*4*3
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
41
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 cdef class Renderer:
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
44 def __cinit__(self):
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
45 # Allocate buffers
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
46 self.vertex_buffer = <Vertex*> malloc(MAX_ELEMENTS * sizeof(Vertex))
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
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
49 def __dealloc__(self):
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
50 free(self.vertex_buffer)
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
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
53 def __init__(self, resource_loader):
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
54 self.texture_manager = TextureManager(resource_loader)
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
55
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 cpdef render_elements(self, elements):
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
58 cdef unsigned short nb_vertices = 0
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
59
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
60 indices_by_texture = {}
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
61
384
690b5faaa0e6 Make rendering of multiple-sprites elements work like single-sprites.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 383
diff changeset
62 objects = chain(*[element.objects for element in elements])
690b5faaa0e6 Make rendering of multiple-sprites elements work like single-sprites.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 383
diff changeset
63 for element in objects:
301
6f1ca1cb5238 Avoid segfaults in the unlikely case a huge number of sprites is rendered.
Thibaut Girka <thib@sitedethib.com>
parents: 245
diff changeset
64 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
65 break
6f1ca1cb5238 Avoid segfaults in the unlikely case a huge number of sprites is rendered.
Thibaut Girka <thib@sitedethib.com>
parents: 245
diff changeset
66
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 301
diff changeset
67 sprite = element.sprite
245
d3ba32a9096e Implement ANM0 instruction 29 and fix 24
Thibaut Girka <thib@sitedethib.com>
parents: 223
diff changeset
68 if sprite and sprite.visible:
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
69 ox, oy = element.x, element.y
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
70 key, (vertices, uvs, colors) = get_sprite_rendering_data(sprite)
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
71 rec = indices_by_texture.setdefault(key, [])
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
72
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
73 # Pack data in buffer
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
74 (x1, y1, z1), (x2, y2, z2), (x3, y3, z3), (x4, y4, z4) = vertices
397
c5ba11ede097 Don’t duplicate values in sprite rendering data.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 396
diff changeset
75 left, right, bottom, top = uvs
c5ba11ede097 Don’t duplicate values in sprite rendering data.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 396
diff changeset
76 r, g, b, a = colors
c5ba11ede097 Don’t duplicate values in sprite rendering data.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 396
diff changeset
77 self.vertex_buffer[nb_vertices] = Vertex(x1 + ox, y1 + oy, z1, left, bottom, r, g, b, a)
c5ba11ede097 Don’t duplicate values in sprite rendering data.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 396
diff changeset
78 self.vertex_buffer[nb_vertices+1] = Vertex(x2 + ox, y2 + oy, z2, right, bottom, r, g, b, a)
c5ba11ede097 Don’t duplicate values in sprite rendering data.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 396
diff changeset
79 self.vertex_buffer[nb_vertices+2] = Vertex(x3 + ox, y3 + oy, z3, right, top, r, g, b, a)
c5ba11ede097 Don’t duplicate values in sprite rendering data.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 396
diff changeset
80 self.vertex_buffer[nb_vertices+3] = Vertex(x4 + ox, y4 + oy, z4, left, top, r, g, b, a)
222
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 # Add indices
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
83 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
84 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
85
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
86 nb_vertices += 4
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
87
395
43413d4ff05b Don’t change the vertex attributes for each texture.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
88 if self.use_fixed_pipeline:
43413d4ff05b Don’t change the vertex attributes for each texture.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
89 glVertexPointer(3, GL_INT, sizeof(Vertex), <long> &self.vertex_buffer[0].x)
43413d4ff05b Don’t change the vertex attributes for each texture.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
90 glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), <long> &self.vertex_buffer[0].u)
43413d4ff05b Don’t change the vertex attributes for each texture.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
91 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), <long> &self.vertex_buffer[0].r)
43413d4ff05b Don’t change the vertex attributes for each texture.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
92 else:
396
34a91f918e7c Use Buffer Objects instead of host pointers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 395
diff changeset
93 glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
34a91f918e7c Use Buffer Objects instead of host pointers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 395
diff changeset
94 glBufferData(GL_ARRAY_BUFFER, nb_vertices * sizeof(Vertex), <long> &self.vertex_buffer[0], GL_DYNAMIC_DRAW)
34a91f918e7c Use Buffer Objects instead of host pointers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 395
diff changeset
95
34a91f918e7c Use Buffer Objects instead of host pointers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 395
diff changeset
96 #TODO: find a way to use offsetof() instead of those ugly hardcoded values.
34a91f918e7c Use Buffer Objects instead of host pointers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 395
diff changeset
97 glVertexAttribPointer(0, 3, GL_INT, False, sizeof(Vertex), 0)
395
43413d4ff05b Don’t change the vertex attributes for each texture.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
98 glEnableVertexAttribArray(0)
396
34a91f918e7c Use Buffer Objects instead of host pointers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 395
diff changeset
99 glVertexAttribPointer(1, 2, GL_FLOAT, False, sizeof(Vertex), 12)
395
43413d4ff05b Don’t change the vertex attributes for each texture.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
100 glEnableVertexAttribArray(1)
396
34a91f918e7c Use Buffer Objects instead of host pointers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 395
diff changeset
101 glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, True, sizeof(Vertex), 20)
395
43413d4ff05b Don’t change the vertex attributes for each texture.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
102 glEnableVertexAttribArray(2)
43413d4ff05b Don’t change the vertex attributes for each texture.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
103
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
104 for (texture_key, blendfunc), indices in indices_by_texture.items():
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
105 nb_indices = len(indices)
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
106 indices = pack(str(nb_indices) + 'H', *indices)
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
107 glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc])
393
9e2cbb2c2c64 Implement THTX, uncompressed textures stored inside ANM files, and use it instead of pyglet’s own wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 384
diff changeset
108 glBindTexture(GL_TEXTURE_2D, self.texture_manager[texture_key])
371
6702bc0215dc Replace GL_QUADS with GL_TRIANGLES, to be GLES compatible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
109 glDrawElements(GL_TRIANGLES, nb_indices, GL_UNSIGNED_SHORT, indices)
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
110
396
34a91f918e7c Use Buffer Objects instead of host pointers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 395
diff changeset
111 if not self.use_fixed_pipeline:
34a91f918e7c Use Buffer Objects instead of host pointers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 395
diff changeset
112 glBindBuffer(GL_ARRAY_BUFFER, 0)
34a91f918e7c Use Buffer Objects instead of host pointers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 395
diff changeset
113
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
114
398
8d252cdb495f Move the background rendering code to pytouhou.ui.renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 397
diff changeset
115 cpdef render_background(self, back):
8d252cdb495f Move the background rendering code to pytouhou.ui.renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 397
diff changeset
116 glEnable(GL_DEPTH_TEST)
8d252cdb495f Move the background rendering code to pytouhou.ui.renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 397
diff changeset
117 for (texture_key, blendfunc), (nb_vertices, vertices, uvs, colors) in get_background_rendering_data(back):
8d252cdb495f Move the background rendering code to pytouhou.ui.renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 397
diff changeset
118 if self.use_fixed_pipeline:
8d252cdb495f Move the background rendering code to pytouhou.ui.renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 397
diff changeset
119 glVertexPointer(3, GL_FLOAT, 0, vertices)
8d252cdb495f Move the background rendering code to pytouhou.ui.renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 397
diff changeset
120 glTexCoordPointer(2, GL_FLOAT, 0, uvs)
8d252cdb495f Move the background rendering code to pytouhou.ui.renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 397
diff changeset
121 glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors)
8d252cdb495f Move the background rendering code to pytouhou.ui.renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 397
diff changeset
122 else:
8d252cdb495f Move the background rendering code to pytouhou.ui.renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 397
diff changeset
123 glVertexAttribPointer(0, 3, GL_FLOAT, False, 0, vertices)
8d252cdb495f Move the background rendering code to pytouhou.ui.renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 397
diff changeset
124 glEnableVertexAttribArray(0)
8d252cdb495f Move the background rendering code to pytouhou.ui.renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 397
diff changeset
125 glVertexAttribPointer(1, 2, GL_FLOAT, False, 0, uvs)
8d252cdb495f Move the background rendering code to pytouhou.ui.renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 397
diff changeset
126 glEnableVertexAttribArray(1)
8d252cdb495f Move the background rendering code to pytouhou.ui.renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 397
diff changeset
127 glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, True, 0, colors)
8d252cdb495f Move the background rendering code to pytouhou.ui.renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 397
diff changeset
128 glEnableVertexAttribArray(2)
8d252cdb495f Move the background rendering code to pytouhou.ui.renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 397
diff changeset
129 glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc])
8d252cdb495f Move the background rendering code to pytouhou.ui.renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 397
diff changeset
130 glBindTexture(GL_TEXTURE_2D, self.texture_manager[texture_key])
8d252cdb495f Move the background rendering code to pytouhou.ui.renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 397
diff changeset
131 glDrawArrays(GL_QUADS, 0, nb_vertices)
8d252cdb495f Move the background rendering code to pytouhou.ui.renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 397
diff changeset
132 glDisable(GL_DEPTH_TEST)
8d252cdb495f Move the background rendering code to pytouhou.ui.renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 397
diff changeset
133
8d252cdb495f Move the background rendering code to pytouhou.ui.renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 397
diff changeset
134
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
135 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
136 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
137 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
138 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
139 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
140 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
141 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
142 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
143
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
144
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
145 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
146 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
147 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
148 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
149
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
150 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
151 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
152 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
153 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
154
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
155 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
156 [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
157 [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
158 [-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
159
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
160
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
161 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
162 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
163 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
164 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
165 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
166
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
167 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
168 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
169 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
170 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
171 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
172 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
173 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
174 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
175
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
176
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
177 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
178 # 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
179 # 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
180 # 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
181 # 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
182 # 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
183 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
184 (192. + dx, 224. - dy, 0.), (0., -1., 0.))
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
185