绘制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.1ver1.txt", "E:\\C++\\High_level_GLSL\\2.1frag1.txt",
 59         "E:\\C++\\High_level_GLSL\\2.1geo1.txt");
 60 
 61     float points[] = {
 62         -0.5f, 0.5f,
 63         0.5f, 0.5f,
 64         0.5f, -0.5f,
 65         -0.5f, -0.5f
 66     };
 67 
 68     /*float points[] = {
 69         -0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
 70         0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
 71         0.5f, -0.5f, 0.0f, 0.0f, 1.0f,
 72         -0.5f, -0.5f, 1.0f, 1.0f, 0.0f
 73     };*/
 74 
 75     unsigned int VAO, VBO;
 76     glGenVertexArrays(1, &VAO);
 77     glGenBuffers(1, &VBO);
 78     glBindVertexArray(VAO);
 79     glBindBuffer(GL_ARRAY_BUFFER, VBO);
 80     glBufferData(GL_ARRAY_BUFFER, sizeof(points), &points, GL_STATIC_DRAW);
 81     glEnableVertexAttribArray(0);
 82     glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
 83     //glEnableVertexAttribArray(1);
 84     //glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(2 * sizeof(float)));
 85     glBindVertexArray(0);
 86 
 87     while (!glfwWindowShouldClose(window))
 88     {
 89         processInput(window);
 90         
 91         glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
 92         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 93 
 94         shader.use();
 95         glBindVertexArray(VAO);
 96         glDrawArrays(GL_POINTS, 0, 4);
 97 
 98         glfwSwapBuffers(window);
 99         glfwPollEvents();
100     }
101 
102     glDeleteVertexArrays(1, &VAO);
103     glDeleteBuffers(1, &VBO);
104     
105 }
106 
107     void processInput(GLFWwindow *window)
108     {
109         if (glfwGetKey(window, GLFW_KEY_ENTER) == GLFW_PRESS)
110             glfwSetWindowShouldClose(window, true);
111     }
112 
113     // glfw: whenever the window size changed (by OS or user resize) this callback function executes
114     // ---------------------------------------------------------------------------------------------
115     void framebuffer_size_callback(GLFWwindow* window, int width, int height)
116     {
117         // make sure the viewport matches the new window dimensions; note that width and 
118         // height will be significantly larger than specified on retina displays.
119         glViewport(0, 0, width, height);
120     }
121 
122     
E:\\C++\\High_level_GLSL\\2.1ver1.txt
1 #version 330 core
2 layout (location = 0) in vec2 aPos;
3 
4 void main()
5 {
6     gl_Position = vec4(aPos, 0.0f, 1.0f);
7 }
E:\\C++\\High_level_GLSL\\2.1frag1.txt
1 #version 330 core
2 out vec4 FragColor;
3 void main()
4 {
5     FragColor = vec4(0.0f, 1.0f, 0.0f, 1.0f);
6 }
E:\\C++\\High_level_GLSL\\2.1geo1.txt
 1 #version 330 core
 2 layout (points) in;
 3 layout (line_strip, max_vertices = 2) out;
 4 
 5 void main()
 6 {
 7     gl_Position = gl_in[0].gl_Position + vec4(-0.1f, 0.0f, 0.0f, 0.0f);
 8     EmitVertex();
 9     gl_Position = gl_in[0].gl_Position + vec4(0.1f, 0.0f, 0.0f, 0.0f);
10     EmitVertex();
11     
12     EndPrimitive();
13 }

Shader.h头文件,只需将链接里的#include <glad/glad.h>换成  

#define GLEW_STATIC
#include <GL/glew.h>

就行