Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

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