source: trunk/src/scene.cpp @ 1963

Last change on this file since 1963 was 1963, checked in by sam, 7 years ago

gpu: add support for triangle strips and fans, and change the DrawElement
calls so that they take a number of vertices or indexes, not elements.

  • Property svn:keywords set to Id
File size: 6.9 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2012 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
17#ifdef WIN32
18#   define WIN32_LEAN_AND_MEAN
19#   include <windows.h>
20#endif
21
22#include "core.h"
23#include "lolgl.h"
24
25extern char const *lolfx_tile;
26
27namespace lol
28{
29
30struct Tile
31{
32    TileSet *tileset;
33    uint32_t prio;
34    vec3 pos;
35    vec2 scale;
36    int id, o;
37};
38
39/*
40 * Scene implementation class
41 */
42
43class SceneData
44{
45    friend class Scene;
46
47private:
48    static int Compare(void const *p1, void const *p2)
49    {
50        Tile const *t1 = (Tile const *)p1;
51        Tile const *t2 = (Tile const *)p2;
52
53        if (t1->pos.z > t2->pos.z)
54            return 1;
55        if (t1->pos.z < t2->pos.z)
56            return -1;
57        return 0;
58    }
59
60    mat4 m_model_matrix;
61    mat4 m_view_matrix;
62    mat4 m_proj_matrix;
63
64    Array<Tile> tiles;
65
66    Shader *m_shader;
67    VertexDeclaration *m_vdecl;
68    Array<VertexBuffer *> bufs;
69
70    static Scene *scene;
71};
72
73Scene *SceneData::scene = NULL;
74
75/*
76 * Public Scene class
77 */
78
79Scene::Scene()
80  : data(new SceneData())
81{
82    data->m_model_matrix = mat4(1.f);
83    data->m_view_matrix = mat4(1.f);
84    data->m_proj_matrix = mat4::ortho(0, Video::GetSize().x,
85                                      0, Video::GetSize().y, -1000.f, 1000.f);
86
87    data->m_shader = 0;
88    data->m_vdecl = new VertexDeclaration(VertexStream<vec3>(VertexUsage::Position),
89                                          VertexStream<vec2>(VertexUsage::TexCoord));
90}
91
92Scene::~Scene()
93{
94    /* FIXME: this must be done while the GL context is still active.
95     * Change the code architecture to make sure of that. */
96    /* FIXME: also, make sure we do not add code to Reset() that will
97     * reallocate stuff */
98    Reset();
99
100    delete data->m_vdecl;
101    delete data;
102}
103
104Scene *Scene::GetDefault()
105{
106    if (!SceneData::scene)
107        SceneData::scene = new Scene();
108    return SceneData::scene;
109}
110
111void Scene::Reset()
112{
113    for (int i = 0; i < data->bufs.Count(); i++)
114        delete data->bufs[i];
115    data->bufs.Empty();
116}
117
118void Scene::SetViewMatrix(mat4 const &m)
119{
120    data->m_view_matrix = m;
121}
122
123void Scene::SetProjMatrix(mat4 const &m)
124{
125    data->m_proj_matrix = m;
126}
127
128mat4 const &Scene::GetViewMatrix(void)
129{
130    return data->m_view_matrix;
131}
132
133mat4 const &Scene::GetProjMatrix(void)
134{
135    return data->m_proj_matrix;
136}
137
138void Scene::AddTile(TileSet *tileset, int id, vec3 pos, int o, vec2 scale)
139{
140    Tile t;
141    /* FIXME: this sorting only works for a 45-degree camera */
142    t.prio = -pos.y - 2 * 32 * pos.z + (o ? 0 : 32);
143    t.tileset = tileset;
144    t.id = id;
145    t.pos = pos;
146    t.o = o;
147    t.scale = scale;
148
149    data->tiles.Push(t);
150}
151
152void Scene::Render() // XXX: rename to Blit()
153{
154    /* Early exit if nothing needs to be rendered */
155    if (!data->tiles.Count())
156        return;
157
158    if (!data->m_shader)
159        data->m_shader = Shader::Create(lolfx_tile);
160
161#if 0
162    // Randomise, then sort.
163    for (int i = 0; i < data->tiles.Count(); i++)
164    {
165        Tile tmp = data->tiles[i];
166        int j = rand() % data->tiles.Count();
167        data->tiles[i] = data->tiles[j];
168        data->tiles[j] = tmp;
169    }
170#endif
171    qsort(&data->tiles[0], data->tiles.Count(),
172          sizeof(Tile), SceneData::Compare);
173
174    // XXX: debug stuff
175    data->m_model_matrix = mat4::translate(320.0f, 240.0f, 0.0f);
176#if 0
177    static float f = 0.0f;
178    f += 0.01f;
179    data->m_model_matrix *= mat4::rotate(6.0f * sinf(f), 1.0f, 0.0f, 0.0f);
180    data->m_model_matrix *= mat4::rotate(17.0f * cosf(f), 0.0f, 0.0f, 1.0f);
181#endif
182    data->m_model_matrix *= mat4::translate(-320.0f, -240.0f, 0.0f);
183#if __ANDROID__
184    data->m_model_matrix = mat4::scale(1280.0f / 640,
185                                       736.0f / 480,
186                                       1.0f) * data->m_model_matrix;
187#endif
188    // XXX: end of debug stuff
189
190    ShaderUniform uni_mat, uni_tex;
191    ShaderAttrib attr_pos, attr_tex;
192    attr_pos = data->m_shader->GetAttribLocation("in_Position", VertexUsage::Position, 0);
193    attr_tex = data->m_shader->GetAttribLocation("in_TexCoord", VertexUsage::TexCoord, 0);
194
195    data->m_shader->Bind();
196
197    uni_mat = data->m_shader->GetUniformLocation("proj_matrix");
198    data->m_shader->SetUniform(uni_mat, data->m_proj_matrix);
199    uni_mat = data->m_shader->GetUniformLocation("view_matrix");
200    data->m_shader->SetUniform(uni_mat, data->m_view_matrix);
201    uni_mat = data->m_shader->GetUniformLocation("model_matrix");
202    data->m_shader->SetUniform(uni_mat, data->m_model_matrix);
203
204#if defined USE_D3D9 || defined _XBOX
205#else
206    uni_tex = data->m_shader->GetUniformLocation("in_Texture");
207    data->m_shader->SetUniform(uni_tex, 0);
208
209#if !defined HAVE_GLES_2X
210    glEnable(GL_TEXTURE_2D);
211#endif
212    glEnable(GL_DEPTH_TEST);
213    glDepthFunc(GL_LEQUAL);
214#if defined HAVE_GL_2X && !defined __APPLE__
215    glEnable(GL_ALPHA_TEST);
216    glAlphaFunc(GL_GEQUAL, 0.01f);
217#endif
218    glEnable(GL_BLEND);
219    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
220#endif
221
222    for (int buf = 0, i = 0, n; i < data->tiles.Count(); i = n, buf += 2)
223    {
224        /* Count how many quads will be needed */
225        for (n = i + 1; n < data->tiles.Count(); n++)
226            if (data->tiles[i].tileset != data->tiles[n].tileset)
227                break;
228
229        /* Create a vertex array object */
230        VertexBuffer *vb1 = new VertexBuffer(6 * 3 * (n - i) * sizeof(float));
231        float *vertex = (float *)vb1->Lock(0, 0);
232        VertexBuffer *vb2 = new VertexBuffer(6 * 2 * (n - i) * sizeof(float));
233        float *texture = (float *)vb2->Lock(0, 0);
234
235        data->bufs.Push(vb1);
236        data->bufs.Push(vb2);
237
238        for (int j = i; j < n; j++)
239        {
240            data->tiles[i].tileset->BlitTile(data->tiles[j].id,
241                            data->tiles[j].pos, data->tiles[j].o,
242                            data->tiles[j].scale,
243                            vertex + 18 * (j - i), texture + 12 * (j - i));
244        }
245
246        vb1->Unlock();
247        vb2->Unlock();
248
249        /* Bind texture */
250        data->tiles[i].tileset->Bind();
251
252        /* Bind vertex and texture coordinate buffers */
253        data->m_vdecl->Bind();
254        data->m_vdecl->SetStream(vb1, attr_pos);
255        data->m_vdecl->SetStream(vb2, attr_tex);
256
257        /* Draw arrays */
258        data->m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, (n - i) * 6);
259        data->m_vdecl->Unbind();
260        data->tiles[i].tileset->Unbind();
261    }
262
263    data->tiles.Empty();
264
265    data->m_shader->Unbind();
266
267#if defined USE_D3D9 || defined _XBOX
268    /* TODO */
269#else
270#if !defined HAVE_GLES_2X
271    glDisable(GL_TEXTURE_2D);
272#endif
273    glDisable(GL_DEPTH_TEST);
274#if defined HAVE_GL_2X && !defined __APPLE__
275    glDisable(GL_ALPHA_TEST);
276#endif
277    glDisable(GL_BLEND);
278#endif
279}
280
281} /* namespace lol */
282
Note: See TracBrowser for help on using the repository browser.