source: trunk/src/scene.cpp @ 821

Last change on this file since 821 was 821, checked in by sam, 10 years ago

gl: do not call glDeleteBuffers with a zero-length list, it crashes on
the PS3.

  • Property svn:keywords set to Id
File size: 9.7 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include <cstdlib>
16#include <cmath>
17
18#ifdef WIN32
19#   define WIN32_LEAN_AND_MEAN
20#   include <windows.h>
21#endif
22
23#include "core.h"
24#include "lolgl.h"
25
26namespace lol
27{
28
29struct Tile
30{
31    TileSet *tileset;
32    uint32_t prio;
33    vec3i pos;
34    int id, o;
35};
36
37static Shader *stdshader = NULL;
38
39/*
40 * Scene implementation class
41 */
42
43class SceneData
44{
45    friend class Scene;
46
47private:
48    static int Compare(void const *p1, void const *p2)
49    {
50        Tile const *t1 = (Tile const *)p1;
51        Tile const *t2 = (Tile const *)p2;
52
53        return t2->prio - t1->prio;
54    }
55
56    mat4 model_matrix;
57
58    Tile *tiles;
59    int ntiles;
60    float angle;
61
62#if defined HAVE_GL_2X
63    GLuint vao;
64#endif
65    GLuint *bufs;
66    int nbufs;
67
68    static Scene *scene;
69};
70
71Scene *SceneData::scene = NULL;
72
73/*
74 * Public Scene class
75 */
76
77Scene::Scene(float angle)
78  : data(new SceneData())
79{
80    data->tiles = 0;
81    data->ntiles = 0;
82    data->angle = angle;
83
84    data->bufs = 0;
85    data->nbufs = 0;
86
87#if defined HAVE_GL_2X
88    glGenVertexArrays(1, &data->vao);
89#endif
90}
91
92Scene::~Scene()
93{
94    /* FIXME: this must be done while the GL context is still active.
95     * Change the code architecture to make sure of that. */
96    /* XXX: The test is necessary because of a crash with PSGL. */
97    if (data->nbufs > 0)
98        glDeleteBuffers(data->nbufs, data->bufs);
99#if defined HAVE_GL_2X
100    glDeleteVertexArrays(1, &data->vao);
101#endif
102    free(data->bufs);
103    delete data;
104}
105
106Scene *Scene::GetDefault()
107{
108    if (!SceneData::scene)
109        SceneData::scene = new Scene(0.0f);
110    return SceneData::scene;
111}
112
113void Scene::Reset()
114{
115    if (SceneData::scene)
116        delete SceneData::scene;
117    SceneData::scene = NULL;
118}
119
120void Scene::AddTile(TileSet *tileset, int id, vec3i pos, int o)
121{
122    if ((data->ntiles % 1024) == 0)
123        data->tiles = (Tile *)realloc(data->tiles,
124                                      (data->ntiles + 1024) * sizeof(Tile));
125    /* FIXME: this sorting only works for a 45-degree camera */
126    data->tiles[data->ntiles].prio = -pos.y - 2 * 32 * pos.z + (o ? 0 : 32);
127    data->tiles[data->ntiles].tileset = tileset;
128    data->tiles[data->ntiles].id = id;
129    data->tiles[data->ntiles].pos = pos;
130    data->tiles[data->ntiles].o = o;
131    data->ntiles++;
132}
133
134void Scene::Render() // XXX: rename to Blit()
135{
136    if (!stdshader)
137    {
138        stdshader = Shader::Create(
139            "#version 130\n"
140            "\n"
141#if defined HAVE_GLES_2X
142            "attribute vec3 in_Position;\n"
143            "attribute vec2 in_TexCoord;\n"
144            "varying vec2 pass_TexCoord;\n"
145#else
146            "in vec3 in_Position;\n"
147            "in vec2 in_TexCoord;\n"
148#endif
149            "uniform mat4 proj_matrix;\n"
150            "uniform mat4 view_matrix;\n"
151            "uniform mat4 model_matrix;\n"
152            "\n"
153            "void main()\n"
154            "{\n"
155            "    gl_Position = proj_matrix * view_matrix * model_matrix"
156            "                * vec4(in_Position, 1.0);\n"
157#if defined HAVE_GLES_2X
158            "    pass_TexCoord = in_TexCoord;\n"
159#else
160            "    gl_TexCoord[0] = vec4(in_TexCoord, 0.0, 0.0);\n"
161#endif
162            "}\n",
163
164            "#version 130\n"
165            "\n"
166            "uniform sampler2D in_Texture;\n"
167#if defined HAVE_GLES_2X
168            "varying vec2 pass_TexCoord;\n"
169#endif
170            "\n"
171            "void main()\n"
172            "{\n"
173#if defined HAVE_GLES_2X
174            "    vec4 col = texture2D(in_Texture, pass_TexCoord);\n"
175            //"    vec4 col = vec4(0.5, 1.0, 0.0, 0.5);\n"
176            //"    vec4 col = vec4(pass_TexCoord * 4.0, 0.0, 0.25);\n"
177#else
178            "    vec4 col = texture2D(in_Texture, vec2(gl_TexCoord[0]));\n"
179#endif
180#if 0
181            "    float mul = 2.0;\n"
182#if 0
183            "    vec2 d1 = mod(vec2(gl_FragCoord), vec2(2.0, 2.0));\n"
184            "    float t1 = mod(3.0 * d1.x + 2.0 * d1.y, 4.0);\n"
185            "    float dx2 = mod(floor(gl_FragCoord.x * 0.5), 2.0);\n"
186            "    float dy2 = mod(floor(gl_FragCoord.y * 0.5), 2.0);\n"
187            "    float t2 = mod(3.0 * dx2 + 2.0 * dy2, 4.0);\n"
188            "    float dx3 = mod(floor(gl_FragCoord.x * 0.25), 2.0);\n"
189            "    float dy3 = mod(floor(gl_FragCoord.y * 0.25), 2.0);\n"
190            "    float t3 = mod(3.0 * dx3 + 2.0 * dy3, 4.0);\n"
191            "    float t1 = (1.0 + 16.0 * t1 + 4.0 * t2 + t3) / 65.0;\n"
192            "    float t2 = t1;\n"
193            "    float t3 = t1;\n"
194#else
195            "    float rand = sin(gl_FragCoord.x * 1.23456) * 123.456\n"
196            "               + cos(gl_FragCoord.y * 2.34567) * 789.012;\n"
197            "    float t1 = mod(sin(rand) * 17.13043, 1.0);\n"
198            "    float t2 = mod(sin(rand) * 27.13043, 1.0);\n"
199            "    float t3 = mod(sin(rand) * 37.13043, 1.0);\n"
200#endif
201            "    float fracx = fract(col.x * mul);\n"
202            "    float fracy = fract(col.y * mul);\n"
203            "    float fracz = fract(col.z * mul);\n"
204            "    fracx = fracx > t1 ? 1.0 : 0.0;\n"
205            "    fracy = fracy > t2 ? 1.0 : 0.0;\n"
206            "    fracz = fracz > t3 ? 1.0 : 0.0;\n"
207            "    col.x = (floor(col.x * mul) + fracx) / mul;\n"
208            "    col.y = (floor(col.y * mul) + fracy) / mul;\n"
209            "    col.z = (floor(col.z * mul) + fracz) / mul;\n"
210#endif
211            "    gl_FragColor = col;\n"
212            "}\n");
213    }
214
215#if 0
216    // Randomise, then sort.
217    for (int i = 0; i < data->ntiles; i++)
218    {
219        Tile tmp = data->tiles[i];
220        int j = rand() % data->ntiles;
221        data->tiles[i] = data->tiles[j];
222        data->tiles[j] = tmp;
223    }
224#endif
225    qsort(data->tiles, data->ntiles, sizeof(Tile), SceneData::Compare);
226
227    // XXX: debug stuff
228    data->model_matrix = mat4::translate(320.0f, 240.0f, 0.0f);
229    data->model_matrix *= mat4::rotate(-data->angle, 1.0f, 0.0f, 0.0f);
230#if 0
231    static float f = 0.0f;
232    f += 0.01f;
233    data->model_matrix *= mat4::rotate(0.1f * sinf(f), 1.0f, 0.0f, 0.0f);
234    data->model_matrix *= mat4::rotate(0.3f * cosf(f), 0.0f, 0.0f, 1.0f);
235#endif
236    data->model_matrix *= mat4::translate(-320.0f, -240.0f, 0.0f);
237    // XXX: end of debug stuff
238
239    GLuint uni_mat, uni_tex, attr_pos, attr_tex;
240    attr_pos = stdshader->GetAttribLocation("in_Position");
241    attr_tex = stdshader->GetAttribLocation("in_TexCoord");
242
243#if !defined __CELLOS_LV2__ // Use cgGetNamedParameter etc.
244    stdshader->Bind();
245
246    uni_mat = stdshader->GetUniformLocation("proj_matrix");
247    glUniformMatrix4fv(uni_mat, 1, GL_FALSE, &Video::GetProjMatrix()[0][0]);
248    uni_mat = stdshader->GetUniformLocation("view_matrix");
249    glUniformMatrix4fv(uni_mat, 1, GL_FALSE, &Video::GetViewMatrix()[0][0]);
250    uni_mat = stdshader->GetUniformLocation("model_matrix");
251    glUniformMatrix4fv(uni_mat, 1, GL_FALSE, &data->model_matrix[0][0]);
252
253    uni_tex = stdshader->GetUniformLocation("in_Texture");
254    glUniform1i(uni_tex, 0);
255#endif
256
257    glEnable(GL_TEXTURE_2D);
258    glEnable(GL_DEPTH_TEST);
259    glDepthFunc(GL_LEQUAL);
260#if defined HAVE_GL_2X
261    glEnable(GL_ALPHA_TEST);
262    glAlphaFunc(GL_GEQUAL, 0.01f);
263#endif
264    glEnable(GL_BLEND);
265    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
266
267    for (int buf = 0, i = 0, n; i < data->ntiles; i = n, buf += 2)
268    {
269        /* Generate new vertex / texture coord buffers if necessary */
270        if (buf + 2 > data->nbufs)
271        {
272            data->bufs = (GLuint *)realloc(data->bufs, (buf + 2) * sizeof(GLuint));
273            glGenBuffers(buf + 2 - data->nbufs, data->bufs + data->nbufs);
274            data->nbufs = buf + 2;
275        }
276
277        /* Count how many quads will be needed */
278        for (n = i + 1; n < data->ntiles; n++)
279            if (data->tiles[i].tileset != data->tiles[n].tileset)
280                break;
281
282        /* Create a vertex array object */
283        float *vertex = (float *)malloc(6 * 3 * (n - i) * sizeof(float));
284        float *texture = (float *)malloc(6 * 2 * (n - i) * sizeof(float));
285
286        for (int j = i; j < n; j++)
287        {
288            data->tiles[i].tileset->BlitTile(data->tiles[j].id,
289                            data->tiles[j].pos, data->tiles[j].o,
290                            vertex + 18 * (j - i), texture + 12 * (j - i));
291        }
292
293        stdshader->Bind();
294
295        /* Bind texture */
296        data->tiles[i].tileset->Bind();
297
298        /* Bind vertex, color and texture coordinate buffers */
299#if defined HAVE_GL_2X
300        glBindVertexArray(data->vao);
301#endif
302#if !defined __CELLOS_LV2__ // Use cgGLEnableClientState etc.
303        glEnableVertexAttribArray(attr_pos);
304        glEnableVertexAttribArray(attr_tex);
305
306        glBindBuffer(GL_ARRAY_BUFFER, data->bufs[buf]);
307        glBufferData(GL_ARRAY_BUFFER, 18 * (n - i) * sizeof(GLfloat),
308                     vertex, GL_STATIC_DRAW);
309        glVertexAttribPointer(attr_pos, 3, GL_FLOAT, GL_FALSE, 0, 0);
310
311        glBindBuffer(GL_ARRAY_BUFFER, data->bufs[buf + 1]);
312        glBufferData(GL_ARRAY_BUFFER, 12 * (n - i) * sizeof(GLfloat),
313                     texture, GL_STATIC_DRAW);
314        glVertexAttribPointer(attr_tex, 2, GL_FLOAT, GL_FALSE, 0, 0);
315#endif
316
317        /* Draw arrays */
318        glDrawArrays(GL_TRIANGLES, 0, (n - i) * 6);
319
320#if defined HAVE_GL_2X
321        glBindVertexArray(0);
322#endif
323#if !defined __CELLOS_LV2__ // Use cgGLEnableClientState etc.
324        glDisableVertexAttribArray(attr_pos);
325        glDisableVertexAttribArray(attr_tex);
326#endif
327
328        free(vertex);
329        free(texture);
330    }
331
332    free(data->tiles);
333    data->tiles = 0;
334    data->ntiles = 0;
335}
336
337} /* namespace lol */
338
Note: See TracBrowser for help on using the repository browser.