You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

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