Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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