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.
 
 
 

191 line
5.8 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. m_vbo->set_data(mesh.data(), mesh.bytes());
  75. m_lines_ibo = std::make_shared<IndexBuffer>(lines_indices.bytes());
  76. m_lines_ibo->set_data(lines_indices.data(), lines_indices.bytes());
  77. m_faces_ibo = std::make_shared<IndexBuffer>(faces_indices.bytes());
  78. m_faces_ibo->set_data(faces_indices.data(), faces_indices.bytes());
  79. return true;
  80. }
  81. virtual void tick_game(float seconds) override
  82. {
  83. WorldEntity::tick_game(seconds);
  84. m_angle += seconds * radians(45.0f);
  85. mat4 anim = mat4::rotate(m_angle, vec3(0, 1, 0));
  86. mat4 model = mat4::translate(vec3(0, 0, -4.5));
  87. mat4 view = mat4::lookat(vec3(0, 2, 0), vec3(0, 0, -4), vec3(0, 1, 0));
  88. mat4 proj = mat4::perspective(radians(45.0f), 640.0f, 480.0f, 0.1f, 10.0f);
  89. m_matrix = proj * view * model * anim;
  90. {
  91. auto context0 = Debug::DrawContext::New(Color::red);
  92. {
  93. auto context1 = Debug::DrawContext::New(Color::blue);
  94. Debug::DrawBox(box3(vec3(0.f), vec3(1.2f)));
  95. Debug::DrawGrid(vec3(0.f), vec3::axis_x, vec3::axis_y, vec3::axis_z, 10.0f);
  96. {
  97. auto context2 = Debug::DrawContext::New(context0);
  98. Debug::DrawBox(box3(vec3(0.f), vec3(1.3f)));
  99. }
  100. {
  101. auto context2 = Debug::DrawContext::New(context0);
  102. context2.SetColor(Color::yellow);
  103. Debug::DrawBox(box3(vec3(-1.f), vec3(1.4f)));
  104. }
  105. }
  106. Debug::DrawBox(box3(vec3(0.f), vec3(1.1f)));
  107. }
  108. }
  109. virtual void tick_draw(float seconds, Scene &scene) override
  110. {
  111. WorldEntity::tick_draw(seconds, scene);
  112. scene.get_renderer()->clear_color(vec4(0.0f, 0.0f, 0.0f, 1.0f));
  113. m_shader->Bind();
  114. m_vdecl->Bind();
  115. m_vdecl->SetStream(m_vbo, m_coord, m_color);
  116. m_shader->SetUniform(m_mvp, m_matrix);
  117. m_lines_ibo->Bind();
  118. m_vdecl->DrawIndexedElements(MeshPrimitive::Lines, m_lines_ibo->size() / sizeof(uint16_t));
  119. m_lines_ibo->Unbind();
  120. m_shader->SetUniform(m_mvp, m_matrix * mat4::scale(0.5f));
  121. m_faces_ibo->Bind();
  122. m_vdecl->DrawIndexedElements(MeshPrimitive::Triangles, m_faces_ibo->size() / sizeof(uint16_t));
  123. m_faces_ibo->Unbind();
  124. m_vdecl->Unbind();
  125. }
  126. virtual bool release_draw() override
  127. {
  128. m_shader.reset();
  129. m_vdecl.reset();
  130. m_vbo.reset();
  131. m_lines_ibo.reset();
  132. m_faces_ibo.reset();
  133. return true;
  134. }
  135. private:
  136. Camera* m_camera = nullptr;
  137. float m_angle = 0.0f;
  138. mat4 m_matrix;
  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. }