Mercurial > touhou
comparison pytouhou/ui/opengl/background.pyx @ 519:b18f0bd30ad0
Optimise background rendering.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sat, 07 Dec 2013 22:16:38 +0100 |
parents | b3193b43a86c |
children | dacdcca59b66 |
comparison
equal
deleted
inserted
replaced
518:75ae628522c9 | 519:b18f0bd30ad0 |
---|---|
17 from pytouhou.lib.opengl cimport \ | 17 from pytouhou.lib.opengl cimport \ |
18 (glVertexPointer, glTexCoordPointer, glColorPointer, | 18 (glVertexPointer, glTexCoordPointer, glColorPointer, |
19 glVertexAttribPointer, glEnableVertexAttribArray, glBlendFunc, | 19 glVertexAttribPointer, glEnableVertexAttribArray, glBlendFunc, |
20 glBindTexture, glBindBuffer, glBufferData, GL_ARRAY_BUFFER, | 20 glBindTexture, glBindBuffer, glBufferData, GL_ARRAY_BUFFER, |
21 GL_STATIC_DRAW, GL_UNSIGNED_BYTE, GL_FLOAT, GL_SRC_ALPHA, | 21 GL_STATIC_DRAW, GL_UNSIGNED_BYTE, GL_FLOAT, GL_SRC_ALPHA, |
22 GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_TEXTURE_2D, glGenBuffers, | 22 GL_ONE_MINUS_SRC_ALPHA, GL_TEXTURE_2D, glGenBuffers, glEnable, |
23 glEnable, glDisable, GL_DEPTH_TEST, glDrawArrays, GL_QUADS) | 23 glDisable, GL_DEPTH_TEST, glDrawElements, GL_TRIANGLES, |
24 GL_UNSIGNED_SHORT, GL_ELEMENT_ARRAY_BUFFER) | |
24 | 25 |
25 from .sprite cimport get_sprite_rendering_data | 26 from .sprite cimport get_sprite_rendering_data |
26 | 27 |
27 | 28 |
28 cdef class BackgroundRenderer: | 29 cdef class BackgroundRenderer: |
29 def __cinit__(self): | |
30 # Allocate buffers | |
31 self.vertex_buffer = <Vertex*> malloc(65536 * sizeof(Vertex)) | |
32 | |
33 | |
34 def __dealloc__(self): | 30 def __dealloc__(self): |
35 free(self.vertex_buffer) | 31 if self.vertex_buffer != NULL: |
32 free(self.vertex_buffer) | |
33 if self.indices != NULL: | |
34 free(self.indices) | |
36 | 35 |
37 | 36 |
38 def __init__(self, use_fixed_pipeline): | 37 def __init__(self, use_fixed_pipeline): |
39 self.use_fixed_pipeline = use_fixed_pipeline | 38 self.use_fixed_pipeline = use_fixed_pipeline |
40 | 39 |
41 if not use_fixed_pipeline: | 40 if not use_fixed_pipeline: |
42 glGenBuffers(1, &self.vbo) | 41 glGenBuffers(1, &self.vbo) |
42 glGenBuffers(1, &self.ibo) | |
43 | 43 |
44 | 44 |
45 cdef void render_background(self): | 45 cdef void render_background(self): |
46 if self.use_fixed_pipeline: | 46 if self.use_fixed_pipeline: |
47 indices = self.indices | |
47 glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &self.vertex_buffer[0].x) | 48 glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &self.vertex_buffer[0].x) |
48 glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &self.vertex_buffer[0].u) | 49 glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &self.vertex_buffer[0].u) |
49 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &self.vertex_buffer[0].r) | 50 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &self.vertex_buffer[0].r) |
50 else: | 51 else: |
51 glBindBuffer(GL_ARRAY_BUFFER, self.vbo) | 52 glBindBuffer(GL_ARRAY_BUFFER, self.vbo) |
53 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.ibo) | |
54 indices = NULL | |
52 | 55 |
53 #TODO: find a way to use offsetof() instead of those ugly hardcoded values. | 56 #TODO: find a way to use offsetof() instead of those ugly hardcoded values. |
54 glVertexAttribPointer(0, 3, GL_FLOAT, False, sizeof(Vertex), <void*>0) | 57 glVertexAttribPointer(0, 3, GL_FLOAT, False, sizeof(Vertex), <void*>0) |
55 glEnableVertexAttribArray(0) | 58 glEnableVertexAttribArray(0) |
56 glVertexAttribPointer(1, 2, GL_FLOAT, False, sizeof(Vertex), <void*>12) | 59 glVertexAttribPointer(1, 2, GL_FLOAT, False, sizeof(Vertex), <void*>12) |
57 glEnableVertexAttribArray(1) | 60 glEnableVertexAttribArray(1) |
58 glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, True, sizeof(Vertex), <void*>20) | 61 glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, True, sizeof(Vertex), <void*>20) |
59 glEnableVertexAttribArray(2) | 62 glEnableVertexAttribArray(2) |
60 | 63 |
61 glEnable(GL_DEPTH_TEST) | 64 glEnable(GL_DEPTH_TEST) |
62 glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[self.blendfunc]) | 65 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) |
63 glBindTexture(GL_TEXTURE_2D, self.texture) | 66 glBindTexture(GL_TEXTURE_2D, self.texture) |
64 glDrawArrays(GL_QUADS, 0, self.nb_vertices) | 67 glDrawElements(GL_TRIANGLES, self.nb_indices, GL_UNSIGNED_SHORT, indices) |
65 glDisable(GL_DEPTH_TEST) | 68 glDisable(GL_DEPTH_TEST) |
66 | 69 |
67 if not self.use_fixed_pipeline: | 70 if not self.use_fixed_pipeline: |
68 glBindBuffer(GL_ARRAY_BUFFER, 0) | 71 glBindBuffer(GL_ARRAY_BUFFER, 0) |
72 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) | |
69 | 73 |
70 | 74 |
71 cdef void load(self, background, Renderer renderer): | 75 cdef void load(self, background, Renderer renderer): |
72 cdef float ox, oy, oz, ox2, oy2, oz2 | 76 cdef float ox, oy, oz, ox2, oy2, oz2 |
73 cdef unsigned short nb_vertices = 0 | 77 cdef GLsizei nb_vertices = 0, nb_indices = 0 |
74 cdef Vertex* vertex_buffer | |
75 | 78 |
76 self.background = background | 79 vertex_buffer = <Vertex*> malloc(65536 * sizeof(Vertex)) |
77 | 80 indices = <GLushort*> malloc(65536 * sizeof(GLushort)) |
78 vertex_buffer = self.vertex_buffer | |
79 | 81 |
80 for ox, oy, oz, model_id, model in background.object_instances: | 82 for ox, oy, oz, model_id, model in background.object_instances: |
81 for ox2, oy2, oz2, width_override, height_override, sprite in model: | 83 for ox2, oy2, oz2, width_override, height_override, sprite in model: |
82 #TODO: view frustum culling | 84 #TODO: view frustum culling |
83 key, (vertices, uvs, colors) = get_sprite_rendering_data(sprite) | 85 key, (vertices, uvs, colors) = get_sprite_rendering_data(sprite) |
84 x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4 = vertices | 86 x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4 = vertices |
85 left, right, bottom, top = uvs | 87 left, right, bottom, top = uvs |
86 r, g, b, a = colors | 88 r, g, b, a = colors |
87 | 89 |
90 # Pack data | |
88 vertex_buffer[nb_vertices] = Vertex(x1 + ox + ox2, y1 + oy + oy2, z1 + oz + oz2, left, bottom, r, g, b, a) | 91 vertex_buffer[nb_vertices] = Vertex(x1 + ox + ox2, y1 + oy + oy2, z1 + oz + oz2, left, bottom, r, g, b, a) |
89 vertex_buffer[nb_vertices+1] = Vertex(x2 + ox + ox2, y2 + oy + oy2, z2 + oz + oz2, right, bottom, r, g, b, a) | 92 vertex_buffer[nb_vertices+1] = Vertex(x2 + ox + ox2, y2 + oy + oy2, z2 + oz + oz2, right, bottom, r, g, b, a) |
90 vertex_buffer[nb_vertices+2] = Vertex(x3 + ox + ox2, y3 + oy + oy2, z3 + oz + oz2, right, top, r, g, b, a) | 93 vertex_buffer[nb_vertices+2] = Vertex(x3 + ox + ox2, y3 + oy + oy2, z3 + oz + oz2, right, top, r, g, b, a) |
91 vertex_buffer[nb_vertices+3] = Vertex(x4 + ox + ox2, y4 + oy + oy2, z4 + oz + oz2, left, top, r, g, b, a) | 94 vertex_buffer[nb_vertices+3] = Vertex(x4 + ox + ox2, y4 + oy + oy2, z4 + oz + oz2, left, top, r, g, b, a) |
92 | 95 |
96 # Add indices | |
97 indices[nb_indices] = nb_vertices | |
98 indices[nb_indices+1] = nb_vertices + 1 | |
99 indices[nb_indices+2] = nb_vertices + 2 | |
100 indices[nb_indices+3] = nb_vertices + 2 | |
101 indices[nb_indices+4] = nb_vertices + 3 | |
102 indices[nb_indices+5] = nb_vertices | |
103 | |
93 nb_vertices += 4 | 104 nb_vertices += 4 |
105 nb_indices += 6 | |
106 | |
107 # We only need to keep the rendered vertices and indices in memory, | |
108 # either in RAM or in VRAM, they will never change until we implement | |
109 # background animation. | |
94 | 110 |
95 self.texture = renderer.textures[key >> 1] | 111 self.texture = renderer.textures[key >> 1] |
96 self.blendfunc = key & 1 | 112 self.nb_indices = nb_indices |
97 self.nb_vertices = nb_vertices | |
98 self.vertex_buffer = <Vertex*> realloc(vertex_buffer, nb_vertices * sizeof(Vertex)) | |
99 | 113 |
100 if not self.use_fixed_pipeline: | 114 if self.use_fixed_pipeline: |
115 self.vertex_buffer = <Vertex*> realloc(vertex_buffer, nb_vertices * sizeof(Vertex)) | |
116 self.indices = <GLushort*> realloc(indices, nb_indices * sizeof(GLushort)) | |
117 else: | |
101 glBindBuffer(GL_ARRAY_BUFFER, self.vbo) | 118 glBindBuffer(GL_ARRAY_BUFFER, self.vbo) |
102 glBufferData(GL_ARRAY_BUFFER, nb_vertices * sizeof(Vertex), &self.vertex_buffer[0], GL_STATIC_DRAW) | 119 glBufferData(GL_ARRAY_BUFFER, nb_vertices * sizeof(Vertex), vertex_buffer, GL_STATIC_DRAW) |
103 glBindBuffer(GL_ARRAY_BUFFER, 0) | 120 glBindBuffer(GL_ARRAY_BUFFER, 0) |
121 | |
122 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.ibo) | |
123 glBufferData(GL_ELEMENT_ARRAY_BUFFER, nb_indices * sizeof(GLushort), indices, GL_STATIC_DRAW) | |
124 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) |