Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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