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.
 
 
 

256 lines
7.8 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 */
  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. Abort();
  147. if (FAILED(m_ibo->Lock(0, 0, (void **)&indices, 0)))
  148. Abort();
  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. Video::SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
  158. m_shader->Bind();
  159. m_shader->SetUniform(m_mvp, m_matrix);
  160. m_vdecl->SetStream(m_vbo, m_coord);
  161. m_vdecl->SetStream(m_cbo, m_color);
  162. m_vdecl->Bind();
  163. #if defined _XBOX || defined USE_D3D9
  164. if (FAILED(g_d3ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW)))
  165. Abort();
  166. if (FAILED(g_d3ddevice->SetIndices(m_ibo)))
  167. Abort();
  168. if (FAILED(g_d3ddevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 0, 0, sizeof(m_indices) / sizeof(*m_indices))))
  169. Abort();
  170. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  171. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo);
  172. int size;
  173. glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
  174. glDrawElements(GL_TRIANGLES, size / sizeof(uint16_t), GL_UNSIGNED_SHORT, 0);
  175. glBindBuffer(GL_ARRAY_BUFFER, 0);
  176. #else
  177. glEnableClientState(GL_VERTEX_ARRAY);
  178. glVertexPointer(3, GL_FLOAT, 0, m_vertices);
  179. glDisableClientState(GL_VERTEX_ARRAY);
  180. #endif
  181. m_vdecl->Unbind();
  182. }
  183. private:
  184. float m_angle;
  185. mat4 m_matrix;
  186. vec3 m_vertices[8];
  187. vec3 m_colors[8];
  188. i16vec3 m_indices[12];
  189. Shader *m_shader;
  190. ShaderAttrib m_coord, m_color;
  191. ShaderUniform m_mvp;
  192. VertexDeclaration *m_vdecl;
  193. VertexBuffer *m_vbo, *m_cbo;
  194. #if defined USE_D3D9
  195. IDirect3DIndexBuffer9 *m_ibo;
  196. #elif defined _XBOX
  197. D3DIndexBuffer *m_ibo;
  198. #else
  199. #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  200. GLuint m_ibo;
  201. #endif
  202. #endif
  203. bool m_ready;
  204. };
  205. int main(int argc, char **argv)
  206. {
  207. Application app("Tutorial 2: Cube", ivec2(640, 480), 60.0f);
  208. #if defined _MSC_VER && !defined _XBOX
  209. _chdir("..");
  210. #elif defined _WIN32 && !defined _XBOX
  211. _chdir("../..");
  212. #endif
  213. new DebugFps(5, 5);
  214. new Cube();
  215. app.Run();
  216. return EXIT_SUCCESS;
  217. }