source: trunk/src/scene.cpp @ 745

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

video: enforce shader-enabled GL implementations, getting rid of a lot
of code.

  • Property svn:keywords set to Id
File size: 6.1 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    uint32_t prio, code;
32    int x, y, z, o;
33};
34
35extern Shader *stdshader;
36extern mat4 model_matrix;
37
38/*
39 * Scene implementation class
40 */
41
42class SceneData
43{
44    friend class Scene;
45
46private:
47    static int Compare(void const *p1, void const *p2)
48    {
49        Tile const *t1 = (Tile const *)p1;
50        Tile const *t2 = (Tile const *)p2;
51
52        return t2->prio - t1->prio;
53    }
54
55    Tile *tiles;
56    int ntiles;
57    float angle;
58
59#if defined HAVE_GL_2X
60    GLuint vao;
61#endif
62    GLuint *bufs;
63    int nbufs;
64
65    static Scene *scene;
66};
67
68Scene *SceneData::scene = NULL;
69
70/*
71 * Public Scene class
72 */
73
74Scene::Scene(float angle)
75  : data(new SceneData())
76{
77    data->tiles = 0;
78    data->ntiles = 0;
79    data->angle = angle;
80
81    data->bufs = 0;
82    data->nbufs = 0;
83
84#if defined HAVE_GL_2X
85    glGenVertexArrays(1, &data->vao);
86#endif
87}
88
89Scene::~Scene()
90{
91    /* FIXME: this must be done while the GL context is still active.
92     * Change the architecture to make sure of that. */
93    glDeleteBuffers(data->nbufs, data->bufs);
94#if defined HAVE_GL_2X
95    glDeleteVertexArrays(1, &data->vao);
96#endif
97    free(data->bufs);
98    delete data;
99}
100
101Scene *Scene::GetDefault()
102{
103    if (!SceneData::scene)
104        SceneData::scene = new Scene(0.0f);
105    return SceneData::scene;
106}
107
108void Scene::Reset()
109{
110    if (SceneData::scene)
111        delete SceneData::scene;
112    SceneData::scene = NULL;
113}
114
115void Scene::AddTile(uint32_t code, int x, int y, int z, int o)
116{
117    if ((data->ntiles % 1024) == 0)
118        data->tiles = (Tile *)realloc(data->tiles,
119                                      (data->ntiles + 1024) * sizeof(Tile));
120    /* FIXME: this sorting only works for a 45-degree camera */
121    data->tiles[data->ntiles].prio = -y - 2 * 32 * z + (o ? 0 : 32);
122    data->tiles[data->ntiles].code = code;
123    data->tiles[data->ntiles].x = x;
124    data->tiles[data->ntiles].y = y;
125    data->tiles[data->ntiles].z = z;
126    data->tiles[data->ntiles].o = o;
127    data->ntiles++;
128}
129
130void Scene::Render() // XXX: rename to Blit()
131{
132#if 0
133    // Randomise, then sort.
134    for (int i = 0; i < data->ntiles; i++)
135    {
136        Tile tmp = data->tiles[i];
137        int j = rand() % data->ntiles;
138        data->tiles[i] = data->tiles[j];
139        data->tiles[j] = tmp;
140    }
141#endif
142    qsort(data->tiles, data->ntiles, sizeof(Tile), SceneData::Compare);
143
144    // XXX: debug stuff
145    model_matrix = mat4::translate(320.0f, 240.0f, 0.0f);
146    model_matrix *= mat4::rotate(-data->angle, 1.0f, 0.0f, 0.0f);
147#if 0
148    static float f = 0.0f;
149    f += 0.01f;
150    model_matrix *= mat4::rotate(0.1f * sinf(f), 1.0f, 0.0f, 0.0f);
151    model_matrix *= mat4::rotate(0.3f * cosf(f), 0.0f, 0.0f, 1.0f);
152#endif
153    model_matrix *= mat4::translate(-320.0f, -240.0f, 0.0f);
154    // XXX: end of debug stuff
155
156    GLuint uni_mat, uni_tex, attr_pos, attr_tex;
157    attr_pos = stdshader->GetAttribLocation("in_Position");
158    attr_tex = stdshader->GetAttribLocation("in_TexCoord");
159
160    stdshader->Bind();
161    uni_mat = stdshader->GetUniformLocation("model_matrix");
162    glUniformMatrix4fv(uni_mat, 1, GL_FALSE, &model_matrix[0][0]);
163    uni_tex = stdshader->GetUniformLocation("in_Texture");
164    glUniform1i(uni_tex, 0);
165
166    glEnable(GL_TEXTURE_2D);
167    glEnable(GL_DEPTH_TEST);
168    glDepthFunc(GL_LEQUAL);
169#if defined HAVE_GL_2X
170    glEnable(GL_ALPHA_TEST);
171    glAlphaFunc(GL_GEQUAL, 0.01f);
172#endif
173    glEnable(GL_BLEND);
174    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
175
176    for (int buf = 0, i = 0, n; i < data->ntiles; i = n, buf += 2)
177    {
178        /* Generate new vertex / texture coord buffers if necessary */
179        if (buf + 2 > data->nbufs)
180        {
181            data->bufs = (GLuint *)realloc(data->bufs, (buf + 2) * sizeof(GLuint));
182            glGenBuffers(buf + 2 - data->nbufs, data->bufs + data->nbufs);
183            data->nbufs = buf + 2;
184        }
185
186        /* Count how many quads will be needed */
187        for (n = i + 1; n < data->ntiles; n++)
188            if (data->tiles[i].code >> 16 != data->tiles[n].code >> 16)
189                break;
190
191        /* Create a vertex array object */
192        float *vertex = (float *)malloc(6 * 3 * (n - i) * sizeof(float));
193        float *texture = (float *)malloc(6 * 2 * (n - i) * sizeof(float));
194
195        for (int j = i; j < n; j++)
196        {
197            Tiler::BlitTile(data->tiles[j].code, data->tiles[j].x,
198                            data->tiles[j].y, data->tiles[j].z, data->tiles[j].o,
199                            vertex + 18 * (j - i), texture + 12 * (j - i));
200        }
201
202        stdshader->Bind();
203
204        /* Bind texture */
205        Tiler::Bind(data->tiles[i].code);
206
207        /* Bind vertex, color and texture coordinate buffers */
208#if defined HAVE_GL_2X
209        glBindVertexArray(data->vao);
210#endif
211        glEnableVertexAttribArray(attr_pos);
212        glEnableVertexAttribArray(attr_tex);
213
214        glBindBuffer(GL_ARRAY_BUFFER, data->bufs[buf]);
215        glBufferData(GL_ARRAY_BUFFER, 18 * (n - i) * sizeof(GLfloat),
216                     vertex, GL_STATIC_DRAW);
217        glVertexAttribPointer(attr_pos, 3, GL_FLOAT, GL_FALSE, 0, 0);
218
219        glBindBuffer(GL_ARRAY_BUFFER, data->bufs[buf + 1]);
220        glBufferData(GL_ARRAY_BUFFER, 12 * (n - i) * sizeof(GLfloat),
221                     texture, GL_STATIC_DRAW);
222        glVertexAttribPointer(attr_tex, 2, GL_FLOAT, GL_FALSE, 0, 0);
223
224        /* Draw arrays */
225        glDrawArrays(GL_TRIANGLES, 0, (n - i) * 6);
226
227#if defined HAVE_GL_2X
228        glBindVertexArray(0);
229#endif
230        glDisableVertexAttribArray(attr_pos);
231        glDisableVertexAttribArray(attr_tex);
232
233        free(vertex);
234        free(texture);
235    }
236
237    free(data->tiles);
238    data->tiles = 0;
239    data->ntiles = 0;
240}
241
242} /* namespace lol */
243
Note: See TracBrowser for help on using the repository browser.