Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. 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(
  70. #if !defined __CELLOS_LV2__ && !defined _XBOX && !defined USE_D3D9
  71. "#version 120\n"
  72. "attribute vec3 in_Vertex;"
  73. "attribute vec3 in_Color;"
  74. "uniform mat4 in_Matrix;"
  75. "varying vec3 pass_Color;"
  76. ""
  77. "void main(void) {"
  78. " gl_Position = in_Matrix * vec4(in_Vertex, 1.0);"
  79. " pass_Color = in_Color;"
  80. "}",
  81. "#version 120\n"
  82. "varying vec3 pass_Color;"
  83. ""
  84. "void main(void) {"
  85. " gl_FragColor = vec4(pass_Color, 1.0);"
  86. "}"
  87. #else
  88. "void main(float3 in_Vertex : POSITION,"
  89. " float3 in_Color : COLOR,"
  90. " uniform float4x4 in_Matrix,"
  91. " out float4 out_Position : POSITION,"
  92. " out float3 pass_Color : COLOR) {"
  93. " pass_Color = in_Color;"
  94. " out_Position = mul(in_Matrix, float4(in_Vertex, 1.0));"
  95. "}",
  96. "void main(float3 pass_Color : COLOR,"
  97. " out float4 out_FragColor : COLOR) {"
  98. " out_FragColor = float4(pass_Color, 1.0);"
  99. "}"
  100. #endif
  101. );
  102. m_mvp = m_shader->GetUniformLocation("in_Matrix");
  103. m_coord = m_shader->GetAttribLocation("in_Vertex",
  104. VertexUsage::Position, 0);
  105. m_color = m_shader->GetAttribLocation("in_Color",
  106. VertexUsage::Color, 0);
  107. m_vdecl =
  108. new VertexDeclaration(VertexStream<vec3,vec3>(VertexUsage::Position,
  109. VertexUsage::Color));
  110. m_vbo = new VertexBuffer(m_mesh.Bytes());
  111. void *mesh = m_vbo->Lock(0, 0);
  112. memcpy(mesh, &m_mesh[0], m_mesh.Bytes());
  113. m_vbo->Unlock();
  114. m_ibo = new IndexBuffer(m_indices.Bytes());
  115. void *indices = m_ibo->Lock(0, 0);
  116. memcpy(indices, &m_indices[0], m_indices.Bytes());
  117. m_ibo->Unlock();
  118. /* FIXME: this object never cleans up */
  119. m_ready = true;
  120. }
  121. Video::SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
  122. m_shader->Bind();
  123. m_shader->SetUniform(m_mvp, m_matrix);
  124. m_vdecl->SetStream(m_vbo, m_coord, m_color);
  125. m_vdecl->Bind();
  126. m_ibo->Bind();
  127. m_vdecl->DrawIndexedElements(MeshPrimitive::Triangles, 0, 0,
  128. m_mesh.Count(), 0, m_indices.Count());
  129. m_ibo->Unbind();
  130. m_vdecl->Unbind();
  131. }
  132. private:
  133. float m_angle;
  134. mat4 m_matrix;
  135. Array<vec3,vec3> m_mesh;
  136. Array<i16vec3> m_indices;
  137. Shader *m_shader;
  138. ShaderAttrib m_coord, m_color;
  139. ShaderUniform m_mvp;
  140. VertexDeclaration *m_vdecl;
  141. VertexBuffer *m_vbo;
  142. IndexBuffer *m_ibo;
  143. bool m_ready;
  144. };
  145. int main(int argc, char **argv)
  146. {
  147. Application app("Tutorial 2: Cube", ivec2(640, 480), 60.0f);
  148. #if defined _MSC_VER && !defined _XBOX
  149. _chdir("..");
  150. #elif defined _WIN32 && !defined _XBOX
  151. _chdir("../..");
  152. #endif
  153. new DebugFps(5, 5);
  154. new Cube();
  155. app.Run();
  156. return EXIT_SUCCESS;
  157. }