comparison pytouhou/ui/opengl/background.pyx @ 513:5e3e0b09a531

Move the OpenGL backend to its own package.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 05 Dec 2013 02:16:31 +0100
parents pytouhou/ui/background.pyx@bfea9e9a6845
children b3193b43a86c
comparison
equal deleted inserted replaced
512:b39ad30c6620 513:5e3e0b09a531
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, use_fixed_pipeline):
39 self.use_fixed_pipeline = use_fixed_pipeline
40
41 if not use_fixed_pipeline:
42 glGenBuffers(1, &self.vbo)
43
44
45 cdef void render_background(self):
46 if self.use_fixed_pipeline:
47 glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &self.vertex_buffer[0].x)
48 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 else:
51 glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
52
53 #TODO: find a way to use offsetof() instead of those ugly hardcoded values.
54 glVertexAttribPointer(0, 3, GL_FLOAT, False, sizeof(Vertex), <void*>0)
55 glEnableVertexAttribArray(0)
56 glVertexAttribPointer(1, 2, GL_FLOAT, False, sizeof(Vertex), <void*>12)
57 glEnableVertexAttribArray(1)
58 glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, True, sizeof(Vertex), <void*>20)
59 glEnableVertexAttribArray(2)
60
61 glEnable(GL_DEPTH_TEST)
62 glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[self.blendfunc])
63 glBindTexture(GL_TEXTURE_2D, self.texture)
64 glDrawArrays(GL_QUADS, 0, self.nb_vertices)
65 glDisable(GL_DEPTH_TEST)
66
67 if not self.use_fixed_pipeline:
68 glBindBuffer(GL_ARRAY_BUFFER, 0)
69
70
71 cdef void load(self, background):
72 cdef float ox, oy, oz, ox2, oy2, oz2
73 cdef unsigned short nb_vertices = 0
74 cdef Vertex* vertex_buffer
75
76 self.background = background
77
78 vertex_buffer = self.vertex_buffer
79
80 for ox, oy, oz, model_id, model in background.object_instances:
81 for ox2, oy2, oz2, width_override, height_override, sprite in model:
82 #TODO: view frustum culling
83 key, (vertices, uvs, colors) = get_sprite_rendering_data(sprite)
84 x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4 = vertices
85 left, right, bottom, top = uvs
86 r, g, b, a = colors
87
88 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)
90 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)
92
93 nb_vertices += 4
94
95 self.texture = key >> 1
96 self.blendfunc = key & 1
97 self.nb_vertices = nb_vertices
98 self.vertex_buffer = <Vertex*> realloc(vertex_buffer, nb_vertices * sizeof(Vertex))
99
100 if not self.use_fixed_pipeline:
101 glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
102 glBufferData(GL_ARRAY_BUFFER, nb_vertices * sizeof(Vertex), &self.vertex_buffer[0], GL_STATIC_DRAW)
103 glBindBuffer(GL_ARRAY_BUFFER, 0)