atm959

main.cpp

Mar 29th, 2019
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.84 KB | None | 0 0
  1. #include <glad/glad.h>
  2. #include <GLFW/glfw3.h>
  3. #include "stb_image.h"
  4.  
  5. #include <glm/glm.hpp>
  6. #include <glm/gtc/matrix_transform.hpp>
  7. #include <glm/gtc/type_ptr.hpp>
  8.  
  9. #include <irrKlang.h>
  10.  
  11. #include "Shader.h"
  12. #include "Texture.h"
  13.  
  14. #include <iostream>
  15. #include <stdlib.h>
  16. #include <time.h>
  17.  
  18. #include <vector>
  19.  
  20. #include <fstream>
  21. #include <sstream>
  22. #include <iostream>
  23. #include <string>
  24.  
  25. #include <thread>
  26.  
  27. #include "OBJ_Loader.h"
  28.  
  29. #include "InkZone.h"
  30.  
  31. using namespace irrklang;
  32.  
  33. struct Vertex {
  34.     Vertex() {}
  35.     Vertex(float x, float y, float z, float u, float v, float r, float g, float b) : pos(x, y, z), texCoord(u, v), color(r, g, b) {}
  36.  
  37.     glm::vec3 pos;
  38.     glm::vec2 texCoord;
  39.     glm::vec3 color;
  40. };
  41.  
  42. #define TERRAIN_SIZE 256
  43.  
  44. void framebuffer_size_callback(GLFWwindow* window, int width, int height);
  45. void processInput(GLFWwindow *window);
  46.  
  47. // settings
  48. const unsigned int SCR_WIDTH = 800;
  49. const unsigned int SCR_HEIGHT = 600;
  50.  
  51. int width, height;
  52.  
  53. float cameraX = 0.0f, cameraZ = 0.0f;
  54.  
  55. ISoundEngine* soundEngine;
  56.  
  57. int amountOfTeam1Color = 0, amountOfTeam2Color = 0;
  58.  
  59. Vertex terrainVertices[(TERRAIN_SIZE * TERRAIN_SIZE)];
  60. int terrainIndices[(TERRAIN_SIZE * TERRAIN_SIZE) * 6];
  61.  
  62. objl::Loader loader;
  63. std::vector<Vertex> modelVertices;
  64. std::vector<int> modelIndices;
  65. int numVert, numInd;
  66.  
  67. std::vector<InkZone*> inkZones;
  68.  
  69. void placeInkSplotch(int originX, int originZ, int radius, float r, float g) {
  70.     for (int i = 0; i < inkZones.size(); i++) { //Check which zone the origin is in
  71.         if (originX > inkZones.at(i)->x1) {
  72.             if (originX < inkZones.at(i)->x2) {
  73.                 if (originZ > inkZones.at(i)->z1) {
  74.                     if (originZ < inkZones.at(i)->z2) {
  75.                         for (int j = 0; j < inkZones.at(i)->verticesInZone.size(); j++) {
  76.                             float xDistance = fabs(modelVertices.at(inkZones.at(i)->verticesInZone.at(j)).pos.x - originX);
  77.                             float zDistance = fabs(modelVertices.at(inkZones.at(i)->verticesInZone.at(j)).pos.z - originZ);
  78.                             if (xDistance < radius) {
  79.                                 if (zDistance < radius) {
  80.                                     modelVertices.at(inkZones.at(i)->verticesInZone.at(j)).color = glm::vec3(r, g, 0.0f);
  81.                                 }
  82.                             }
  83.                         }
  84.                     }
  85.                 }
  86.             }
  87.         }
  88.     }
  89.     glBufferData(GL_ARRAY_BUFFER, modelVertices.size() * sizeof(Vertex), &modelVertices[0], GL_DYNAMIC_DRAW);
  90. }
  91.  
  92. unsigned int VBO, VAO, EBO;
  93. void initTerrainRender() {
  94.     glGenVertexArrays(1, &VAO);
  95.     glGenBuffers(1, &VBO);
  96.     glGenBuffers(1, &EBO);
  97.  
  98.     glBindVertexArray(VAO);
  99.  
  100.     glBindBuffer(GL_ARRAY_BUFFER, VBO);
  101.     glBufferData(GL_ARRAY_BUFFER, modelVertices.size() * sizeof(Vertex), &modelVertices[0], GL_DYNAMIC_DRAW);
  102.  
  103.     glEnableVertexAttribArray(0);
  104.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
  105.     glEnableVertexAttribArray(1);
  106.     glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
  107.     glEnableVertexAttribArray(2);
  108.     glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(5 * sizeof(float)));
  109.  
  110.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
  111.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, modelIndices.size() * sizeof(int), &modelIndices[0], GL_STATIC_DRAW);
  112. }
  113.  
  114. void prepareTerrainRender() {
  115.     glBindVertexArray(VAO);
  116.     glBindBuffer(GL_ARRAY_BUFFER, VBO);
  117.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
  118.     glEnable(GL_DEPTH_TEST);
  119. }
  120.  
  121. unsigned int HUDVBO, HUDVAO;
  122. void initHUDRender() {
  123.     glGenVertexArrays(1, &HUDVAO);
  124.     glGenBuffers(1, &HUDVBO);
  125.  
  126.     glBindVertexArray(HUDVAO);
  127.  
  128.     glBindBuffer(GL_ARRAY_BUFFER, HUDVBO);
  129.  
  130.     glEnableVertexAttribArray(0);
  131.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
  132.     glEnableVertexAttribArray(1);
  133.     glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
  134. }
  135.  
  136. void prepareHUDRender() {
  137.     glBindVertexArray(HUDVAO);
  138.     glBindBuffer(GL_ARRAY_BUFFER, HUDVBO);
  139.     glDisable(GL_DEPTH_TEST);
  140. }
  141.  
  142. int countTeam1Ink() {
  143.     int amount = 0;
  144.     for (int i = 0; i < (TERRAIN_SIZE * TERRAIN_SIZE); i++) {
  145.         if (modelVertices[i].color.r > 0.5f) amount++;
  146.     }
  147.     return amount;
  148. }
  149.  
  150. int countTeam2Ink() {
  151.     int amount = 0;
  152.     for (int i = 0; i < (TERRAIN_SIZE * TERRAIN_SIZE); i++) {
  153.         if (modelVertices[i].color.g > 0.5f) amount++;
  154.     }
  155.     return amount;
  156. }
  157.  
  158. glm::mat4 proj;
  159. glm::mat4 view;
  160. glm::mat4 model;
  161. glm::mat4 identity;
  162.  
  163. Shader inkShader;
  164. Shader HUDShader;
  165. Shader postShader;
  166.  
  167. int fontWidths[256];
  168.  
  169. void renderString(std::string s, int x, int y, float r, float g, float b) {
  170.     Vertex HUDVertices[6];
  171.     char ch;
  172.     float u, v, u2, v2;
  173.  
  174.     HUDShader.setVec3("color", glm::vec3(r, g, b));
  175.  
  176.     for (int i = 0; i < s.length(); i++) {
  177.         ch = s[i] - 32;
  178.  
  179.         u = (float)((ch % 32) * 32) / 1024;
  180.         v = (float)((ch / 32) * 32) / 256;
  181.         u2 = (float)(((ch % 32) * 32) + 32) / 1024;
  182.         v2 = (float)(((ch / 32) * 32) + 32) / 256;
  183.  
  184.         HUDVertices[0] = Vertex{ 0.0f, 1.0f, 0.0f, u, v2, 0.0f, 0.0f, 0.0f };
  185.         HUDVertices[1] = Vertex{ 1.0f, 0.0f, 0.0f, u2, v, 0.0f, 0.0f, 0.0f };
  186.         HUDVertices[2] = Vertex{ 0.0f, 0.0f, 0.0f, u, v, 0.0f, 0.0f, 0.0f };
  187.         HUDVertices[3] = Vertex{ 0.0f, 1.0f, 0.0f, u, v2, 0.0f, 0.0f, 0.0f };
  188.         HUDVertices[4] = Vertex{ 1.0f, 1.0f, 0.0f, u2, v2, 0.0f, 0.0f, 0.0f };
  189.         HUDVertices[5] = Vertex{ 1.0f, 0.0f, 0.0f, u2, v, 0.0f, 0.0f, 0.0f };
  190.  
  191.         glBufferData(GL_ARRAY_BUFFER, sizeof(HUDVertices), HUDVertices, GL_DYNAMIC_DRAW);
  192.  
  193.         model = identity;
  194.         model = glm::translate(model, glm::vec3(x, y, 0.0f));
  195.         model = glm::scale(model, glm::vec3(32, 32, 1));
  196.  
  197.         HUDShader.setMat4("model", model);
  198.  
  199.         glDrawArrays(GL_TRIANGLES, 0, 6);
  200.  
  201.         x += fontWidths[s[i]];
  202.     }
  203.  
  204.     HUDVertices[0] = Vertex{ 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f };
  205.     HUDVertices[1] = Vertex{ 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f };
  206.     HUDVertices[2] = Vertex{ 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
  207.     HUDVertices[3] = Vertex{ 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f };
  208.     HUDVertices[4] = Vertex{ 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f };
  209.     HUDVertices[5] = Vertex{ 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f };
  210.  
  211.     glBufferData(GL_ARRAY_BUFFER, sizeof(HUDVertices), HUDVertices, GL_DYNAMIC_DRAW);
  212. }
  213.  
  214. void loadFontWidthsFromFile() {
  215.     std::ifstream fontDataFile;
  216.     std::stringstream fontDataStream;
  217.     std::string fontDataString;
  218.     fontDataFile.open("res/FontData.csv");
  219.     fontDataStream << fontDataFile.rdbuf();
  220.     fontDataFile.close();
  221.     fontDataString = fontDataStream.str();
  222.  
  223.     for (int i = 0; i < fontDataString.length() - 5; i++) {
  224.         if (fontDataString.substr(i, 5) == "Char ") {
  225.             i += 5;
  226.             int j = 0;
  227.             while (fontDataString.substr(i + j, 1) != " ") {
  228.                 j++;
  229.             }
  230.             std::string charNum = fontDataString.substr(i, j);
  231.             i += j;
  232.             if (fontDataString.substr(i, 12) == " Base Width,") {
  233.                 i += 12;
  234.                 j = 0;
  235.                 while (fontDataString.substr(i + j, 1) != "\n") {
  236.                     j++;
  237.                 }
  238.                 std::string widthNum = fontDataString.substr(i, j);
  239.                 i += j;
  240.                 fontWidths[std::stoi(charNum)] = std::stoi(widthNum);
  241.             }
  242.         }
  243.     }
  244. }
  245.  
  246. int fps = 0;
  247. int lastTime = 0;
  248. time_t now;
  249. struct tm *date, *lastDate;
  250.  
  251. unsigned int FBO;
  252. unsigned int FBOTex;
  253.  
  254. int main() {
  255.     time_t begin_time; //time at the start of the loop
  256.     time_t current_time; //time when checking for a one-second interval
  257.     double frames = 0; //number of times passed through the loop
  258.  
  259.     time(&begin_time); //sets begin_time
  260.  
  261.     loadFontWidthsFromFile();
  262.  
  263.     /*
  264.     loader.LoadFile("res/cave.obj");
  265.  
  266.     numVert = loader.LoadedVertices.size();
  267.     numInd = loader.LoadedIndices.size();
  268.  
  269.     for (int i = 0; i < numVert; i++) {
  270.         modelVertices.push_back({(float)loader.LoadedVertices.at(i).Position.X, (float)loader.LoadedVertices.at(i).Position.Y, (float)loader.LoadedVertices.at(i).Position.Z, (float)0.0f, (float)0.0f, 0.0f, 0.0f, 0.0f});
  271.     }
  272.     for (int i = 0; i < numInd; i++) {
  273.         modelIndices.push_back(i);
  274.     }
  275.     */
  276.  
  277.     soundEngine = createIrrKlangDevice();
  278.     //soundEngine->play2D("res/audio/Kinetosis (Diss-Pair) [Patch 4.0] - Splatoon 2 Soundtrack.mp3", TRUE);
  279.  
  280.     width = SCR_WIDTH;
  281.     height = SCR_HEIGHT;
  282.  
  283.     glfwInit();
  284.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  285.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  286.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  287.  
  288. #ifdef __APPLE__
  289.     glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // uncomment this statement to fix compilation on OS X
  290. #endif
  291.  
  292.     GLFWwindow* window = glfwCreateWindow(width, height, "OpenGL Game 3D", NULL, NULL);
  293.     if (window == NULL) {
  294.         std::cout << "Failed to create GLFW window" << std::endl;
  295.         glfwTerminate();
  296.         return -1;
  297.     }
  298.     glfwMakeContextCurrent(window);
  299.     glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
  300.  
  301.     if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
  302.         std::cout << "Failed to initialize GLAD" << std::endl;
  303.         return -1;
  304.     }
  305.  
  306.     glGenFramebuffers(1, &FBO);
  307.     glBindFramebuffer(GL_FRAMEBUFFER, FBO);
  308.     glDrawBuffer(GL_COLOR_ATTACHMENT0);
  309.  
  310.     glGenTextures(1, &FBOTex);
  311.     glActiveTexture(GL_TEXTURE2);
  312.     glBindTexture(GL_TEXTURE_2D, FBOTex);
  313.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
  314.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  315.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  316.     glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, FBOTex, 0);
  317.  
  318.     for (int i = 0; i < TERRAIN_SIZE * TERRAIN_SIZE; i++) {
  319.         terrainVertices[i] = Vertex{ (float)(i % TERRAIN_SIZE), 0.0f, (float)(i / TERRAIN_SIZE), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
  320.     }
  321.     for (int i = 0; i < (TERRAIN_SIZE - 1) * (TERRAIN_SIZE - 1); i++) {
  322.         terrainIndices[(i * 6)] = i + TERRAIN_SIZE;
  323.         terrainIndices[(i * 6) + 1] = i + 1;
  324.         terrainIndices[(i * 6) + 2] = i;
  325.         terrainIndices[(i * 6) + 3] = i + TERRAIN_SIZE;
  326.         terrainIndices[(i * 6) + 4] = i + TERRAIN_SIZE + 1;
  327.         terrainIndices[(i * 6) + 5] = i + 1;
  328.     }
  329.  
  330.     for (int i = 0; i < (TERRAIN_SIZE * TERRAIN_SIZE); i++) {
  331.         modelVertices.push_back(terrainVertices[i]);
  332.     }
  333.     for (int i = 0; i < (TERRAIN_SIZE * TERRAIN_SIZE) * 6; i++) {
  334.         modelIndices.push_back(terrainIndices[i]);
  335.     }
  336.  
  337.     for (int i = 0; i < 64; i++) {
  338.         float z1 = (((float)TERRAIN_SIZE / 64) * i) - 1;
  339.         float z2 = z1 + ((float)TERRAIN_SIZE / 64) + 1;
  340.         inkZones.push_back(new InkZone(0.0f, (float)TERRAIN_SIZE, z1, z2));
  341.     }
  342.  
  343.     int j = 0;
  344.     for (int i = 0; i < inkZones.size(); i++) {
  345.         for (int j = 0; j < modelVertices.size(); j++) {
  346.             if (modelVertices.at(j).pos.x > inkZones.at(i)->x1) {
  347.                 if (modelVertices.at(j).pos.x < inkZones.at(i)->x2) {
  348.                     if (modelVertices.at(j).pos.z > inkZones.at(i)->z1) {
  349.                         if (modelVertices.at(j).pos.z < inkZones.at(i)->z2) {
  350.                             inkZones.at(i)->verticesInZone.push_back(j);
  351.                         }
  352.                     }
  353.                 }
  354.             }
  355.         }
  356.     }
  357.  
  358.     for (int i = 0; i < inkZones.size(); i++) {
  359.         printf("(%g, %g), (%g, %g), %d\n", inkZones.at(i)->x1, inkZones.at(i)->x2, inkZones.at(i)->z1, inkZones.at(i)->z2, inkZones.at(i)->verticesInZone.size());
  360.     }
  361.  
  362.     initTerrainRender();
  363.  
  364.     /*
  365.     for (int i = 0; i < 256; i++) {
  366.         placeInkSplotch(128 + ((glfwGetTime() * 4) * sin(glfwGetTime())), 128 + ((glfwGetTime() * 4) * cos(glfwGetTime())), 10, 1, 0);
  367.         placeInkSplotch(rand() % TERRAIN_SIZE, rand() % TERRAIN_SIZE, rand() % 10, 0, 1);
  368.     }
  369.     */
  370.  
  371.     initHUDRender();
  372.  
  373.     glEnable(GL_BLEND);
  374.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  375.  
  376.     glEnable(GL_DEPTH_TEST);
  377.  
  378.     glEnable(GL_CULL_FACE);
  379.     glCullFace(GL_BACK);
  380.  
  381.     //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  382.  
  383.     Texture grass("res/textures/34293708-soil-seamless-texture.jpg");
  384.     Texture font("res/textures/SplatFont.bmp");
  385.     Texture dudv("res/textures/dudv.png");
  386.     grass.bindToTexUnit(0);
  387.     font.bindToTexUnit(1);
  388.     dudv.bindToTexUnit(3);
  389.  
  390.     srand(time(NULL));
  391.  
  392.     inkShader.initShader("res/shaders/inkVertex.glsl", "res/shaders/inkFragment.glsl");
  393.     HUDShader.initShader("res/shaders/vertex.glsl", "res/shaders/fragment.glsl");
  394.     postShader.initShader("res/shaders/postVertex.glsl", "res/shaders/postFragment.glsl");
  395.  
  396.     while (!glfwWindowShouldClose(window)) {
  397.         frames++; //increments frames
  398.         time(&current_time); //sets current_time
  399.  
  400.         processInput(window);
  401.  
  402.         glBindFramebuffer(GL_FRAMEBUFFER, FBO);
  403.         glViewport(0, 0, width, height);
  404.  
  405.         //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  406.  
  407.         glClearColor(0.5f, 0.5f, 1.0f, 1.0f);
  408.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  409.  
  410.         prepareTerrainRender();
  411.  
  412.         placeInkSplotch(rand() % TERRAIN_SIZE, rand() % TERRAIN_SIZE, rand() % 10, 1, 0);
  413.         placeInkSplotch(rand() % TERRAIN_SIZE, rand() % TERRAIN_SIZE, rand() % 10, 0, 1);
  414.  
  415.         proj = glm::perspective(glm::radians(45.0f), (float)width / (float)height, 0.1f, 1000.0f);
  416.        
  417.         cameraX = 128;// (128 + (128 * sin((float)glfwGetTime() * 0.4f)));
  418.         cameraZ = 0;// (128 + (128 * cos((float)glfwGetTime() * 0.4f)));
  419.  
  420.         view = identity;
  421.         view = glm::lookAt(glm::vec3(cameraX, 20, cameraZ), glm::vec3(128, 0, 128), glm::vec3(0, 1, 0));
  422.  
  423.         model = identity;
  424.         //model = glm::scale(model, glm::vec3(32.0f, 32, 32));
  425.  
  426.         inkShader.use();
  427.         inkShader.setMat4("projection", proj);
  428.         inkShader.setMat4("view", view);
  429.         inkShader.setMat4("model", model);
  430.         inkShader.setInt("tex", 0);
  431.         inkShader.setVec3("team1Color", 0.0f, 0.0f, 1.0f);
  432.         inkShader.setVec3("team2Color", 1.0f, 1.0f, 0.0f);
  433.         inkShader.setFloat("fogEnabled", 0);
  434.  
  435.         glDrawElements(GL_TRIANGLES, (TERRAIN_SIZE * TERRAIN_SIZE) * 6, GL_UNSIGNED_INT, (void*) 0);
  436.  
  437.         prepareHUDRender();
  438.  
  439.         proj = identity;
  440.         proj = glm::ortho(0.0f, (float)width, (float)height, 0.0f, -1.0f, 1.0f);
  441.         view = identity;
  442.         model = identity;
  443.         model = glm::translate(model, glm::vec3(100, 100, 0.0f));
  444.         model = glm::scale(model, glm::vec3(256, 256, 1));
  445.  
  446.         HUDShader.use();
  447.         HUDShader.setMat4("projection", proj);
  448.         HUDShader.setMat4("view", view);
  449.         HUDShader.setInt("tex", 1);
  450.  
  451.         //amountOfTeam1Color = countTeam1Ink();
  452.         //amountOfTeam2Color = countTeam2Ink();
  453.  
  454.         renderString("FPS: " + std::to_string(fps), 10, 10, 1.0f, 1.0f, 1.0f);
  455.         renderString("Amount of Team 1 Ink: " + std::to_string(amountOfTeam1Color), 10, 42, 1.0f, 1.0f, 1.0f);
  456.         renderString("Amount of Team 2 Ink: " + std::to_string(amountOfTeam2Color), 10, 74, 1.0f, 1.0f, 1.0f);
  457.  
  458.         renderString("L A M P", 10, 100, 0.0f, 0.0f, 1.0f);
  459.  
  460.         renderString("The connection is unstable.", 10, 200, 1.0f, 1.0f, 0.0f);
  461.  
  462.         if (amountOfTeam1Color > amountOfTeam2Color) {
  463.             renderString("Team 1 is winning!", 100, 300, 1.0f, 1.0f, 1.0f);
  464.         } else if(amountOfTeam1Color < amountOfTeam2Color) {
  465.             renderString("Team 2 is winning!", 100, 300, 1.0f, 1.0f, 1.0f);
  466.         } else {
  467.             renderString("It's a draw!", 100, 300, 1.0f, 1.0f, 1.0f);
  468.         }
  469.  
  470.         glBindFramebuffer(GL_FRAMEBUFFER, 0);
  471.  
  472.         //glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  473.  
  474.         model = identity;
  475.         model = glm::scale(model, glm::vec3(width, height, 1.0f));
  476.  
  477.         glActiveTexture(GL_TEXTURE2);
  478.         glBindTexture(GL_TEXTURE_2D, FBOTex);
  479.  
  480.         postShader.use();
  481.         postShader.setMat4("projection", proj);
  482.         postShader.setMat4("model", model);
  483.         postShader.setInt("tex", 2);
  484.         postShader.setInt("dudv", 3);
  485.         postShader.setFloat("time", glfwGetTime());
  486.  
  487.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  488.  
  489.         glDrawArrays(GL_TRIANGLES, 0, 6);
  490.  
  491.         glfwSwapBuffers(window);
  492.         glfwPollEvents();
  493.  
  494.         if (difftime(current_time, begin_time) >= 1.0) {
  495.             fps = frames;
  496.             frames = 0;
  497.             time(&begin_time);
  498.         }
  499.         if (difftime(current_time, begin_time) < 0.0) {
  500.             time(&begin_time);
  501.         }
  502.     }
  503.  
  504.     glDeleteVertexArrays(1, &VAO);
  505.     glDeleteBuffers(1, &VBO);
  506.  
  507.     grass.destroy();
  508.  
  509.     glfwTerminate();
  510.     return 0;
  511. }
  512.  
  513. void processInput(GLFWwindow *window) {
  514.     if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
  515.         glfwSetWindowShouldClose(window, true);
  516.  
  517.     int count;
  518.     const unsigned char* axes = glfwGetJoystickButtons(GLFW_JOYSTICK_1, &count);
  519. }
  520.  
  521. void framebuffer_size_callback(GLFWwindow* window, int wwidth, int wheight) {
  522.     if ((wwidth > 0) && (wheight > 0)) { //To fix crashing caused by minimizing the window
  523.         width = wwidth;
  524.         height = wheight;
  525.         glViewport(0, 0, wwidth, wheight);
  526.         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, wwidth, wheight, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
  527.     }
  528. }
Advertisement
Add Comment
Please, Sign In to add comment