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

241 строка
7.5 KiB

  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 vertices/colors */
  42. m_mesh[0] = vec3(-1.0, -1.0, 1.0); m_mesh[1] = vec3(1.0, 0.0, 1.0);
  43. m_mesh[2] = vec3( 1.0, -1.0, 1.0); m_mesh[3] = vec3(0.0, 1.0, 0.0);
  44. m_mesh[4] = vec3( 1.0, 1.0, 1.0); m_mesh[5] = vec3(1.0, 0.5, 0.0);
  45. m_mesh[6] = vec3(-1.0, 1.0, 1.0); m_mesh[7] = vec3(1.0, 1.0, 0.0);
  46. /* Back */
  47. m_mesh[8] = vec3(-1.0, -1.0, -1.0); m_mesh[9] = vec3(1.0, 0.0, 0.0);
  48. m_mesh[10] = vec3( 1.0, -1.0, -1.0); m_mesh[11] = vec3(0.0, 0.5, 0.0);
  49. m_mesh[12] = vec3( 1.0, 1.0, -1.0); m_mesh[13] = vec3(0.0, 0.5, 1.0);
  50. m_mesh[14] = vec3(-1.0, 1.0, -1.0); m_mesh[15] = vec3(0.0, 0.0, 1.0);
  51. m_indices[0] = i16vec3(0, 1, 2);
  52. m_indices[1] = i16vec3(2, 3, 0);
  53. m_indices[2] = i16vec3(1, 5, 6);
  54. m_indices[3] = i16vec3(6, 2, 1);
  55. m_indices[4] = i16vec3(7, 6, 5);
  56. m_indices[5] = i16vec3(5, 4, 7);
  57. m_indices[6] = i16vec3(4, 0, 3);
  58. m_indices[7] = i16vec3(3, 7, 4);
  59. m_indices[8] = i16vec3(4, 5, 1);
  60. m_indices[9] = i16vec3(1, 0, 4);
  61. m_indices[10] = i16vec3(3, 2, 6);
  62. m_indices[11] = i16vec3(6, 7, 3);
  63. m_ready = false;
  64. }
  65. virtual void TickGame(float deltams)
  66. {
  67. WorldEntity::TickGame(deltams);
  68. m_angle += deltams / 1000.0f * 45.0f;
  69. mat4 anim = mat4::rotate(m_angle, vec3(0, 1, 0));
  70. mat4 model = mat4::translate(vec3(0, 0, -4));
  71. mat4 view = mat4::lookat(vec3(0, 2, 0), vec3(0, 0, -4), vec3(0, 1, 0));
  72. mat4 proj = mat4::perspective(45.0f, 640.0f, 480.0f, 0.1f, 10.0f);
  73. m_matrix = proj * view * model * anim;
  74. }
  75. virtual void TickDraw(float deltams)
  76. {
  77. WorldEntity::TickDraw(deltams);
  78. if (!m_ready)
  79. {
  80. m_shader = Shader::Create(
  81. #if !defined __CELLOS_LV2__ && !defined _XBOX && !defined USE_D3D9
  82. "#version 120\n"
  83. "attribute vec3 in_Vertex;"
  84. "attribute vec3 in_Color;"
  85. "uniform mat4 in_Matrix;"
  86. "varying vec3 pass_Color;"
  87. ""
  88. "void main(void) {"
  89. " gl_Position = in_Matrix * vec4(in_Vertex, 1.0);"
  90. " pass_Color = in_Color;"
  91. "}",
  92. "#version 120\n"
  93. "varying vec3 pass_Color;"
  94. ""
  95. "void main(void) {"
  96. " gl_FragColor = vec4(pass_Color, 1.0);"
  97. "}"
  98. #else
  99. "void main(float3 in_Vertex : POSITION,"
  100. " float3 in_Color : COLOR,"
  101. " uniform float4x4 in_Matrix,"
  102. " out float4 out_Position : POSITION,"
  103. " out float3 pass_Color : COLOR) {"
  104. " pass_Color = in_Color;"
  105. " out_Position = mul(in_Matrix, float4(in_Vertex, 1.0));"
  106. "}",
  107. "void main(float3 pass_Color : COLOR,"
  108. " out float4 out_FragColor : COLOR) {"
  109. " out_FragColor = float4(pass_Color, 1.0);"
  110. "}"
  111. #endif
  112. );
  113. m_mvp = m_shader->GetUniformLocation("in_Matrix");
  114. m_coord = m_shader->GetAttribLocation("in_Vertex",
  115. VertexUsage::Position, 0);
  116. m_color = m_shader->GetAttribLocation("in_Color",
  117. VertexUsage::Color, 0);
  118. m_vdecl =
  119. new VertexDeclaration(VertexStream<vec3,vec3>(VertexUsage::Position,
  120. VertexUsage::Color));
  121. m_vbo = new VertexBuffer(sizeof(m_mesh));
  122. void *mesh = m_vbo->Lock(0, 0);
  123. memcpy(mesh, m_mesh, sizeof(m_mesh));
  124. m_vbo->Unlock();
  125. #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__ && !defined _XBOX && !defined USE_D3D9
  126. /* Method 1: store vertex buffer on the GPU memory */
  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 || defined USE_D3D9
  132. int16_t *indices;
  133. if (FAILED(g_d3ddevice->CreateIndexBuffer(sizeof(m_indices), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &m_ibo, NULL)))
  134. Abort();
  135. if (FAILED(m_ibo->Lock(0, 0, (void **)&indices, 0)))
  136. Abort();
  137. memcpy(indices, m_indices, sizeof(m_indices));
  138. m_ibo->Unlock();
  139. #else
  140. /* TODO */
  141. #endif
  142. /* FIXME: this object never cleans up */
  143. m_ready = true;
  144. }
  145. Video::SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
  146. m_shader->Bind();
  147. m_shader->SetUniform(m_mvp, m_matrix);
  148. m_vdecl->SetStream(m_vbo, m_coord, m_color);
  149. m_vdecl->Bind();
  150. #if defined _XBOX || defined USE_D3D9
  151. if (FAILED(g_d3ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW)))
  152. Abort();
  153. if (FAILED(g_d3ddevice->SetIndices(m_ibo)))
  154. Abort();
  155. if (FAILED(g_d3ddevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, sizeof(m_indices) / sizeof(*m_indices))))
  156. Abort();
  157. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  158. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo);
  159. int size;
  160. glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
  161. glDrawElements(GL_TRIANGLES, size / sizeof(uint16_t), GL_UNSIGNED_SHORT, 0);
  162. glBindBuffer(GL_ARRAY_BUFFER, 0);
  163. #else
  164. /* FIXME */
  165. glEnableClientState(GL_VERTEX_ARRAY);
  166. glVertexPointer(3, GL_FLOAT, 0, m_vertices);
  167. glDisableClientState(GL_VERTEX_ARRAY);
  168. #endif
  169. m_vdecl->Unbind();
  170. }
  171. private:
  172. float m_angle;
  173. mat4 m_matrix;
  174. vec3 m_mesh[16];
  175. i16vec3 m_indices[12];
  176. Shader *m_shader;
  177. ShaderAttrib m_coord, m_color;
  178. ShaderUniform m_mvp;
  179. VertexDeclaration *m_vdecl;
  180. VertexBuffer *m_vbo;
  181. #if defined USE_D3D9
  182. IDirect3DIndexBuffer9 *m_ibo;
  183. #elif defined _XBOX
  184. D3DIndexBuffer *m_ibo;
  185. #else
  186. #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  187. GLuint m_ibo;
  188. #endif
  189. #endif
  190. bool m_ready;
  191. };
  192. int main(int argc, char **argv)
  193. {
  194. Application app("Tutorial 2: Cube", ivec2(640, 480), 60.0f);
  195. #if defined _MSC_VER && !defined _XBOX
  196. _chdir("..");
  197. #elif defined _WIN32 && !defined _XBOX
  198. _chdir("../..");
  199. #endif
  200. new DebugFps(5, 5);
  201. new Cube();
  202. app.Run();
  203. return EXIT_SUCCESS;
  204. }