Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

02_cube.cpp 4.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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)
  56. {
  57. WorldEntity::TickDraw(seconds);
  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("in_Vertex",
  63. VertexUsage::Position, 0);
  64. m_color = m_shader->GetAttribLocation("in_Color",
  65. VertexUsage::Color, 0);
  66. m_vdecl =
  67. new VertexDeclaration(VertexStream<vec3,vec3>(VertexUsage::Position,
  68. VertexUsage::Color));
  69. m_vbo = new VertexBuffer(m_mesh.Bytes());
  70. void *mesh = m_vbo->Lock(0, 0);
  71. memcpy(mesh, &m_mesh[0], m_mesh.Bytes());
  72. m_vbo->Unlock();
  73. m_lines_ibo = new IndexBuffer(m_lines_indices.Bytes());
  74. void *indices = m_lines_ibo->Lock(0, 0);
  75. memcpy(indices, &m_lines_indices[0], m_lines_indices.Bytes());
  76. m_lines_ibo->Unlock();
  77. m_faces_ibo = new IndexBuffer(m_faces_indices.Bytes());
  78. indices = m_faces_ibo->Lock(0, 0);
  79. memcpy(indices, &m_faces_indices[0], m_faces_indices.Bytes());
  80. m_faces_ibo->Unlock();
  81. /* FIXME: this object never cleans up */
  82. m_ready = true;
  83. }
  84. g_renderer->SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
  85. m_shader->Bind();
  86. m_vdecl->SetStream(m_vbo, m_coord, m_color);
  87. m_vdecl->Bind();
  88. m_shader->SetUniform(m_mvp, m_matrix);
  89. m_lines_ibo->Bind();
  90. m_vdecl->DrawIndexedElements(MeshPrimitive::Lines, 0, 0,
  91. m_mesh.Count(), 0, m_lines_indices.Count());
  92. m_lines_ibo->Unbind();
  93. m_shader->SetUniform(m_mvp, m_matrix * mat4::scale(0.5f));
  94. m_faces_ibo->Bind();
  95. m_vdecl->DrawIndexedElements(MeshPrimitive::Triangles, 0, 0,
  96. m_mesh.Count(), 0, m_faces_indices.Count());
  97. m_faces_ibo->Unbind();
  98. m_vdecl->Unbind();
  99. }
  100. private:
  101. float m_angle;
  102. mat4 m_matrix;
  103. Array<vec3,vec3> m_mesh;
  104. Array<uint16_t> m_lines_indices, m_faces_indices;
  105. Shader *m_shader;
  106. ShaderAttrib m_coord, m_color;
  107. ShaderUniform m_mvp;
  108. VertexDeclaration *m_vdecl;
  109. VertexBuffer *m_vbo;
  110. IndexBuffer *m_lines_ibo, *m_faces_ibo;
  111. bool m_ready;
  112. };
  113. int main(int argc, char **argv)
  114. {
  115. System::Init(argc, argv);
  116. Application app("Tutorial 2: Cube", ivec2(640, 480), 60.0f);
  117. new DebugFps(5, 5);
  118. new Cube();
  119. app.Run();
  120. return EXIT_SUCCESS;
  121. }