Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

02_cube.cpp 4.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. extern char const *lolfx_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_indices << i16vec3(0, 1, 2);
  35. m_indices << i16vec3(2, 3, 0);
  36. m_indices << i16vec3(1, 5, 6);
  37. m_indices << i16vec3(6, 2, 1);
  38. m_indices << i16vec3(7, 6, 5);
  39. m_indices << i16vec3(5, 4, 7);
  40. m_indices << i16vec3(4, 0, 3);
  41. m_indices << i16vec3(3, 7, 4);
  42. m_indices << i16vec3(4, 5, 1);
  43. m_indices << i16vec3(1, 0, 4);
  44. m_indices << i16vec3(3, 2, 6);
  45. m_indices << i16vec3(6, 7, 3);
  46. m_ready = false;
  47. }
  48. virtual void TickGame(float seconds)
  49. {
  50. WorldEntity::TickGame(seconds);
  51. m_angle += seconds * 45.0f;
  52. mat4 anim = mat4::rotate(m_angle, vec3(0, 1, 0));
  53. mat4 model = mat4::translate(vec3(0, 0, -4.5));
  54. mat4 view = mat4::lookat(vec3(0, 2, 0), vec3(0, 0, -4), vec3(0, 1, 0));
  55. mat4 proj = mat4::perspective(45.0f, 640.0f, 480.0f, 0.1f, 10.0f);
  56. m_matrix = proj * view * model * anim;
  57. }
  58. virtual void TickDraw(float seconds)
  59. {
  60. WorldEntity::TickDraw(seconds);
  61. if (!m_ready)
  62. {
  63. m_shader = Shader::Create(lolfx_02_cube);
  64. m_mvp = m_shader->GetUniformLocation("in_Matrix");
  65. m_coord = m_shader->GetAttribLocation("in_Vertex",
  66. VertexUsage::Position, 0);
  67. m_color = m_shader->GetAttribLocation("in_Color",
  68. VertexUsage::Color, 0);
  69. m_vdecl =
  70. new VertexDeclaration(VertexStream<vec3,vec3>(VertexUsage::Position,
  71. VertexUsage::Color));
  72. m_vbo = new VertexBuffer(m_mesh.Bytes());
  73. void *mesh = m_vbo->Lock(0, 0);
  74. memcpy(mesh, &m_mesh[0], m_mesh.Bytes());
  75. m_vbo->Unlock();
  76. m_ibo = new IndexBuffer(m_indices.Bytes());
  77. void *indices = m_ibo->Lock(0, 0);
  78. memcpy(indices, &m_indices[0], m_indices.Bytes());
  79. m_ibo->Unlock();
  80. /* FIXME: this object never cleans up */
  81. m_ready = true;
  82. }
  83. Video::SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
  84. m_shader->Bind();
  85. m_shader->SetUniform(m_mvp, m_matrix);
  86. m_vdecl->SetStream(m_vbo, m_coord, m_color);
  87. m_vdecl->Bind();
  88. m_ibo->Bind();
  89. m_vdecl->DrawIndexedElements(MeshPrimitive::Triangles, 0, 0,
  90. m_mesh.Count(), 0, m_indices.Count() * 3);
  91. m_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<i16vec3> m_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_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. }