shader.hpp: add geometry shader support
parent
bff9e6ad4f
commit
ea036f403c
|
@ -19,31 +19,38 @@ public:
|
||||||
unsigned int ID;
|
unsigned int ID;
|
||||||
// constructor generates the shader on the fly
|
// constructor generates the shader on the fly
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
Shader(const char *vertexPath, const char *fragmentPath)
|
Shader(const char* geometryPath, const char *vertexPath, const char *fragmentPath)
|
||||||
{
|
{
|
||||||
// 1. retrieve the vertex/fragment source code from filePath
|
// 1. retrieve the vertex/fragment source code from filePath
|
||||||
std::string vertexCode;
|
std::string vertexCode;
|
||||||
std::string fragmentCode;
|
std::string fragmentCode;
|
||||||
|
std::string geometryCode;
|
||||||
std::ifstream vShaderFile;
|
std::ifstream vShaderFile;
|
||||||
std::ifstream fShaderFile;
|
std::ifstream fShaderFile;
|
||||||
|
std::ifstream gShaderFile;
|
||||||
// ensure ifstream objects can throw exceptions:
|
// ensure ifstream objects can throw exceptions:
|
||||||
vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||||
fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||||
|
gShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// open files
|
// open files
|
||||||
vShaderFile.open(vertexPath);
|
vShaderFile.open(vertexPath);
|
||||||
fShaderFile.open(fragmentPath);
|
fShaderFile.open(fragmentPath);
|
||||||
std::stringstream vShaderStream, fShaderStream;
|
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)
|
||||||
{
|
{
|
||||||
|
@ -51,8 +58,9 @@ public:
|
||||||
}
|
}
|
||||||
const char *vShaderCode = vertexCode.c_str();
|
const char *vShaderCode = vertexCode.c_str();
|
||||||
const char *fShaderCode = fragmentCode.c_str();
|
const char *fShaderCode = fragmentCode.c_str();
|
||||||
|
const char *gShaderCode = geometryCode.c_str();
|
||||||
// 2. compile shaders
|
// 2. compile shaders
|
||||||
unsigned int vertex, fragment;
|
unsigned int vertex, fragment, geometry;
|
||||||
// vertex shader
|
// vertex shader
|
||||||
vertex = glCreateShader(GL_VERTEX_SHADER);
|
vertex = glCreateShader(GL_VERTEX_SHADER);
|
||||||
glShaderSource(vertex, 1, &vShaderCode, NULL);
|
glShaderSource(vertex, 1, &vShaderCode, NULL);
|
||||||
|
@ -63,15 +71,23 @@ public:
|
||||||
glShaderSource(fragment, 1, &fShaderCode, NULL);
|
glShaderSource(fragment, 1, &fShaderCode, NULL);
|
||||||
glCompileShader(fragment);
|
glCompileShader(fragment);
|
||||||
checkCompileErrors(fragment, "FRAGMENT");
|
checkCompileErrors(fragment, "FRAGMENT");
|
||||||
|
// geometry shader
|
||||||
|
geometry = glCreateShader(GL_GEOMETRY_SHADER);
|
||||||
|
glShaderSource(geometry, 1, &gShaderCode, NULL);
|
||||||
|
glCompileShader(geometry);
|
||||||
|
checkCompileErrors(geometry, "GEOMETRY");
|
||||||
|
|
||||||
// shader Program
|
// shader Program
|
||||||
ID = glCreateProgram();
|
ID = glCreateProgram();
|
||||||
glAttachShader(ID, vertex);
|
glAttachShader(ID, vertex);
|
||||||
glAttachShader(ID, fragment);
|
glAttachShader(ID, fragment);
|
||||||
|
glAttachShader(ID, geometry);
|
||||||
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);
|
||||||
}
|
}
|
||||||
// activate the shader
|
// activate the shader
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
|
|
Loading…
Reference in New Issue