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.

пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. //
  2. // Lol Engine - Cube tutorial
  3. //
  4. // Copyright: (c) 2011 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 "lolgl.h"
  15. #include "loldebug.h"
  16. using namespace std;
  17. using namespace lol;
  18. #if defined _WIN32 && defined USE_D3D9
  19. # define FAR
  20. # define NEAR
  21. # include <d3d9.h>
  22. #endif
  23. #if USE_SDL && defined __APPLE__
  24. # include <SDL_main.h>
  25. #endif
  26. #if defined _WIN32
  27. # undef main /* FIXME: still needed? */
  28. # include <direct.h>
  29. #endif
  30. #if defined USE_D3D9
  31. extern IDirect3DDevice9 *g_d3ddevice;
  32. #elif defined _XBOX
  33. extern D3DDevice *g_d3ddevice;
  34. #endif
  35. class Cube : public WorldEntity
  36. {
  37. public:
  38. Cube()
  39. {
  40. m_angle = 0;
  41. /* Front */
  42. m_vertices[0] = vec3(-1.0, -1.0, 1.0);
  43. m_vertices[1] = vec3( 1.0, -1.0, 1.0);
  44. m_vertices[2] = vec3( 1.0, 1.0, 1.0);
  45. m_vertices[3] = vec3(-1.0, 1.0, 1.0);
  46. /* Back */
  47. m_vertices[4] = vec3(-1.0, -1.0, -1.0);
  48. m_vertices[5] = vec3( 1.0, -1.0, -1.0);
  49. m_vertices[6] = vec3( 1.0, 1.0, -1.0);
  50. m_vertices[7] = vec3(-1.0, 1.0, -1.0);
  51. m_colors[0] = vec3(1.0, 0.0, 0.0);
  52. m_colors[1] = vec3(0.0, 1.0, 0.0);
  53. m_colors[2] = vec3(0.0, 0.0, 1.0);
  54. m_colors[3] = vec3(1.0, 1.0, 1.0);
  55. m_colors[4] = vec3(1.0, 0.0, 0.0);
  56. m_colors[5] = vec3(0.0, 1.0, 0.0);
  57. m_colors[6] = vec3(0.0, 0.0, 1.0);
  58. m_colors[7] = vec3(1.0, 1.0, 1.0);
  59. m_indices[0] = i16vec3(0, 1, 2);
  60. m_indices[1] = i16vec3(2, 3, 0);
  61. m_indices[2] = i16vec3(1, 5, 6);
  62. m_indices[3] = i16vec3(6, 2, 1);
  63. m_indices[4] = i16vec3(7, 6, 5);
  64. m_indices[5] = i16vec3(5, 4, 7);
  65. m_indices[6] = i16vec3(4, 0, 3);
  66. m_indices[7] = i16vec3(3, 7, 4);
  67. m_indices[8] = i16vec3(4, 5, 1);
  68. m_indices[9] = i16vec3(1, 0, 4);
  69. m_indices[10] = i16vec3(3, 2, 6);
  70. m_indices[11] = i16vec3(6, 7, 3);
  71. m_ready = false;
  72. }
  73. virtual void TickGame(float deltams)
  74. {
  75. WorldEntity::TickGame(deltams);
  76. m_angle += deltams / 1000.0f * 45.0f;
  77. mat4 anim = mat4::rotate(m_angle, vec3(0, 1, 0));
  78. mat4 model = mat4::translate(vec3(0, 0, -4));
  79. mat4 view = mat4::lookat(vec3(0, 2, 0), vec3(0, 0, -4), vec3(0, 1, 0));
  80. mat4 proj = mat4::perspective(45.0f, 640.0f, 480.0f, 0.1f, 10.0f);
  81. m_matrix = proj * view * model * anim;
  82. }
  83. virtual void TickDraw(float deltams)
  84. {
  85. WorldEntity::TickDraw(deltams);
  86. if (!m_ready)
  87. {
  88. m_shader = Shader::Create(
  89. #if !defined __CELLOS_LV2__ && !defined _XBOX && !defined USE_D3D9
  90. "#version 120\n"
  91. "attribute vec3 in_Vertex;"
  92. "attribute vec3 in_Color;"
  93. "uniform mat4 in_Matrix;"
  94. "varying vec3 pass_Color;"
  95. ""
  96. "void main(void) {"
  97. " gl_Position = in_Matrix * vec4(in_Vertex, 1.0);"
  98. " pass_Color = in_Color;"
  99. "}",
  100. "#version 120\n"
  101. "varying vec3 pass_Color;"
  102. ""
  103. "void main(void) {"
  104. " gl_FragColor = vec4(pass_Color, 1.0);"
  105. "}"
  106. #else
  107. "void main(float3 in_Vertex : POSITION,"
  108. " float3 in_Color : COLOR,"
  109. " uniform float4x4 in_Matrix,"
  110. " out float4 out_Position : POSITION,"
  111. " out float3 pass_Color : COLOR) {"
  112. " pass_Color = in_Color;"
  113. " out_Position = mul(in_Matrix, float4(in_Vertex, 1.0));"
  114. "}",
  115. "void main(float3 pass_Color : COLOR,"
  116. " out float4 out_FragColor : COLOR) {"
  117. " out_FragColor = float4(pass_Color, 1.0);"
  118. "}"
  119. #endif
  120. );
  121. m_mvp = m_shader->GetUniformLocation("in_Matrix");
  122. m_coord = m_shader->GetAttribLocation("in_Vertex",
  123. VertexUsage::Position, 0);
  124. m_color = m_shader->GetAttribLocation("in_Color",
  125. VertexUsage::Color, 0);
  126. m_vdecl =
  127. new VertexDeclaration(VertexStream<vec3>(VertexUsage::Position),
  128. VertexStream<vec3>(VertexUsage::Color));
  129. m_vbo = new VertexBuffer(sizeof(m_vertices));
  130. void *vertices = m_vbo->Lock(0, 0);
  131. memcpy(vertices, m_vertices, sizeof(m_vertices));
  132. m_vbo->Unlock();
  133. m_cbo = new VertexBuffer(sizeof(m_colors));
  134. void *colors = m_cbo->Lock(0, 0);
  135. memcpy(colors, m_colors, sizeof(m_colors));
  136. m_cbo->Unlock();
  137. #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__ && !defined _XBOX && !defined USE_D3D9
  138. /* Method 1: store vertex buffer on the GPU memory */
  139. glGenBuffers(1, &m_ibo);
  140. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo);
  141. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(m_indices), m_indices,
  142. GL_STATIC_DRAW);
  143. #elif defined _XBOX || defined USE_D3D9
  144. int16_t *indices;
  145. if (FAILED(g_d3ddevice->CreateIndexBuffer(sizeof(m_indices), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &m_ibo, NULL)))
  146. exit(0);
  147. if (FAILED(m_ibo->Lock(0, 0, (void **)&indices, 0)))
  148. exit(0);
  149. memcpy(indices, m_indices, sizeof(m_indices));
  150. m_ibo->Unlock();
  151. #else
  152. /* TODO */
  153. #endif
  154. /* FIXME: this object never cleans up */
  155. m_ready = true;
  156. }
  157. m_shader->Bind();
  158. m_shader->SetUniform(m_mvp, m_matrix);
  159. m_vdecl->Bind();
  160. m_vdecl->SetStream(m_vbo, m_coord);
  161. m_vdecl->SetStream(m_cbo, m_color);
  162. #if defined _XBOX || defined USE_D3D9
  163. g_d3ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
  164. g_d3ddevice->SetIndices(m_ibo);
  165. g_d3ddevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 0, 0, sizeof(m_indices) / sizeof(*m_indices));
  166. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  167. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo);
  168. int size;
  169. glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
  170. glDrawElements(GL_TRIANGLES, size / sizeof(uint16_t), GL_UNSIGNED_SHORT, 0);
  171. glBindBuffer(GL_ARRAY_BUFFER, 0);
  172. #else
  173. glEnableClientState(GL_VERTEX_ARRAY);
  174. glVertexPointer(3, GL_FLOAT, 0, m_vertices);
  175. glDisableClientState(GL_VERTEX_ARRAY);
  176. #endif
  177. m_vdecl->Unbind();
  178. }
  179. private:
  180. float m_angle;
  181. mat4 m_matrix;
  182. vec3 m_vertices[8];
  183. vec3 m_colors[8];
  184. i16vec3 m_indices[12];
  185. Shader *m_shader;
  186. ShaderAttrib m_coord, m_color;
  187. ShaderUniform m_mvp;
  188. VertexDeclaration *m_vdecl;
  189. VertexBuffer *m_vbo, *m_cbo;
  190. #if defined USE_D3D9
  191. IDirect3DIndexBuffer9 *m_ibo;
  192. #elif defined _XBOX
  193. D3DIndexBuffer *m_ibo;
  194. #else
  195. #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  196. GLuint m_ibo;
  197. #endif
  198. #endif
  199. bool m_ready;
  200. };
  201. int main(int argc, char **argv)
  202. {
  203. Application app("Tutorial 2: Cube", ivec2(640, 480), 60.0f);
  204. #if defined _MSC_VER && !defined _XBOX
  205. _chdir("..");
  206. #elif defined _WIN32 && !defined _XBOX
  207. _chdir("../..");
  208. #endif
  209. new DebugFps(5, 5);
  210. new Cube();
  211. app.Run();
  212. return EXIT_SUCCESS;
  213. }