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

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