Advertisement
Guest User

main.cpp

a guest
Jun 27th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.92 KB | None | 0 0
  1. #define GLEW_STATIC
  2. #define GLM_FORCE_RADIANS
  3. #include <glew.h>
  4. #include <GLFW\glfw3.h>
  5. #include <glm\glm.hpp>
  6. #include <glm\gtc\matrix_transform.hpp>
  7. #include <glm\gtc\type_ptr.hpp>
  8. #include <SOIL.h>
  9.  
  10. #include <iostream>
  11. #include <fstream>
  12. #include <string>
  13. #include <chrono>
  14. #include <vector>
  15.  
  16. #include "utils.h"
  17. #include "object.h"
  18. #include "camera.h"
  19.  
  20. using namespace std;
  21.  
  22. // cube vertices
  23. float cube[] =
  24. {
  25.     -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
  26.     0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
  27.     0.5f,  0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
  28.     0.5f,  0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
  29.     -0.5f,  0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
  30.     -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
  31.  
  32.     -0.5f, -0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
  33.     0.5f, -0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
  34.     0.5f,  0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
  35.     0.5f,  0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
  36.     -0.5f,  0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
  37.     -0.5f, -0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
  38.  
  39.     -0.5f,  0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
  40.     -0.5f,  0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
  41.     -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
  42.     -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
  43.     -0.5f, -0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
  44.     -0.5f,  0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
  45.  
  46.     0.5f,  0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
  47.     0.5f,  0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
  48.     0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
  49.     0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
  50.     0.5f, -0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
  51.     0.5f,  0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
  52.  
  53.     -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
  54.     0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
  55.     0.5f, -0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
  56.     0.5f, -0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
  57.     -0.5f, -0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
  58.     -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
  59.  
  60.     -0.5f,  0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
  61.     0.5f,  0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
  62.     0.5f,  0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
  63.     0.5f,  0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
  64.     -0.5f,  0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
  65.     -0.5f,  0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f
  66. };
  67.  
  68. // ignore this
  69. float triangle[] =
  70. {
  71.     -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f,
  72.     0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f,
  73.     0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f
  74. };
  75.  
  76. // and this too
  77. float cube2 [] =
  78. {
  79.     -0.5f, -0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
  80.     0.5f, -0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
  81.     0.5f,  0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
  82.     0.5f,  0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
  83.     -0.5f,  0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
  84.     -0.5f, -0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
  85.  
  86.     -0.5f, -0.5f,  0.5f,  0.0f,  0.0f, 1.0f,
  87.     0.5f, -0.5f,  0.5f,  0.0f,  0.0f, 1.0f,
  88.     0.5f,  0.5f,  0.5f,  0.0f,  0.0f, 1.0f,
  89.     0.5f,  0.5f,  0.5f,  0.0f,  0.0f, 1.0f,
  90.     -0.5f,  0.5f,  0.5f,  0.0f,  0.0f, 1.0f,
  91.     -0.5f, -0.5f,  0.5f,  0.0f,  0.0f, 1.0f,
  92.  
  93.     -0.5f,  0.5f,  0.5f, -1.0f,  0.0f,  0.0f,
  94.     -0.5f,  0.5f, -0.5f, -1.0f,  0.0f,  0.0f,
  95.     -0.5f, -0.5f, -0.5f, -1.0f,  0.0f,  0.0f,
  96.     -0.5f, -0.5f, -0.5f, -1.0f,  0.0f,  0.0f,
  97.     -0.5f, -0.5f,  0.5f, -1.0f,  0.0f,  0.0f,
  98.     -0.5f,  0.5f,  0.5f, -1.0f,  0.0f,  0.0f,
  99.  
  100.     0.5f,  0.5f,  0.5f,  1.0f,  0.0f,  0.0f,
  101.     0.5f,  0.5f, -0.5f,  1.0f,  0.0f,  0.0f,
  102.     0.5f, -0.5f, -0.5f,  1.0f,  0.0f,  0.0f,
  103.     0.5f, -0.5f, -0.5f,  1.0f,  0.0f,  0.0f,
  104.     0.5f, -0.5f,  0.5f,  1.0f,  0.0f,  0.0f,
  105.     0.5f,  0.5f,  0.5f,  1.0f,  0.0f,  0.0f,
  106.  
  107.     -0.5f, -0.5f, -0.5f,  0.0f, -1.0f,  0.0f,
  108.     0.5f, -0.5f, -0.5f,  0.0f, -1.0f,  0.0f,
  109.     0.5f, -0.5f,  0.5f,  0.0f, -1.0f,  0.0f,
  110.     0.5f, -0.5f,  0.5f,  0.0f, -1.0f,  0.0f,
  111.     -0.5f, -0.5f,  0.5f,  0.0f, -1.0f,  0.0f,
  112.     -0.5f, -0.5f, -0.5f,  0.0f, -1.0f,  0.0f,
  113.  
  114.     -0.5f,  0.5f, -0.5f,  0.0f,  1.0f,  0.0f,
  115.     0.5f,  0.5f, -0.5f,  0.0f,  1.0f,  0.0f,
  116.     0.5f,  0.5f,  0.5f,  0.0f,  1.0f,  0.0f,
  117.     0.5f,  0.5f,  0.5f,  0.0f,  1.0f,  0.0f,
  118.     -0.5f,  0.5f,  0.5f,  0.0f,  1.0f,  0.0f,
  119.     -0.5f,  0.5f, -0.5f,  0.0f,  1.0f,  0.0f
  120. };
  121.  
  122. // not this, it's the quad to render the framebuffer texture to
  123. float quad [] =
  124. {
  125.     -1.0f,  1.0f,  0.0f, 1.0f,
  126.     -1.0f, -1.0f,  0.0f, 0.0f,
  127.     1.0f, -1.0f,  1.0f, 0.0f,
  128.  
  129.     -1.0f,  1.0f,  0.0f, 1.0f,
  130.     1.0f, -1.0f,  1.0f, 0.0f,
  131.     1.0f,  1.0f,  1.0f, 1.0f
  132. };
  133.  
  134. bool keys[1024];
  135. camera cam(glm::vec3(0.0f, 0.0f, 3.5f), glm::vec3(0.0f, 0.0f, -1.0f), glm::vec3(0.0f, 1.0f, 0.0f), 0.05f);
  136. float deltaT = 0.0f;
  137. float lastFrame = 0.0f;
  138.  
  139. float lastX = 512.0f;
  140. float lastY = 384.0f;
  141.  
  142. void error(GLenum e)
  143. {
  144.     switch (e)
  145.     {
  146.     case GL_INVALID_ENUM:
  147.         cout << "Error: GL_INVALID_ENUM" << endl;
  148.         system("pause");
  149.         glfwTerminate();
  150.         break;
  151.     case GL_INVALID_VALUE:
  152.         cout << "Error: GL_INVALID_VALUE" << endl;
  153.         system("pause");
  154.         glfwTerminate();
  155.         break;
  156.     case GL_INVALID_OPERATION:
  157.         cout << "Error: GL_INVALID_OPERATION" << endl;
  158.         system("pause");
  159.         glfwTerminate();
  160.         break;
  161.     case GL_INVALID_FRAMEBUFFER_OPERATION:
  162.         cout << "Error: GL_INVALID_FRAMEBUFFER_OPERATION" << endl;
  163.         system("pause");
  164.         glfwTerminate();
  165.         break;
  166.     case GL_OUT_OF_MEMORY:
  167.         cout << "Error: GL_OUT_OF_MEMORY" << endl;
  168.         system("pause");
  169.         glfwTerminate();
  170.         break;
  171.     case GL_NO_ERROR:
  172.         cout << "No error reported" << endl;
  173.         break;
  174.     }
  175. }
  176.  
  177. void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
  178. {
  179.     if (key >= 0 && key < 1024)
  180.     {
  181.         if (action == GLFW_PRESS)
  182.             keys[key] = true;
  183.         else if (action == GLFW_RELEASE)
  184.             keys[key] = false;
  185.     }
  186. }
  187.  
  188. void mouse_callback(GLFWwindow* window, double xpos, double ypos)
  189. {
  190.     float x_offset = xpos - lastX;
  191.     float y_offset = lastY - ypos;
  192.     lastX = xpos;
  193.     lastY = ypos;
  194.  
  195.     float sensitivity = 0.1f;
  196.     x_offset *= sensitivity;
  197.     y_offset *= sensitivity;
  198.  
  199.     cam.yaw += x_offset;
  200.     cam.pitch += y_offset;
  201.  
  202.     if (cam.pitch > 89.0f)
  203.         cam.pitch = 89.0f;
  204.     if (cam.pitch < -89.0f)
  205.         cam.pitch = -89.0f;
  206.    
  207.     cam.update();
  208. }
  209.  
  210. void processKeys()
  211. {
  212.     if (keys[GLFW_KEY_W])
  213.     {
  214.         cam.cameraPos += cam.speed * cam.cameraFront;
  215.     }
  216.     if (keys[GLFW_KEY_S])
  217.     {
  218.         cam.cameraPos -= cam.speed * cam.cameraFront;
  219.     }
  220.     if (keys[GLFW_KEY_A])
  221.     {
  222.         cam.cameraPos -= glm::normalize(glm::cross(cam.cameraFront, cam.cameraUp)) * cam.speed;
  223.     }
  224.     if (keys[GLFW_KEY_D])
  225.     {
  226.         cam.cameraPos += glm::normalize(glm::cross(cam.cameraFront, cam.cameraUp)) * cam.speed;
  227.     }
  228. }
  229.  
  230. int main()
  231. {
  232.     glfwInit();
  233.  
  234.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
  235.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  236.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  237.     glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  238.     glfwWindowHint(GLFW_SAMPLES, 4);
  239.     glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
  240.  
  241.     GLFWwindow* window = glfwCreateWindow(1024, 768, "opengl tutorial", nullptr, nullptr);
  242.  
  243.     if (window == nullptr)
  244.     {
  245.         cout << "Failed to create window" << endl;
  246.         glfwTerminate();
  247.         system("pause");
  248.         return -1;
  249.     }
  250.  
  251.     glfwMakeContextCurrent(window);
  252.  
  253.     glfwSetCursorPos(window, 1024 / 2, 768 / 2);
  254.     glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
  255.     glfwSetKeyCallback(window, (GLFWkeyfun) key_callback);
  256.     glfwSetCursorPosCallback(window, (GLFWcursorposfun) mouse_callback);
  257.  
  258.     cout << "Address of window: " << window << endl;
  259.  
  260.     glewExperimental = GL_TRUE;
  261.     glewInit();
  262.  
  263.     glEnable(GL_DEPTH_TEST);
  264.     glEnable(GL_STENCIL_TEST);
  265.  
  266.     GLuint vao;
  267.     glGenVertexArrays(1, &vao);
  268.     glBindVertexArray(vao);
  269.  
  270.     GLuint prog = setupProgram("vertex.glsl", "fragment.glsl");
  271.     GLuint prog2 = setupProgram("vertex_2.glsl", "fragment_2.glsl");
  272.     GLuint progOutline = setupProgram("vertex_outline.glsl", "frag_outline.glsl");
  273.     GLuint progQuad = setupProgram("vertex_quad.glsl", "frag_quad.glsl");
  274.  
  275.     glUseProgram(progQuad);
  276.  
  277.     GLuint vboQuad;
  278.     glGenBuffers(1, &vboQuad);
  279.     glBindBuffer(GL_ARRAY_BUFFER, vboQuad);
  280.     glBufferData(GL_ARRAY_BUFFER, sizeof(quad), quad, GL_STATIC_DRAW);
  281.  
  282.     GLint quadPos = glGetAttribLocation(progQuad, "pos");
  283.     glEnableVertexAttribArray(quadPos);
  284.     glVertexAttribPointer(quadPos, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), nullptr);
  285.     GLint texCoord = glGetAttribLocation(progQuad, "texcoord");
  286.     glEnableVertexAttribArray(texCoord);
  287.     glVertexAttribPointer(texCoord, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*) (2 * sizeof(float)));
  288.  
  289.     glUseProgram(prog);
  290.  
  291.     float rot = 0.0f;
  292.     float camX = 1.0f;
  293.     float camY = 1.2f;
  294.     float camZ = 3.5f;
  295.  
  296.     glm::mat4 model;
  297.     model = glm::mat4(1.0f) * glm::rotate(model, glm::radians(rot), glm::vec3(0.0f, 1.0f, 0.0f));
  298.  
  299.     glm::mat4 view;
  300.     view = cam.lookAt();
  301.  
  302.     glm::mat4 proj;
  303.     proj = glm::perspective(glm::radians(45.0f), 1024.0f / 768.0f, 1.0f, 100.0f);
  304.  
  305.     object x(cube, 36, 8, "sand.png", prog, glm::vec3(0.0f, 0.0f, 0.0f), view, proj);
  306.     x.init();
  307.  
  308.     glUseProgram(prog);
  309.  
  310.     object block(cube, 36, 8, "box-texture.png", prog, glm::vec3(0.0f, 0.0f, 0.0f), view, proj, true, false);
  311.     block.init();
  312.  
  313.     GLuint fbo;
  314.     glGenFramebuffers(1, &fbo);
  315.     glBindFramebuffer(GL_FRAMEBUFFER, fbo);
  316.  
  317.     GLuint texFbo;
  318.     glGenTextures(1, &texFbo);
  319.     glBindTexture(GL_TEXTURE_2D, texFbo);
  320.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1024, 768, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
  321.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  322.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  323.     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texFbo, 0);
  324.  
  325.     GLuint rbo;
  326.     glGenRenderbuffers(1, &rbo);
  327.     glBindRenderbuffer(GL_RENDERBUFFER, rbo);
  328.     glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 1024, 768);
  329.     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo);
  330.  
  331.     if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
  332.         cout << "Error: Framebuffer not complete" << endl;
  333.  
  334.     while (!glfwWindowShouldClose(window))
  335.     {
  336.         if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
  337.             glfwSetWindowShouldClose(window, GL_TRUE);
  338.  
  339.         float currentFrame = glfwGetTime();
  340.         deltaT = currentFrame - lastFrame;
  341.         lastFrame = currentFrame;
  342.  
  343.         cam.speed = 4.0f * deltaT;
  344.  
  345.         glfwPollEvents();
  346.         processKeys();
  347.         block.view = cam.lookAt();
  348.  
  349.         glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  350.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  351.  
  352.         glUseProgram(prog);
  353.         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
  354.         glEnable(GL_DEPTH_TEST);
  355.         block.update();
  356.         block.draw();
  357.  
  358.         glBindFramebuffer(GL_FRAMEBUFFER, 0);
  359.         glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
  360.         glClear(GL_COLOR_BUFFER_BIT);
  361.         glUseProgram(progQuad);
  362.         glDisable(GL_DEPTH_TEST);
  363.         glBindBuffer(GL_ARRAY_BUFFER, vboQuad);
  364.         glEnableVertexAttribArray(quadPos);
  365.         glVertexAttribPointer(quadPos, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), nullptr);
  366.         glEnableVertexAttribArray(texCoord);
  367.         glVertexAttribPointer(texCoord, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*) (2 * sizeof(float)));
  368.         glBindTexture(GL_TEXTURE_2D, texFbo);
  369.         glDrawArrays(GL_TRIANGLES, 0, 6);
  370.  
  371.         glfwSwapBuffers(window);
  372.     }
  373.  
  374.     glDeleteVertexArrays(1, &vao);
  375.     glDeleteFramebuffers(1, &fbo);
  376.  
  377.     glfwTerminate();
  378.  
  379.     return 0;
  380. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement