Mercurial > touhou
comparison pytouhou/ui/background.pyx @ 423:d8630c086926
Replace Pyglet with our own Cython OpenGL wrapper.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Tue, 16 Jul 2013 21:07:15 +0200 |
parents | |
children | 5d7bb2fd74f7 |
comparison
equal
deleted
inserted
replaced
422:52829ebe2561 | 423:d8630c086926 |
---|---|
1 # -*- encoding: utf-8 -*- | |
2 ## | |
3 ## Copyright (C) 2013 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> | |
4 ## | |
5 ## This program is free software; you can redistribute it and/or modify | |
6 ## it under the terms of the GNU General Public License as published | |
7 ## by the Free Software Foundation; version 3 only. | |
8 ## | |
9 ## This program is distributed in the hope that it will be useful, | |
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 ## GNU General Public License for more details. | |
13 ## | |
14 | |
15 from libc.stdlib cimport malloc, free, realloc | |
16 | |
17 from pytouhou.lib.opengl cimport \ | |
18 (glVertexPointer, glTexCoordPointer, glColorPointer, | |
19 glVertexAttribPointer, glEnableVertexAttribArray, glBlendFunc, | |
20 glBindTexture, glBindBuffer, glBufferData, GL_ARRAY_BUFFER, | |
21 GL_STATIC_DRAW, GL_UNSIGNED_BYTE, GL_FLOAT, GL_SRC_ALPHA, | |
22 GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_TEXTURE_2D, glGenBuffers, | |
23 glEnable, glDisable, GL_DEPTH_TEST, glDrawArrays, GL_QUADS) | |
24 | |
25 from .sprite cimport get_sprite_rendering_data | |
26 | |
27 | |
28 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): | |
35 free(self.vertex_buffer) | |
36 | |
37 | |
38 def __init__(self, texture_manager, use_fixed_pipeline): | |
39 self.texture_manager = texture_manager | |
40 self.use_fixed_pipeline = use_fixed_pipeline | |
41 | |
42 if not use_fixed_pipeline: | |
43 glGenBuffers(1, &self.vbo) | |
44 | |
45 | |
46 cpdef render_background(self): | |
47 if self.use_fixed_pipeline: | |
48 glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &self.vertex_buffer[0].x) | |
49 glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &self.vertex_buffer[0].u) | |
50 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &self.vertex_buffer[0].r) | |
51 else: | |
52 glBindBuffer(GL_ARRAY_BUFFER, self.vbo) | |
53 | |
54 #TODO: find a way to use offsetof() instead of those ugly hardcoded values. | |
55 glVertexAttribPointer(0, 3, GL_FLOAT, False, sizeof(Vertex), <void*>0) | |
56 glEnableVertexAttribArray(0) | |
57 glVertexAttribPointer(1, 2, GL_FLOAT, False, sizeof(Vertex), <void*>12) | |
58 glEnableVertexAttribArray(1) | |
59 glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, True, sizeof(Vertex), <void*>20) | |
60 glEnableVertexAttribArray(2) | |
61 | |
62 glEnable(GL_DEPTH_TEST) | |
63 glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[self.blendfunc]) | |
64 glBindTexture(GL_TEXTURE_2D, self.texture_manager[self.texture_key]) | |
65 glDrawArrays(GL_QUADS, 0, self.nb_vertices) | |
66 glDisable(GL_DEPTH_TEST) | |
67 | |
68 if not self.use_fixed_pipeline: | |
69 glBindBuffer(GL_ARRAY_BUFFER, 0) | |
70 | |
71 | |
72 cpdef prerender(self, background): | |
73 cdef float ox, oy, oz, ox2, oy2, oz2 | |
74 cdef unsigned short nb_vertices = 0 | |
75 cdef Vertex* vertex_buffer | |
76 | |
77 vertex_buffer = self.vertex_buffer | |
78 | |
79 for ox, oy, oz, model_id, model in background.object_instances: | |
80 for ox2, oy2, oz2, width_override, height_override, sprite in model: | |
81 #TODO: view frustum culling | |
82 key, (vertices, uvs, colors) = get_sprite_rendering_data(sprite) | |
83 (x1, y1, z1), (x2, y2, z2), (x3, y3, z3), (x4, y4, z4) = vertices | |
84 left, right, bottom, top = uvs | |
85 r, g, b, a = colors | |
86 | |
87 vertex_buffer[nb_vertices] = Vertex(x1 + ox + ox2, y1 + oy + oy2, z1 + oz + oz2, left, bottom, r, g, b, a) | |
88 vertex_buffer[nb_vertices+1] = Vertex(x2 + ox + ox2, y2 + oy + oy2, z2 + oz + oz2, right, bottom, r, g, b, a) | |
89 vertex_buffer[nb_vertices+2] = Vertex(x3 + ox + ox2, y3 + oy + oy2, z3 + oz + oz2, right, top, r, g, b, a) | |
90 vertex_buffer[nb_vertices+3] = Vertex(x4 + ox + ox2, y4 + oy + oy2, z4 + oz + oz2, left, top, r, g, b, a) | |
91 | |
92 nb_vertices += 4 | |
93 | |
94 self.texture_key, self.blendfunc = key | |
95 self.nb_vertices = nb_vertices | |
96 self.vertex_buffer = <Vertex*> realloc(vertex_buffer, nb_vertices * sizeof(Vertex)) | |
97 | |
98 if not self.use_fixed_pipeline: | |
99 glBindBuffer(GL_ARRAY_BUFFER, self.vbo) | |
100 glBufferData(GL_ARRAY_BUFFER, nb_vertices * sizeof(Vertex), &self.vertex_buffer[0], GL_STATIC_DRAW) | |
101 glBindBuffer(GL_ARRAY_BUFFER, 0) |