Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define GLEW_STATIC
- #include <glew.h>
- // GLFW
- #include <glfw3.h>
- #include <iostream>
- #include "Shader.h"
- #include "Camera.h"
- #include <SOIL.h>
- #include <glm/glm.hpp>
- #include <glm/gtc/matrix_transform.hpp>
- #include <glm/gtc/type_ptr.hpp>
- bool keys[1024];
- const GLuint WIDTH = 800, HEIGHT = 600;
- Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
- GLfloat lastX = WIDTH / 2;
- GLfloat lastY = HEIGHT / 2;
- GLfloat deltaTime = 0.0f; // Time between current frame and last frame
- GLfloat lastFrame = 0.0f;
- glm::vec3 lightPos(1.2f, 1.0f, 2.0f);
- void Do_Movement()
- {
- // Camera controls
- if (keys[GLFW_KEY_W])
- camera.ProcessKeyboard(FORWARD, deltaTime);
- if (keys[GLFW_KEY_S])
- camera.ProcessKeyboard(BACKWARD, deltaTime);
- if (keys[GLFW_KEY_A])
- camera.ProcessKeyboard(LEFT, deltaTime);
- if (keys[GLFW_KEY_D])
- camera.ProcessKeyboard(RIGHT, deltaTime);
- }
- // Is called whenever a key is pressed/released via GLFW
- void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
- {
- //cout << key << endl;
- if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
- glfwSetWindowShouldClose(window, GL_TRUE);
- if (key >= 0 && key < 1024)
- {
- if (action == GLFW_PRESS)
- keys[key] = true;
- else if (action == GLFW_RELEASE)
- keys[key] = false;
- }
- }
- bool firstMouse = true;
- void mouse_callback(GLFWwindow* window, double xpos, double ypos)
- {
- if (firstMouse)
- {
- lastX = xpos;
- lastY = ypos;
- firstMouse = false;
- }
- GLfloat xoffset = xpos - lastX;
- GLfloat yoffset = lastY - ypos; // Reversed since y-coordinates go from bottom to left
- lastX = xpos;
- lastY = ypos;
- camera.ProcessMouseMovement(xoffset, yoffset);
- }
- void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
- {
- camera.ProcessMouseScroll(yoffset);
- }
- GLuint InitVBO(GLfloat vertices[])
- {
- GLuint VBO;
- glGenBuffers(1, &VBO);
- glBindBuffer(GL_ARRAY_BUFFER, VBO);
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
- return VBO;
- }
- int main()
- {
- glfwInit();
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
- GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", nullptr, nullptr);
- if (window == nullptr)
- {
- std::cout << "Failed to create GLFW window" << std::endl;
- glfwTerminate();
- return -1;
- }
- glfwMakeContextCurrent(window);
- glfwSetKeyCallback(window, key_callback);
- glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
- glfwSetCursorPosCallback(window, mouse_callback);
- glfwSetScrollCallback(window, scroll_callback);
- glewExperimental = GL_TRUE;
- if (glewInit() != GLEW_OK)
- {
- std::cout << "Failed to initialize GLEW" << std::endl;
- return -1;
- }
- glViewport(0, 0, WIDTH, HEIGHT);
- Shader ourShader("vertexShader.txt", "fragmentShader.txt");
- Shader lampShader("lampVertexShader.txt", "lampFragmentShader.txt");
- float vertices[] = {
- -0.5f, -0.5f, -0.5f,
- 0.5f, -0.5f, -0.5f,
- 0.5f, 0.5f, -0.5f,
- 0.5f, 0.5f, -0.5f,
- -0.5f, 0.5f, -0.5f,
- -0.5f, -0.5f, -0.5f,
- -0.5f, -0.5f, 0.5f,
- 0.5f, -0.5f, 0.5f,
- 0.5f, 0.5f, 0.5f,
- 0.5f, 0.5f, 0.5f,
- -0.5f, 0.5f, 0.5f,
- -0.5f, -0.5f, 0.5f,
- -0.5f, 0.5f, 0.5f,
- -0.5f, 0.5f, -0.5f,
- -0.5f, -0.5f, -0.5f,
- -0.5f, -0.5f, -0.5f,
- -0.5f, -0.5f, 0.5f,
- -0.5f, 0.5f, 0.5f,
- 0.5f, 0.5f, 0.5f,
- 0.5f, 0.5f, -0.5f,
- 0.5f, -0.5f, -0.5f,
- 0.5f, -0.5f, -0.5f,
- 0.5f, -0.5f, 0.5f,
- 0.5f, 0.5f, 0.5f,
- -0.5f, -0.5f, -0.5f,
- 0.5f, -0.5f, -0.5f,
- 0.5f, -0.5f, 0.5f,
- 0.5f, -0.5f, 0.5f,
- -0.5f, -0.5f, 0.5f,
- -0.5f, -0.5f, -0.5f,
- -0.5f, 0.5f, -0.5f,
- 0.5f, 0.5f, -0.5f,
- 0.5f, 0.5f, 0.5f,
- 0.5f, 0.5f, 0.5f,
- -0.5f, 0.5f, 0.5f,
- -0.5f, 0.5f, -0.5f
- };
- 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)
- };
- GLuint indices[] = { // Note that we start from 0!
- 0, 1, 3, // First Triangle
- 1, 2, 3 // Second Triangle
- };
- GLuint VBO, VAO, EBO;
- glGenVertexArrays(1, &VAO);
- glGenBuffers(1, &VBO);
- glGenBuffers(1, &EBO);
- glBindVertexArray(VAO);
- glBindBuffer(GL_ARRAY_BUFFER, VBO);
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
- // Position attribute
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0);
- glEnableVertexAttribArray(0);
- glBindVertexArray(0); // Unbind VAO
- GLuint lightVAO;
- glGenVertexArrays(1, &lightVAO);
- glBindVertexArray(lightVAO);
- // Так как VBO объекта-контейнера уже содержит все необходимые данные, то нам нужно только связать с ним новый VAO
- glBindBuffer(GL_ARRAY_BUFFER, VBO);
- // Настраиваем атрибуты (нашей лампе понадобятся только координаты вершин)
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
- glEnableVertexAttribArray(0);
- glBindVertexArray(0);
- // Container
- int width, height;
- unsigned char* image = SOIL_load_image("container.jpg", &width, &height, 0, SOIL_LOAD_RGB);
- glEnable(GL_DEPTH_TEST);
- while (!glfwWindowShouldClose(window))
- {
- glfwPollEvents();
- Do_Movement();
- GLfloat currentFrame = glfwGetTime();
- deltaTime = currentFrame - lastFrame;
- lastFrame = currentFrame;
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT);
- // Activate shader
- ourShader.Use();
- GLint objectColorLoc = glGetUniformLocation(ourShader.Program, "objectColor");
- GLint lightColorLoc = glGetUniformLocation(ourShader.Program, "lightColor");
- glUniform3f(objectColorLoc, 1.0f, 0.5f, 0.31f);
- glUniform3f(lightColorLoc, 1.0f, 1.0f, 1.0f);
- glm::mat4 view;
- view = camera.GetViewMatrix();
- glm::mat4 projection;
- projection = glm::perspective(camera.Zoom, (float) HEIGHT / (float) WIDTH, 0.1f, 1000.0f);
- GLint modelLoc = glGetUniformLocation(ourShader.Program, "model");
- GLint viewLoc = glGetUniformLocation(ourShader.Program, "view");
- glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
- GLint projectionLoc = glGetUniformLocation(ourShader.Program, "projection");
- glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
- // Draw container
- glBindVertexArray(VAO);
- for (GLuint i = 0; i < 10; i++)
- {
- glm::mat4 model = { { 1, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, 1, 0 }, { 0, 0, 0, 1 } };
- model = glm::translate(model, cubePositions[i]);
- model = glm::rotate(model, glm::radians((GLfloat)glfwGetTime() * 50.0f),
- glm::vec3(0.5f, 1.0f, 0.5f + sin(glm::radians((GLfloat)glfwGetTime() * 50.0f + i * 20.0f)) / 2));
- glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
- glDrawArrays(GL_TRIANGLES, 0, 36);
- }
- glBindVertexArray(0);
- glBindVertexArray(0);
- lampShader.Use();
- // Get location objects for the matrices on the lamp shader (these could be different on a different shader)
- modelLoc = glGetUniformLocation(lampShader.Program, "model");
- viewLoc = glGetUniformLocation(lampShader.Program, "view");
- projectionLoc = glGetUniformLocation(lampShader.Program, "projection");
- // Set matrices
- glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
- glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
- glm::mat4 model = glm::mat4();
- model = glm::translate(model, lightPos);
- model = glm::scale(model, glm::vec3(0.2f)); // Make it a smaller cube
- glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
- // Draw the light object (using light's vertex attributes)
- glBindVertexArray(lightVAO);
- glDrawArrays(GL_TRIANGLES, 0, 36);
- glBindVertexArray(0);
- glfwSwapBuffers(window);
- }
- glDeleteVertexArrays(1, &VAO);
- glDeleteBuffers(1, &VBO);
- glDeleteBuffers(1, &EBO);
- glfwTerminate();
- return 0;
- }
- /*
- GLuint texture1, texture2;
- glGenTextures(1, &texture1);
- glBindTexture(GL_TEXTURE_2D, texture1);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
- glGenerateMipmap(GL_TEXTURE_2D);
- SOIL_free_image_data(image);
- glBindTexture(GL_TEXTURE_2D, 0);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- // Awesomeface
- image = SOIL_load_image("pidor.jpg", &width, &height, 0, SOIL_LOAD_RGB);
- glGenTextures(1, &texture2);
- glBindTexture(GL_TEXTURE_2D, texture2);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
- glGenerateMipmap(GL_TEXTURE_2D);
- SOIL_free_image_data(image);
- glBindTexture(GL_TEXTURE_2D, 0);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement