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 | #define LOL_INPUT_V2 |
---|
12 | |
---|
13 | #if defined HAVE_CONFIG_H |
---|
14 | # include "config.h" |
---|
15 | #endif |
---|
16 | |
---|
17 | #include "core.h" |
---|
18 | #include "loldebug.h" |
---|
19 | |
---|
20 | using namespace std; |
---|
21 | using namespace lol; |
---|
22 | |
---|
23 | LOLFX_RESOURCE_DECLARE(07_input); |
---|
24 | |
---|
25 | enum |
---|
26 | { |
---|
27 | KEY_MANUAL_ROTATION, |
---|
28 | KEY_DRAG_MESH, |
---|
29 | KEY_MAX |
---|
30 | }; |
---|
31 | |
---|
32 | enum |
---|
33 | { |
---|
34 | AXIS_DRAG_PITCH, |
---|
35 | AXIS_DRAG_YAW, |
---|
36 | AXIS_PITCH, |
---|
37 | AXIS_YAW, |
---|
38 | AXIS_MAX |
---|
39 | }; |
---|
40 | |
---|
41 | Controller* controller; |
---|
42 | |
---|
43 | class Cube : public WorldEntity |
---|
44 | { |
---|
45 | public: |
---|
46 | Cube() |
---|
47 | { |
---|
48 | m_pitch_angle = 0; |
---|
49 | m_yaw_angle = 0; |
---|
50 | m_autorot = true; |
---|
51 | |
---|
52 | /* Front vertices/colors */ |
---|
53 | m_mesh.Push(vec3(-1.0, -1.0, 1.0), vec3(1.0, 0.0, 1.0)); |
---|
54 | m_mesh.Push(vec3( 1.0, -1.0, 1.0), vec3(0.0, 1.0, 0.0)); |
---|
55 | m_mesh.Push(vec3( 1.0, 1.0, 1.0), vec3(1.0, 0.5, 0.0)); |
---|
56 | m_mesh.Push(vec3(-1.0, 1.0, 1.0), vec3(1.0, 1.0, 0.0)); |
---|
57 | /* Back */ |
---|
58 | m_mesh.Push(vec3(-1.0, -1.0, -1.0), vec3(1.0, 0.0, 0.0)); |
---|
59 | m_mesh.Push(vec3( 1.0, -1.0, -1.0), vec3(0.0, 0.5, 0.0)); |
---|
60 | m_mesh.Push(vec3( 1.0, 1.0, -1.0), vec3(0.0, 0.5, 1.0)); |
---|
61 | m_mesh.Push(vec3(-1.0, 1.0, -1.0), vec3(0.0, 0.0, 1.0)); |
---|
62 | |
---|
63 | m_faces_indices << 0 << 1 << 2 << 2 << 3 << 0; |
---|
64 | m_faces_indices << 1 << 5 << 6 << 6 << 2 << 1; |
---|
65 | m_faces_indices << 7 << 6 << 5 << 5 << 4 << 7; |
---|
66 | m_faces_indices << 4 << 0 << 3 << 3 << 7 << 4; |
---|
67 | m_faces_indices << 4 << 5 << 1 << 1 << 0 << 4; |
---|
68 | m_faces_indices << 3 << 2 << 6 << 6 << 7 << 3; |
---|
69 | |
---|
70 | m_lines_indices << 0 << 1 << 1 << 2 << 2 << 3 << 3 << 0; |
---|
71 | m_lines_indices << 4 << 5 << 5 << 6 << 6 << 7 << 7 << 4; |
---|
72 | m_lines_indices << 0 << 4 << 1 << 5 << 2 << 6 << 3 << 7; |
---|
73 | |
---|
74 | m_text = new Text(NULL, "data/font/ascii.png"); |
---|
75 | m_text->SetPos(ivec3(5, 5, 1)); |
---|
76 | Ticker::Ref(m_text); |
---|
77 | |
---|
78 | m_ready = false; |
---|
79 | } |
---|
80 | |
---|
81 | ~Cube() |
---|
82 | { |
---|
83 | Ticker::Unref(m_text); |
---|
84 | } |
---|
85 | |
---|
86 | virtual void TickGame(float seconds) |
---|
87 | { |
---|
88 | WorldEntity::TickGame(seconds); |
---|
89 | |
---|
90 | if (controller->GetKey(KEY_MANUAL_ROTATION).IsPressed()) |
---|
91 | m_autorot = !m_autorot; |
---|
92 | |
---|
93 | if (controller->GetKey(KEY_DRAG_MESH).IsDown()) |
---|
94 | { |
---|
95 | InputDevice::CaptureMouse(true); |
---|
96 | m_pitch_angle -= controller->GetAxis(AXIS_DRAG_PITCH).GetValue() * seconds * 100; |
---|
97 | m_yaw_angle += controller->GetAxis(AXIS_DRAG_YAW).GetValue() * seconds * 100; |
---|
98 | } |
---|
99 | else |
---|
100 | { |
---|
101 | InputDevice::CaptureMouse(false); |
---|
102 | if (m_autorot) |
---|
103 | m_yaw_angle += seconds * 20; |
---|
104 | } |
---|
105 | if (lol::abs(controller->GetAxis(AXIS_PITCH).GetValue()) > 0.2f) |
---|
106 | m_pitch_angle -= controller->GetAxis(AXIS_PITCH).GetValue() * seconds * 100; |
---|
107 | if (lol::abs(controller->GetAxis(AXIS_YAW).GetValue()) > 0.2f) |
---|
108 | m_yaw_angle += controller->GetAxis(AXIS_YAW).GetValue() * seconds * 100; |
---|
109 | |
---|
110 | InputDevice* mouse = InputDevice::Get("Mouse"); |
---|
111 | if (mouse) |
---|
112 | { |
---|
113 | char buf[128]; |
---|
114 | std::sprintf(buf, "cursor: (%0.3f, %0.3f) - pixel (%d, %d)", |
---|
115 | mouse->GetCursor(0).x, mouse->GetCursor(0).y, |
---|
116 | mouse->GetCursorPixel(0).x, mouse->GetCursorPixel(0).y); |
---|
117 | m_text->SetText(buf); |
---|
118 | } |
---|
119 | else |
---|
120 | { |
---|
121 | m_text->SetText("no mouse detected"); |
---|
122 | } |
---|
123 | |
---|
124 | mat4 anim = mat4::fromeuler_yxz(m_yaw_angle, m_pitch_angle, 0.f); |
---|
125 | mat4 model = mat4::translate(vec3(0, 0, -4.5)); |
---|
126 | mat4 view = mat4::lookat(vec3(0, 2, 0), vec3(0, 0, -4), vec3(0, 1, 0)); |
---|
127 | mat4 proj = mat4::perspective(45.0f, 640.0f, 480.0f, 0.1f, 10.0f); |
---|
128 | |
---|
129 | m_matrix = proj * view * model * anim; |
---|
130 | } |
---|
131 | |
---|
132 | virtual void TickDraw(float seconds) |
---|
133 | { |
---|
134 | WorldEntity::TickDraw(seconds); |
---|
135 | |
---|
136 | if (!m_ready) |
---|
137 | { |
---|
138 | m_shader = Shader::Create(LOLFX_RESOURCE_NAME(07_input)); |
---|
139 | |
---|
140 | m_mvp = m_shader->GetUniformLocation("in_Matrix"); |
---|
141 | m_coord = m_shader->GetAttribLocation("in_Vertex", |
---|
142 | VertexUsage::Position, 0); |
---|
143 | m_color = m_shader->GetAttribLocation("in_Color", |
---|
144 | VertexUsage::Color, 0); |
---|
145 | |
---|
146 | m_vdecl = |
---|
147 | new VertexDeclaration(VertexStream<vec3,vec3>(VertexUsage::Position, |
---|
148 | VertexUsage::Color)); |
---|
149 | |
---|
150 | m_vbo = new VertexBuffer(m_mesh.Bytes()); |
---|
151 | void *mesh = m_vbo->Lock(0, 0); |
---|
152 | memcpy(mesh, &m_mesh[0], m_mesh.Bytes()); |
---|
153 | m_vbo->Unlock(); |
---|
154 | |
---|
155 | m_lines_ibo = new IndexBuffer(m_lines_indices.Bytes()); |
---|
156 | void *indices = m_lines_ibo->Lock(0, 0); |
---|
157 | memcpy(indices, &m_lines_indices[0], m_lines_indices.Bytes()); |
---|
158 | m_lines_ibo->Unlock(); |
---|
159 | |
---|
160 | m_faces_ibo = new IndexBuffer(m_faces_indices.Bytes()); |
---|
161 | indices = m_faces_ibo->Lock(0, 0); |
---|
162 | memcpy(indices, &m_faces_indices[0], m_faces_indices.Bytes()); |
---|
163 | m_faces_ibo->Unlock(); |
---|
164 | |
---|
165 | /* FIXME: this object never cleans up */ |
---|
166 | m_ready = true; |
---|
167 | } |
---|
168 | |
---|
169 | g_renderer->SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f)); |
---|
170 | |
---|
171 | m_shader->Bind(); |
---|
172 | m_vdecl->SetStream(m_vbo, m_coord, m_color); |
---|
173 | m_vdecl->Bind(); |
---|
174 | |
---|
175 | m_shader->SetUniform(m_mvp, m_matrix); |
---|
176 | m_lines_ibo->Bind(); |
---|
177 | m_vdecl->DrawIndexedElements(MeshPrimitive::Lines, 0, 0, |
---|
178 | m_mesh.Count(), 0, m_lines_indices.Count()); |
---|
179 | m_lines_ibo->Unbind(); |
---|
180 | |
---|
181 | m_shader->SetUniform(m_mvp, m_matrix * mat4::scale(0.5f)); |
---|
182 | m_faces_ibo->Bind(); |
---|
183 | m_vdecl->DrawIndexedElements(MeshPrimitive::Triangles, 0, 0, |
---|
184 | m_mesh.Count(), 0, m_faces_indices.Count()); |
---|
185 | m_faces_ibo->Unbind(); |
---|
186 | |
---|
187 | m_vdecl->Unbind(); |
---|
188 | } |
---|
189 | |
---|
190 | private: |
---|
191 | bool m_autorot; |
---|
192 | float m_pitch_angle; |
---|
193 | float m_yaw_angle; |
---|
194 | mat4 m_matrix; |
---|
195 | Array<vec3,vec3> m_mesh; |
---|
196 | Array<uint16_t> m_lines_indices, m_faces_indices; |
---|
197 | |
---|
198 | Shader *m_shader; |
---|
199 | ShaderAttrib m_coord, m_color; |
---|
200 | ShaderUniform m_mvp; |
---|
201 | VertexDeclaration *m_vdecl; |
---|
202 | VertexBuffer *m_vbo; |
---|
203 | IndexBuffer *m_lines_ibo, *m_faces_ibo; |
---|
204 | |
---|
205 | Text *m_text; |
---|
206 | bool m_ready; |
---|
207 | }; |
---|
208 | |
---|
209 | int main(int argc, char **argv) |
---|
210 | { |
---|
211 | System::Init(argc, argv); |
---|
212 | |
---|
213 | Application app("Tutorial 7: Input", ivec2(640, 480), 60.0f); |
---|
214 | |
---|
215 | new DebugFps(5, 5); |
---|
216 | new Cube(); |
---|
217 | |
---|
218 | controller = new Controller(KEY_MAX, AXIS_MAX); |
---|
219 | controller->GetKey(KEY_MANUAL_ROTATION).Bind("Keyboard", "Space"); |
---|
220 | controller->GetKey(KEY_DRAG_MESH).Bind("Mouse", "ButtonLeft"); |
---|
221 | controller->GetAxis(AXIS_DRAG_PITCH).Bind("Mouse", "Y"); |
---|
222 | controller->GetAxis(AXIS_DRAG_YAW).Bind("Mouse", "X"); |
---|
223 | controller->GetAxis(AXIS_PITCH).Bind("Joystick1", "Axis2"); |
---|
224 | controller->GetAxis(AXIS_YAW).Bind("Joystick1", "Axis1"); |
---|
225 | |
---|
226 | app.Run(); |
---|
227 | |
---|
228 | return EXIT_SUCCESS; |
---|
229 | } |
---|
230 | |
---|