Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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