shader: make geometry shader not mandatory

hud
EmaMaker 2023-09-17 10:40:21 +02:00
parent 41275486a6
commit 6670f3b41c
1 changed files with 30 additions and 20 deletions

View File

@ -34,60 +34,70 @@ public:
gShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit); gShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try try
{ {
std::stringstream vShaderStream, fShaderStream, gShaderStream;
if(geometryPath){
gShaderFile.open(geometryPath);
gShaderStream << gShaderFile.rdbuf();
gShaderFile.close();
geometryCode = gShaderStream.str();
}
// open files // open files
vShaderFile.open(vertexPath); vShaderFile.open(vertexPath);
fShaderFile.open(fragmentPath); fShaderFile.open(fragmentPath);
gShaderFile.open(geometryPath);
std::stringstream vShaderStream, fShaderStream, gShaderStream;
// read file's buffer contents into streams // read file's buffer contents into streams
vShaderStream << vShaderFile.rdbuf(); vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf(); fShaderStream << fShaderFile.rdbuf();
gShaderStream << gShaderFile.rdbuf();
// close file handlers // close file handlers
vShaderFile.close(); vShaderFile.close();
fShaderFile.close(); fShaderFile.close();
gShaderFile.close();
// convert stream into string // convert stream into string
vertexCode = vShaderStream.str(); vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str(); fragmentCode = fShaderStream.str();
geometryCode = gShaderStream.str();
} }
catch (std::ifstream::failure &e) catch (std::ifstream::failure &e)
{ {
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ: " << e.what() << std::endl; std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ: " << e.what() << std::endl;
} }
const char *vShaderCode = vertexCode.c_str(); // shader Program
const char *fShaderCode = fragmentCode.c_str(); ID = glCreateProgram();
const char *gShaderCode = geometryCode.c_str();
// 2. compile shaders
unsigned int vertex, fragment, geometry; unsigned int vertex, fragment, geometry;
// vertex shader
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vShaderCode, NULL);
glCompileShader(vertex);
checkCompileErrors(vertex, "VERTEX");
// fragment Shader
fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &fShaderCode, NULL);
glCompileShader(fragment);
checkCompileErrors(fragment, "FRAGMENT");
// geometry shader // geometry shader
if(geometryPath){
const char *gShaderCode = geometryCode.c_str();
geometry = glCreateShader(GL_GEOMETRY_SHADER); geometry = glCreateShader(GL_GEOMETRY_SHADER);
glShaderSource(geometry, 1, &gShaderCode, NULL); glShaderSource(geometry, 1, &gShaderCode, NULL);
glCompileShader(geometry); glCompileShader(geometry);
checkCompileErrors(geometry, "GEOMETRY"); checkCompileErrors(geometry, "GEOMETRY");
// shader Program
ID = glCreateProgram();
glAttachShader(ID, vertex);
glAttachShader(ID, fragment);
glAttachShader(ID, geometry); glAttachShader(ID, geometry);
}
// vertex shader
const char *vShaderCode = vertexCode.c_str();
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vShaderCode, NULL);
glCompileShader(vertex);
checkCompileErrors(vertex, "VERTEX");
glAttachShader(ID, vertex);
// fragment Shader
const char *fShaderCode = fragmentCode.c_str();
fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &fShaderCode, NULL);
glCompileShader(fragment);
checkCompileErrors(fragment, "FRAGMENT");
glAttachShader(ID, fragment);
// Constructu the program
glLinkProgram(ID); glLinkProgram(ID);
checkCompileErrors(ID, "PROGRAM"); checkCompileErrors(ID, "PROGRAM");
// delete the shaders as they're linked into our program now and no longer necessary // delete the shaders as they're linked into our program now and no longer necessary
glDeleteShader(vertex); glDeleteShader(vertex);
glDeleteShader(fragment); glDeleteShader(fragment);
glDeleteShader(geometry); if(geometryPath) glDeleteShader(geometry);
} }
// activate the shader // activate the shader
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------