231 lines
7.1 KiB

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