Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <glad/glad.h>
- #include <GLFW/glfw3.h>
- #include <glm/glm.hpp>
- #include <glm/gtc/matrix_transform.hpp>
- #include <glm/gtc/type_ptr.hpp>
- #include <iostream>
- #include "Utils/ShaderLoader.h"
- #include "Utils/TextureLoader.h"
- #include "Utils/Camera.h"
- void framebuffer_size_callback(GLFWwindow* window, int width, int height);
- void mouse_callback(GLFWwindow* window, double xpos, double ypos);
- void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
- void processInput(GLFWwindow *window);
- const unsigned int SCR_WIDTH = 800;
- const unsigned int SCR_HEIGHT = 600;
- Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
- float lastX = SCR_WIDTH / 2.0f;
- float lastY = SCR_HEIGHT / 2.0f;
- bool firstMouse = true;
- float deltaTime = 0.0f;
- float lastFrame = 0.0f;
- int main()
- {
- glfwInit();
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- #ifdef __APPLE__
- glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
- #endif
- GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
- if (window == NULL)
- {
- std::cout << "Failed to create GLFW window" << std::endl;
- glfwTerminate();
- return -1;
- }
- glfwMakeContextCurrent(window);
- glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
- glfwSetCursorPosCallback(window, mouse_callback);
- glfwSetScrollCallback(window, scroll_callback);
- glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
- if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
- {
- std::cout << "Failed to initialize GLAD" << std::endl;
- return -1;
- }
- glEnable(GL_DEPTH_TEST);
- int lightningShader = loadShaders("Colors", "Colors");
- int lampShader = loadShaders("Lamp", "Lamp");
- float vertices[] = {
- // positions // normals // texture coords
- -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
- 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f,
- 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
- 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
- -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f,
- -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
- -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
- 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f,
- 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
- 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
- -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
- -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
- -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
- -0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
- -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
- -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
- -0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
- -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
- 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
- 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
- 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
- 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
- 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
- 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
- -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,
- 0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f,
- 0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,
- 0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,
- -0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f,
- -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,
- -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
- 0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
- 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
- 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
- -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
- -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f
- };
- // positions all containers
- glm::vec3 cubePositions[] = {
- glm::vec3(0.0f, 0.0f, 0.0f),
- glm::vec3(2.0f, 5.0f, -15.0f),
- glm::vec3(-1.5f, -2.2f, -2.5f),
- glm::vec3(-3.8f, -2.0f, -12.3f),
- glm::vec3(2.4f, -0.4f, -3.5f),
- glm::vec3(-1.7f, 3.0f, -7.5f),
- glm::vec3(1.3f, -2.0f, -2.5f),
- glm::vec3(1.5f, 2.0f, -2.5f),
- glm::vec3(1.5f, 0.2f, -1.5f),
- glm::vec3(-1.3f, 1.0f, -1.5f)
- };
- unsigned int VBO, cubeVAO;
- glGenVertexArrays(1, &cubeVAO);
- glGenBuffers(1, &VBO);
- glBindBuffer(GL_ARRAY_BUFFER, VBO);
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
- glBindVertexArray(cubeVAO);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
- glEnableVertexAttribArray(1);
- glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
- glEnableVertexAttribArray(2);
- unsigned int lightVAO;
- glGenVertexArrays(1, &lightVAO);
- glBindVertexArray(lightVAO);
- glBindBuffer(GL_ARRAY_BUFFER, VBO);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
- glEnableVertexAttribArray(0);
- auto diffuseMap = loadTexture("container2.png");
- auto specularMap = loadTexture("container2_specular.png");
- glUseProgram(lightningShader);
- glUniform1i(glGetUniformLocation(lightningShader, "material.diffuse"), 0);
- glUniform1i(glGetUniformLocation(lightningShader, "material.specular"), 1);
- while (!glfwWindowShouldClose(window))
- {
- float currentFrame = glfwGetTime();
- deltaTime = currentFrame - lastFrame;
- lastFrame = currentFrame;
- processInput(window);
- glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glUseProgram(lightningShader);
- glUniform3fv(glGetUniformLocation(lightningShader, "light.position"), 1, &camera.Position[0]);
- glUniform3fv(glGetUniformLocation(lightningShader, "light.direction"), 1, &camera.Front[0]);
- glUniform1f(glGetUniformLocation(lightningShader, "light.cutOff"), glm::radians(17.5f));
- glUniform1f(glGetUniformLocation(lightningShader, "light.outerCutOff"), glm::radians(12.5f));
- glUniform3fv(glGetUniformLocation(lightningShader, "viewPos"), 1, &camera.Position[0]);
- glUniform3f(glGetUniformLocation(lightningShader, "light.amdient"), 0.1f, 0.1f, 0.1f);
- glUniform3f(glGetUniformLocation(lightningShader, "light.diffuse"), 0.8f, 0.8f, 0.8f);
- glUniform3f(glGetUniformLocation(lightningShader, "light.specular"), 1.0f, 1.0f, 1.0f);
- glUniform1f(glGetUniformLocation(lightningShader, "light.constant"), 1.0f);
- glUniform1f(glGetUniformLocation(lightningShader, "light.linear"), 0.09f);
- glUniform1f(glGetUniformLocation(lightningShader, "light.quadratic"), 0.032f);
- glUniform1f(glGetUniformLocation(lightningShader, "light.shininess"), 32.0f);
- glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
- glm::mat4 view = camera.GetViewMatrix();
- glUniformMatrix4fv(glGetUniformLocation(lightningShader, "projection"), 1, GL_FALSE, &projection[0][0]);
- glUniformMatrix4fv(glGetUniformLocation(lightningShader, "view"), 1, GL_FALSE, &view[0][0]);
- glm::mat4 model = glm::mat4(1.0f);
- glUniformMatrix4fv(glGetUniformLocation(lightningShader, "model"), 1, GL_FALSE, &model[0][0]);
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, diffuseMap);
- glActiveTexture(GL_TEXTURE1);
- glBindTexture(GL_TEXTURE_2D, specularMap);
- glBindVertexArray(cubeVAO);
- for (unsigned int i = 0; i < 10; i++)
- {
- glm::mat4 model = glm::mat4(1.0f);
- model = glm::translate(model, cubePositions[i]);
- float angle = 20.0f * i;
- model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
- glUniformMatrix4fv(glGetUniformLocation(lightningShader, "model"), 1, GL_FALSE, &model[0][0]);
- glDrawArrays(GL_TRIANGLES, 0, 36);
- }
- //glUseProgram(lampShader);
- //glUniformMatrix4fv(glGetUniformLocation(lampShader, "view"), 1, GL_FALSE, &view[0][0]);
- //glUniformMatrix4fv(glGetUniformLocation(lampShader, "projection"), 1, GL_FALSE, &projection[0][0]);
- //model = glm::mat4(1.0f);
- //model = glm::translate(model, lightPos);
- //model = glm::scale(model, glm::vec3(0.2f));
- //glUniformMatrix4fv(glGetUniformLocation(lampShader, "model"), 1, GL_FALSE, &model[0][0]);
- //glBindVertexArray(lightVAO);
- //glDrawArrays(GL_TRIANGLES, 0, 36);
- glfwSwapBuffers(window);
- glfwPollEvents();
- }
- glDeleteVertexArrays(1, &cubeVAO);
- glDeleteVertexArrays(1, &lightVAO);
- glDeleteBuffers(1, &VBO);
- glfwTerminate();
- return 0;
- }
- void processInput(GLFWwindow *window)
- {
- if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
- glfwSetWindowShouldClose(window, true);
- if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
- camera.ProcessKeyboard(FORWARD, deltaTime);
- if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
- camera.ProcessKeyboard(BACKWARD, deltaTime);
- if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
- camera.ProcessKeyboard(LEFT, deltaTime);
- if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
- camera.ProcessKeyboard(RIGHT, deltaTime);
- if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
- camera.ProcessKeyboard(UP, deltaTime);
- if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
- camera.ProcessKeyboard(DOWN, deltaTime);
- }
- void framebuffer_size_callback(GLFWwindow* window, int width, int height)
- {
- glViewport(0, 0, width, height);
- }
- void mouse_callback(GLFWwindow* window, double xpos, double ypos)
- {
- if (firstMouse)
- {
- lastX = xpos;
- lastY = ypos;
- firstMouse = false;
- }
- float xoffset = xpos - lastX;
- float yoffset = lastY - ypos; // reversed since y-coordinates go from bottom to top
- lastX = xpos;
- lastY = ypos;
- camera.ProcessMouseMovement(xoffset, yoffset);
- }
- // glfw: whenever the mouse scroll wheel scrolls, this callback is called
- // ----------------------------------------------------------------------
- void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
- {
- camera.ProcessMouseScroll(yoffset);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement