Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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