marichan022

grafika-problem1

Jun 19th, 2019
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <GL/glew.h>
  3. #include <GLFW/glfw3.h>
  4. #include <glm/glm.hpp>
  5. #include <glm/gtc/matrix_transform.hpp>
  6. #include <glm/gtc/type_ptr.hpp>
  7. #include <glm/gtx/string_cast.hpp>
  8.  
  9. #include "shader.h"
  10. #include "sphere.h"
  11.  
  12. int main()
  13. {
  14.     if( !glfwInit() )
  15.     {
  16.         std::cerr << "GLFW nije inicijaliziran\n";
  17.         getchar();
  18.         return -1;
  19.     }
  20.  
  21.     glfwWindowHint(GLFW_SAMPLES, 4);
  22.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  23.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  24.     glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // samo za MacOS
  25.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  26.  
  27.     GLFWwindow* window = glfwCreateWindow( 1024, 768, "prog", nullptr, nullptr);
  28.     if( window == nullptr )
  29.     {
  30.         std::cerr << "Nije prošao init glfw prozora. Npr. neki Intel GPU nisu 3.3 kompatibilni.\n";
  31.         getchar();
  32.         glfwTerminate();
  33.         return -1;
  34.     }
  35.     glfwMakeContextCurrent(window);
  36.  
  37.     if (glewInit() != GLEW_OK)
  38.     {
  39.         std::cerr << "GLEW nije inicijaliziran\n";
  40.         getchar();
  41.         glfwTerminate();
  42.         return -1;
  43.     }
  44.  
  45.     glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
  46.  
  47.     glEnable(GL_DEPTH_TEST);
  48.     glEnable(GL_CULL_FACE);
  49.     glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
  50.     glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
  51.  
  52.     Sphere sphere(2.0f, 36, 18, true);
  53.  
  54.     unsigned int VAO, VBO, EBO;
  55.     glGenVertexArrays(1, &VAO);
  56.     glBindVertexArray(VAO);
  57.  
  58.     glGenBuffers(1, &VBO);
  59.     glBindBuffer(GL_ARRAY_BUFFER, VBO);
  60.     glBufferData(GL_ARRAY_BUFFER, sphere.getInterleavedVertexSize(), sphere.getInterleavedVertices(), GL_STATIC_DRAW);
  61.    
  62.     glGenBuffers(1, &EBO);
  63.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
  64.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sphere.getIndexSize(), sphere.getIndices(), GL_STATIC_DRAW);
  65.    
  66.     int stride = sphere.getInterleavedStride();
  67.     // index, broj, tip, normalited, stride, offset
  68.     glVertexAttribPointer(0, 3, GL_FLOAT, false, stride, (void*)0);
  69.     glEnableVertexAttribArray(0);
  70.     glVertexAttribPointer(1, 3, GL_FLOAT, false, stride, (void*)(3 * sizeof(float)));
  71.     glEnableVertexAttribArray(1);
  72.     glVertexAttribPointer(2, 2, GL_FLOAT, false, stride, (void*)(6 * sizeof(float)));
  73.     glEnableVertexAttribArray(2);
  74.  
  75.     // const std::string vert_glsl = shaders_folder + "/vert.glsl";
  76.     // const std::string frag_glsl = shaders_folder + "/frag.glsl";
  77.     std::vector<ShaderInfo> shaders = {
  78.         {GL_VERTEX_SHADER, "shaders/vert.glsl"},
  79.         {GL_FRAGMENT_SHADER, "shaders/frag.glsl"}
  80.     };
  81.  
  82.     Shader shader(shaders);
  83.  
  84.     do {
  85.         glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  86.  
  87.         shader.use();
  88.         glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
  89.         glBindVertexArray(VAO);
  90.         glDrawElements(GL_TRIANGLES, sphere.getIndexCount(), GL_UNSIGNED_INT, (void*)0);
  91.         glBindVertexArray(0);
  92.  
  93.         glfwSwapBuffers(window);
  94.         glfwPollEvents();
  95.     }
  96.     while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
  97.            glfwWindowShouldClose(window) == 0 );
  98.  
  99.     glfwTerminate();
  100.  
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment