source: trunk/src/scene.cpp @ 728

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

gl: reactivate depth test and blending on GLES 1.x.

  • Property svn:keywords set to Id
File size: 7.8 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 <cstdio>
17#include <cmath>
18
19#ifdef WIN32
20#   define WIN32_LEAN_AND_MEAN
21#   include <windows.h>
22#endif
23
24#include "core.h"
25#include "lolgl.h"
26
27namespace lol
28{
29
30struct Tile
31{
32    uint32_t prio, code;
33    int x, y, z, o;
34};
35
36#if defined HAVE_GL_2X || defined HAVE_GLES_2X
37extern Shader *stdshader;
38#endif
39extern mat4 model_matrix;
40
41/*
42 * Scene implementation class
43 */
44
45class SceneData
46{
47    friend class Scene;
48
49private:
50    static int Compare(void const *p1, void const *p2)
51    {
52        Tile const *t1 = (Tile const *)p1;
53        Tile const *t2 = (Tile const *)p2;
54
55        return t2->prio - t1->prio;
56    }
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(uint32_t code, int x, int y, int z, 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 = -y - 2 * 32 * z + (o ? 0 : 32);
125    data->tiles[data->ntiles].code = code;
126    data->tiles[data->ntiles].x = x;
127    data->tiles[data->ntiles].y = y;
128    data->tiles[data->ntiles].z = z;
129    data->tiles[data->ntiles].o = o;
130    data->ntiles++;
131}
132
133void Scene::Render() // XXX: rename to Blit()
134{
135#if 0
136    // Randomise, then sort.
137    for (int i = 0; i < data->ntiles; i++)
138    {
139        Tile tmp = data->tiles[i];
140        int j = rand() % data->ntiles;
141        data->tiles[i] = data->tiles[j];
142        data->tiles[j] = tmp;
143    }
144#endif
145    qsort(data->tiles, data->ntiles, sizeof(Tile), SceneData::Compare);
146
147    // XXX: debug stuff
148    model_matrix = mat4::translate(320.0f, 240.0f, 0.0f);
149    model_matrix *= mat4::rotate(-data->angle, 1.0f, 0.0f, 0.0f);
150#if 0
151    static float f = 0.0f;
152    f += 0.01f;
153    model_matrix *= mat4::rotate(0.1f * sinf(f), 1.0f, 0.0f, 0.0f);
154    model_matrix *= mat4::rotate(0.3f * cosf(f), 0.0f, 0.0f, 1.0f);
155#endif
156    model_matrix *= mat4::translate(-320.0f, -240.0f, 0.0f);
157    // XXX: end of debug stuff
158
159#if defined HAVE_GL_2X || defined HAVE_GLES_2X
160    GLuint uni_mat, uni_tex, attr_pos, attr_tex;
161    attr_pos = stdshader->GetAttribLocation("in_Position");
162    attr_tex = stdshader->GetAttribLocation("in_TexCoord");
163
164    stdshader->Bind();
165    uni_mat = stdshader->GetUniformLocation("model_matrix");
166    glUniformMatrix4fv(uni_mat, 1, GL_FALSE, &model_matrix[0][0]);
167    uni_tex = stdshader->GetUniformLocation("in_Texture");
168    glUniform1i(uni_tex, 0);
169
170    glEnable(GL_TEXTURE_2D);
171    glEnable(GL_DEPTH_TEST);
172    glDepthFunc(GL_LEQUAL);
173#   if !defined HAVE_GLES_2X
174    glEnable(GL_ALPHA_TEST);
175    glAlphaFunc(GL_GEQUAL, 0.01f);
176#   endif
177    glEnable(GL_BLEND);
178    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
179#else
180    glEnable(GL_DEPTH_TEST);
181    glDepthFunc(GL_LEQUAL);
182    glEnable(GL_ALPHA_TEST);
183    glAlphaFunc(GL_GEQUAL, 0.01f);
184    glEnable(GL_BLEND);
185    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
186
187    glMatrixMode(GL_MODELVIEW);
188    glPushMatrix();
189    glLoadIdentity();
190    glMultMatrixf(&model_matrix[0][0]);
191
192    /* Set up state machine */
193    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
194    glEnable(GL_TEXTURE_2D);
195    glEnableClientState(GL_VERTEX_ARRAY);
196#endif
197
198    for (int buf = 0, i = 0, n; i < data->ntiles; i = n, buf += 2)
199    {
200        /* Generate new vertex / texture coord buffers if necessary */
201        if (buf + 2 > data->nbufs)
202        {
203            data->bufs = (GLuint *)realloc(data->bufs, (buf + 2) * sizeof(GLuint));
204            glGenBuffers(buf + 2 - data->nbufs, data->bufs + data->nbufs);
205            data->nbufs = buf + 2;
206        }
207
208        /* Count how many quads will be needed */
209        for (n = i + 1; n < data->ntiles; n++)
210            if (data->tiles[i].code >> 16 != data->tiles[n].code >> 16)
211                break;
212
213        /* Create a vertex array object */
214        float *vertex = (float *)malloc(6 * 3 * (n - i) * sizeof(float));
215        float *texture = (float *)malloc(6 * 2 * (n - i) * sizeof(float));
216
217        for (int j = i; j < n; j++)
218        {
219            Tiler::BlitTile(data->tiles[j].code, data->tiles[j].x,
220                            data->tiles[j].y, data->tiles[j].z, data->tiles[j].o,
221                            vertex + 18 * (j - i), texture + 12 * (j - i));
222        }
223
224#if defined HAVE_GL_2X || defined HAVE_GLES_2X
225        stdshader->Bind();
226#endif
227
228        /* Bind texture */
229        Tiler::Bind(data->tiles[i].code);
230
231        /* Bind vertex, color and texture coordinate buffers */
232#if defined HAVE_GL_2X || defined HAVE_GLES_2X
233#   if !defined HAVE_GLES_2X
234        glBindVertexArray(data->vao);
235#   endif
236        glEnableVertexAttribArray(attr_pos);
237        glEnableVertexAttribArray(attr_tex);
238
239        glBindBuffer(GL_ARRAY_BUFFER, data->bufs[buf]);
240        glBufferData(GL_ARRAY_BUFFER, 18 * (n - i) * sizeof(GLfloat),
241                     vertex, GL_STATIC_DRAW);
242        glVertexAttribPointer(attr_pos, 3, GL_FLOAT, GL_FALSE, 0, 0);
243
244        glBindBuffer(GL_ARRAY_BUFFER, data->bufs[buf + 1]);
245        glBufferData(GL_ARRAY_BUFFER, 12 * (n - i) * sizeof(GLfloat),
246                     texture, GL_STATIC_DRAW);
247        glVertexAttribPointer(attr_tex, 2, GL_FLOAT, GL_FALSE, 0, 0);
248#elif defined HAVE_GL_1X
249        glBindBuffer(GL_ARRAY_BUFFER, data->bufs[buf]);
250        glBufferData(GL_ARRAY_BUFFER, 18 * (n - i) * sizeof(GLfloat),
251                     vertex, GL_STATIC_DRAW);
252        glVertexPointer(3, GL_FLOAT, 0, NULL);
253
254        glBindBuffer(GL_ARRAY_BUFFER, data->bufs[buf + 1]);
255        glBufferData(GL_ARRAY_BUFFER, 12 * (n - i) * sizeof(GLfloat),
256                     texture, GL_STATIC_DRAW);
257        glTexCoordPointer(2, GL_FLOAT, 0, NULL);
258#else
259        glVertexPointer(3, GL_FLOAT, 0, vertex);
260        glTexCoordPointer(2, GL_FLOAT, 0, texture);
261#endif
262
263        /* Draw arrays */
264        glDrawArrays(GL_TRIANGLES, 0, (n - i) * 6);
265
266#if defined HAVE_GL_2X || defined HAVE_GLES_2X
267#   if !defined HAVE_GLES_2X
268        glBindVertexArray(0);
269#   endif
270        glDisableVertexAttribArray(attr_pos);
271        glDisableVertexAttribArray(attr_tex);
272#endif
273
274        free(vertex);
275        free(texture);
276    }
277
278#if defined HAVE_GL_1X || defined HAVE_GLES_1X
279    /* Disable state machine features */
280    glDisableClientState(GL_VERTEX_ARRAY);
281    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
282
283    /* Restore matrices */
284    glMatrixMode(GL_PROJECTION);
285    glPopMatrix();
286    glMatrixMode(GL_MODELVIEW);
287    glPopMatrix();
288#endif
289
290    free(data->tiles);
291    data->tiles = 0;
292    data->ntiles = 0;
293}
294
295} /* namespace lol */
296
Note: See TracBrowser for help on using the repository browser.