opengl 几何着色器

简介: opengl 几何着色器
  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>

就行

相关文章
|
6月前
|
XML 小程序 Java
【Android App】三维投影OpenGL ES的讲解及着色器实现(附源码和演示 超详细)
【Android App】三维投影OpenGL ES的讲解及着色器实现(附源码和演示 超详细)
126 0
|
数据可视化
QT+OpenGL几何着色器
输入布局限定符可以从顶点着色器接收下列任何一个图元值: ● points:绘制GL_POINTS图元时 ● lines:绘制GL_LINES或GL_LINE_STRIP时 ● lines_adjacency:GL_ADJACENCY或GL_LINESTRIP_ADJACENCY ● triangles:GL_TRIANGLES、GL_TRIANGLE_STRIP或GL_TRIANGLE_FAN ● triangles_adjacency:GL_TRIANGLES_ADJACENCY或GL_TRIANGLE_STRIP_ADJACENCY
118 0
|
缓存 索引
OpenGL学习笔记(十三):将纹理贴图应用到四边形上,对VAO/VBO/EBO/纹理/着色器的使用方式进行总结
OpenGL学习笔记(十三):将纹理贴图应用到四边形上,对VAO/VBO/EBO/纹理/着色器的使用方式进行总结
OpenGL学习笔记(十三):将纹理贴图应用到四边形上,对VAO/VBO/EBO/纹理/着色器的使用方式进行总结
|
编译器 C语言 C++
OpenGL学习笔记(十一):封装自己的着色器类
OpenGL学习笔记(十一):封装自己的着色器类
OpenGL学习笔记(十一):封装自己的着色器类
|
存储 缓存 API
三、OpenGL ES GLSL语言 & 自定义着色器常用API
OpenGL ES GLSL语言 & 自定义着色器常用API
314 0
三、OpenGL ES GLSL语言 & 自定义着色器常用API
|
存储
了解OpenGL中的投影方式、存储着色器分类、常见图元连接方式
了解OpenGL中的投影方式、存储着色器分类、常见图元连接方式
240 0
了解OpenGL中的投影方式、存储着色器分类、常见图元连接方式
|
存储 芯片 异构计算
|
编译器
OpenGL ES着色器代码语法
因为着色器里面没有编译器提示, 所以熟悉着色器代码语法是非常有必要的
217 0
|
缓存 C++
Opengl ES之FBO
Opengl ES连载系列
153 0