You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

188 line
6.0 KiB

  1. //
  2. // Lol Engine — Cube tutorial
  3. //
  4. // Copyright © 2011—2019 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. {
  41. m_camera = new Camera();
  42. m_camera->SetProjection(mat4::perspective(radians(30.f), 640.f, 480.f, .1f, 1000.f));
  43. m_camera->SetView(mat4::lookat(vec3(-15.f, 5.f, 0.f),
  44. vec3(0.f, -1.f, 0.f),
  45. vec3(0.f, 1.f, 0.f)));
  46. Scene& scene = Scene::GetScene();
  47. scene.PushCamera(m_camera);
  48. Ticker::Ref(m_camera);
  49. }
  50. ~Cube()
  51. {
  52. Scene& scene = Scene::GetScene();
  53. scene.PopCamera(m_camera);
  54. Ticker::Unref(m_camera);
  55. }
  56. virtual bool init_draw() override
  57. {
  58. m_shader = Shader::Create(LOLFX_RESOURCE_NAME(02_cube));
  59. m_mvp = m_shader->GetUniformLocation("u_matrix");
  60. m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0);
  61. m_color = m_shader->GetAttribLocation(VertexUsage::Color, 0);
  62. m_vdecl = std::make_shared<VertexDeclaration>(
  63. VertexStream<vec3,vec3>(VertexUsage::Position,
  64. VertexUsage::Color));
  65. m_vbo = std::make_shared<VertexBuffer>(m_mesh.bytes());
  66. void *mesh = m_vbo->Lock(0, 0);
  67. memcpy(mesh, m_mesh.data(), m_mesh.bytes());
  68. m_vbo->Unlock();
  69. m_lines_ibo = std::make_shared<IndexBuffer>(m_lines_indices.bytes());
  70. void *indices = m_lines_ibo->Lock(0, 0);
  71. memcpy(indices, m_lines_indices.data(), m_lines_indices.bytes());
  72. m_lines_ibo->Unlock();
  73. m_faces_ibo = std::make_shared<IndexBuffer>(m_faces_indices.bytes());
  74. indices = m_faces_ibo->Lock(0, 0);
  75. memcpy(indices, m_faces_indices.data(), m_faces_indices.bytes());
  76. m_faces_ibo->Unlock();
  77. return true;
  78. }
  79. virtual void tick_game(float seconds) override
  80. {
  81. WorldEntity::tick_game(seconds);
  82. m_angle += seconds * radians(45.0f);
  83. mat4 anim = mat4::rotate(m_angle, vec3(0, 1, 0));
  84. mat4 model = mat4::translate(vec3(0, 0, -4.5));
  85. mat4 view = mat4::lookat(vec3(0, 2, 0), vec3(0, 0, -4), vec3(0, 1, 0));
  86. mat4 proj = mat4::perspective(radians(45.0f), 640.0f, 480.0f, 0.1f, 10.0f);
  87. m_matrix = proj * view * model * anim;
  88. {
  89. auto context0 = Debug::DrawContext::New(Color::red);
  90. {
  91. auto context1 = Debug::DrawContext::New(Color::blue);
  92. Debug::DrawBox(box3(vec3(0.f), vec3(1.2f)));
  93. Debug::DrawGrid(vec3(0.f), vec3::axis_x, vec3::axis_y, vec3::axis_z, 10.0f);
  94. {
  95. auto context2 = Debug::DrawContext::New(context0);
  96. Debug::DrawBox(box3(vec3(0.f), vec3(1.3f)));
  97. }
  98. {
  99. auto context2 = Debug::DrawContext::New(context0);
  100. context2.SetColor(Color::yellow);
  101. Debug::DrawBox(box3(vec3(-1.f), vec3(1.4f)));
  102. }
  103. }
  104. Debug::DrawBox(box3(vec3(0.f), vec3(1.1f)));
  105. }
  106. }
  107. virtual void tick_draw(float seconds, Scene &scene) override
  108. {
  109. WorldEntity::tick_draw(seconds, scene);
  110. scene.get_renderer()->SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
  111. m_shader->Bind();
  112. m_vdecl->SetStream(m_vbo, m_coord, m_color);
  113. m_vdecl->Bind();
  114. m_shader->SetUniform(m_mvp, m_matrix);
  115. m_lines_ibo->Bind();
  116. m_vdecl->DrawIndexedElements(MeshPrimitive::Lines, m_lines_indices.count());
  117. m_lines_ibo->Unbind();
  118. m_shader->SetUniform(m_mvp, m_matrix * mat4::scale(0.5f));
  119. m_faces_ibo->Bind();
  120. m_vdecl->DrawIndexedElements(MeshPrimitive::Triangles, m_faces_indices.count());
  121. m_faces_ibo->Unbind();
  122. m_vdecl->Unbind();
  123. }
  124. virtual bool release_draw() override
  125. {
  126. m_shader.reset();
  127. m_vdecl.reset();
  128. m_vbo.reset();
  129. m_lines_ibo.reset();
  130. m_faces_ibo.reset();
  131. return true;
  132. }
  133. private:
  134. Camera* m_camera = nullptr;
  135. float m_angle;
  136. mat4 m_matrix;
  137. array<vec3,vec3> m_mesh;
  138. array<uint16_t> m_lines_indices, m_faces_indices;
  139. std::shared_ptr<Shader> m_shader;
  140. ShaderAttrib m_coord, m_color;
  141. ShaderUniform m_mvp;
  142. std::shared_ptr<VertexDeclaration> m_vdecl;
  143. std::shared_ptr<VertexBuffer> m_vbo;
  144. std::shared_ptr<IndexBuffer> m_lines_ibo, m_faces_ibo;
  145. };
  146. int main(int argc, char **argv)
  147. {
  148. sys::init(argc, argv);
  149. Application app("Tutorial 2: Cube", ivec2(640, 480), 60.0f);
  150. new DebugFps(5, 5);
  151. new Cube();
  152. app.Run();
  153. return EXIT_SUCCESS;
  154. }