Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

149 lignes
4.6 KiB

  1. //
  2. // Lol Engine — Cube tutorial
  3. //
  4. // Copyright © 2011—2015 Sam Hocevar <sam@hocevar.net>
  5. //
  6. // Lol Engine is free software. It comes without any warranty, to
  7. // the extent permitted by applicable law. You can redistribute it
  8. // and/or modify it under the terms of the Do What the Fuck You Want
  9. // to Public License, Version 2, as published by the WTFPL Task Force.
  10. // See http://www.wtfpl.net/ for more details.
  11. //
  12. #if HAVE_CONFIG_H
  13. # include "config.h"
  14. #endif
  15. #include <lol/engine.h>
  16. #include "loldebug.h"
  17. using namespace lol;
  18. LOLFX_RESOURCE_DECLARE(02_cube);
  19. class Cube : public WorldEntity
  20. {
  21. public:
  22. Cube()
  23. : m_angle(0),
  24. m_mesh({ /* Front vertices/colors */
  25. { vec3(-1.0, -1.0, 1.0), vec3(1.0, 0.0, 1.0) },
  26. { vec3( 1.0, -1.0, 1.0), vec3(0.0, 1.0, 0.0) },
  27. { vec3( 1.0, 1.0, 1.0), vec3(1.0, 0.5, 0.0) },
  28. { vec3(-1.0, 1.0, 1.0), vec3(1.0, 1.0, 0.0) },
  29. /* Back */
  30. { vec3(-1.0, -1.0, -1.0), vec3(1.0, 0.0, 0.0) },
  31. { vec3( 1.0, -1.0, -1.0), vec3(0.0, 0.5, 0.0) },
  32. { vec3( 1.0, 1.0, -1.0), vec3(0.0, 0.5, 1.0) },
  33. { vec3(-1.0, 1.0, -1.0), vec3(0.0, 0.0, 1.0) } }),
  34. m_lines_indices({ 0, 1, 1, 2, 2, 3, 3, 0,
  35. 4, 5, 5, 6, 6, 7, 7, 4,
  36. 0, 4, 1, 5, 2, 6, 3, 7, }),
  37. m_faces_indices({ 0, 1, 2, 2, 3, 0, 1, 5, 6, 6, 2, 1,
  38. 7, 6, 5, 5, 4, 7, 4, 0, 3, 3, 7, 4,
  39. 4, 5, 1, 1, 0, 4, 3, 2, 6, 6, 7, 3, }),
  40. m_ready(false)
  41. {
  42. }
  43. virtual void TickGame(float seconds)
  44. {
  45. WorldEntity::TickGame(seconds);
  46. m_angle += seconds * radians(45.0f);
  47. mat4 anim = mat4::rotate(m_angle, vec3(0, 1, 0));
  48. mat4 model = mat4::translate(vec3(0, 0, -4.5));
  49. mat4 view = mat4::lookat(vec3(0, 2, 0), vec3(0, 0, -4), vec3(0, 1, 0));
  50. mat4 proj = mat4::perspective(radians(45.0f), 640.0f, 480.0f, 0.1f, 10.0f);
  51. m_matrix = proj * view * model * anim;
  52. }
  53. virtual void TickDraw(float seconds, Scene &scene)
  54. {
  55. WorldEntity::TickDraw(seconds, scene);
  56. if (!m_ready)
  57. {
  58. m_shader = Shader::Create(LOLFX_RESOURCE_NAME(02_cube));
  59. m_mvp = m_shader->GetUniformLocation("in_Matrix");
  60. m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0);
  61. m_color = m_shader->GetAttribLocation(VertexUsage::Color, 0);
  62. m_vdecl =
  63. new VertexDeclaration(VertexStream<vec3,vec3>(VertexUsage::Position,
  64. VertexUsage::Color));
  65. m_vbo = new VertexBuffer(m_mesh.bytes());
  66. void *mesh = m_vbo->Lock(0, 0);
  67. memcpy(mesh, &m_mesh[0], m_mesh.bytes());
  68. m_vbo->Unlock();
  69. m_lines_ibo = new IndexBuffer(m_lines_indices.bytes());
  70. void *indices = m_lines_ibo->Lock(0, 0);
  71. memcpy(indices, &m_lines_indices[0], m_lines_indices.bytes());
  72. m_lines_ibo->Unlock();
  73. m_faces_ibo = new IndexBuffer(m_faces_indices.bytes());
  74. indices = m_faces_ibo->Lock(0, 0);
  75. memcpy(indices, &m_faces_indices[0], m_faces_indices.bytes());
  76. m_faces_ibo->Unlock();
  77. /* FIXME: this object never cleans up */
  78. m_ready = true;
  79. }
  80. Renderer::Get()->SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
  81. m_shader->Bind();
  82. m_vdecl->SetStream(m_vbo, m_coord, m_color);
  83. m_vdecl->Bind();
  84. m_shader->SetUniform(m_mvp, m_matrix);
  85. m_lines_ibo->Bind();
  86. m_vdecl->DrawIndexedElements(MeshPrimitive::Lines, 0, 0,
  87. m_mesh.count(), 0, m_lines_indices.count());
  88. m_lines_ibo->Unbind();
  89. m_shader->SetUniform(m_mvp, m_matrix * mat4::scale(0.5f));
  90. m_faces_ibo->Bind();
  91. m_vdecl->DrawIndexedElements(MeshPrimitive::Triangles, 0, 0,
  92. m_mesh.count(), 0, m_faces_indices.count());
  93. m_faces_ibo->Unbind();
  94. m_vdecl->Unbind();
  95. }
  96. private:
  97. float m_angle;
  98. mat4 m_matrix;
  99. array<vec3,vec3> m_mesh;
  100. array<uint16_t> m_lines_indices, m_faces_indices;
  101. Shader *m_shader;
  102. ShaderAttrib m_coord, m_color;
  103. ShaderUniform m_mvp;
  104. VertexDeclaration *m_vdecl;
  105. VertexBuffer *m_vbo;
  106. IndexBuffer *m_lines_ibo, *m_faces_ibo;
  107. bool m_ready;
  108. };
  109. int main(int argc, char **argv)
  110. {
  111. System::Init(argc, argv);
  112. Application app("Tutorial 2: Cube", ivec2(640, 480), 60.0f);
  113. new DebugFps(5, 5);
  114. new Cube();
  115. app.Run();
  116. return EXIT_SUCCESS;
  117. }