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

13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. #endif
  24. class Cube : public WorldEntity
  25. {
  26. public:
  27. Cube()
  28. {
  29. m_angle = 0;
  30. /* Front */
  31. m_vertices[0] = vec3(-1.0, -1.0, 1.0);
  32. m_vertices[1] = vec3( 1.0, -1.0, 1.0);
  33. m_vertices[2] = vec3( 1.0, 1.0, 1.0);
  34. m_vertices[3] = vec3(-1.0, 1.0, 1.0);
  35. /* Back */
  36. m_vertices[4] = vec3(-1.0, -1.0, -1.0);
  37. m_vertices[5] = vec3( 1.0, -1.0, -1.0);
  38. m_vertices[6] = vec3( 1.0, 1.0, -1.0);
  39. m_vertices[7] = vec3(-1.0, 1.0, -1.0);
  40. m_colors[0] = vec3(1.0, 0.0, 0.0);
  41. m_colors[1] = vec3(0.0, 1.0, 0.0);
  42. m_colors[2] = vec3(0.0, 0.0, 1.0);
  43. m_colors[3] = vec3(1.0, 1.0, 1.0);
  44. m_colors[4] = vec3(1.0, 0.0, 0.0);
  45. m_colors[5] = vec3(0.0, 1.0, 0.0);
  46. m_colors[6] = vec3(0.0, 0.0, 1.0);
  47. m_colors[7] = vec3(1.0, 1.0, 1.0);
  48. m_indices[0] = i16vec3(0, 1, 2);
  49. m_indices[1] = i16vec3(2, 3, 0);
  50. m_indices[2] = i16vec3(1, 5, 6);
  51. m_indices[3] = i16vec3(6, 2, 1);
  52. m_indices[4] = i16vec3(7, 6, 5);
  53. m_indices[5] = i16vec3(5, 4, 7);
  54. m_indices[6] = i16vec3(4, 0, 3);
  55. m_indices[7] = i16vec3(3, 7, 4);
  56. m_indices[8] = i16vec3(4, 5, 1);
  57. m_indices[9] = i16vec3(1, 0, 4);
  58. m_indices[10] = i16vec3(3, 2, 6);
  59. m_indices[11] = i16vec3(6, 7, 3);
  60. }
  61. virtual void TickGame(float deltams)
  62. {
  63. WorldEntity::TickGame(deltams);
  64. m_angle += deltams / 1000.0f * 45.0f;
  65. mat4 anim = mat4::rotate(m_angle, vec3(0, 1, 0));
  66. mat4 model = mat4::translate(vec3(0, 0, -4));
  67. mat4 view = mat4::lookat(vec3(0, 2, 0), vec3(0, 0, -4), vec3(0, 1, 0));
  68. mat4 proj = mat4::perspective(45.0f, 640.0f, 480.0f, 0.1f, 10.0f);
  69. m_matrix = proj * view * model * anim;
  70. }
  71. virtual void TickDraw(float deltams)
  72. {
  73. WorldEntity::TickDraw(deltams);
  74. if (!m_ready)
  75. {
  76. m_shader = Shader::Create(
  77. "#version 120\n"
  78. "attribute vec3 in_Vertex;"
  79. "attribute vec3 in_Color;"
  80. "uniform mat4 in_Matrix;"
  81. "varying vec3 pass_Color;"
  82. ""
  83. "void main(void) {"
  84. " gl_Position = in_Matrix * vec4(in_Vertex, 1.0);"
  85. " pass_Color = in_Color;"
  86. "}",
  87. "#version 120\n"
  88. "varying vec3 pass_Color;"
  89. ""
  90. "void main(void) {"
  91. " gl_FragColor = vec4(pass_Color, 1.0);"
  92. "}");
  93. m_coord = m_shader->GetAttribLocation("in_Vertex");
  94. m_color = m_shader->GetAttribLocation("in_Color");
  95. m_mvp = m_shader->GetUniformLocation("in_Matrix");
  96. m_ready = true;
  97. #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  98. /* Method 1: store vertex buffer on the GPU memory */
  99. glGenBuffers(1, &m_vbo);
  100. glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
  101. glBufferData(GL_ARRAY_BUFFER, sizeof(m_vertices), m_vertices,
  102. GL_STATIC_DRAW);
  103. glGenBuffers(1, &m_cbo);
  104. glBindBuffer(GL_ARRAY_BUFFER, m_cbo);
  105. glBufferData(GL_ARRAY_BUFFER, sizeof(m_colors), m_colors,
  106. GL_STATIC_DRAW);
  107. glGenBuffers(1, &m_ibo);
  108. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo);
  109. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(m_indices), m_indices,
  110. GL_STATIC_DRAW);
  111. #else
  112. #endif
  113. /* FIXME: this object never cleans up */
  114. }
  115. m_shader->Bind();
  116. m_shader->SetUniform(m_mvp, m_matrix);
  117. #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  118. glEnableVertexAttribArray(m_coord);
  119. glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
  120. glVertexAttribPointer(m_coord, 3, GL_FLOAT, GL_FALSE, 0, 0);
  121. glEnableVertexAttribArray(m_color);
  122. glBindBuffer(GL_ARRAY_BUFFER, m_cbo);
  123. glVertexAttribPointer(m_color, 3, GL_FLOAT, GL_FALSE, 0, 0);
  124. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo);
  125. int size;
  126. glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
  127. glDrawElements(GL_TRIANGLES, size / sizeof(uint16_t), GL_UNSIGNED_SHORT, 0);
  128. glDisableVertexAttribArray(m_coord);
  129. glDisableVertexAttribArray(m_color);
  130. glBindBuffer(GL_ARRAY_BUFFER, 0);
  131. #else
  132. glEnableClientState(GL_VERTEX_ARRAY);
  133. glVertexPointer(3, GL_FLOAT, 0, m_vertices);
  134. glDisableClientState(GL_VERTEX_ARRAY);
  135. #endif
  136. }
  137. private:
  138. float m_angle;
  139. mat4 m_matrix;
  140. vec3 m_vertices[8];
  141. vec3 m_colors[8];
  142. i16vec3 m_indices[12];
  143. Shader *m_shader;
  144. #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  145. GLuint m_vbo, m_cbo, m_ibo;
  146. #endif
  147. int m_coord, m_color, m_mvp;
  148. bool m_ready;
  149. };
  150. int main(int argc, char **argv)
  151. {
  152. Application app("Tutorial 2: Cube", ivec2(640, 480), 60.0f);
  153. new DebugFps(5, 5);
  154. new Cube();
  155. app.Run();
  156. return EXIT_SUCCESS;
  157. }