2022-08-07 12:10:12 +08:00
|
|
|
#ifndef SHADER_H
|
|
|
|
#define SHADER_H
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <GL/glew.h>
|
|
|
|
|
|
|
|
class Shader
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
GLuint Program;
|
|
|
|
// Constructor generates the shader on the fly
|
|
|
|
Shader(const GLchar* vertexPath, const GLchar* fragmentPath)
|
|
|
|
{
|
|
|
|
// 1. Retrieve the vertex/fragment source code from filePath
|
|
|
|
std::string vertexCode;
|
|
|
|
std::string fragmentCode;
|
|
|
|
std::ifstream vShaderFile;
|
|
|
|
std::ifstream fShaderFile;
|
|
|
|
// ensures ifstream objects can throw exceptions:
|
|
|
|
vShaderFile.exceptions(std::ifstream::badbit);
|
|
|
|
fShaderFile.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();
|
|
|
|
}
|
|
|
|
catch (std::ifstream::failure e)
|
|
|
|
{
|
|
|
|
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl;
|
|
|
|
}
|
|
|
|
const GLchar* vShaderCode = vertexCode.c_str();
|
|
|
|
const GLchar * fShaderCode = fragmentCode.c_str();
|
|
|
|
// 2. Compile shaders
|
|
|
|
GLuint vertex, fragment;
|
|
|
|
GLint success;
|
|
|
|
GLchar infoLog[512];
|
|
|
|
// Vertex Shader
|
2023-02-22 13:38:31 +08:00
|
|
|
vertex = glCreateShader(GL_VERTEX_SHADER);
|
|
|
|
glShaderSource(vertex, 1, &vShaderCode, NULL);
|
|
|
|
glCompileShader(vertex);
|
2022-08-07 12:10:12 +08:00
|
|
|
// Print compile errors if any
|
2023-02-22 13:38:31 +08:00
|
|
|
glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
|
2022-08-07 12:10:12 +08:00
|
|
|
if (!success)
|
|
|
|
{
|
|
|
|
glGetShaderInfoLog(vertex, 512, NULL, infoLog);
|
|
|
|
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
|
|
|
|
}
|
|
|
|
// Fragment Shader
|
2023-02-22 13:38:31 +08:00
|
|
|
fragment = glCreateShader(GL_FRAGMENT_SHADER);
|
2022-08-07 12:10:12 +08:00
|
|
|
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::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
|
|
|
|
}
|
|
|
|
// Shader Program
|
2023-02-22 13:38:31 +08:00
|
|
|
this->Program = glCreateProgram();
|
|
|
|
glAttachShader(this->Program, vertex);
|
|
|
|
glAttachShader(this->Program, fragment);
|
|
|
|
glLinkProgram(this->Program);
|
2022-08-07 12:10:12 +08:00
|
|
|
// Print linking errors if any
|
|
|
|
glGetProgramiv(this->Program, GL_LINK_STATUS, &success);
|
|
|
|
if (!success)
|
|
|
|
{
|
|
|
|
glGetProgramInfoLog(this->Program, 512, NULL, infoLog);
|
|
|
|
std::cout << "ERROR::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);
|
|
|
|
|
|
|
|
}
|
|
|
|
// Uses the current shader
|
|
|
|
void Use()
|
|
|
|
{
|
|
|
|
glUseProgram(this->Program);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|