source: trunk/src/scene.cpp @ 666

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

Experimental GL mode works!

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