source: trunk/tutorial/02_cube.cpp @ 2277

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

build: hide LolFx external declarations behind macros.

  • Property svn:keywords set to Id
File size: 4.2 KB
Line 
1//
2// Lol Engine - Cube tutorial
3//
4// Copyright: (c) 2011-2013 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://www.wtfpl.net/ for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include "core.h"
16#include "loldebug.h"
17
18using namespace std;
19using namespace lol;
20
21LOLFX_RESOURCE_DECLARE(02_cube);
22
23class Cube : public WorldEntity
24{
25public:
26    Cube()
27    {
28        m_angle = 0;
29
30        /* Front vertices/colors */
31        m_mesh.Push(vec3(-1.0, -1.0,  1.0), vec3(1.0, 0.0, 1.0));
32        m_mesh.Push(vec3( 1.0, -1.0,  1.0), vec3(0.0, 1.0, 0.0));
33        m_mesh.Push(vec3( 1.0,  1.0,  1.0), vec3(1.0, 0.5, 0.0));
34        m_mesh.Push(vec3(-1.0,  1.0,  1.0), vec3(1.0, 1.0, 0.0));
35        /* Back */
36        m_mesh.Push(vec3(-1.0, -1.0, -1.0), vec3(1.0, 0.0, 0.0));
37        m_mesh.Push(vec3( 1.0, -1.0, -1.0), vec3(0.0, 0.5, 0.0));
38        m_mesh.Push(vec3( 1.0,  1.0, -1.0), vec3(0.0, 0.5, 1.0));
39        m_mesh.Push(vec3(-1.0,  1.0, -1.0), vec3(0.0, 0.0, 1.0));
40
41        m_indices << i16vec3(0, 1, 2);
42        m_indices << i16vec3(2, 3, 0);
43        m_indices << i16vec3(1, 5, 6);
44        m_indices << i16vec3(6, 2, 1);
45        m_indices << i16vec3(7, 6, 5);
46        m_indices << i16vec3(5, 4, 7);
47        m_indices << i16vec3(4, 0, 3);
48        m_indices << i16vec3(3, 7, 4);
49        m_indices << i16vec3(4, 5, 1);
50        m_indices << i16vec3(1, 0, 4);
51        m_indices << i16vec3(3, 2, 6);
52        m_indices << i16vec3(6, 7, 3);
53
54        m_ready = false;
55    }
56
57    virtual void TickGame(float seconds)
58    {
59        WorldEntity::TickGame(seconds);
60
61        m_angle += seconds * 45.0f;
62
63        mat4 anim = mat4::rotate(m_angle, vec3(0, 1, 0));
64        mat4 model = mat4::translate(vec3(0, 0, -4.5));
65        mat4 view = mat4::lookat(vec3(0, 2, 0), vec3(0, 0, -4), vec3(0, 1, 0));
66        mat4 proj = mat4::perspective(45.0f, 640.0f, 480.0f, 0.1f, 10.0f);
67
68        m_matrix = proj * view * model * anim;
69    }
70
71    virtual void TickDraw(float seconds)
72    {
73        WorldEntity::TickDraw(seconds);
74
75        if (!m_ready)
76        {
77            m_shader = Shader::Create(LOLFX_RESOURCE_NAME(02_cube));
78
79            m_mvp = m_shader->GetUniformLocation("in_Matrix");
80            m_coord = m_shader->GetAttribLocation("in_Vertex",
81                                                  VertexUsage::Position, 0);
82            m_color = m_shader->GetAttribLocation("in_Color",
83                                                  VertexUsage::Color, 0);
84
85            m_vdecl =
86              new VertexDeclaration(VertexStream<vec3,vec3>(VertexUsage::Position,
87                                                            VertexUsage::Color));
88
89            m_vbo = new VertexBuffer(m_mesh.Bytes());
90            void *mesh = m_vbo->Lock(0, 0);
91            memcpy(mesh, &m_mesh[0], m_mesh.Bytes());
92            m_vbo->Unlock();
93
94            m_ibo = new IndexBuffer(m_indices.Bytes());
95            void *indices = m_ibo->Lock(0, 0);
96            memcpy(indices, &m_indices[0], m_indices.Bytes());
97            m_ibo->Unlock();
98
99            /* FIXME: this object never cleans up */
100            m_ready = true;
101        }
102
103        Video::SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
104
105        m_shader->Bind();
106        m_shader->SetUniform(m_mvp, m_matrix);
107        m_vdecl->SetStream(m_vbo, m_coord, m_color);
108        m_vdecl->Bind();
109        m_ibo->Bind();
110        m_vdecl->DrawIndexedElements(MeshPrimitive::Triangles, 0, 0,
111                                     m_mesh.Count(), 0, m_indices.Count() * 3);
112        m_ibo->Unbind();
113        m_vdecl->Unbind();
114    }
115
116private:
117    float m_angle;
118    mat4 m_matrix;
119    Array<vec3,vec3> m_mesh;
120    Array<i16vec3> m_indices;
121
122    Shader *m_shader;
123    ShaderAttrib m_coord, m_color;
124    ShaderUniform m_mvp;
125    VertexDeclaration *m_vdecl;
126    VertexBuffer *m_vbo;
127    IndexBuffer *m_ibo;
128
129    bool m_ready;
130};
131
132int main(int argc, char **argv)
133{
134    System::Init(argc, argv);
135
136    Application app("Tutorial 2: Cube", ivec2(640, 480), 60.0f);
137
138    new DebugFps(5, 5);
139    new Cube();
140
141    app.Run();
142
143    return EXIT_SUCCESS;
144}
145
Note: See TracBrowser for help on using the repository browser.