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 | |
---|
18 | using namespace std; |
---|
19 | using 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 | |
---|
30 | class Cube : public WorldEntity |
---|
31 | { |
---|
32 | public: |
---|
33 | Cube() |
---|
34 | { |
---|
35 | m_angle = 0; |
---|
36 | |
---|
37 | /* Front vertices/colors */ |
---|
38 | m_mesh.Append(vec3(-1.0, -1.0, 1.0), vec3(1.0, 0.0, 1.0)); |
---|
39 | m_mesh.Append(vec3( 1.0, -1.0, 1.0), vec3(0.0, 1.0, 0.0)); |
---|
40 | m_mesh.Append(vec3( 1.0, 1.0, 1.0), vec3(1.0, 0.5, 0.0)); |
---|
41 | m_mesh.Append(vec3(-1.0, 1.0, 1.0), vec3(1.0, 1.0, 0.0)); |
---|
42 | /* Back */ |
---|
43 | m_mesh.Append(vec3(-1.0, -1.0, -1.0), vec3(1.0, 0.0, 0.0)); |
---|
44 | m_mesh.Append(vec3( 1.0, -1.0, -1.0), vec3(0.0, 0.5, 0.0)); |
---|
45 | m_mesh.Append(vec3( 1.0, 1.0, -1.0), vec3(0.0, 0.5, 1.0)); |
---|
46 | m_mesh.Append(vec3(-1.0, 1.0, -1.0), vec3(0.0, 0.0, 1.0)); |
---|
47 | |
---|
48 | m_indices << i16vec3(0, 1, 2); |
---|
49 | m_indices << i16vec3(2, 3, 0); |
---|
50 | m_indices << i16vec3(1, 5, 6); |
---|
51 | m_indices << i16vec3(6, 2, 1); |
---|
52 | m_indices << i16vec3(7, 6, 5); |
---|
53 | m_indices << i16vec3(5, 4, 7); |
---|
54 | m_indices << i16vec3(4, 0, 3); |
---|
55 | m_indices << i16vec3(3, 7, 4); |
---|
56 | m_indices << i16vec3(4, 5, 1); |
---|
57 | m_indices << i16vec3(1, 0, 4); |
---|
58 | m_indices << i16vec3(3, 2, 6); |
---|
59 | m_indices << i16vec3(6, 7, 3); |
---|
60 | |
---|
61 | m_ready = false; |
---|
62 | } |
---|
63 | |
---|
64 | virtual void TickGame(float seconds) |
---|
65 | { |
---|
66 | WorldEntity::TickGame(seconds); |
---|
67 | |
---|
68 | m_angle += seconds * 45.0f; |
---|
69 | |
---|
70 | mat4 anim = mat4::rotate(m_angle, vec3(0, 1, 0)); |
---|
71 | mat4 model = mat4::translate(vec3(0, 0, -4.5)); |
---|
72 | mat4 view = mat4::lookat(vec3(0, 2, 0), vec3(0, 0, -4), vec3(0, 1, 0)); |
---|
73 | mat4 proj = mat4::perspective(45.0f, 640.0f, 480.0f, 0.1f, 10.0f); |
---|
74 | |
---|
75 | m_matrix = proj * view * model * anim; |
---|
76 | } |
---|
77 | |
---|
78 | virtual void TickDraw(float seconds) |
---|
79 | { |
---|
80 | WorldEntity::TickDraw(seconds); |
---|
81 | |
---|
82 | if (!m_ready) |
---|
83 | { |
---|
84 | m_shader = Shader::Create( |
---|
85 | #if !defined __CELLOS_LV2__ && !defined _XBOX && !defined USE_D3D9 |
---|
86 | "#version 120\n" |
---|
87 | "attribute vec3 in_Vertex;" |
---|
88 | "attribute vec3 in_Color;" |
---|
89 | "uniform mat4 in_Matrix;" |
---|
90 | "varying vec3 pass_Color;" |
---|
91 | "" |
---|
92 | "void main(void) {" |
---|
93 | " gl_Position = in_Matrix * vec4(in_Vertex, 1.0);" |
---|
94 | " pass_Color = in_Color;" |
---|
95 | "}", |
---|
96 | |
---|
97 | "#version 120\n" |
---|
98 | "varying vec3 pass_Color;" |
---|
99 | "" |
---|
100 | "void main(void) {" |
---|
101 | " gl_FragColor = vec4(pass_Color, 1.0);" |
---|
102 | "}" |
---|
103 | #else |
---|
104 | "void main(float3 in_Vertex : POSITION," |
---|
105 | " float3 in_Color : COLOR," |
---|
106 | " uniform float4x4 in_Matrix," |
---|
107 | " out float4 out_Position : POSITION," |
---|
108 | " out float3 pass_Color : COLOR) {" |
---|
109 | " pass_Color = in_Color;" |
---|
110 | " out_Position = mul(in_Matrix, float4(in_Vertex, 1.0));" |
---|
111 | "}", |
---|
112 | |
---|
113 | "void main(float3 pass_Color : COLOR," |
---|
114 | " out float4 out_FragColor : COLOR) {" |
---|
115 | " out_FragColor = float4(pass_Color, 1.0);" |
---|
116 | "}" |
---|
117 | #endif |
---|
118 | ); |
---|
119 | m_mvp = m_shader->GetUniformLocation("in_Matrix"); |
---|
120 | m_coord = m_shader->GetAttribLocation("in_Vertex", |
---|
121 | VertexUsage::Position, 0); |
---|
122 | m_color = m_shader->GetAttribLocation("in_Color", |
---|
123 | VertexUsage::Color, 0); |
---|
124 | |
---|
125 | m_vdecl = |
---|
126 | new VertexDeclaration(VertexStream<vec3,vec3>(VertexUsage::Position, |
---|
127 | VertexUsage::Color)); |
---|
128 | |
---|
129 | m_vbo = new VertexBuffer(m_mesh.Bytes()); |
---|
130 | void *mesh = m_vbo->Lock(0, 0); |
---|
131 | memcpy(mesh, &m_mesh[0], m_mesh.Bytes()); |
---|
132 | m_vbo->Unlock(); |
---|
133 | |
---|
134 | m_ibo = new IndexBuffer(m_indices.Bytes()); |
---|
135 | void *indices = m_ibo->Lock(0, 0); |
---|
136 | memcpy(indices, &m_indices[0], m_indices.Bytes()); |
---|
137 | m_ibo->Unlock(); |
---|
138 | |
---|
139 | /* FIXME: this object never cleans up */ |
---|
140 | m_ready = true; |
---|
141 | } |
---|
142 | |
---|
143 | Video::SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f)); |
---|
144 | |
---|
145 | m_shader->Bind(); |
---|
146 | m_shader->SetUniform(m_mvp, m_matrix); |
---|
147 | m_vdecl->SetStream(m_vbo, m_coord, m_color); |
---|
148 | m_vdecl->Bind(); |
---|
149 | m_ibo->Bind(); |
---|
150 | m_vdecl->DrawIndexedElements(MeshPrimitive::Triangles, 0, 0, |
---|
151 | m_mesh.Count(), 0, m_indices.Count()); |
---|
152 | m_ibo->Unbind(); |
---|
153 | m_vdecl->Unbind(); |
---|
154 | } |
---|
155 | |
---|
156 | private: |
---|
157 | float m_angle; |
---|
158 | mat4 m_matrix; |
---|
159 | Array<vec3,vec3> m_mesh; |
---|
160 | Array<i16vec3> m_indices; |
---|
161 | |
---|
162 | Shader *m_shader; |
---|
163 | ShaderAttrib m_coord, m_color; |
---|
164 | ShaderUniform m_mvp; |
---|
165 | VertexDeclaration *m_vdecl; |
---|
166 | VertexBuffer *m_vbo; |
---|
167 | IndexBuffer *m_ibo; |
---|
168 | |
---|
169 | bool m_ready; |
---|
170 | }; |
---|
171 | |
---|
172 | int main(int argc, char **argv) |
---|
173 | { |
---|
174 | Application app("Tutorial 2: Cube", ivec2(640, 480), 60.0f); |
---|
175 | |
---|
176 | #if defined _MSC_VER && !defined _XBOX |
---|
177 | _chdir(".."); |
---|
178 | #elif defined _WIN32 && !defined _XBOX |
---|
179 | _chdir("../.."); |
---|
180 | #endif |
---|
181 | |
---|
182 | new DebugFps(5, 5); |
---|
183 | new Cube(); |
---|
184 | |
---|
185 | app.Run(); |
---|
186 | |
---|
187 | return EXIT_SUCCESS; |
---|
188 | } |
---|
189 | |
---|