Advertisement
Guest User

main.cpp

a guest
Apr 21st, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.11 KB | None | 0 0
  1. #include <glad/glad.h>
  2. #include <GLFW/glfw3.h>
  3.  
  4. #include <glm/glm.hpp>
  5. #include <glm/gtc/matrix_transform.hpp>
  6. #include <glm/gtc/type_ptr.hpp>
  7.  
  8. #include <iostream>
  9. #include "Utils/ShaderLoader.h"
  10. #include "Utils/TextureLoader.h"
  11. #include "Utils/Camera.h"
  12.  
  13.  
  14. void framebuffer_size_callback(GLFWwindow* window, int width, int height);
  15. void mouse_callback(GLFWwindow* window, double xpos, double ypos);
  16. void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
  17. void processInput(GLFWwindow *window);
  18.  
  19. const unsigned int SCR_WIDTH = 800;
  20. const unsigned int SCR_HEIGHT = 600;
  21.  
  22. Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
  23. float lastX = SCR_WIDTH / 2.0f;
  24. float lastY = SCR_HEIGHT / 2.0f;
  25. bool firstMouse = true;
  26.  
  27. float deltaTime = 0.0f;
  28. float lastFrame = 0.0f;
  29.  
  30. int main()
  31. {
  32. glfwInit();
  33. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  34. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  35. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  36.  
  37. #ifdef __APPLE__
  38. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  39. #endif
  40.  
  41. GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
  42. if (window == NULL)
  43. {
  44. std::cout << "Failed to create GLFW window" << std::endl;
  45. glfwTerminate();
  46. return -1;
  47. }
  48. glfwMakeContextCurrent(window);
  49. glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
  50. glfwSetCursorPosCallback(window, mouse_callback);
  51. glfwSetScrollCallback(window, scroll_callback);
  52.  
  53. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
  54.  
  55. if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
  56. {
  57. std::cout << "Failed to initialize GLAD" << std::endl;
  58. return -1;
  59. }
  60.  
  61. glEnable(GL_DEPTH_TEST);
  62.  
  63. int lightningShader = loadShaders("Colors", "Colors");
  64. int lampShader = loadShaders("Lamp", "Lamp");
  65. float vertices[] = {
  66. // positions // normals // texture coords
  67. -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
  68. 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f,
  69. 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
  70. 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
  71. -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f,
  72. -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
  73.  
  74. -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
  75. 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f,
  76. 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
  77. 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
  78. -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
  79. -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
  80.  
  81. -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
  82. -0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
  83. -0.5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1.0f, 0.0f, 1.0f, 0.0f,
  106. -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
  107. -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f
  108. };
  109. // positions all containers
  110. glm::vec3 cubePositions[] = {
  111. glm::vec3(0.0f, 0.0f, 0.0f),
  112. glm::vec3(2.0f, 5.0f, -15.0f),
  113. glm::vec3(-1.5f, -2.2f, -2.5f),
  114. glm::vec3(-3.8f, -2.0f, -12.3f),
  115. glm::vec3(2.4f, -0.4f, -3.5f),
  116. glm::vec3(-1.7f, 3.0f, -7.5f),
  117. glm::vec3(1.3f, -2.0f, -2.5f),
  118. glm::vec3(1.5f, 2.0f, -2.5f),
  119. glm::vec3(1.5f, 0.2f, -1.5f),
  120. glm::vec3(-1.3f, 1.0f, -1.5f)
  121. };
  122.  
  123. unsigned int VBO, cubeVAO;
  124. glGenVertexArrays(1, &cubeVAO);
  125. glGenBuffers(1, &VBO);
  126.  
  127. glBindBuffer(GL_ARRAY_BUFFER, VBO);
  128. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  129.  
  130. glBindVertexArray(cubeVAO);
  131.  
  132. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
  133. glEnableVertexAttribArray(0);
  134.  
  135. glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
  136. glEnableVertexAttribArray(1);
  137.  
  138. glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
  139. glEnableVertexAttribArray(2);
  140.  
  141. unsigned int lightVAO;
  142. glGenVertexArrays(1, &lightVAO);
  143. glBindVertexArray(lightVAO);
  144. glBindBuffer(GL_ARRAY_BUFFER, VBO);
  145.  
  146. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
  147. glEnableVertexAttribArray(0);
  148.  
  149. auto diffuseMap = loadTexture("container2.png");
  150. auto specularMap = loadTexture("container2_specular.png");
  151.  
  152. glUseProgram(lightningShader);
  153. glUniform1i(glGetUniformLocation(lightningShader, "material.diffuse"), 0);
  154. glUniform1i(glGetUniformLocation(lightningShader, "material.specular"), 1);
  155.  
  156. while (!glfwWindowShouldClose(window))
  157. {
  158. float currentFrame = glfwGetTime();
  159. deltaTime = currentFrame - lastFrame;
  160. lastFrame = currentFrame;
  161.  
  162. processInput(window);
  163.  
  164. glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
  165. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  166.  
  167.  
  168. glUseProgram(lightningShader);
  169. glUniform3fv(glGetUniformLocation(lightningShader, "light.position"), 1, &camera.Position[0]);
  170. glUniform3fv(glGetUniformLocation(lightningShader, "light.direction"), 1, &camera.Front[0]);
  171. glUniform1f(glGetUniformLocation(lightningShader, "light.cutOff"), glm::radians(17.5f));
  172. glUniform1f(glGetUniformLocation(lightningShader, "light.outerCutOff"), glm::radians(12.5f));
  173. glUniform3fv(glGetUniformLocation(lightningShader, "viewPos"), 1, &camera.Position[0]);
  174.  
  175. glUniform3f(glGetUniformLocation(lightningShader, "light.amdient"), 0.1f, 0.1f, 0.1f);
  176. glUniform3f(glGetUniformLocation(lightningShader, "light.diffuse"), 0.8f, 0.8f, 0.8f);
  177. glUniform3f(glGetUniformLocation(lightningShader, "light.specular"), 1.0f, 1.0f, 1.0f);
  178.  
  179. glUniform1f(glGetUniformLocation(lightningShader, "light.constant"), 1.0f);
  180. glUniform1f(glGetUniformLocation(lightningShader, "light.linear"), 0.09f);
  181. glUniform1f(glGetUniformLocation(lightningShader, "light.quadratic"), 0.032f);
  182.  
  183. glUniform1f(glGetUniformLocation(lightningShader, "light.shininess"), 32.0f);
  184.  
  185. glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
  186. glm::mat4 view = camera.GetViewMatrix();
  187.  
  188. glUniformMatrix4fv(glGetUniformLocation(lightningShader, "projection"), 1, GL_FALSE, &projection[0][0]);
  189. glUniformMatrix4fv(glGetUniformLocation(lightningShader, "view"), 1, GL_FALSE, &view[0][0]);
  190.  
  191. glm::mat4 model = glm::mat4(1.0f);
  192. glUniformMatrix4fv(glGetUniformLocation(lightningShader, "model"), 1, GL_FALSE, &model[0][0]);
  193.  
  194. glActiveTexture(GL_TEXTURE0);
  195. glBindTexture(GL_TEXTURE_2D, diffuseMap);
  196.  
  197. glActiveTexture(GL_TEXTURE1);
  198. glBindTexture(GL_TEXTURE_2D, specularMap);
  199.  
  200. glBindVertexArray(cubeVAO);
  201. for (unsigned int i = 0; i < 10; i++)
  202. {
  203. glm::mat4 model = glm::mat4(1.0f);
  204. model = glm::translate(model, cubePositions[i]);
  205. float angle = 20.0f * i;
  206. model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
  207. glUniformMatrix4fv(glGetUniformLocation(lightningShader, "model"), 1, GL_FALSE, &model[0][0]);
  208.  
  209. glDrawArrays(GL_TRIANGLES, 0, 36);
  210. }
  211.  
  212. //glUseProgram(lampShader);
  213. //glUniformMatrix4fv(glGetUniformLocation(lampShader, "view"), 1, GL_FALSE, &view[0][0]);
  214. //glUniformMatrix4fv(glGetUniformLocation(lampShader, "projection"), 1, GL_FALSE, &projection[0][0]);
  215.  
  216. //model = glm::mat4(1.0f);
  217. //model = glm::translate(model, lightPos);
  218. //model = glm::scale(model, glm::vec3(0.2f));
  219. //glUniformMatrix4fv(glGetUniformLocation(lampShader, "model"), 1, GL_FALSE, &model[0][0]);
  220.  
  221. //glBindVertexArray(lightVAO);
  222. //glDrawArrays(GL_TRIANGLES, 0, 36);
  223.  
  224. glfwSwapBuffers(window);
  225. glfwPollEvents();
  226. }
  227.  
  228. glDeleteVertexArrays(1, &cubeVAO);
  229. glDeleteVertexArrays(1, &lightVAO);
  230. glDeleteBuffers(1, &VBO);
  231.  
  232. glfwTerminate();
  233. return 0;
  234. }
  235.  
  236. void processInput(GLFWwindow *window)
  237. {
  238. if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
  239. glfwSetWindowShouldClose(window, true);
  240.  
  241. if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
  242. camera.ProcessKeyboard(FORWARD, deltaTime);
  243. if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
  244. camera.ProcessKeyboard(BACKWARD, deltaTime);
  245. if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
  246. camera.ProcessKeyboard(LEFT, deltaTime);
  247. if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
  248. camera.ProcessKeyboard(RIGHT, deltaTime);
  249. if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
  250. camera.ProcessKeyboard(UP, deltaTime);
  251. if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
  252. camera.ProcessKeyboard(DOWN, deltaTime);
  253. }
  254.  
  255. void framebuffer_size_callback(GLFWwindow* window, int width, int height)
  256. {
  257. glViewport(0, 0, width, height);
  258. }
  259.  
  260. void mouse_callback(GLFWwindow* window, double xpos, double ypos)
  261. {
  262. if (firstMouse)
  263. {
  264. lastX = xpos;
  265. lastY = ypos;
  266. firstMouse = false;
  267. }
  268.  
  269. float xoffset = xpos - lastX;
  270. float yoffset = lastY - ypos; // reversed since y-coordinates go from bottom to top
  271.  
  272. lastX = xpos;
  273. lastY = ypos;
  274.  
  275. camera.ProcessMouseMovement(xoffset, yoffset);
  276. }
  277.  
  278. // glfw: whenever the mouse scroll wheel scrolls, this callback is called
  279. // ----------------------------------------------------------------------
  280. void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
  281. {
  282. camera.ProcessMouseScroll(yoffset);
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement