妈耶...终于扛过来了.
// OpenGLDemo.cpp: 定义控制台应用程序的入口点。 #include "stdafx.h" #include <glad/glad.h> #include <GLFW/glfw3.h> #include <iostream> #include <stdio.h> #include <stdlib.h> #include <fstream> #include <string> #include <cstring> using namespace std; void framebuffer_size_callback(GLFWwindow* window, int width, int height); void processInput(GLFWwindow* window); char* readTheFile(string strSource); const char* vertexShaderSource = readTheFile("vertexShaderSource.vert"); const char* fragmentShaderSource = readTheFile("fragmentShaderSource.frag"); class MyTriangle { public: float vertices[18] = { .5f,.5f,.0f,//右上 -.5f,.5f,.0f,//左上 .5f,-.5f,.0f,//右下 -.5f,-.5f,.0f//左下 }; unsigned int indices[6] = { 0,2,1, 1,3,2 }; //生成顶点缓冲对象 ID:VBO unsigned int VBO; //生成顶点数组对象 ID:VAO unsigned int VAO; //生成引索对象 unsigned int EBO; //储存 顶点着色器 unsigned int vertexShader; //储存 片段着色器 unsigned int fragmentShader; //存储 着色器程序 unsigned int shaderProgram; void drawMyGraph(){ vertexShaderInit(); FragmentShaderInit(); shaderProgramLinker(); vertexInput(); } private: void vertexInput() { glGenBuffers(1, &VBO); glGenVertexArrays(1, &VAO); glGenBuffers(1, &EBO); // 1. 绑定VAO , VBO glBindVertexArray(VAO); // 2. 复制顶点数组到缓冲中供OpenGL使用 //将缓冲对象 绑定到GL_ARRAY_BUFFER目标 glBindBuffer(GL_ARRAY_BUFFER, VBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); //定义顶点数据复制到缓冲内存 glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices,GL_STATIC_DRAW); // 3. 设置顶点属性指针 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); } void vertexShaderInit() { //创建一个顶点着色器对象 vertexShader = glCreateShader(GL_VERTEX_SHADER); //着色器源码附着到着色器对象上 glShaderSource(vertexShader, 1, &vertexShaderSource, NULL); //编译着色器对象 glCompileShader(vertexShader); //检测着色编译是否成功 int success; char infoLog[22]; glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success); if (!success) { glGetShaderInfoLog(vertexShader, 512, NULL, infoLog); std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl; } } void FragmentShaderInit() { fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragmentShader, 1, &fragmentShaderSource,NULL); glCompileShader(fragmentShader); //检测着色编译是否成功 int success; char infoLog[22]; glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success); if (!success) { glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog); std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl; } } void shaderProgramLinker() { //创建着色器程序对象 shaderProgram = glCreateProgram(); //附加着色器到着色器程序 glAttachShader(shaderProgram, vertexShader); glAttachShader(shaderProgram, fragmentShader); glLinkProgram(shaderProgram); int success; char infoLog[22]; glGetProgramiv(shaderProgram, GL_LINK_STATUS,&success); if (!success) { glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog); std::cout << "ERROR::SHADER::LINKE_PROGRAM::COMPILATION_FAILED\n" << infoLog << std::endl; } glDeleteShader(vertexShader); glDeleteShader(fragmentShader); } }; int main() { glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,3); glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE); GLFWwindow* window = glfwCreateWindow(800, 600, "Oh!I see you!", NULL, NULL); if (window == NULL) { std::cout << "Failed to create the windows" << std::endl; glfwTerminate(); return -1; } glfwMakeContextCurrent(window); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { std::cout << "Failed to initialize GLAD" << std::endl; return -1; } MyTriangle myTriangle; myTriangle.drawMyGraph(); //启用线段模式 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); while (!glfwWindowShouldClose(window)) { //输入处理 processInput(window); //渲染指令 glClearColor(0.2f,0.3f,0.3f,1.0f); glClear(GL_COLOR_BUFFER_BIT); // 4. 当我们渲染一个物体时要使用着色器程序 glUseProgram(myTriangle.shaderProgram); glBindVertexArray(myTriangle.VAO); // 3. 绘制物体 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); glBindVertexArray(0); glfwSwapBuffers(window); glfwPollEvents(); } glDeleteVertexArrays(1, &myTriangle.VAO); glDeleteBuffers(1, &myTriangle.VBO); glfwTerminate(); return 0; } void framebuffer_size_callback(GLFWwindow* windows, int width, int height) { glViewport(0, 0, width, height); } void processInput(GLFWwindow* window) { if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) { glfwSetWindowShouldClose(window, true); } } //.frag .vert文件读取 char* readTheFile(string strSource) { std::ifstream myfile(strSource); std::string str((std::istreambuf_iterator<char>(myfile)), std::istreambuf_iterator<char>()); //str数组长度一定要 +1, /*原因: https://blog.csdn.net/ShiQW5696/article/details/80676290 */ int len = str.length(); char* result = new char[len]; strcpy_s(result, len + 1, str.c_str()); return result; }
原文地址:https://www.cnblogs.com/--zz/p/9697161.html
时间: 2024-10-08 15:28:16