source: trunk/src/scene.cpp @ 792

Last change on this file since 792 was 792, checked in by sam, 9 years ago

tiler: ensure AddTile() and BlitTile() accept vec3i arguments.

  • Property svn:keywords set to Id
File size: 9.6 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 architecture to make sure of that. */
96    glDeleteBuffers(data->nbufs, data->bufs);
97#if defined HAVE_GL_2X
98    glDeleteVertexArrays(1, &data->vao);
99#endif
100    free(data->bufs);
101    delete data;
102}
103
104Scene *Scene::GetDefault()
105{
106    if (!SceneData::scene)
107        SceneData::scene = new Scene(0.0f);
108    return SceneData::scene;
109}
110
111void Scene::Reset()
112{
113    if (SceneData::scene)
114        delete SceneData::scene;
115    SceneData::scene = NULL;
116}
117
118void Scene::AddTile(TileSet *tileset, int id, vec3i pos, int o)
119{
120    if ((data->ntiles % 1024) == 0)
121        data->tiles = (Tile *)realloc(data->tiles,
122                                      (data->ntiles + 1024) * sizeof(Tile));
123    /* FIXME: this sorting only works for a 45-degree camera */
124    data->tiles[data->ntiles].prio = -pos.y - 2 * 32 * pos.z + (o ? 0 : 32);
125    data->tiles[data->ntiles].tileset = tileset;
126    data->tiles[data->ntiles].id = id;
127    data->tiles[data->ntiles].pos = pos;
128    data->tiles[data->ntiles].o = o;
129    data->ntiles++;
130}
131
132void Scene::Render() // XXX: rename to Blit()
133{
134    if (!stdshader)
135    {
136        stdshader = Shader::Create(
137            "#version 130\n"
138            "\n"
139#if defined HAVE_GLES_2X
140            "attribute vec3 in_Position;\n"
141            "attribute vec2 in_TexCoord;\n"
142            "varying vec2 pass_TexCoord;\n"
143#else
144            "in vec3 in_Position;\n"
145            "in vec2 in_TexCoord;\n"
146#endif
147            "uniform mat4 proj_matrix;\n"
148            "uniform mat4 view_matrix;\n"
149            "uniform mat4 model_matrix;\n"
150            "\n"
151            "void main()\n"
152            "{\n"
153            "    gl_Position = proj_matrix * view_matrix * model_matrix"
154            "                * vec4(in_Position, 1.0);\n"
155#if defined HAVE_GLES_2X
156            "    pass_TexCoord = in_TexCoord;\n"
157#else
158            "    gl_TexCoord[0] = vec4(in_TexCoord, 0.0, 0.0);\n"
159#endif
160            "}\n",
161
162            "#version 130\n"
163            "\n"
164            "uniform sampler2D in_Texture;\n"
165#if defined HAVE_GLES_2X
166            "varying vec2 pass_TexCoord;\n"
167#endif
168            "\n"
169            "void main()\n"
170            "{\n"
171#if defined HAVE_GLES_2X
172            "    vec4 col = texture2D(in_Texture, pass_TexCoord);\n"
173            //"    vec4 col = vec4(0.5, 1.0, 0.0, 0.5);\n"
174            //"    vec4 col = vec4(pass_TexCoord * 4.0, 0.0, 0.25);\n"
175#else
176            "    vec4 col = texture2D(in_Texture, vec2(gl_TexCoord[0]));\n"
177#endif
178#if 0
179            "    float mul = 2.0;\n"
180#if 0
181            "    vec2 d1 = mod(vec2(gl_FragCoord), vec2(2.0, 2.0));\n"
182            "    float t1 = mod(3.0 * d1.x + 2.0 * d1.y, 4.0);\n"
183            "    float dx2 = mod(floor(gl_FragCoord.x * 0.5), 2.0);\n"
184            "    float dy2 = mod(floor(gl_FragCoord.y * 0.5), 2.0);\n"
185            "    float t2 = mod(3.0 * dx2 + 2.0 * dy2, 4.0);\n"
186            "    float dx3 = mod(floor(gl_FragCoord.x * 0.25), 2.0);\n"
187            "    float dy3 = mod(floor(gl_FragCoord.y * 0.25), 2.0);\n"
188            "    float t3 = mod(3.0 * dx3 + 2.0 * dy3, 4.0);\n"
189            "    float t1 = (1.0 + 16.0 * t1 + 4.0 * t2 + t3) / 65.0;\n"
190            "    float t2 = t1;\n"
191            "    float t3 = t1;\n"
192#else
193            "    float rand = sin(gl_FragCoord.x * 1.23456) * 123.456\n"
194            "               + cos(gl_FragCoord.y * 2.34567) * 789.012;\n"
195            "    float t1 = mod(sin(rand) * 17.13043, 1.0);\n"
196            "    float t2 = mod(sin(rand) * 27.13043, 1.0);\n"
197            "    float t3 = mod(sin(rand) * 37.13043, 1.0);\n"
198#endif
199            "    float fracx = fract(col.x * mul);\n"
200            "    float fracy = fract(col.y * mul);\n"
201            "    float fracz = fract(col.z * mul);\n"
202            "    fracx = fracx > t1 ? 1.0 : 0.0;\n"
203            "    fracy = fracy > t2 ? 1.0 : 0.0;\n"
204            "    fracz = fracz > t3 ? 1.0 : 0.0;\n"
205            "    col.x = (floor(col.x * mul) + fracx) / mul;\n"
206            "    col.y = (floor(col.y * mul) + fracy) / mul;\n"
207            "    col.z = (floor(col.z * mul) + fracz) / mul;\n"
208#endif
209            "    gl_FragColor = col;\n"
210            "}\n");
211    }
212
213#if 0
214    // Randomise, then sort.
215    for (int i = 0; i < data->ntiles; i++)
216    {
217        Tile tmp = data->tiles[i];
218        int j = rand() % data->ntiles;
219        data->tiles[i] = data->tiles[j];
220        data->tiles[j] = tmp;
221    }
222#endif
223    qsort(data->tiles, data->ntiles, sizeof(Tile), SceneData::Compare);
224
225    // XXX: debug stuff
226    data->model_matrix = mat4::translate(320.0f, 240.0f, 0.0f);
227    data->model_matrix *= mat4::rotate(-data->angle, 1.0f, 0.0f, 0.0f);
228#if 0
229    static float f = 0.0f;
230    f += 0.01f;
231    data->model_matrix *= mat4::rotate(0.1f * sinf(f), 1.0f, 0.0f, 0.0f);
232    data->model_matrix *= mat4::rotate(0.3f * cosf(f), 0.0f, 0.0f, 1.0f);
233#endif
234    data->model_matrix *= mat4::translate(-320.0f, -240.0f, 0.0f);
235    // XXX: end of debug stuff
236
237    GLuint uni_mat, uni_tex, attr_pos, attr_tex;
238    attr_pos = stdshader->GetAttribLocation("in_Position");
239    attr_tex = stdshader->GetAttribLocation("in_TexCoord");
240
241#if !defined __CELLOS_LV2__ // Use cgGetNamedParameter etc.
242    stdshader->Bind();
243
244    uni_mat = stdshader->GetUniformLocation("proj_matrix");
245    glUniformMatrix4fv(uni_mat, 1, GL_FALSE, &Video::GetProjMatrix()[0][0]);
246    uni_mat = stdshader->GetUniformLocation("view_matrix");
247    glUniformMatrix4fv(uni_mat, 1, GL_FALSE, &Video::GetViewMatrix()[0][0]);
248    uni_mat = stdshader->GetUniformLocation("model_matrix");
249    glUniformMatrix4fv(uni_mat, 1, GL_FALSE, &data->model_matrix[0][0]);
250
251    uni_tex = stdshader->GetUniformLocation("in_Texture");
252    glUniform1i(uni_tex, 0);
253#endif
254
255    glEnable(GL_TEXTURE_2D);
256    glEnable(GL_DEPTH_TEST);
257    glDepthFunc(GL_LEQUAL);
258#if defined HAVE_GL_2X
259    glEnable(GL_ALPHA_TEST);
260    glAlphaFunc(GL_GEQUAL, 0.01f);
261#endif
262    glEnable(GL_BLEND);
263    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
264
265    for (int buf = 0, i = 0, n; i < data->ntiles; i = n, buf += 2)
266    {
267        /* Generate new vertex / texture coord buffers if necessary */
268        if (buf + 2 > data->nbufs)
269        {
270            data->bufs = (GLuint *)realloc(data->bufs, (buf + 2) * sizeof(GLuint));
271            glGenBuffers(buf + 2 - data->nbufs, data->bufs + data->nbufs);
272            data->nbufs = buf + 2;
273        }
274
275        /* Count how many quads will be needed */
276        for (n = i + 1; n < data->ntiles; n++)
277            if (data->tiles[i].tileset != data->tiles[n].tileset)
278                break;
279
280        /* Create a vertex array object */
281        float *vertex = (float *)malloc(6 * 3 * (n - i) * sizeof(float));
282        float *texture = (float *)malloc(6 * 2 * (n - i) * sizeof(float));
283
284        for (int j = i; j < n; j++)
285        {
286            data->tiles[i].tileset->BlitTile(data->tiles[j].id,
287                            data->tiles[j].pos, data->tiles[j].o,
288                            vertex + 18 * (j - i), texture + 12 * (j - i));
289        }
290
291        stdshader->Bind();
292
293        /* Bind texture */
294        data->tiles[i].tileset->Bind();
295
296        /* Bind vertex, color and texture coordinate buffers */
297#if defined HAVE_GL_2X
298        glBindVertexArray(data->vao);
299#endif
300#if !defined __CELLOS_LV2__ // Use cgGLEnableClientState etc.
301        glEnableVertexAttribArray(attr_pos);
302        glEnableVertexAttribArray(attr_tex);
303
304        glBindBuffer(GL_ARRAY_BUFFER, data->bufs[buf]);
305        glBufferData(GL_ARRAY_BUFFER, 18 * (n - i) * sizeof(GLfloat),
306                     vertex, GL_STATIC_DRAW);
307        glVertexAttribPointer(attr_pos, 3, GL_FLOAT, GL_FALSE, 0, 0);
308
309        glBindBuffer(GL_ARRAY_BUFFER, data->bufs[buf + 1]);
310        glBufferData(GL_ARRAY_BUFFER, 12 * (n - i) * sizeof(GLfloat),
311                     texture, GL_STATIC_DRAW);
312        glVertexAttribPointer(attr_tex, 2, GL_FLOAT, GL_FALSE, 0, 0);
313#endif
314
315        /* Draw arrays */
316        glDrawArrays(GL_TRIANGLES, 0, (n - i) * 6);
317
318#if defined HAVE_GL_2X
319        glBindVertexArray(0);
320#endif
321#if !defined __CELLOS_LV2__ // Use cgGLEnableClientState etc.
322        glDisableVertexAttribArray(attr_pos);
323        glDisableVertexAttribArray(attr_tex);
324#endif
325
326        free(vertex);
327        free(texture);
328    }
329
330    free(data->tiles);
331    data->tiles = 0;
332    data->ntiles = 0;
333}
334
335} /* namespace lol */
336
Note: See TracBrowser for help on using the repository browser.