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 3.4 KiB

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