Mercurial > touhou
annotate pytouhou/ui/renderer.pyx @ 413:6d7dbcb31d95
Let the gc do its work, don’t keep useless references.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Wed, 12 Jun 2013 18:32:35 +0200 |
parents | 5fe6cd6ceb48 |
children | d8630c086926 |
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 |
399
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
15 from libc.stdlib cimport malloc, free, realloc |
384
690b5faaa0e6
Make rendering of multiple-sprites elements work like single-sprites.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
383
diff
changeset
|
16 from itertools import chain |
222
5cac48b328ad
Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
17 |
5cac48b328ad
Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
18 import ctypes |
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 from struct import pack |
5cac48b328ad
Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
21 |
383
0537af9125a7
Replace wildcard imports with normal ones.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
371
diff
changeset
|
22 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
|
23 glVertexAttribPointer, glEnableVertexAttribArray, |
383
0537af9125a7
Replace wildcard imports with normal ones.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
371
diff
changeset
|
24 glBlendFunc, glBindTexture, glDrawElements, |
396
34a91f918e7c
Use Buffer Objects instead of host pointers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
395
diff
changeset
|
25 glBindBuffer, glBufferData, GL_ARRAY_BUFFER, |
399
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
26 GL_DYNAMIC_DRAW, GL_STATIC_DRAW, GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT, |
396
34a91f918e7c
Use Buffer Objects instead of host pointers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
395
diff
changeset
|
27 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
|
28 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
|
29 glEnable, glDisable, GL_DEPTH_TEST, glDrawArrays, GL_QUADS) |
222
5cac48b328ad
Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
30 |
5cac48b328ad
Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
31 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
|
32 from .texture cimport TextureManager |
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)) |
399
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
42 self.background_vertex_buffer = <VertexFloat*> malloc(65536 * sizeof(Vertex)) |
222
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 |
5cac48b328ad
Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
45 def __dealloc__(self): |
5cac48b328ad
Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
46 free(self.vertex_buffer) |
399
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
47 free(self.background_vertex_buffer) |
222
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 |
5cac48b328ad
Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
50 def __init__(self, resource_loader): |
5cac48b328ad
Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
51 self.texture_manager = TextureManager(resource_loader) |
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 |
5cac48b328ad
Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
54 cpdef render_elements(self, elements): |
5cac48b328ad
Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
55 cdef unsigned short nb_vertices = 0 |
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 indices_by_texture = {} |
5cac48b328ad
Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
58 |
384
690b5faaa0e6
Make rendering of multiple-sprites elements work like single-sprites.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
383
diff
changeset
|
59 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
|
60 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
|
61 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
|
62 break |
6f1ca1cb5238
Avoid segfaults in the unlikely case a huge number of sprites is rendered.
Thibaut Girka <thib@sitedethib.com>
parents:
245
diff
changeset
|
63 |
304
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
301
diff
changeset
|
64 sprite = element.sprite |
245
d3ba32a9096e
Implement ANM0 instruction 29 and fix 24
Thibaut Girka <thib@sitedethib.com>
parents:
223
diff
changeset
|
65 if sprite and sprite.visible: |
222
5cac48b328ad
Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
66 ox, oy = element.x, element.y |
5cac48b328ad
Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
67 key, (vertices, uvs, colors) = get_sprite_rendering_data(sprite) |
5cac48b328ad
Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
68 rec = indices_by_texture.setdefault(key, []) |
5cac48b328ad
Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
69 |
5cac48b328ad
Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
70 # Pack data in buffer |
5cac48b328ad
Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
71 (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
|
72 left, right, bottom, top = uvs |
c5ba11ede097
Don’t duplicate values in sprite rendering data.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
396
diff
changeset
|
73 r, g, b, a = colors |
c5ba11ede097
Don’t duplicate values in sprite rendering data.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
396
diff
changeset
|
74 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
|
75 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
|
76 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
|
77 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
|
78 |
5cac48b328ad
Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
79 # Add indices |
5cac48b328ad
Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
80 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
|
81 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
|
82 |
5cac48b328ad
Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
83 nb_vertices += 4 |
5cac48b328ad
Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
84 |
410
9d790ca73c13
Don’t render null-sized arrays of elements.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
399
diff
changeset
|
85 if nb_vertices == 0: |
9d790ca73c13
Don’t render null-sized arrays of elements.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
399
diff
changeset
|
86 return |
9d790ca73c13
Don’t render null-sized arrays of elements.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
399
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 |
399
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
115 cpdef render_background(self): |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
116 if self.use_fixed_pipeline: |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
117 glVertexPointer(3, GL_FLOAT, sizeof(VertexFloat), <long> &self.background_vertex_buffer[0].x) |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
118 glTexCoordPointer(2, GL_FLOAT, sizeof(VertexFloat), <long> &self.background_vertex_buffer[0].u) |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
119 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(VertexFloat), <long> &self.background_vertex_buffer[0].r) |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
120 else: |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
121 glBindBuffer(GL_ARRAY_BUFFER, self.back_vbo) |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
122 |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
123 #TODO: find a way to use offsetof() instead of those ugly hardcoded values. |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
124 glVertexAttribPointer(0, 3, GL_FLOAT, False, sizeof(VertexFloat), 0) |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
125 glEnableVertexAttribArray(0) |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
126 glVertexAttribPointer(1, 2, GL_FLOAT, False, sizeof(VertexFloat), 12) |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
127 glEnableVertexAttribArray(1) |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
128 glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, True, sizeof(VertexFloat), 20) |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
129 glEnableVertexAttribArray(2) |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
130 |
398
8d252cdb495f
Move the background rendering code to pytouhou.ui.renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
397
diff
changeset
|
131 glEnable(GL_DEPTH_TEST) |
399
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
132 glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[self.blendfunc]) |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
133 glBindTexture(GL_TEXTURE_2D, self.texture_manager[self.texture_key]) |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
134 glDrawArrays(GL_QUADS, 0, self.nb_vertices) |
398
8d252cdb495f
Move the background rendering code to pytouhou.ui.renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
397
diff
changeset
|
135 glDisable(GL_DEPTH_TEST) |
8d252cdb495f
Move the background rendering code to pytouhou.ui.renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
397
diff
changeset
|
136 |
399
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
137 if not self.use_fixed_pipeline: |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
138 glBindBuffer(GL_ARRAY_BUFFER, 0) |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
139 |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
140 |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
141 cpdef prerender_background(self, background): |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
142 cdef float ox, oy, oz, ox2, oy2, oz2 |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
143 cdef unsigned short nb_vertices = 0 |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
144 cdef VertexFloat* vertex_buffer |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
145 |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
146 vertex_buffer = self.background_vertex_buffer |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
147 |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
148 for ox, oy, oz, model_id, model in background.object_instances: |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
149 for ox2, oy2, oz2, width_override, height_override, sprite in model: |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
150 #TODO: view frustum culling |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
151 key, (vertices, uvs, colors) = get_sprite_rendering_data(sprite) |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
152 (x1, y1, z1), (x2, y2, z2), (x3, y3, z3), (x4, y4, z4) = vertices |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
153 left, right, bottom, top = uvs |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
154 r, g, b, a = colors |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
155 |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
156 vertex_buffer[nb_vertices] = VertexFloat(x1 + ox + ox2, y1 + oy + oy2, z1 + oz + oz2, left, bottom, r, g, b, a) |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
157 vertex_buffer[nb_vertices+1] = VertexFloat(x2 + ox + ox2, y2 + oy + oy2, z2 + oz + oz2, right, bottom, r, g, b, a) |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
158 vertex_buffer[nb_vertices+2] = VertexFloat(x3 + ox + ox2, y3 + oy + oy2, z3 + oz + oz2, right, top, r, g, b, a) |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
159 vertex_buffer[nb_vertices+3] = VertexFloat(x4 + ox + ox2, y4 + oy + oy2, z4 + oz + oz2, left, top, r, g, b, a) |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
160 |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
161 nb_vertices += 4 |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
162 |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
163 self.texture_key, self.blendfunc = key |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
164 self.nb_vertices = nb_vertices |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
165 self.background_vertex_buffer = <VertexFloat*> realloc(vertex_buffer, nb_vertices * sizeof(VertexFloat)) |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
166 |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
167 if not self.use_fixed_pipeline: |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
168 glBindBuffer(GL_ARRAY_BUFFER, self.back_vbo) |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
169 glBufferData(GL_ARRAY_BUFFER, nb_vertices * sizeof(VertexFloat), <long> &self.background_vertex_buffer[0], GL_STATIC_DRAW) |
1c773544eaeb
Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
398
diff
changeset
|
170 glBindBuffer(GL_ARRAY_BUFFER, 0) |