Advertisement
marichan022

grafika-kvadar

Jun 22nd, 2019
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.69 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.  
  7. #include "shader.h"
  8.  
  9. using point3 = glm::vec3;
  10. using color = glm::vec3;
  11. float deltaT = 0, lastF = 0;
  12. std::vector<point3> control_points;
  13. std::vector<point3> curve_points;
  14. void process_keyboard(GLFWwindow* w);
  15.     glm::vec3 cameraPos = glm::vec3(0.f,0.f,10.f);
  16.     glm::vec3 cameraFront = glm::vec3(-0.4f,0.f,1.f);
  17.     glm::vec3 lightPos = glm::vec3(2.f, -1.f, 5.f);
  18.     color lightColor = color(1.f);
  19.     color objectColor = color(0.071, 0.682, 0.82);
  20.  
  21.     glm::vec3 cameraUp = glm::vec3(0,1,0);
  22.  
  23.     glm::mat4 model = glm::mat4(1.f);
  24.     glm::mat4 proj = glm::perspective(glm::radians(45.f), 16.f/9.f, 0.1f, 100.f);
  25.     glm::mat4 view = glm::lookAt(cameraPos, cameraPos - cameraFront, glm::vec3(0.0, 1.0, 0.0));
  26. float floor_data[] = {
  27.     //straznja
  28.    -1.0f, -1.0f, -1.0f, 0.f, 0.f, -1.f, 0.0f, 0.0f, //dl
  29.     1.0f, -1.0f, -1.0f, 0.f, 0.f, -1.f, 1.0f, 0.0f, //dd
  30.     1.0f,  1.0f, -1.0f, 0.f, 0.f, -1.f, 1.0f, 1.0f, //gd
  31.    -1.0f,  1.0f, -1.0f, 0.f, 0.f, -1.f, 0.0f, 1.0f,  //gl
  32.     //lijeva
  33.    -1.0f, -1.0f, -1.0f, -1.f, 0.f, 0.f, 0.0f, 0.0f,  //ds
  34.    -1.0f, -1.0f,  1.0f, -1.f, 0.f, 0.f, 1.0f, 0.0f,  //dp
  35.    -1.0f,  1.0f,  1.0f, -1.f, 0.f, 0.f, 1.0f, 1.0f,  //gp
  36.    -1.0f,  1.0f, -1.0f, -1.f, 0.f, 0.f, 0.0f, 1.0f,   //gs
  37.     //desna
  38.     1.0f, -1.0f, -1.0f, 1.f, 0.f, 0.f, 0.0f, 0.0f,  //ds
  39.     1.0f, -1.0f,  1.0f, 1.f, 0.f, 0.f, 1.0f, 0.0f,  //dp
  40.     1.0f,  1.0f,  1.0f, 1.f, 0.f, 0.f, 1.0f, 1.0f,  //gp
  41.     1.0f,  1.0f, -1.0f, 1.f, 0.f, 0.f, 0.0f, 1.0f,   //gs
  42.     //gornja
  43.    -1.0f,  1.0f, -1.0f, 0.f, 1.f, 0.f, 0.0f, 0.0f,  //sl
  44.     1.0f,  1.0f, -1.0f, 0.f, 1.f, 0.f, 1.0f, 0.0f,  //sd
  45.     1.0f,  1.0f,  1.0f, 0.f, 1.f, 0.f, 1.0f, 1.0f,  //pd
  46.    -1.0f,  1.0f,  1.0f, 0.f, 1.f, 0.f, 0.0f, 1.0f,   //pl
  47.     //donja
  48.    -1.0f, -1.0f, -1.0f, 0.f, -1.f, 0.f, 0.0f, 0.0f,  //sl
  49.     1.0f, -1.0f, -1.0f, 0.f, -1.f, 0.f, 1.0f, 0.0f,  //sd
  50.     1.0f, -1.0f,  1.0f, 0.f, -1.f, 0.f, 1.0f, 1.0f,  //pd
  51.    -1.0f, -1.0f,  1.0f, 0.f, -1.f, 0.f, 0.0f, 1.0f   //pl
  52. };
  53. int floor_indices[] = {
  54.     //straznja
  55.     0, 1, 2,
  56.     2, 3, 0,
  57.     //lijeva
  58.     4, 5, 6,
  59.     6, 7, 4,
  60.     //desna
  61.     8, 9, 10,
  62.     10, 11, 8,
  63.     //gornja
  64.     12, 13, 14,
  65.     14, 15, 12,
  66.     //donja
  67.     16, 17, 18,
  68.     18, 19, 16
  69. };
  70. int main()
  71. {
  72.     if( !glfwInit() )
  73.     {
  74.         std::cerr << "GLFW nije inicijaliziran\n";
  75.         getchar();
  76.         return -1;
  77.     }
  78.  
  79.     glfwWindowHint(GLFW_SAMPLES, 4);
  80.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  81.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  82.     glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // samo za MacOS
  83.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  84.  
  85.     GLFWwindow* window = glfwCreateWindow( 1024, 768, "prog", nullptr, nullptr);
  86.     if( window == nullptr )
  87.     {
  88.         std::cerr << "Nije prošao init glfw prozora. Npr. neki Intel GPU nisu 3.3 kompatibilni.\n";
  89.         getchar();
  90.         glfwTerminate();
  91.         return -1;
  92.     }
  93.     glfwMakeContextCurrent(window);
  94.  
  95.     if (glewInit() != GLEW_OK)
  96.     {
  97.         std::cerr << "GLEW nije inicijaliziran\n";
  98.         getchar();
  99.         glfwTerminate();
  100.         return -1;
  101.     }
  102.  
  103.     glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
  104.  
  105.     glEnable(GL_DEPTH_TEST);
  106.     glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
  107.     glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
  108.  
  109.     unsigned int VAO, VBO, EBO;
  110.     glGenVertexArrays(1, &VAO);
  111.     glBindVertexArray(VAO);
  112.  
  113.     glGenBuffers(1, &VBO);
  114.     glBindBuffer(GL_ARRAY_BUFFER, VBO);
  115.     glBufferData(GL_ARRAY_BUFFER, sizeof(floor_data), floor_data, GL_STATIC_DRAW);
  116.    
  117.     glGenBuffers(1, &EBO);
  118.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
  119.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(floor_indices), floor_indices, GL_STATIC_DRAW);
  120.    
  121.         glVertexAttribPointer(0, 3, GL_FLOAT, false, 8*sizeof(float), (void*)0);
  122.         glVertexAttribPointer(1, 3, GL_FLOAT, false, 8*sizeof(float), (void*)(3 * sizeof(float)));
  123.         glVertexAttribPointer(2, 2, GL_FLOAT, false, 8*sizeof(float), (void*)(6 * sizeof(float)));
  124.         glEnableVertexAttribArray(0);
  125.         glEnableVertexAttribArray(1);
  126.         glEnableVertexAttribArray(2);
  127.    
  128.     std::vector<ShaderInfo> shaders = {
  129.         {GL_VERTEX_SHADER, "shaders/vert.glsl"},
  130.         {GL_FRAGMENT_SHADER, "shaders/sphere_frag.glsl"}
  131.     };
  132.     Shader shader(shaders);
  133.     glEnable(GL_DEPTH_TEST);
  134.     glDisable(GL_CULL_FACE);
  135.  
  136.     do {
  137.         glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  138.         float currentF = glfwGetTime();
  139.         deltaT = currentF - lastF;
  140.         lastF = currentF;
  141.         process_keyboard(window);
  142.         view = glm::lookAt(cameraPos, cameraPos - cameraFront, glm::vec3(0.0, 1.0, 0.0));
  143.         glCullFace(GL_BACK);
  144.         shader.use();
  145.         shader.setMat4("model", model);
  146.         shader.setMat4("proj", proj);
  147.         shader.setMat4("view", view);
  148.  
  149.         shader.setVec3("objectColor", objectColor);
  150.         shader.setVec3("lightColor", lightColor);
  151.         shader.setVec3("viewPos", cameraPos);
  152.         shader.setVec3("lightPos", lightPos);
  153.  
  154.         glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
  155.         glBindBuffer(GL_ARRAY_BUFFER, VBO);
  156.        
  157.     glBindVertexArray(VAO);
  158.         glDrawElements(GL_TRIANGLES, 30, GL_UNSIGNED_INT, 0);
  159.         glBindVertexArray(0);
  160.        
  161.         glDisableVertexAttribArray(0);
  162.         glDisableVertexAttribArray(1);
  163.         glDisableVertexAttribArray(2);
  164.  
  165.         glfwSwapBuffers(window);
  166.         glfwPollEvents();
  167.     }
  168.     while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
  169.            glfwWindowShouldClose(window) == 0 );
  170.  
  171.     glfwTerminate();
  172.  
  173.     return 0;
  174. }
  175.  
  176. void process_keyboard(GLFWwindow* w)
  177. {
  178.     float camSpeed = 4 * deltaT;
  179.  
  180.     if(glfwGetKey(w,GLFW_KEY_W) == GLFW_PRESS)
  181.     {
  182.         cameraPos -= cameraFront * camSpeed;
  183.     }
  184.     else if(glfwGetKey(w,GLFW_KEY_S) == GLFW_PRESS)
  185.     {
  186.         cameraPos += cameraFront * camSpeed;
  187.     }
  188.     else if(glfwGetKey(w,GLFW_KEY_A) == GLFW_PRESS)
  189.     {
  190.         cameraPos += glm::cross(cameraFront, cameraUp) * camSpeed;
  191.     }
  192.     else if(glfwGetKey(w,GLFW_KEY_D) == GLFW_PRESS)
  193.     {
  194.         cameraPos -= glm::cross(cameraFront, cameraUp) * camSpeed;
  195.     }
  196.     else if(glfwGetKey(w,GLFW_KEY_Q) == GLFW_PRESS)
  197.     {
  198.         cameraFront = glm::rotate(glm::mat4(1.f), std::acos(std::cos(deltaT)), cameraUp) * glm::vec4(cameraFront, 1.f);
  199.     }
  200.     else if(glfwGetKey(w,GLFW_KEY_E) == GLFW_PRESS)
  201.     {
  202.         cameraFront = glm::rotate(glm::mat4(1.f), -std::acos(std::cos(deltaT)), cameraUp) * glm::vec4(cameraFront, 1.f);
  203.     }
  204.     if(glfwGetKey(w,GLFW_KEY_R) == GLFW_PRESS)
  205.     {
  206.         cameraPos += glm::vec3(0,1,0) * camSpeed;
  207.     }
  208.     if(glfwGetKey(w,GLFW_KEY_T) == GLFW_PRESS)
  209.     {
  210.         cameraPos -= glm::vec3(0,1,0) * camSpeed;      
  211.     }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement