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