您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

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