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.
 
 
 

194 line
6.0 KiB

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