Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

129 righe
3.5 KiB

  1. //
  2. // Lol Engine - Triangle 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 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. #endif
  27. class Triangle : public WorldEntity
  28. {
  29. public:
  30. Triangle()
  31. {
  32. m_vertices[0] = vec2( 0.0, 0.8);
  33. m_vertices[1] = vec2(-0.8, -0.8);
  34. m_vertices[2] = vec2( 0.8, -0.8);
  35. }
  36. virtual void TickDraw(float deltams)
  37. {
  38. WorldEntity::TickDraw(deltams);
  39. if (!m_ready)
  40. {
  41. m_shader = Shader::Create(
  42. "#version 120\n"
  43. "attribute vec2 coord2d;"
  44. "void main(void) {"
  45. " gl_Position = vec4(coord2d, 0.0, 1.0);"
  46. "}",
  47. "#version 120\n"
  48. "void main(void) {"
  49. " gl_FragColor[0] = 0.0;"
  50. " gl_FragColor[1] = 0.0;"
  51. " gl_FragColor[2] = 1.0;"
  52. "}");
  53. m_attrib = m_shader->GetAttribLocation("coord2d");
  54. m_ready = true;
  55. #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  56. /* Method 1: store vertex buffer on the GPU memory */
  57. glGenBuffers(1, &m_vbo);
  58. glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
  59. glBufferData(GL_ARRAY_BUFFER, sizeof(m_vertices), m_vertices,
  60. GL_STATIC_DRAW);
  61. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  62. /* Method 2: upload vertex information at each frame */
  63. #else
  64. #endif
  65. /* FIXME: this object never cleans up */
  66. }
  67. m_shader->Bind();
  68. #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  69. glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
  70. glEnableVertexAttribArray(m_attrib);
  71. glVertexAttribPointer(m_attrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
  72. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  73. /* Never used for now */
  74. glEnableVertexAttribArray(m_attrib);
  75. glVertexAttribPointer(m_attrib, 2, GL_FLOAT, GL_FALSE, 0, m_vertices);
  76. #else
  77. glEnableClientState(GL_VERTEX_ARRAY);
  78. glVertexPointer(3, GL_FLOAT, 0, m_vertices);
  79. #endif
  80. glDrawArrays(GL_TRIANGLES, 0, 3);
  81. #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  82. glDisableVertexAttribArray(m_attrib);
  83. glBindBuffer(GL_ARRAY_BUFFER, 0);
  84. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  85. /* Never used for now */
  86. glDisableVertexAttribArray(m_attrib);
  87. #else
  88. glDisableClientState(GL_VERTEX_ARRAY);
  89. #endif
  90. }
  91. private:
  92. vec2 m_vertices[3];
  93. Shader *m_shader;
  94. #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  95. GLuint m_vbo;
  96. #endif
  97. int m_attrib;
  98. bool m_ready;
  99. };
  100. int main(int argc, char **argv)
  101. {
  102. Application app("Tutorial 1: Triangle", ivec2(640, 480), 60.0f);
  103. new DebugFps(5, 5);
  104. new Triangle();
  105. app.Run();
  106. return EXIT_SUCCESS;
  107. }