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.
 
 
 

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