Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.16 KB | None | 0 0
  1. #include <glimac/SDLWindowManager.hpp>
  2. #include <GL/glew.h>
  3. #include <iostream>
  4. #include <glimac/Program.hpp>
  5. #include <glimac/FilePath.hpp>
  6. #include <glimac/glm.hpp>
  7. #include <vector>
  8.  
  9. using namespace glimac;
  10.  
  11. struct Vertex2DColor {
  12.     glm::vec2 position;
  13.     glm::vec3 color;
  14.  
  15.     Vertex2DColor() {
  16.  
  17.     }
  18.  
  19.     Vertex2DColor(glm::vec2 position, glm::vec3 color) {
  20.         this->position = position;
  21.         this->color = color;
  22.     }
  23. };
  24.  
  25. int main(int argc, char **argv) {
  26.     // Initialize SDL and open a window
  27.     SDLWindowManager windowManager(800, 800
  28.             , "GLImac");
  29.  
  30.     // Initialize glew for OpenGL3+ support
  31.     GLenum glewInitError = glewInit();
  32.     if (GLEW_OK != glewInitError) {
  33.         std::cerr << glewGetErrorString(glewInitError) << std::endl;
  34.         return EXIT_FAILURE;
  35.     }
  36.  
  37.     std::cout << "OpenGL Version : " << glGetString(GL_VERSION) << std::endl;
  38.     std::cout << "GLEW Version : " << glewGetString(GLEW_VERSION) << std::endl;
  39.  
  40.     FilePath applicationPath(argv[0]);
  41.     Program program =
  42.             loadProgram(applicationPath.dirPath() + "shaders/triangle.vs.glsl",
  43.                         applicationPath.dirPath() + "shaders/triangle.fs.glsl");
  44.     program.use();
  45.  
  46.     /*********************************
  47.      * HERE SHOULD COME THE INITIALIZATION CODE
  48.      *********************************/
  49.  
  50.     const GLuint VERTEX_ATTR_POSITION = 3;
  51.     const GLuint VERTEX_ATTR_COLOR = 8;
  52.     const GLuint NBR_TRIANGLE = 80;
  53.     const float RADIUS = 0.5;
  54.  
  55.     GLuint vbo;
  56.  
  57.     glGenBuffers(1, &vbo);
  58.     glBindBuffer(GL_ARRAY_BUFFER, vbo);
  59.  
  60.     std::vector<Vertex2DColor> vertices = {
  61.             Vertex2DColor(glm::vec2(0, 0), glm::vec3(0.5, 0.5, 0.5)), Vertex2DColor(glm::vec2(0.5, 0), glm::vec3(0.5, 0.5, 0.5))
  62.     };
  63.  
  64.     // => Tableau de sommets : un seul exemplaire de chaque sommet
  65.     vertices[0] = Vertex2DColor(glm::vec2(0, 0), glm::vec3(1, 0, 0));
  66.     vertices[1] = Vertex2DColor(glm::vec2(0.5, 0), glm::vec3(1, 0, 0));
  67.     float angle = (2*glm::pi<float>())/NBR_TRIANGLE;
  68.     for (int i = 2; i < NBR_TRIANGLE; i++) {
  69.         float p = angle * (float)i;
  70.         vertices[i+1] = Vertex2DColor(glm::vec2(RADIUS*glm::cos(p), RADIUS * glm::sin(p)), glm::vec3(1, 0, 0));
  71.     }
  72.  
  73.     glBufferData(GL_ARRAY_BUFFER, (NBR_TRIANGLE + 1)  * sizeof(Vertex2DColor), vertices.data(), GL_STATIC_DRAW);
  74.     glBindBuffer(GL_ARRAY_BUFFER, 0);
  75.  
  76.     // => Creation du IBO
  77.     GLuint ibo;
  78.     glGenBuffers(1, &ibo);
  79.  
  80.     // => On bind sur GL_ELEMENT_ARRAY_BUFFER, cible reservée pour les IBOs
  81.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
  82.  
  83.     uint32_t indices[NBR_TRIANGLE*3];
  84.     for (int i = 1; i < NBR_TRIANGLE; i += 3) {
  85.         printf("%d\n", i);
  86.         indices[i] = 0;
  87.         indices[i + 1] = i;
  88.         indices[i + 2] = i + 1;
  89.     }
  90.  
  91.  
  92.     GLuint vao;
  93.     glGenVertexArrays(1, &vao);
  94.     glBindVertexArray(vao);
  95.     glEnableVertexAttribArray(VERTEX_ATTR_POSITION);
  96.     glEnableVertexAttribArray(VERTEX_ATTR_COLOR);
  97.  
  98.  
  99.     glBindBuffer(GL_ARRAY_BUFFER, vbo);
  100.     glVertexAttribPointer(VERTEX_ATTR_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex2DColor), (const GLvoid *) offsetof(Vertex2DColor, position));
  101.     glVertexAttribPointer(VERTEX_ATTR_COLOR, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex2DColor),
  102.                           (const GLvoid *) (offsetof(Vertex2DColor, color)));
  103.  
  104.  
  105.     glBindBuffer(GL_ARRAY_BUFFER, 0);
  106.     glBindVertexArray(0);
  107.  
  108.  
  109.     // Application loop:
  110.     bool done = false;
  111.     while (!done) {
  112.         // Event loop:
  113.         SDL_Event e;
  114.         while (windowManager.pollEvent(e)) {
  115.             if (e.type == SDL_QUIT) {
  116.                 done = true; // Leave the loop after this iteration
  117.             }
  118.         }
  119.  
  120.         /*********************************
  121.          * HERE SHOULD COME THE RENDERING CODE
  122.          *********************************/
  123.         glClear(GL_COLOR_BUFFER_BIT);
  124.         glBindVertexArray(vao);
  125.         glDrawArrays(GL_TRIANGLES, 0, NBR_TRIANGLE*3);
  126.  
  127.         glBindVertexArray(0);
  128.  
  129.  
  130.         // Update the display
  131.         windowManager.swapBuffers();
  132.     }
  133.  
  134.     glDeleteBuffers(1, &vbo);
  135.     glDeleteVertexArrays(1, &vao);
  136.  
  137.     return EXIT_SUCCESS;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement