Advertisement
marichan022

bunny in a box

Jun 23rd, 2019
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.59 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <GL/glew.h>
  4. #include <GLFW/glfw3.h>
  5. #include <glm/glm.hpp>
  6. #include <glm/gtc/matrix_transform.hpp>
  7. #include <glm/gtx/string_cast.hpp>
  8.  
  9. #include "objloader.h"
  10. #include "shader.h"
  11. #include "textures.h"
  12.  
  13. void process_keyboard(GLFWwindow* w);
  14. glm::vec3 move();
  15.  
  16. float deltaT = 0, lastF = 0;
  17. bool jump = false;
  18.  
  19. glm::vec3 cameraPos = glm::vec3(10.7f, -0.4f, 11.6f);
  20. glm::vec3 cameraFront = glm::vec3(0.4f, 0.f, 0.9f);
  21. glm::vec3 lightPos = glm::vec3(1.4f, 0.f, 5.f);
  22. glm::vec3 lightColor = glm::vec3 (1.f);
  23. glm::vec3 objectColor = glm::vec3 (0.071, 0.682, 0.82);
  24. glm::vec3 cameraUp = glm::vec3(0,1,0);
  25.  
  26. float box_vertices[] = {
  27.     //prednja
  28.    -1.0f, -0.7f,  1.0f, 0.f, 0.f, 1.f, 0.0f, 0.0f,  //dl
  29.     1.0f, -0.7f,  1.0f, 0.f, 0.f, 1.f, 1.0f, 0.0f,  //dd
  30.     1.0f,  0.7f,  1.0f, 0.f, 0.f, 1.f, 1.0f, 1.0f,  //gd
  31.    -1.0f,  0.7f,  1.0f, 0.f, 0.f, 1.f, 0.0f, 1.0f,  //gl
  32.     //straznja
  33.    -1.0f, -0.7f, -1.0f, 1.f, 0.f, 0.f, 0.0f, 0.0f, //dl
  34.     1.0f, -0.7f, -1.0f, 1.f, 0.f, 0.f, 1.0f, 0.0f, //dd
  35.     1.0f,  0.7f, -1.0f, 1.f, 0.f, 0.f, 1.0f, 1.0f, //gd
  36.    -1.0f,  0.7f, -1.0f, 1.f, 0.f, 0.f, 0.0f, 1.0f, //gl
  37.     //lijeva
  38.    -1.0f, -0.7f, -1.0f, 1.f, 0.f, 0.f, 0.0f, 0.0f,  //ds
  39.    -1.0f, -0.7f,  1.0f, 1.f, 0.f, 0.f, 1.0f, 0.0f,  //dp
  40.    -1.0f,  0.7f,  1.0f, 1.f, 0.f, 0.f, 1.0f, 1.0f,  //gp
  41.    -1.0f,  0.7f, -1.0f, 1.f, 0.f, 0.f, 0.0f, 1.0f,  //gs
  42.     //gornja
  43.    -1.0f,  0.7f, -1.0f, 1.f, 0.f, 0.f, 0.0f, 0.0f,  //sl
  44.     1.0f,  0.7f, -1.0f, 1.f, 0.f, 0.f, 1.0f, 0.0f,  //sd
  45.     1.0f,  0.7f,  1.0f, 1.f, 0.f, 0.f, 1.0f, 1.0f,  //pd
  46.    -1.0f,  0.7f,  1.0f, 1.f, 0.f, 0.f, 0.0f, 1.0f,  //pl
  47.     //donja
  48.    -1.0f, -0.7f, -1.0f, 0.f, 0.f, 1.f, 0.0f, 1.0f,  //sl
  49.     1.0f, -0.7f, -1.0f, 0.f, 0.f, 1.f, 1.0f, 1.0f,  //sd
  50.     1.0f, -0.7f,  1.0f, 0.f, 0.f, 1.f, 1.0f, 0.0f,  //pd
  51.    -1.0f, -0.7f,  1.0f, 0.f, 0.f, 1.f, 0.0f, 0.0f   //pl
  52. };
  53. int box_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. std::vector<glm::vec3> curve_points;
  71. void calc_curve()
  72. {
  73.     const int num = 50, start = -2, end = 5;
  74.     std::vector<double> ts(num);
  75.     const double dx = (end - start) / (num - 1);
  76.     for (int i = 0; i < num; ++i)
  77.     {
  78.         ts[i] = start + i*dx;
  79.     }
  80.     for(const auto& t : ts)
  81.     {
  82.         if(t < 4)
  83.         {
  84.             curve_points.emplace_back(glm::vec3(t, 2.f, 0.f));
  85.         }
  86.     }
  87. }
  88.  
  89. int main()
  90. {
  91.     if( !glfwInit() )
  92.     {
  93.         std::cerr << "GLFW nije inicijaliziran\n";
  94.         getchar();
  95.         return -1;
  96.     }
  97.  
  98.     glfwWindowHint(GLFW_SAMPLES, 4);
  99.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  100.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  101.     glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // samo za MacOS
  102.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  103.  
  104.     GLFWwindow* window = glfwCreateWindow( 1024, 768, "prog", nullptr, nullptr);
  105.     if( window == nullptr )
  106.     {
  107.         std::cerr << "Nije prošao init glfw prozora. Npr. neki Intel GPU nisu 3.3 kompatibilni.\n";
  108.         getchar();
  109.         glfwTerminate();
  110.         return -1;
  111.     }
  112.     glfwMakeContextCurrent(window);
  113.  
  114.     if (glewInit() != GLEW_OK)
  115.     {
  116.         std::cerr << "GLEW nije inicijaliziran\n";
  117.         getchar();
  118.         glfwTerminate();
  119.         return -1;
  120.     }
  121.  
  122.     glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
  123.  
  124.     glEnable(GL_DEPTH_TEST);
  125.     glDisable(GL_CULL_FACE);
  126.     glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
  127.     glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
  128.  
  129.     std::vector<glm::vec3> bunny_vertices, bunny_normals;
  130.     bool res = loadOBJ("..//..//models//bunny.obj", bunny_vertices, bunny_normals);
  131.     calc_curve();
  132.     std::vector<ShaderInfo> shaders = {
  133.         {GL_VERTEX_SHADER, "shaders/vert.glsl"},
  134.         {GL_FRAGMENT_SHADER, "shaders/frag.glsl"}
  135.     };
  136.     Shader shader(shaders);
  137.     unsigned int box_texture = loadTexture("../../images/metal_03.jpg");
  138.  
  139.     glm::mat4 model(1.f), proj, view;
  140.     proj = glm::perspective(glm::radians(45.f), 16.f/9.f, 0.1f, 100.f);
  141.    
  142.     unsigned int VAO, VBO[3], EBO, NBO;
  143.     glGenVertexArrays(1, &VAO);
  144.     glBindVertexArray(VAO);
  145.  
  146.     glGenBuffers(3, VBO);
  147.     glGenBuffers(1, &EBO);
  148.     glGenBuffers(1, &NBO);
  149.  
  150.     glEnableVertexAttribArray(0);
  151.     glEnableVertexAttribArray(1);
  152.     glEnableVertexAttribArray(2);
  153.     do {
  154.         glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  155.         glCullFace(GL_BACK);
  156.  
  157.         float currentF = glfwGetTime();
  158.         deltaT = currentF - lastF;
  159.         lastF = currentF;
  160.         process_keyboard(window);
  161.         view = glm::lookAt(cameraPos, cameraPos - cameraFront, glm::vec3(0.0, 1.0, 0.0));
  162.  
  163.         // box
  164.         model = glm::translate(glm::mat4(1.f), glm::vec3(-2.f, 3.f, 0.f));
  165.         model = glm::scale(model, glm::vec3(1.8f));
  166.  
  167.         shader.use();
  168.         shader.setMat4("model", model);
  169.         shader.setMat4("proj", proj);
  170.         shader.setMat4("view", view);
  171.         shader.setVec3("lightColor", lightColor);
  172.         shader.setVec3("viewPos", cameraPos);
  173.         shader.setVec3("lightPos", lightPos);
  174.         shader.setInt("texture", 0);
  175.         glActiveTexture(GL_TEXTURE0);
  176.         glBindTexture(GL_TEXTURE_2D, box_texture);
  177.  
  178.         glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
  179.         glBufferData(GL_ARRAY_BUFFER, sizeof(box_vertices), box_vertices, GL_STATIC_DRAW);
  180.         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
  181.         glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(box_indices), box_indices, GL_STATIC_DRAW);
  182.        
  183.         glVertexAttribPointer(0, 3, GL_FLOAT, false, 8*sizeof(float), (void*)0);
  184.         glVertexAttribPointer(1, 3, GL_FLOAT, false, 8*sizeof(float), (void*)(3 * sizeof(float)));
  185.         glVertexAttribPointer(2, 2, GL_FLOAT, false, 8*sizeof(float), (void*)(6 * sizeof(float)));
  186.  
  187.         glDrawElements(GL_TRIANGLES, 30, GL_UNSIGNED_INT, 0);
  188.  
  189.         // bunny
  190.         glm::vec3 bunny_pos = jump ? move() : glm::vec3(-2.2f, 2.f, 0.f);
  191.         model = glm::translate(glm::mat4(1.f), bunny_pos);
  192.         model = glm::scale(model, glm::vec3(0.8f));
  193.         model = glm::rotate(model, glm::radians(100.f), glm::vec3(0.f, 1.f, 0.f));
  194.         shader.setMat4("model", model);
  195.        
  196.         glBindBuffer(GL_ARRAY_BUFFER, VBO[1]);
  197.         glBufferData(GL_ARRAY_BUFFER, bunny_vertices.size() * sizeof(glm::vec3), &bunny_vertices[0], GL_STATIC_DRAW);
  198.         glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
  199.  
  200.         glBindBuffer(GL_ARRAY_BUFFER, NBO);
  201.         glBufferData(GL_ARRAY_BUFFER, bunny_normals.size() * sizeof(glm::vec3), &bunny_normals[0], GL_STATIC_DRAW);
  202.         glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
  203.  
  204.         glDrawArrays(GL_TRIANGLES, 0, bunny_vertices.size());
  205.        
  206.         model = glm::mat4(1.f);
  207.         shader.setMat4("model", model);
  208.  
  209.         glBindBuffer(GL_ARRAY_BUFFER, VBO[2]);
  210.         glBufferData(GL_ARRAY_BUFFER, curve_points.size() * sizeof(glm::vec3), &curve_points.front(), GL_STATIC_DRAW);
  211.        
  212.         glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*) 0);
  213.         glDrawArrays(GL_LINE_STRIP, 0, curve_points.size());
  214.  
  215.         glfwSwapBuffers(window);
  216.         glfwPollEvents();
  217.     }
  218.     while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
  219.            glfwWindowShouldClose(window) == 0 );
  220.     glDisableVertexAttribArray(0);
  221.     glDisableVertexAttribArray(1);
  222.     glDisableVertexAttribArray(2);
  223.  
  224.     glfwTerminate();
  225.  
  226.     return 0;
  227. }
  228.  
  229. glm::vec3 move()
  230. {
  231.  
  232. }
  233.  
  234. void process_keyboard(GLFWwindow* w)
  235. {
  236.     float camSpeed = 4 * deltaT;
  237.  
  238.     if(glfwGetKey(w,GLFW_KEY_G) == GLFW_PRESS)
  239.     {
  240.         jump = true;      
  241.     }
  242.     if(glfwGetKey(w,GLFW_KEY_S) == GLFW_PRESS)
  243.     {
  244.         jump = false;      
  245.     }
  246.     if(glfwGetKey(w,GLFW_KEY_I) == GLFW_PRESS)
  247.     {
  248.         cameraPos -= cameraFront * camSpeed;
  249.     }
  250.     if(glfwGetKey(w,GLFW_KEY_K) == GLFW_PRESS)
  251.     {
  252.         cameraPos += cameraFront * camSpeed;
  253.     }
  254.     if(glfwGetKey(w,GLFW_KEY_J) == GLFW_PRESS)
  255.     {
  256.         cameraPos += glm::cross(cameraFront, cameraUp) * camSpeed;
  257.     }
  258.     if(glfwGetKey(w,GLFW_KEY_L) == GLFW_PRESS)
  259.     {
  260.         cameraPos -= glm::cross(cameraFront, cameraUp) * camSpeed;
  261.     }
  262.     if(glfwGetKey(w,GLFW_KEY_U) == GLFW_PRESS)
  263.     {
  264.         cameraFront = glm::rotate(glm::mat4(1.f), std::acos(std::cos(deltaT)), cameraUp) * glm::vec4(cameraFront, 1.f);
  265.     }
  266.     if(glfwGetKey(w,GLFW_KEY_O) == GLFW_PRESS)
  267.     {
  268.         cameraFront = glm::rotate(glm::mat4(1.f), -std::acos(std::cos(deltaT)), cameraUp) * glm::vec4(cameraFront, 1.f);
  269.     }
  270.     if(glfwGetKey(w,GLFW_KEY_N) == GLFW_PRESS)
  271.     {
  272.         cameraPos += glm::vec3(0,1,0) * camSpeed;
  273.     }
  274.     if(glfwGetKey(w,GLFW_KEY_M) == GLFW_PRESS)
  275.     {
  276.         cameraPos -= glm::vec3(0,1,0) * camSpeed;      
  277.     }
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement