source: trunk/src/scene.cpp @ 656

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

More shader crap. Texture coordinates now work. Still disabled.

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