Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #define GLEW_STATIC
- #include <GL/glew.h>
- #include <GLFW/glfw3.h>
- //#include <GL/GL.h>
- //#include <GL/GLU.h>
- #include <SOIL.h>
- #include <glm/glm.hpp>
- #include <glm/gtc/matrix_transform.hpp>
- #include <glm/gtc/type_ptr.hpp>
- #include "Shader.h"
- void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
- void mouse_callback(GLFWwindow* window, double xpos, double ypos);
- void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
- void do_movement();
- const GLuint WIDTH = 800, HEIGHT = 600;
- glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);
- glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
- glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
- GLfloat yaw = -90.0f; // Yaw is initialized to -90.0 degrees since a yaw of 0.0 results in a direction vector pointing to the right (due to how Eular angles work) so we initially rotate a bit to the left.
- GLfloat pitch = 0.0f;
- GLfloat lastX = WIDTH / 2.0;
- GLfloat lastY = HEIGHT / 2.0;
- GLfloat fov = 45.0f;
- bool keys[1024];
- // Deltatime
- GLfloat deltaTime = 0.0f; // Time between current frame and last frame
- GLfloat lastFrame = 0.0f; // Time of last frame
- int main()
- {
- //Инициализация GLFW
- glfwInit();
- //Настройка GLFW
- //Задается минимальная требуемая версия OpenGL.
- //Мажорная
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
- //Минорная
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
- //Установка профайла для которого создается контекст
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- //Выключение возможности изменения размера окна
- glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
- GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "ProjectKovaleva", nullptr, nullptr);
- glfwMakeContextCurrent(window);
- glfwSetKeyCallback(window, key_callback);
- glfwSetCursorPosCallback(window, mouse_callback);
- glfwSetScrollCallback(window, scroll_callback);
- // GLFW Options
- glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
- glewExperimental = GL_TRUE;
- glewInit();
- // int width, height;
- // glfwGetFramebufferSize(window, &width, &height);
- // glViewport(0, 0, width, height);
- glViewport(0, 0, WIDTH, HEIGHT);
- glEnable(GL_DEPTH_TEST);
- Shader ourShader("C:\\Users\\Виктория\\Desktop\\проги\\MashGraph\\MashGraph\\textures.vs.txt", "C:\\Users\\Виктория\\Desktop\\проги\\MashGraph\\MashGraph\\textures.frag.txt");
- /*GLfloat vertices[] = {
- // Positions // Texture Coords
- 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, // Top Right
- 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, // Bottom Right
- -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, // Bottom Left
- -0.5f, 0.5f, 0.0f, 0.0f, 1.0f // Top Left
- };
- GLuint indices[] = { // Note that we start from 0!
- 0, 1, 3, // First Triangle
- 1, 2, 3 // Second Triangle
- };*/
- GLfloat vertices[] = {
- -0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
- 0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
- 0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
- 0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
- -0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
- -0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
- -0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
- 0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
- 0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
- 0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
- -0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
- -0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
- -0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
- -0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
- -0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
- -0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
- -0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
- -0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
- 0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
- 0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
- 0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
- 0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
- 0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
- 0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
- -0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
- 0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
- 0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
- 0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
- -0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
- -0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
- -0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
- 0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
- 0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
- 0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
- -0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
- -0.5f, 0.5f, -0.5f, 0.0f, 1.0f
- };
- // World space positions of our cubes
- 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 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);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0);
- glEnableVertexAttribArray(0);
- // glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
- // glEnableVertexAttribArray(1);
- glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
- glEnableVertexAttribArray(2);
- glBindVertexArray(0);
- // Загрузим и создадим текстуру
- GLuint texture1;
- GLuint texture2;
- ////////////////// Texture1
- glGenTextures(1, &texture1);
- glBindTexture(GL_TEXTURE_2D, texture1);
- 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);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- int width, height;
- unsigned char* image = SOIL_load_image("C:\\Users\\Виктория\\Desktop\\проги\\MashGraph\\MashGraph\\wood.jpg", &width, &height, 0, SOIL_LOAD_RGB);
- 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);
- /////////////////////////// Texture2
- glGenTextures(1, &texture2);
- glBindTexture(GL_TEXTURE_2D, texture2);
- 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);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- image = SOIL_load_image("C:\\Users\\Виктория\\Desktop\\проги\\MashGraph\\MashGraph\\200px-Awesome.svg.png", &width, &height, 0, SOIL_LOAD_RGB);
- 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);
- while (!glfwWindowShouldClose(window))
- {
- // Calculate deltatime of current frame
- GLfloat currentFrame = glfwGetTime();
- deltaTime = currentFrame - lastFrame;
- lastFrame = currentFrame;
- glfwPollEvents();
- do_movement();
- glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, texture1);
- glUniform1i(glGetUniformLocation(ourShader.Program, "ourTexture1"), 0);
- glActiveTexture(GL_TEXTURE1);
- glBindTexture(GL_TEXTURE_2D, texture2);
- glUniform1i(glGetUniformLocation(ourShader.Program, "ourTexture2"), 1);
- ourShader.Use();
- // glm::mat4 transform;
- // transform = glm::translate(transform, glm::vec3(0.5f, -0.5f, 0.0f));
- // transform = glm::rotate(transform, (GLfloat)glfwGetTime() * 10.0f, glm::vec3(0.0f, 0.0f, 1.0f));
- // GLuint transformLoc = glGetUniformLocation(ourShader.Program, "transform");
- // glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(transform));
- glm::mat4 view;
- view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
- // Projection
- glm::mat4 projection;
- projection = glm::perspective(fov, (GLfloat)WIDTH / (GLfloat)HEIGHT, 0.1f, 100.0f);
- // Get the uniform locations
- GLint modelLoc = glGetUniformLocation(ourShader.Program, "model");
- GLint viewLoc = glGetUniformLocation(ourShader.Program, "view");
- GLint projLoc = glGetUniformLocation(ourShader.Program, "projection");
- // Pass the matrices to the shader
- glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
- glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
- glBindVertexArray(VAO);
- for (GLuint i = 0; i < 10; i++)
- {
- // Calculate the model matrix for each object and pass it to shader before drawing
- glm::mat4 model;
- model = glm::translate(model, cubePositions[i]);
- GLfloat angle = 20.0f * i;
- model = glm::rotate(model, angle, glm::vec3(1.0f, 0.3f, 0.5f));
- glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
- glDrawArrays(GL_TRIANGLES, 0, 36);
- }
- // glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
- // glDrawArrays(GL_TRIANGLES, 0, 36);
- /////////////// Вторая трансформация
- // transform = glm::mat4(1.0f);
- // transform = glm::translate(transform, glm::vec3(-0.5f, 0.5f, 0.0f));
- // GLfloat scaleAmount = sin(glfwGetTime());
- // transform = glm::scale(transform, glm::vec3(scaleAmount, scaleAmount, scaleAmount));
- // glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(transform));
- // glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
- glBindVertexArray(0);
- glfwSwapBuffers(window);
- }
- glDeleteVertexArrays(1, &VAO);
- glDeleteBuffers(1, &VBO);
- // glDeleteBuffers(1, &EBO);
- glfwTerminate();
- return 0;
- }
- void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
- {
- 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;
- }
- }
- void do_movement()
- {
- // Camera controls
- GLfloat cameraSpeed = 5.0f * deltaTime;
- if (keys[GLFW_KEY_W])
- cameraPos += cameraSpeed * cameraFront;
- if (keys[GLFW_KEY_S])
- cameraPos -= cameraSpeed * cameraFront;
- if (keys[GLFW_KEY_A])
- cameraPos -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
- if (keys[GLFW_KEY_D])
- cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
- }
- 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;
- GLfloat sensitivity = 0.05; // Change this value to your liking
- xoffset *= sensitivity;
- yoffset *= sensitivity;
- yaw += xoffset;
- pitch += yoffset;
- // Make sure that when pitch is out of bounds, screen doesn't get flipped
- if (pitch > 89.0f)
- pitch = 89.0f;
- if (pitch < -89.0f)
- pitch = -89.0f;
- glm::vec3 front;
- front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
- front.y = sin(glm::radians(pitch));
- front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
- cameraFront = glm::normalize(front);
- }
- void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
- {
- if (fov >= 1.0f && fov <= 45.0f)
- fov -= yoffset;
- if (fov <= 1.0f)
- fov = 1.0f;
- if (fov >= 45.0f)
- fov = 45.0f;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement