Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "VriesShader.h"
- #include <string>
- #include <fstream>
- #include <sstream>
- #include <iostream>
- #include <GL/glew.h>
- // Constructor generates the shader on the fly
- VriesShaderProgram::VriesShaderProgram(const GLchar* vertexPath, const GLchar* fragmentPath, const GLchar* geometryPath)
- {
- // 0. determine if geometry shader is used.
- this->geometryShaderYes = false;
- if (geometryPath != nullptr)
- {
- this->geometryShaderYes = true;
- }
- // 1. Retrieve the vertex/fragment (and maybe geometry) source code from filePath
- std::string vertexCode;
- std::string fragmentCode;
- std::ifstream vShaderFile;
- std::ifstream fShaderFile;
- std::string geometryCode;
- std::ifstream gShaderFile;
- //ensures ifstream objects can throw exceptions:
- vShaderFile.exceptions(std::ifstream::badbit);
- fShaderFile.exceptions(std::ifstream::badbit);
- if (geometryShaderYes)
- {
- gShaderFile.exceptions(std::ifstream::badbit);
- }
- try
- {
- // Open files
- vShaderFile.open(vertexPath);
- fShaderFile.open(fragmentPath);
- std::stringstream vShaderStream, fShaderStream;
- // Read file's buffer contents into streams
- vShaderStream << vShaderFile.rdbuf();
- fShaderStream << fShaderFile.rdbuf();
- // close file handlers
- vShaderFile.close();
- fShaderFile.close();
- // Convert stream into string
- vertexCode = vShaderStream.str();
- fragmentCode = fShaderStream.str();
- if (this->geometryShaderYes)
- {
- gShaderFile.open(geometryPath);
- std::stringstream gShaderStream;
- gShaderStream << gShaderFile.rdbuf();
- gShaderFile.close();
- geometryCode = gShaderStream.str();
- }
- }
- catch (std::ifstream::failure e)
- {
- std::cout << "ERROR_CODE::3::SHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl;
- }
- const GLchar* vShaderCode = vertexCode.c_str();
- const GLchar* fShaderCode = fragmentCode.c_str();
- const GLchar* gShaderCode;
- if (geometryShaderYes)
- {
- gShaderCode = geometryCode.c_str();
- }
- // 2. Compile shaders
- GLuint vertex, fragment, geometry;
- GLint success;
- GLchar infoLog[512];
- // Vertex Shader
- vertex = glCreateShader(GL_VERTEX_SHADER);
- glShaderSource(vertex, 1, &vShaderCode, NULL);
- glCompileShader(vertex);
- // Print compile errors if any
- glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
- if (!success)
- {
- glGetShaderInfoLog(vertex, 512, NULL, infoLog);
- std::cout << "ERROR_CODE::4::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
- }
- // Fragment Shader
- fragment = glCreateShader(GL_FRAGMENT_SHADER);
- glShaderSource(fragment, 1, &fShaderCode, NULL);
- glCompileShader(fragment);
- // Print compile errors if any
- glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
- if (!success)
- {
- glGetShaderInfoLog(fragment, 512, NULL, infoLog);
- std::cout << "ERROR_CODE::5::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
- }
- if (this->geometryShaderYes)
- {
- // Geometry Shader
- geometry = glCreateShader(GL_GEOMETRY_SHADER);
- glShaderSource(geometry, 1, &gShaderCode, NULL);
- glCompileShader(geometry);
- // Print compile errors if any
- glGetShaderiv(geometry, GL_COMPILE_STATUS, &success);
- if (!success)
- {
- glGetShaderInfoLog(fragment, 512, NULL, infoLog);
- std::cout << "ERROR_CODE::5::SHADER::GEOMETRY::COMPILATION_FAILED\n" << infoLog << std::endl;
- }
- }
- // Shader Program
- this->Program = glCreateProgram();
- glAttachShader(this->Program, vertex);
- glAttachShader(this->Program, fragment);
- if (this->geometryShaderYes)
- {
- glAttachShader(this->Program, geometry);
- }
- glLinkProgram(this->Program);
- // Print linking errors if any
- glGetProgramiv(this->Program, GL_LINK_STATUS, &success);
- if (!success)
- {
- glGetProgramInfoLog(this->Program, 512, NULL, infoLog);
- std::cout << "ERROR_CODE::6::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
- }
- // Delete the shaders as they're linked into our program now and no longer necessery
- glDeleteShader(vertex);
- glDeleteShader(fragment);
- if (this->geometryShaderYes)
- {
- glDeleteShader(geometry);
- }
- }
- // Uses the current shader
- void VriesShaderProgram::Use()
- {
- glUseProgram(this->Program);
- }
Advertisement
Add Comment
Please, Sign In to add comment