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.

tut01.cpp 5.3 KiB

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