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
RevLine 
[1043]1//
2// Lol Engine - Cube tutorial
3//
[1310]4// Copyright: (c) 2011-2012 Sam Hocevar <sam@hocevar.net>
[1043]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
[1179]22#   include <direct.h>
[1043]23#endif
24
[1392]25extern char const *lolfx_02_cube;
26
[1043]27class Cube : public WorldEntity
28{
29public:
30    Cube()
31    {
[1045]32        m_angle = 0;
33
[1238]34        /* Front vertices/colors */
[1326]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));
[1043]39        /* Back */
[1326]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));
[1043]44
[1269]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);
[1172]57
58        m_ready = false;
[1043]59    }
60
[1310]61    virtual void TickGame(float seconds)
[1043]62    {
[1310]63        WorldEntity::TickGame(seconds);
[1043]64
[1310]65        m_angle += seconds * 45.0f;
[1043]66
[1045]67        mat4 anim = mat4::rotate(m_angle, vec3(0, 1, 0));
[1242]68        mat4 model = mat4::translate(vec3(0, 0, -4.5));
[1043]69        mat4 view = mat4::lookat(vec3(0, 2, 0), vec3(0, 0, -4), vec3(0, 1, 0));
[1045]70        mat4 proj = mat4::perspective(45.0f, 640.0f, 480.0f, 0.1f, 10.0f);
71
[1043]72        m_matrix = proj * view * model * anim;
73    }
74
[1310]75    virtual void TickDraw(float seconds)
[1043]76    {
[1310]77        WorldEntity::TickDraw(seconds);
[1043]78
79        if (!m_ready)
80        {
[1392]81            m_shader = Shader::Create(lolfx_02_cube);
[1043]82
[1045]83            m_mvp = m_shader->GetUniformLocation("in_Matrix");
[1227]84            m_coord = m_shader->GetAttribLocation("in_Vertex",
85                                                  VertexUsage::Position, 0);
86            m_color = m_shader->GetAttribLocation("in_Color",
87                                                  VertexUsage::Color, 0);
[1043]88
[1226]89            m_vdecl =
[1238]90              new VertexDeclaration(VertexStream<vec3,vec3>(VertexUsage::Position,
91                                                            VertexUsage::Color));
[1227]92
[1243]93            m_vbo = new VertexBuffer(m_mesh.Bytes());
[1238]94            void *mesh = m_vbo->Lock(0, 0);
[1243]95            memcpy(mesh, &m_mesh[0], m_mesh.Bytes());
[1227]96            m_vbo->Unlock();
97
[1247]98            m_ibo = new IndexBuffer(m_indices.Bytes());
99            void *indices = m_ibo->Lock(0, 0);
[1243]100            memcpy(indices, &m_indices[0], m_indices.Bytes());
[1196]101            m_ibo->Unlock();
[1043]102
103            /* FIXME: this object never cleans up */
[1227]104            m_ready = true;
[1043]105        }
106
[1238]107        Video::SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
[1236]108
[1043]109        m_shader->Bind();
[1045]110        m_shader->SetUniform(m_mvp, m_matrix);
[1238]111        m_vdecl->SetStream(m_vbo, m_coord, m_color);
[1236]112        m_vdecl->Bind();
[1247]113        m_ibo->Bind();
[1248]114        m_vdecl->DrawIndexedElements(MeshPrimitive::Triangles, 0, 0,
[1963]115                                     m_mesh.Count(), 0, m_indices.Count() * 3);
[1247]116        m_ibo->Unbind();
[1233]117        m_vdecl->Unbind();
[1043]118    }
119
120private:
121    float m_angle;
122    mat4 m_matrix;
[1255]123    Array<vec3,vec3> m_mesh;
[1243]124    Array<i16vec3> m_indices;
[1248]125
[1043]126    Shader *m_shader;
[1227]127    ShaderAttrib m_coord, m_color;
128    ShaderUniform m_mvp;
[1226]129    VertexDeclaration *m_vdecl;
[1238]130    VertexBuffer *m_vbo;
[1247]131    IndexBuffer *m_ibo;
[1248]132
[1043]133    bool m_ready;
134};
135
[1050]136int main(int argc, char **argv)
[1043]137{
138    Application app("Tutorial 2: Cube", ivec2(640, 480), 60.0f);
139
[1196]140#if defined _MSC_VER && !defined _XBOX
[1178]141    _chdir("..");
[1196]142#elif defined _WIN32 && !defined _XBOX
[1178]143    _chdir("../..");
144#endif
145
[1043]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.