Advertisement
Guest User

Untitled

a guest
Dec 8th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // GLEW нужно подключать до GLFW.
  2. // GLEW
  3. #define GLEW_STATIC
  4. #include <glew.h>
  5. // GLFW
  6. #include <glfw3.h>
  7. #include <iostream>
  8. #include "Shader.h"
  9. #include <SOIL.h>
  10.  
  11.  
  12. void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
  13. {
  14.     // Когда пользователь нажимает ESC, мы устанавливаем свойство WindowShouldClose в true,
  15.     // и приложение после этого закроется
  16.     if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  17.         glfwSetWindowShouldClose(window, GL_TRUE);
  18. }
  19.  
  20. GLuint InitVBO(GLfloat vertices[])
  21. {
  22.     GLuint VBO;
  23.     glGenBuffers(1, &VBO);
  24.     glBindBuffer(GL_ARRAY_BUFFER, VBO);
  25.     glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  26.  
  27.     return VBO;
  28. }
  29.  
  30. int main()
  31. {
  32.     glfwInit();
  33.  
  34.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  35.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  36.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  37.     glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
  38.  
  39.     GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", nullptr, nullptr);
  40.     if (window == nullptr)
  41.     {
  42.         std::cout << "Failed to create GLFW window" << std::endl;
  43.         glfwTerminate();
  44.         return -1;
  45.     }
  46.     glfwMakeContextCurrent(window);
  47.     glfwSetKeyCallback(window, key_callback);
  48.  
  49.     glewExperimental = GL_TRUE;
  50.     if (glewInit() != GLEW_OK)
  51.     {
  52.         std::cout << "Failed to initialize GLEW" << std::endl;
  53.         return -1;
  54.     }
  55.  
  56.     int window_width, window_height;
  57.     glfwGetFramebufferSize(window, &window_width, &window_height);
  58.     glViewport(0, 0, window_width, window_height);
  59.  
  60.     Shader ourShader("vertexShader.txt", "fragmentShader.txt");
  61.  
  62.     GLfloat vertices[] = {
  63.         // Позиции          // Цвета             // Текстурные координаты
  64.          0.5f,  0.5f, 0.0f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f,   // Верхний правый
  65.          0.5f, -0.5f, 0.0f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f,   // Нижний правый
  66.         -0.5f, -0.5f, 0.0f,   0.0f, 0.0f, 1.0f,   0.0f, 0.0f,   // Нижний левый
  67.         -0.5f,  0.5f, 0.0f,   1.0f, 1.0f, 0.0f,   0.0f, 1.0f    // Верхний левый
  68.     };
  69.  
  70.     GLfloat texCoords[] = {
  71.     0.0f, 0.0f,  // Нижний левый угол
  72.     1.0f, 0.0f,  // Нижний правый угол
  73.     0.5f, 1.0f   // Верхняя центральная сторона
  74.     };
  75.  
  76.     GLuint indices[] = {  // Note that we start from 0!
  77.         0, 1, 3, // First Triangle
  78.         1, 2, 3  // Second Triangle
  79.     };
  80.  
  81.     GLuint VBO, VAO, EBO;
  82.     glGenVertexArrays(1, &VAO);
  83.     glGenBuffers(1, &VBO);
  84.     glGenBuffers(1, &EBO);
  85.  
  86.     glBindVertexArray(VAO);
  87.  
  88.     glBindBuffer(GL_ARRAY_BUFFER, VBO);
  89.     glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  90.  
  91.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
  92.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
  93.  
  94.     // Position attribute
  95.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)0);
  96.     glEnableVertexAttribArray(0);
  97.     // Color attribute
  98.     glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
  99.     glEnableVertexAttribArray(1);
  100.     // TexCoord attribute
  101.     glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));
  102.     glEnableVertexAttribArray(2);
  103.  
  104.     glBindVertexArray(0); // Unbind VAO
  105.  
  106.  
  107.     // Container
  108.     int width, height;
  109.     unsigned char* image = SOIL_load_image("container.jpg", &width, &height, 0, SOIL_LOAD_RGB);
  110.  
  111.     GLuint texture1, texture2;
  112.  
  113.     glGenTextures(1, &texture1);
  114.     glBindTexture(GL_TEXTURE_2D, texture1);
  115.  
  116.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
  117.     glGenerateMipmap(GL_TEXTURE_2D);
  118.  
  119.     SOIL_free_image_data(image);
  120.     glBindTexture(GL_TEXTURE_2D, 0);
  121.  
  122.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  123.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  124.  
  125.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  126.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  127.  
  128.  
  129.  
  130.     // Awesomeface
  131.     image = SOIL_load_image("knukles.jpg", &width, &height, 0, SOIL_LOAD_RGB);
  132.  
  133.     glGenTextures(1, &texture2);
  134.     glBindTexture(GL_TEXTURE_2D, texture2);
  135.  
  136.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
  137.     glGenerateMipmap(GL_TEXTURE_2D);
  138.  
  139.     SOIL_free_image_data(image);
  140.     glBindTexture(GL_TEXTURE_2D, 0);
  141.  
  142.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  143.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  144.  
  145.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  146.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  147.  
  148.  
  149.  
  150.     while (!glfwWindowShouldClose(window))
  151.     {
  152.         glfwPollEvents();
  153.  
  154.         glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  155.         glClear(GL_COLOR_BUFFER_BIT);
  156.        
  157.         glActiveTexture(GL_TEXTURE0);
  158.         glBindTexture(GL_TEXTURE_2D, texture1);
  159.         glUniform1i(glGetUniformLocation(ourShader.Program, "ourTexture1"), 0);
  160.  
  161.         glActiveTexture(GL_TEXTURE1);
  162.         glBindTexture(GL_TEXTURE_2D, texture2);
  163.         glUniform1i(glGetUniformLocation(ourShader.Program, "ourTexture2"), 1);
  164.  
  165.         // Activate shader
  166.         ourShader.Use();
  167.  
  168.         // Draw container
  169.         glBindVertexArray(VAO);
  170.         glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
  171.         glBindVertexArray(0);
  172.  
  173.         glfwSwapBuffers(window);
  174.     }
  175.  
  176.     glDeleteVertexArrays(1, &VAO);
  177.     glDeleteBuffers(1, &VBO);
  178.  
  179.     glfwTerminate();
  180.  
  181.     return 0;
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement