OpenGL 读取,使用 .vert .frag 着色器 文件 C++
本文作用:
写一个读取OpenGL里面 .vert .frag 这些文件的读取器。
读取函数代码:
//.frag .vert文件读取 char* readTheFile(string strSource) { //fstream 中判断是否成功打开文件 //https://blog.csdn.net/fruitz/article/details/41624921 std::ifstream myfile(strSource); //打开失败 if (myfile.fail()) { std::cout << "Can not open it " << std::endl; throw new runtime_error("IO stream corrupted"); } //转换成字符串 std::string str((std::istreambuf_iterator<char>(myfile)), std::istreambuf_iterator<char>()); //关闭文件处理器 myfile.close(); //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; }
另一种的着色器 Shader类管理方法(读取方法类似)
https://www.cnblogs.com/--zz/p/9704454.html
完整测试代码:
vertexShaderSource.vert
#version 330 core
layout(location = 0) in vec3 aPos;
OpenGLDemo.cpp
// 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"); class MyTriangle { public: float vertices[9] = { .5f,.5f,.0f, -.5f,.5f,.0f, .0f,-.5f,.0f }; void draw() { //生成缓冲对象 ID:VBO unsigned int VBO; glGenBuffers(1, &VBO); //将缓冲对象 绑定到GL_ARRAY_BUFFER目标 glBindBuffer(GL_ARRAY_BUFFER, VBO); //定义顶点数据复制到缓冲内存 glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); //创建一个顶点着色器对象 unsigned int vertexShader; 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; } } }; int main() { printf("%sA", vertexShaderSource); 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.draw(); while (!glfwWindowShouldClose(window)) { //输入处理 processInput(window); //渲染指令 glClearColor(0.2f,0.3f,0.3f,1.0f); glClear(GL_COLOR_BUFFER_BIT); glfwSwapBuffers(window); glfwPollEvents(); } 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 t(strSource); std::string str((std::istreambuf_iterator<char>(t)), 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; }