您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

209 行
5.8 KiB

  1. //
  2. // Lol Engine - Triangle tutorial
  3. //
  4. // Copyright: (c) 2012 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 __native_client__
  27. # define main old_main
  28. #endif
  29. #if defined _WIN32
  30. # undef main /* FIXME: still needed? */
  31. # include <direct.h>
  32. #endif
  33. #if defined USE_D3D9
  34. extern IDirect3DDevice9 *g_d3ddevice;
  35. #elif defined _XBOX
  36. extern D3DDevice *g_d3ddevice;
  37. #endif
  38. class Triangle : public WorldEntity
  39. {
  40. public:
  41. Triangle()
  42. {
  43. m_vertices[0] = vec2( 0.0, 0.8);
  44. m_vertices[1] = vec2(-0.8, -0.8);
  45. m_vertices[2] = vec2( 0.8, -0.8);
  46. m_ready = false;
  47. }
  48. virtual void TickDraw(float deltams)
  49. {
  50. WorldEntity::TickDraw(deltams);
  51. if (!m_ready)
  52. {
  53. VertexBuffer vb(VertexDeclaration<vec2, float, ivec3>(0, 0, 1));
  54. m_shader = Shader::Create(
  55. #if !defined __CELLOS_LV2__ && !defined _XBOX && !defined USE_D3D9
  56. "#version 120\n"
  57. "attribute vec2 in_Position;"
  58. "void main(void) {"
  59. " gl_Position = vec4(in_Position, 0.0, 1.0);"
  60. "}",
  61. "#version 120\n"
  62. "void main(void) {"
  63. " gl_FragColor = vec4(0.7, 0.5, 0.2, 1.0);"
  64. "}"
  65. #else
  66. "void main(float2 in_Position : POSITION,"
  67. " out float4 out_Position : POSITION) {"
  68. " out_Position = float4(in_Position, 0.0, 1.0);"
  69. "}",
  70. "void main(out float4 out_FragColor : COLOR) {"
  71. " out_FragColor = float4(0.7, 0.5, 0.2, 1.0);"
  72. "}"
  73. #endif
  74. );
  75. #if !defined _XBOX && !defined USE_D3D9
  76. m_attrib = m_shader->GetAttribLocation("in_Position");
  77. #endif
  78. m_ready = true;
  79. #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__ && !defined _XBOX && !defined USE_D3D9
  80. /* Method 1: store vertex buffer on the GPU memory */
  81. glGenBuffers(1, &m_vbo);
  82. glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
  83. glBufferData(GL_ARRAY_BUFFER, sizeof(m_vertices), m_vertices,
  84. GL_STATIC_DRAW);
  85. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__ && !defined _XBOX && !defined USE_D3D9
  86. /* Method 2: upload vertex information at each frame */
  87. #elif defined _XBOX || defined USE_D3D9
  88. D3DVERTEXELEMENT9 const elements[2] =
  89. {
  90. { 0, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
  91. D3DDECL_END()
  92. };
  93. g_d3ddevice->CreateVertexDeclaration(elements, &m_vdecl);
  94. if (FAILED(g_d3ddevice->CreateVertexBuffer(sizeof(m_vertices), D3DUSAGE_WRITEONLY, NULL, D3DPOOL_MANAGED, &m_vbo, NULL)))
  95. exit(0);
  96. vec2 *vertices;
  97. if (FAILED(m_vbo->Lock(0, 0, (void **)&vertices, 0)))
  98. exit(0);
  99. memcpy(vertices, m_vertices, sizeof(m_vertices));
  100. m_vbo->Unlock();
  101. #else
  102. #endif
  103. /* FIXME: this object never cleans up */
  104. }
  105. m_shader->Bind();
  106. #if defined _XBOX || defined USE_D3D9
  107. g_d3ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
  108. g_d3ddevice->SetVertexDeclaration(m_vdecl);
  109. g_d3ddevice->SetStreamSource(0, m_vbo, 0, sizeof(*m_vertices));
  110. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  111. glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
  112. glEnableVertexAttribArray(m_attrib);
  113. glVertexAttribPointer(m_attrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
  114. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  115. /* Never used for now */
  116. glEnableVertexAttribArray(m_attrib);
  117. glVertexAttribPointer(m_attrib, 2, GL_FLOAT, GL_FALSE, 0, m_vertices);
  118. #else
  119. glEnableClientState(GL_VERTEX_ARRAY);
  120. glVertexPointer(3, GL_FLOAT, 0, m_vertices);
  121. #endif
  122. #if defined _XBOX || defined USE_D3D9
  123. g_d3ddevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
  124. #else
  125. glDrawArrays(GL_TRIANGLES, 0, 3);
  126. #endif
  127. #if defined _XBOX || defined USE_D3D9
  128. /* FIXME: do we need to unset anything here? */
  129. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  130. glDisableVertexAttribArray(m_attrib);
  131. glBindBuffer(GL_ARRAY_BUFFER, 0);
  132. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  133. /* Never used for now */
  134. glDisableVertexAttribArray(m_attrib);
  135. #else
  136. glDisableClientState(GL_VERTEX_ARRAY);
  137. #endif
  138. }
  139. private:
  140. vec2 m_vertices[3];
  141. Shader *m_shader;
  142. #if defined USE_D3D9
  143. IDirect3DVertexDeclaration9 *m_vdecl;
  144. IDirect3DVertexBuffer9 *m_vbo;
  145. #elif defined _XBOX
  146. D3DVertexDeclaration *m_vdecl;
  147. D3DVertexBuffer *m_vbo;
  148. #else
  149. int m_attrib;
  150. # if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  151. GLuint m_vbo;
  152. # endif
  153. #endif
  154. bool m_ready;
  155. };
  156. int main(int argc, char **argv)
  157. {
  158. Application app("Tutorial 1: Triangle", ivec2(640, 480), 60.0f);
  159. #if defined _MSC_VER && !defined _XBOX
  160. _chdir("..");
  161. #elif defined _WIN32 && !defined _XBOX
  162. _chdir("../..");
  163. #endif
  164. new DebugFps(5, 5);
  165. new Triangle();
  166. app.Run();
  167. return EXIT_SUCCESS;
  168. }