source: trunk/src/scene.cpp @ 657

Last change on this file since 657 was 657, checked in by sam, 11 years ago

Clean up stuff in the shader crap.

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