opengl.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include <cstring>
  2. #include <cstdio>
  3. #include <thread>
  4. #include <vector>
  5. #include <GL/glew.h>
  6. #include <GL/gl.h>
  7. #include "simple/graphical/initializer.h"
  8. #include "simple/interactive/initializer.h"
  9. #include "simple/interactive/event.h"
  10. #include "simple/graphical/software_window.h"
  11. #include "simple/graphical/gl_window.h"
  12. #include "simple/graphical/algorithm.h"
  13. using namespace std::chrono_literals;
  14. using namespace simple;
  15. using namespace graphical::color_literals;
  16. using graphical::int2;
  17. using graphical::range2D;
  18. using graphical::float2;
  19. using graphical::anchored_rect;
  20. using graphical::gl_window;
  21. int main() try
  22. {
  23. graphical::initializer graphics;
  24. interactive::initializer input;
  25. gl_window::global.require<gl_window::attribute::major_version>(2);
  26. gl_window::global.require<gl_window::attribute::minor_version>(1);
  27. gl_window::global.request<gl_window::attribute::stencil>(8);
  28. gl_window win("opengl", int2(600, 600), gl_window::flags::borderless);
  29. win.require_vsync(gl_window::vsync_mode::enabled);
  30. float2 win_size(win.size());
  31. glewInit();
  32. GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
  33. // Compile Vertex Shader
  34. char const * VertexSourcePointer = R"shader(
  35. #version 120
  36. #extension GL_ARB_explicit_attrib_location : require
  37. layout(location = 0) in vec3 vertexPosition_modelspace;
  38. void main()
  39. {
  40. gl_Position.xyz = vertexPosition_modelspace;
  41. gl_Position.w = 1.0;
  42. }
  43. )shader";
  44. glShaderSource(VertexShaderID, 1, &VertexSourcePointer , nullptr);
  45. glCompileShader(VertexShaderID);
  46. // Check Vertex Shader
  47. GLint Result = GL_FALSE;
  48. int InfoLogLength;
  49. glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
  50. glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
  51. if ( InfoLogLength > 0 ){
  52. std::vector<char> VertexShaderErrorMessage(InfoLogLength+1);
  53. glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, VertexShaderErrorMessage.data());
  54. printf("%s\n", VertexShaderErrorMessage.data());
  55. }
  56. GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
  57. // Compile Fragment Shader
  58. char const * FragmentSourcePointer = R"shader(
  59. #version 120
  60. #extension GL_ARB_explicit_attrib_location : require
  61. out vec3 color;
  62. void main()
  63. {
  64. color = vec3(1,0,0);
  65. }
  66. )shader";
  67. glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL);
  68. glCompileShader(FragmentShaderID);
  69. // Check Fragment Shader
  70. glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
  71. glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
  72. if ( InfoLogLength > 0 ){
  73. std::vector<char> FragmentShaderErrorMessage(InfoLogLength+1);
  74. glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, FragmentShaderErrorMessage.data());
  75. printf("%s\n", FragmentShaderErrorMessage.data());
  76. }
  77. GLuint ProgramID = glCreateProgram();
  78. glAttachShader(ProgramID, VertexShaderID);
  79. glAttachShader(ProgramID, FragmentShaderID);
  80. glLinkProgram(ProgramID);
  81. // Check the program
  82. glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
  83. glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
  84. if ( InfoLogLength > 0 ){
  85. std::vector<char> ProgramErrorMessage(InfoLogLength+1);
  86. glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, ProgramErrorMessage.data());
  87. printf("%s\n", ProgramErrorMessage.data());
  88. }
  89. glDetachShader(ProgramID, VertexShaderID);
  90. glDetachShader(ProgramID, FragmentShaderID);
  91. glDeleteShader(VertexShaderID);
  92. glDeleteShader(FragmentShaderID);
  93. glClearColor(0,0,0.4,0);
  94. glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  95. glUseProgram(ProgramID);
  96. GLuint VertexArrayID;
  97. glGenVertexArrays(1, &VertexArrayID);
  98. glBindVertexArray(VertexArrayID);
  99. auto vertex_data = geom::vector {
  100. geom::vector(-1.0f, -1.0f, 0.0f),
  101. geom::vector(1.0f, -1.0f, 0.0f),
  102. geom::vector(0.0f, 1.0f, 0.0f),
  103. };
  104. vertex_data /= 5.f;
  105. // This will identify our vertex buffer
  106. GLuint vertexbuffer;
  107. // Generate 1 buffer, put the resulting identifier in vertexbuffer
  108. glGenBuffers(1, &vertexbuffer);
  109. // The following commands will talk about our 'vertexbuffer' buffer
  110. glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
  111. // Give our vertices to OpenGL.
  112. bool done = false;
  113. const auto x = geom::vector(1.f, 0.f, 0.f);
  114. while(!done)
  115. {
  116. using namespace interactive;
  117. while(auto e = next_event())
  118. {
  119. std::visit(support::overload
  120. {
  121. [&](quit_request){ done = true; },
  122. [](auto&&){}
  123. }, *e);
  124. }
  125. glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  126. glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_data), &vertex_data, GL_STATIC_DRAW);
  127. // 1st attribute buffer : vertices
  128. glEnableVertexAttribArray(0);
  129. glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
  130. glVertexAttribPointer(
  131. 0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
  132. 3, // size
  133. GL_FLOAT, // type
  134. GL_FALSE, // normalized?
  135. 0, // stride
  136. (void*)0 // array buffer offset
  137. );
  138. // Draw the triangle !
  139. glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle
  140. glDisableVertexAttribArray(0);
  141. if(vertex_data[2] >= x)
  142. {
  143. vertex_data -= x * 2;
  144. }
  145. vertex_data += x * 0.03;
  146. win.update();
  147. // std::this_thread::sleep_for(16ms);
  148. }
  149. glDeleteBuffers(1, &vertexbuffer);
  150. glDeleteVertexArrays(1, &VertexArrayID);
  151. return 0;
  152. }
  153. catch(...)
  154. {
  155. if(errno)
  156. std::puts(std::strerror(errno));
  157. throw;
  158. }