几何着色器,绘制4个房子

  1 #define GLEW_STATIC
  2 #include <GL/glew.h>
  3 #include <GLFW/glfw3.h>
  4 
  5 #include "Shader.h"
  6 #include <fstream>
  7 #include <iostream>
  8 using namespace std;
  9 
 10 void framebuffer_size_callback(GLFWwindow* window, int width, int height);
 11 void processInput(GLFWwindow *window);
 12 
 13 
 14 // settings
 15 const unsigned int SCR_WIDTH = 800;
 16 const unsigned int SCR_HEIGHT = 600;
 17 
 18 
 19 // timing
 20 float deltaTime = 0.0f;
 21 float lastFrame = 0.0f;
 22 
 23 int main()
 24 {
 25     // glfw: initialize and configure
 26     // ------------------------------
 27     glfwInit();
 28     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
 29     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
 30     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
 31 
 32 #ifdef __APPLE__
 33     glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // uncomment this statement to fix compilation on OS X
 34 #endif
 35 
 36                                                          // glfw window creation
 37                                                          // --------------------
 38     GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
 39     if (window == NULL)
 40     {
 41         std::cout << "Failed to create GLFW window" << std::endl;
 42         glfwTerminate();
 43         return -1;
 44     }
 45     glfwMakeContextCurrent(window);
 46     glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
 47 
 48     
 49     glewExperimental = GL_TRUE;
 50     if (glewInit() != GLEW_OK)
 51     {
 52         cout << "Failed to initialize GLEW!" << endl;
 53         return -1;
 54     }
 55     // configure global opengl state
 56     // -----------------------------
 57 
 58     Shader shader("E:\C++\High_level_GLSL\2.2ver1.txt", "E:\C++\High_level_GLSL\2.2frag1.txt",
 59         "E:\C++\High_level_GLSL\2.2geo1.txt");
 60 
 61     
 62 
 63     float points[] = {
 64         -0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
 65         0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
 66         0.5f, -0.5f, 0.0f, 0.0f, 1.0f,
 67         -0.5f, -0.5f, 1.0f, 1.0f, 0.0f
 68     };
 69 
 70     unsigned int VAO, VBO;
 71     glGenVertexArrays(1, &VAO);
 72     glGenBuffers(1, &VBO);
 73     glBindVertexArray(VAO);
 74     glBindBuffer(GL_ARRAY_BUFFER, VBO);
 75     glBufferData(GL_ARRAY_BUFFER, sizeof(points), &points, GL_STATIC_DRAW);
 76     glEnableVertexAttribArray(0);
 77     glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
 78     glEnableVertexAttribArray(1);
 79     glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(2 * sizeof(float)));
 80     glBindVertexArray(0);
 81 
 82     while (!glfwWindowShouldClose(window))
 83     {
 84         processInput(window);
 85         
 86         glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
 87         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 88 
 89         shader.use();
 90         glBindVertexArray(VAO);
 91         glDrawArrays(GL_POINTS, 0, 4);
 92 
 93         glfwSwapBuffers(window);
 94         glfwPollEvents();
 95     }
 96 
 97     glDeleteVertexArrays(1, &VAO);
 98     glDeleteBuffers(1, &VBO);
 99     
100 }
101 
102     void processInput(GLFWwindow *window)
103     {
104         if (glfwGetKey(window, GLFW_KEY_ENTER) == GLFW_PRESS)
105             glfwSetWindowShouldClose(window, true);
106     }
107 
108     // glfw: whenever the window size changed (by OS or user resize) this callback function executes
109     // ---------------------------------------------------------------------------------------------
110     void framebuffer_size_callback(GLFWwindow* window, int width, int height)
111     {
112         // make sure the viewport matches the new window dimensions; note that width and 
113         // height will be significantly larger than specified on retina displays.
114         glViewport(0, 0, width, height);
115     }
116 
117     

学习网址:https://learnopengl-cn.github.io/04%20Advanced%20OpenGL/09%20Geometry%20Shader/#_4

原文地址:https://www.cnblogs.com/hi3254014978/p/9692981.html