25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

02_cube.cpp 4.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include "core.h"
  14. #include "loldebug.h"
  15. using namespace std;
  16. using namespace lol;
  17. LOLFX_RESOURCE_DECLARE(02_cube);
  18. class Cube : public WorldEntity
  19. {
  20. public:
  21. Cube()
  22. : m_angle(0),
  23. m_mesh({ /* Front vertices/colors */
  24. { vec3(-1.0, -1.0, 1.0), vec3(1.0, 0.0, 1.0) },
  25. { vec3( 1.0, -1.0, 1.0), vec3(0.0, 1.0, 0.0) },
  26. { vec3( 1.0, 1.0, 1.0), vec3(1.0, 0.5, 0.0) },
  27. { vec3(-1.0, 1.0, 1.0), vec3(1.0, 1.0, 0.0) },
  28. /* Back */
  29. { vec3(-1.0, -1.0, -1.0), vec3(1.0, 0.0, 0.0) },
  30. { vec3( 1.0, -1.0, -1.0), vec3(0.0, 0.5, 0.0) },
  31. { vec3( 1.0, 1.0, -1.0), vec3(0.0, 0.5, 1.0) },
  32. { vec3(-1.0, 1.0, -1.0), vec3(0.0, 0.0, 1.0) } }),
  33. m_lines_indices({ 0, 1, 1, 2, 2, 3, 3, 0,
  34. 4, 5, 5, 6, 6, 7, 7, 4,
  35. 0, 4, 1, 5, 2, 6, 3, 7, }),
  36. m_faces_indices({ 0, 1, 2, 2, 3, 0, 1, 5, 6, 6, 2, 1,
  37. 7, 6, 5, 5, 4, 7, 4, 0, 3, 3, 7, 4,
  38. 4, 5, 1, 1, 0, 4, 3, 2, 6, 6, 7, 3, }),
  39. m_ready(false)
  40. {
  41. }
  42. virtual void TickGame(float seconds)
  43. {
  44. WorldEntity::TickGame(seconds);
  45. m_angle += seconds * 45.0f;
  46. mat4 anim = mat4::rotate(m_angle, vec3(0, 1, 0));
  47. mat4 model = mat4::translate(vec3(0, 0, -4.5));
  48. mat4 view = mat4::lookat(vec3(0, 2, 0), vec3(0, 0, -4), vec3(0, 1, 0));
  49. mat4 proj = mat4::perspective(45.0f, 640.0f, 480.0f, 0.1f, 10.0f);
  50. m_matrix = proj * view * model * anim;
  51. }
  52. virtual void TickDraw(float seconds, Scene &scene)
  53. {
  54. WorldEntity::TickDraw(seconds, scene);
  55. if (!m_ready)
  56. {
  57. m_shader = Shader::Create(LOLFX_RESOURCE_NAME(02_cube));
  58. m_mvp = m_shader->GetUniformLocation("in_Matrix");
  59. m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0);
  60. m_color = m_shader->GetAttribLocation(VertexUsage::Color, 0);
  61. m_vdecl =
  62. new VertexDeclaration(VertexStream<vec3,vec3>(VertexUsage::Position,
  63. VertexUsage::Color));
  64. m_vbo = new VertexBuffer(m_mesh.Bytes());
  65. void *mesh = m_vbo->Lock(0, 0);
  66. memcpy(mesh, &m_mesh[0], m_mesh.Bytes());
  67. m_vbo->Unlock();
  68. m_lines_ibo = new IndexBuffer(m_lines_indices.Bytes());
  69. void *indices = m_lines_ibo->Lock(0, 0);
  70. memcpy(indices, &m_lines_indices[0], m_lines_indices.Bytes());
  71. m_lines_ibo->Unlock();
  72. m_faces_ibo = new IndexBuffer(m_faces_indices.Bytes());
  73. indices = m_faces_ibo->Lock(0, 0);
  74. memcpy(indices, &m_faces_indices[0], m_faces_indices.Bytes());
  75. m_faces_ibo->Unlock();
  76. /* FIXME: this object never cleans up */
  77. m_ready = true;
  78. }
  79. g_renderer->SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
  80. m_shader->Bind();
  81. m_vdecl->SetStream(m_vbo, m_coord, m_color);
  82. m_vdecl->Bind();
  83. m_shader->SetUniform(m_mvp, m_matrix);
  84. m_lines_ibo->Bind();
  85. m_vdecl->DrawIndexedElements(MeshPrimitive::Lines, 0, 0,
  86. m_mesh.Count(), 0, m_lines_indices.Count());
  87. m_lines_ibo->Unbind();
  88. m_shader->SetUniform(m_mvp, m_matrix * mat4::scale(0.5f));
  89. m_faces_ibo->Bind();
  90. m_vdecl->DrawIndexedElements(MeshPrimitive::Triangles, 0, 0,
  91. m_mesh.Count(), 0, m_faces_indices.Count());
  92. m_faces_ibo->Unbind();
  93. m_vdecl->Unbind();
  94. }
  95. private:
  96. float m_angle;
  97. mat4 m_matrix;
  98. array<vec3,vec3> m_mesh;
  99. array<uint16_t> m_lines_indices, m_faces_indices;
  100. Shader *m_shader;
  101. ShaderAttrib m_coord, m_color;
  102. ShaderUniform m_mvp;
  103. VertexDeclaration *m_vdecl;
  104. VertexBuffer *m_vbo;
  105. IndexBuffer *m_lines_ibo, *m_faces_ibo;
  106. bool m_ready;
  107. };
  108. int main(int argc, char **argv)
  109. {
  110. System::Init(argc, argv);
  111. Application app("Tutorial 2: Cube", ivec2(640, 480), 60.0f);
  112. new DebugFps(5, 5);
  113. new Cube();
  114. app.Run();
  115. return EXIT_SUCCESS;
  116. }