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

Last change on this file since 1645 was 1645, checked in by sam, 8 years ago

core: switch the Win32/x64 binaries to the Windows subsystem so that they
do not open a console at startup.

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