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 | |
---|
18 | using namespace std; |
---|
19 | using namespace lol; |
---|
20 | |
---|
21 | LOLFX_RESOURCE_DECLARE(02_cube); |
---|
22 | |
---|
23 | class Cube : public WorldEntity |
---|
24 | { |
---|
25 | public: |
---|
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_faces_indices << 0 << 1 << 2 << 2 << 3 << 0; |
---|
42 | m_faces_indices << 1 << 5 << 6 << 6 << 2 << 1; |
---|
43 | m_faces_indices << 7 << 6 << 5 << 5 << 4 << 7; |
---|
44 | m_faces_indices << 4 << 0 << 3 << 3 << 7 << 4; |
---|
45 | m_faces_indices << 4 << 5 << 1 << 1 << 0 << 4; |
---|
46 | m_faces_indices << 3 << 2 << 6 << 6 << 7 << 3; |
---|
47 | |
---|
48 | m_lines_indices << 0 << 1 << 1 << 2 << 2 << 3 << 3 << 0; |
---|
49 | m_lines_indices << 4 << 5 << 5 << 6 << 6 << 7 << 7 << 4; |
---|
50 | m_lines_indices << 0 << 4 << 1 << 5 << 2 << 6 << 3 << 7; |
---|
51 | |
---|
52 | m_ready = false; |
---|
53 | } |
---|
54 | |
---|
55 | virtual void TickGame(float seconds) |
---|
56 | { |
---|
57 | WorldEntity::TickGame(seconds); |
---|
58 | |
---|
59 | m_angle += seconds * 45.0f; |
---|
60 | |
---|
61 | mat4 anim = mat4::rotate(m_angle, vec3(0, 1, 0)); |
---|
62 | mat4 model = mat4::translate(vec3(0, 0, -4.5)); |
---|
63 | mat4 view = mat4::lookat(vec3(0, 2, 0), vec3(0, 0, -4), vec3(0, 1, 0)); |
---|
64 | mat4 proj = mat4::perspective(45.0f, 640.0f, 480.0f, 0.1f, 10.0f); |
---|
65 | |
---|
66 | m_matrix = proj * view * model * anim; |
---|
67 | } |
---|
68 | |
---|
69 | virtual void TickDraw(float seconds) |
---|
70 | { |
---|
71 | WorldEntity::TickDraw(seconds); |
---|
72 | |
---|
73 | if (!m_ready) |
---|
74 | { |
---|
75 | m_shader = Shader::Create(LOLFX_RESOURCE_NAME(02_cube)); |
---|
76 | |
---|
77 | m_mvp = m_shader->GetUniformLocation("in_Matrix"); |
---|
78 | m_coord = m_shader->GetAttribLocation("in_Vertex", |
---|
79 | VertexUsage::Position, 0); |
---|
80 | m_color = m_shader->GetAttribLocation("in_Color", |
---|
81 | VertexUsage::Color, 0); |
---|
82 | |
---|
83 | m_vdecl = |
---|
84 | new VertexDeclaration(VertexStream<vec3,vec3>(VertexUsage::Position, |
---|
85 | VertexUsage::Color)); |
---|
86 | |
---|
87 | m_vbo = new VertexBuffer(m_mesh.Bytes()); |
---|
88 | void *mesh = m_vbo->Lock(0, 0); |
---|
89 | memcpy(mesh, &m_mesh[0], m_mesh.Bytes()); |
---|
90 | m_vbo->Unlock(); |
---|
91 | |
---|
92 | m_lines_ibo = new IndexBuffer(m_lines_indices.Bytes()); |
---|
93 | void *indices = m_lines_ibo->Lock(0, 0); |
---|
94 | memcpy(indices, &m_lines_indices[0], m_lines_indices.Bytes()); |
---|
95 | m_lines_ibo->Unlock(); |
---|
96 | |
---|
97 | m_faces_ibo = new IndexBuffer(m_faces_indices.Bytes()); |
---|
98 | indices = m_faces_ibo->Lock(0, 0); |
---|
99 | memcpy(indices, &m_faces_indices[0], m_faces_indices.Bytes()); |
---|
100 | m_faces_ibo->Unlock(); |
---|
101 | |
---|
102 | /* FIXME: this object never cleans up */ |
---|
103 | m_ready = true; |
---|
104 | } |
---|
105 | |
---|
106 | Video::SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f)); |
---|
107 | |
---|
108 | m_shader->Bind(); |
---|
109 | m_vdecl->SetStream(m_vbo, m_coord, m_color); |
---|
110 | m_vdecl->Bind(); |
---|
111 | |
---|
112 | m_shader->SetUniform(m_mvp, m_matrix); |
---|
113 | m_lines_ibo->Bind(); |
---|
114 | m_vdecl->DrawIndexedElements(MeshPrimitive::Lines, 0, 0, |
---|
115 | m_mesh.Count(), 0, m_lines_indices.Count()); |
---|
116 | m_lines_ibo->Unbind(); |
---|
117 | |
---|
118 | m_shader->SetUniform(m_mvp, m_matrix * mat4::scale(0.5f)); |
---|
119 | m_faces_ibo->Bind(); |
---|
120 | m_vdecl->DrawIndexedElements(MeshPrimitive::Triangles, 0, 0, |
---|
121 | m_mesh.Count(), 0, m_faces_indices.Count()); |
---|
122 | m_faces_ibo->Unbind(); |
---|
123 | |
---|
124 | m_vdecl->Unbind(); |
---|
125 | } |
---|
126 | |
---|
127 | private: |
---|
128 | float m_angle; |
---|
129 | mat4 m_matrix; |
---|
130 | Array<vec3,vec3> m_mesh; |
---|
131 | Array<uint16_t> m_lines_indices, m_faces_indices; |
---|
132 | |
---|
133 | Shader *m_shader; |
---|
134 | ShaderAttrib m_coord, m_color; |
---|
135 | ShaderUniform m_mvp; |
---|
136 | VertexDeclaration *m_vdecl; |
---|
137 | VertexBuffer *m_vbo; |
---|
138 | IndexBuffer *m_lines_ibo, *m_faces_ibo; |
---|
139 | |
---|
140 | bool m_ready; |
---|
141 | }; |
---|
142 | |
---|
143 | int main(int argc, char **argv) |
---|
144 | { |
---|
145 | System::Init(argc, argv); |
---|
146 | |
---|
147 | Application app("Tutorial 2: Cube", ivec2(640, 480), 60.0f); |
---|
148 | |
---|
149 | new DebugFps(5, 5); |
---|
150 | new Cube(); |
---|
151 | |
---|
152 | app.Run(); |
---|
153 | |
---|
154 | return EXIT_SUCCESS; |
---|
155 | } |
---|
156 | |
---|