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

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

build: reorganise all the build stuff so that it lies in build/ and
make sure each .vcxproj file is with its corresponding source code.

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