選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

147 行
4.5 KiB

  1. //
  2. // Lol Engine - Cube tutorial
  3. //
  4. // Copyright: (c) 2011-2013 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://www.wtfpl.net/ for more details.
  9. //
  10. #if HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <lol/engine.h>
  14. #include "loldebug.h"
  15. using namespace lol;
  16. LOLFX_RESOURCE_DECLARE(02_cube);
  17. class Cube : public WorldEntity
  18. {
  19. public:
  20. Cube()
  21. : m_angle(0),
  22. m_mesh({ /* Front vertices/colors */
  23. { vec3(-1.0, -1.0, 1.0), vec3(1.0, 0.0, 1.0) },
  24. { vec3( 1.0, -1.0, 1.0), vec3(0.0, 1.0, 0.0) },
  25. { vec3( 1.0, 1.0, 1.0), vec3(1.0, 0.5, 0.0) },
  26. { vec3(-1.0, 1.0, 1.0), vec3(1.0, 1.0, 0.0) },
  27. /* Back */
  28. { vec3(-1.0, -1.0, -1.0), vec3(1.0, 0.0, 0.0) },
  29. { vec3( 1.0, -1.0, -1.0), vec3(0.0, 0.5, 0.0) },
  30. { vec3( 1.0, 1.0, -1.0), vec3(0.0, 0.5, 1.0) },
  31. { vec3(-1.0, 1.0, -1.0), vec3(0.0, 0.0, 1.0) } }),
  32. m_lines_indices({ 0, 1, 1, 2, 2, 3, 3, 0,
  33. 4, 5, 5, 6, 6, 7, 7, 4,
  34. 0, 4, 1, 5, 2, 6, 3, 7, }),
  35. m_faces_indices({ 0, 1, 2, 2, 3, 0, 1, 5, 6, 6, 2, 1,
  36. 7, 6, 5, 5, 4, 7, 4, 0, 3, 3, 7, 4,
  37. 4, 5, 1, 1, 0, 4, 3, 2, 6, 6, 7, 3, }),
  38. m_ready(false)
  39. {
  40. }
  41. virtual void TickGame(float seconds)
  42. {
  43. WorldEntity::TickGame(seconds);
  44. m_angle += seconds * 45.0f;
  45. mat4 anim = mat4::rotate(m_angle, vec3(0, 1, 0));
  46. mat4 model = mat4::translate(vec3(0, 0, -4.5));
  47. mat4 view = mat4::lookat(vec3(0, 2, 0), vec3(0, 0, -4), vec3(0, 1, 0));
  48. mat4 proj = mat4::perspective(45.0f, 640.0f, 480.0f, 0.1f, 10.0f);
  49. m_matrix = proj * view * model * anim;
  50. }
  51. virtual void TickDraw(float seconds, Scene &scene)
  52. {
  53. WorldEntity::TickDraw(seconds, scene);
  54. if (!m_ready)
  55. {
  56. m_shader = Shader::Create(LOLFX_RESOURCE_NAME(02_cube));
  57. m_mvp = m_shader->GetUniformLocation("in_Matrix");
  58. m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0);
  59. m_color = m_shader->GetAttribLocation(VertexUsage::Color, 0);
  60. m_vdecl =
  61. new VertexDeclaration(VertexStream<vec3,vec3>(VertexUsage::Position,
  62. VertexUsage::Color));
  63. m_vbo = new VertexBuffer(m_mesh.bytes());
  64. void *mesh = m_vbo->Lock(0, 0);
  65. memcpy(mesh, &m_mesh[0], m_mesh.bytes());
  66. m_vbo->Unlock();
  67. m_lines_ibo = new IndexBuffer(m_lines_indices.bytes());
  68. void *indices = m_lines_ibo->Lock(0, 0);
  69. memcpy(indices, &m_lines_indices[0], m_lines_indices.bytes());
  70. m_lines_ibo->Unlock();
  71. m_faces_ibo = new IndexBuffer(m_faces_indices.bytes());
  72. indices = m_faces_ibo->Lock(0, 0);
  73. memcpy(indices, &m_faces_indices[0], m_faces_indices.bytes());
  74. m_faces_ibo->Unlock();
  75. /* FIXME: this object never cleans up */
  76. m_ready = true;
  77. }
  78. Renderer::Get()->SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
  79. m_shader->Bind();
  80. m_vdecl->SetStream(m_vbo, m_coord, m_color);
  81. m_vdecl->Bind();
  82. m_shader->SetUniform(m_mvp, m_matrix);
  83. m_lines_ibo->Bind();
  84. m_vdecl->DrawIndexedElements(MeshPrimitive::Lines, 0, 0,
  85. m_mesh.count(), 0, m_lines_indices.count());
  86. m_lines_ibo->Unbind();
  87. m_shader->SetUniform(m_mvp, m_matrix * mat4::scale(0.5f));
  88. m_faces_ibo->Bind();
  89. m_vdecl->DrawIndexedElements(MeshPrimitive::Triangles, 0, 0,
  90. m_mesh.count(), 0, m_faces_indices.count());
  91. m_faces_ibo->Unbind();
  92. m_vdecl->Unbind();
  93. }
  94. private:
  95. float m_angle;
  96. mat4 m_matrix;
  97. array<vec3,vec3> m_mesh;
  98. array<uint16_t> m_lines_indices, m_faces_indices;
  99. Shader *m_shader;
  100. ShaderAttrib m_coord, m_color;
  101. ShaderUniform m_mvp;
  102. VertexDeclaration *m_vdecl;
  103. VertexBuffer *m_vbo;
  104. IndexBuffer *m_lines_ibo, *m_faces_ibo;
  105. bool m_ready;
  106. };
  107. int main(int argc, char **argv)
  108. {
  109. System::Init(argc, argv);
  110. Application app("Tutorial 2: Cube", ivec2(640, 480), 60.0f);
  111. new DebugFps(5, 5);
  112. new Cube();
  113. app.Run();
  114. return EXIT_SUCCESS;
  115. }