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.
 
 
 

158 lines
4.4 KiB

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