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

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