Created
May 23, 2020 09:21
-
-
Save alexmercerind/6b15e7eb3872d5a284f25a93fab7c625 to your computer and use it in GitHub Desktop.
Shader Class GLEW OpenGL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <GL/glew.h> | |
#include <fstream> | |
class compile_shader { | |
public: | |
GLuint vertexShaderID; | |
GLuint fragmentShaderID; | |
GLuint shaderProgramID; | |
GLint vertexstate; | |
GLint fragmentstate; | |
GLint programstate; | |
GLchar vertexstatelog[512]; | |
GLchar fragmentstatelog[512]; | |
GLchar programstatelog[512]; | |
compile_shader(std::string vertexshader, std::string fragmentshader) { | |
this->vertexShaderID = glCreateShader(GL_VERTEX_SHADER); | |
std::string vertexSource = read_file(vertexshader); | |
const GLchar* vertexShaderSource = vertexSource.c_str(); | |
glShaderSource(this->vertexShaderID, 1, &vertexShaderSource, nullptr); | |
glCompileShader(this->vertexShaderID); | |
this->fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER); | |
std::string fragmentSource = read_file(fragmentshader); | |
const GLchar* fragmentShaderSource = fragmentSource.c_str(); | |
glShaderSource(this->fragmentShaderID, 1, &fragmentShaderSource, nullptr); | |
glCompileShader(this->fragmentShaderID); | |
glGetShaderiv(this->vertexShaderID, GL_COMPILE_STATUS, &vertexstate); | |
glGetShaderiv(this->fragmentShaderID, GL_COMPILE_STATUS, &fragmentstate); | |
if (this->vertexstate && this->fragmentstate) { | |
glGetShaderInfoLog(this->vertexShaderID, 512, NULL, this->vertexstatelog); | |
glGetShaderInfoLog(this->fragmentShaderID, 512, NULL, this->fragmentstatelog); | |
std::cout << "Vertex and fragment shaders are successfully compiled." << std::endl << vertexstatelog << std::endl << fragmentstatelog << std::endl; | |
}; | |
this->create_shader(); | |
}; | |
void create_shader() { | |
this->shaderProgramID = glCreateProgram(); | |
glAttachShader(this->shaderProgramID, this->vertexShaderID); | |
glAttachShader(this->shaderProgramID, this->fragmentShaderID); | |
glLinkProgram(this->shaderProgramID); | |
glGetProgramiv(this->shaderProgramID, GL_LINK_STATUS, &this->programstate); | |
if (this->programstate) { | |
glGetProgramInfoLog(this->shaderProgramID, 512, NULL, this->programstatelog); | |
std::cout << "Shader program is successfully linked." << std::endl << programstatelog << std::endl; | |
}; | |
}; | |
std::string read_file(std::string filename) { | |
using namespace std; | |
string output; | |
string buffer; | |
ifstream file(filename); | |
while (getline(file, buffer)) { | |
output.append(buffer + "\n"); | |
}; | |
file.close(); | |
return output; | |
}; | |
void use_shader() { | |
glUseProgram(this->shaderProgramID); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment