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

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


相关文章
|
存储 编解码 算法
凹凸贴图和法线贴图的区别
凹凸贴图适用于一些简单的凹凸效果,而法线贴图则更适合提供更高精度和真实感的凹凸纹理效果
273 1
|
23天前
ThreeJs绘制圆柱体
这篇文章介绍了在Three.js中绘制圆柱体的方法,包括创建圆柱体几何体、设置材质以及将其正确放置在三维场景中的技巧。
30 0
ThreeJs绘制圆柱体
|
5月前
cavans图形绘制(直线、矩形、原型、贝塞尔曲线、渐变背景) 2
cavans图形绘制(直线、矩形、原型、贝塞尔曲线、渐变背景) 2
|
5月前
cavans图形绘制(直线、矩形、原型、贝塞尔曲线、渐变背景) 1
cavans图形绘制(直线、矩形、原型、贝塞尔曲线、渐变背景)
|
6月前
|
算法
[Halcon&几何] 矩形顶点和对角连线角度计算
[Halcon&几何] 矩形顶点和对角连线角度计算
128 0
|
C++ 计算机视觉 Python
C++ VS OpenGL绘制教室三维立体旋转图像
C++ VS OpenGL绘制教室三维立体旋转图像
125 0
C++ VS OpenGL绘制教室三维立体旋转图像
关于已知线段,如何求封闭图形轮廓的一些猜想
关于已知线段,如何求封闭图形轮廓的一些猜想
|
前端开发 数据可视化 图形学
【数学篇】09 # 如何用仿射变换对几何图形进行坐标变换?
【数学篇】09 # 如何用仿射变换对几何图形进行坐标变换?
151 0
【数学篇】09 # 如何用仿射变换对几何图形进行坐标变换?
|
计算机视觉
五、OpenCV绘制线、矩形、圆等基本几何形状
通过javaOpenCV中的Imgproc函数进行简单几何图形的绘制
130 0
五、OpenCV绘制线、矩形、圆等基本几何形状