2020-02-12 00:29:35 -08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
//DGL
|
|
|
|
#include "DGL_FundamentalTypes.hpp"
|
|
|
|
#include "DGL_MiscTypes.hpp"
|
|
|
|
#include "DGL_Enum.hpp"
|
2020-02-13 22:13:16 -08:00
|
|
|
#include "DGL_Space.hpp"
|
2020-02-12 00:29:35 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
2020-02-14 23:21:27 -08:00
|
|
|
namespace DGL
|
2020-02-12 00:29:35 -08:00
|
|
|
{
|
|
|
|
// Some Raw Source Defaults:
|
|
|
|
|
|
|
|
RawString<const char> RawVertextShaderSource =
|
|
|
|
"#version 330 core\n"
|
|
|
|
|
|
|
|
"layout (location = 0) in vec3 aPos;\n"
|
|
|
|
|
|
|
|
"void main()\n"
|
|
|
|
"{\n"
|
|
|
|
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
|
|
|
|
"}\0";
|
|
|
|
|
|
|
|
RawString<const char> RawFragmentShaderSource =
|
|
|
|
"#version 400\n"
|
|
|
|
|
|
|
|
"out vec4 frag_colour;"
|
|
|
|
|
|
|
|
"void main() {"
|
|
|
|
|
|
|
|
" frag_colour = vec4(0.5, 0.0, 0.5, 1.0);"
|
|
|
|
|
|
|
|
"}";
|
|
|
|
|
|
|
|
|
2020-02-13 22:13:16 -08:00
|
|
|
// Default Shaders
|
|
|
|
|
|
|
|
ID<ShaderProgram> RawShader ,
|
|
|
|
SimpleShader ,
|
|
|
|
SimpleShader_Transformed ;
|
|
|
|
|
|
|
|
ID<CoordSpace> ScreenSpaceVarID;
|
|
|
|
|
|
|
|
|
|
|
|
// Forward Declarations
|
|
|
|
|
2020-02-14 23:21:27 -08:00
|
|
|
sfn GetShaderInfo ( ID<Shader > _shader , gSize _logLength , ptr<gSize> _infoLengthRef , RawString<gChar> _infoLogRef) -> void;
|
|
|
|
sfn QueryShader ( ID<Shader > _shaderToQuery , EShaderInfo _shaderInfoDesired, ptr<gInt > _requestedInfoObject ) -> void;
|
|
|
|
sfn MakeShader (Ref(ID<Shader >) _shaderIDHolder , EShaderType _typeOfShader , uInt64 _numberOfStringElements, ptr<RawString<const gChar>> _sourceCode, ptr<const gInt> _lengthsOfStrings) -> void;
|
|
|
|
sfn MakeShaderProgram(Ref(ID<ShaderProgram>) _shaderProgramIDHolder, const ID<Shader> _vertexShader, const ID<Shader> _fragShader) -> void;
|
2020-02-13 22:13:16 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
2020-02-12 00:29:35 -08:00
|
|
|
// Functions
|
|
|
|
|
2020-02-13 22:13:16 -08:00
|
|
|
sfn ActiveUniforms(ID<Shader> _shaderToQueryForUniforms) -> GLint
|
|
|
|
{
|
|
|
|
GLint uniforms;
|
|
|
|
|
|
|
|
glGetProgramiv(_shaderToQueryForUniforms, GL_ACTIVE_UNIFORMS, &uniforms);
|
|
|
|
|
|
|
|
for (int i = 0; i < uniforms; i++)
|
|
|
|
{
|
|
|
|
int name_len = -1, num = -1;
|
|
|
|
|
|
|
|
GLenum type = GL_ZERO;
|
|
|
|
|
|
|
|
char name[100];
|
|
|
|
|
|
|
|
glGetActiveUniform(_shaderToQueryForUniforms, GLuint(i), sizeof(name) - 1, &name_len, &num, &type, name);
|
|
|
|
|
|
|
|
name[name_len] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return uniforms;
|
|
|
|
}
|
|
|
|
|
2020-02-12 00:29:35 -08:00
|
|
|
sfn AttachShader(ID<ShaderProgram> _shaderProgram, ID<Shader> _shaderToAttach)
|
|
|
|
{
|
|
|
|
glAttachShader(_shaderProgram, _shaderToAttach);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sfn BindShaderSource(ID<Shader> _shader, uInt64 _numberOfStringElements, ptr< RawString<const gChar>> _sourceCode, ptr<const gInt> _lengthsOfStrings)
|
|
|
|
{
|
|
|
|
glShaderSource(_shader, _numberOfStringElements, _sourceCode, _lengthsOfStrings);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-13 22:13:16 -08:00
|
|
|
sfn CompileShader(const ID<Shader> _shaderToCompile)
|
2020-02-12 00:29:35 -08:00
|
|
|
{
|
|
|
|
glCompileShader(_shaderToCompile);
|
|
|
|
|
2020-02-13 22:13:16 -08:00
|
|
|
gInt Result = gInt(EBool::False);
|
|
|
|
|
|
|
|
gSize InfoLogLength;
|
|
|
|
|
|
|
|
QueryShader(_shaderToCompile, EShaderInfo::CompileStatus, Address(Result));
|
|
|
|
|
|
|
|
if (!Result)
|
|
|
|
{
|
|
|
|
QueryShader(_shaderToCompile, EShaderInfo::InfoLogLength, Address(InfoLogLength));
|
|
|
|
|
|
|
|
if (InfoLogLength > 0)
|
|
|
|
{
|
|
|
|
std::vector<char> ErrorMessage(InfoLogLength + 1);
|
|
|
|
|
|
|
|
GetShaderInfo(_shaderToCompile, InfoLogLength, nullptr, Address(ErrorMessage.at(0)));
|
|
|
|
|
|
|
|
throw std::runtime_error(Address(ErrorMessage.at(0)));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Shader compilation failed and did not get a proper info log.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-12 00:29:35 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sfn CreateShader(EShaderType _typeOfShader) -> ID<Shader>
|
|
|
|
{
|
|
|
|
return glCreateShader(GLenum(_typeOfShader));
|
|
|
|
}
|
|
|
|
|
|
|
|
sfn CreateShaderProgram() -> ID<ShaderProgram>
|
|
|
|
{
|
|
|
|
return glCreateProgram();
|
|
|
|
}
|
|
|
|
|
|
|
|
sfn DeleteShader(ID<Shader> _shaderToDelete)
|
|
|
|
{
|
|
|
|
glDeleteShader(_shaderToDelete);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2020-02-13 22:13:16 -08:00
|
|
|
|
|
|
|
sfn DetachShader(ID<ShaderProgram> _shaderProgram, ID<Shader> _shaderToDetach)
|
|
|
|
{
|
|
|
|
glDetachShader(_shaderProgram, _shaderToDetach);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sfn GetShaderInfo(ID<Shader> _shader, gSize _logLength, ptr<gSize> _infoLengthRef, RawString<gChar> _infoLogRef) -> void
|
|
|
|
{
|
|
|
|
glGetShaderInfoLog(_shader, _logLength, _infoLengthRef, _infoLogRef);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sfn GetShaderProgramInfo(ID<ShaderProgram> _shaderProgram, gSize _logLength, ptr<gSize> _infoLengthRef, RawString<gChar> _infoLogRef) -> void
|
|
|
|
{
|
|
|
|
glGetProgramInfoLog(_shaderProgram, _logLength, _infoLengthRef, _infoLogRef);
|
2020-02-14 23:21:27 -08:00
|
|
|
|
|
|
|
return;
|
2020-02-13 22:13:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
sfn GetUniformVariable(const ID<ShaderProgram> _programID, RawString<const char> _nameOfVariable) -> ID<Matrix>
|
|
|
|
{
|
|
|
|
return glGetUniformLocation(_programID, _nameOfVariable);
|
|
|
|
}
|
|
|
|
|
2020-02-12 00:29:35 -08:00
|
|
|
sfn LinkProgramShader(ID<ShaderProgram> _shaderProgramToLink)
|
|
|
|
{
|
|
|
|
glLinkProgram(_shaderProgramToLink);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-13 22:13:16 -08:00
|
|
|
sfn QueryShader(ID<Shader> _shaderToQuery, EShaderInfo _shaderInfoDesired, ptr<gInt> _requestedInfoObject) -> void
|
2020-02-12 00:29:35 -08:00
|
|
|
{
|
2020-02-13 22:13:16 -08:00
|
|
|
glGetShaderiv(_shaderToQuery, GLenum(_shaderInfoDesired), _requestedInfoObject);
|
2020-02-14 23:21:27 -08:00
|
|
|
|
|
|
|
return;
|
2020-02-13 22:13:16 -08:00
|
|
|
}
|
2020-02-12 00:29:35 -08:00
|
|
|
|
2020-02-13 22:13:16 -08:00
|
|
|
sfn QueryShaderProgram(ID<ShaderProgram> _shaderToQuery, EShaderProgramInfo _shaderProgramInfoDesired, ptr<gInt> _requestedInfoObject) -> void
|
|
|
|
{
|
|
|
|
glGetProgramiv(_shaderToQuery, GLenum(_shaderProgramInfoDesired), _requestedInfoObject);
|
2020-02-14 23:21:27 -08:00
|
|
|
|
|
|
|
return;
|
2020-02-13 22:13:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
sfn LoadShaders(RawString<const char> _vertexShaderFilePath, RawString<const char> _fragmentShaderFilePath) -> ID<ShaderProgram>
|
|
|
|
{
|
|
|
|
using std::cout ;
|
|
|
|
using std::endl ;
|
|
|
|
using std::ifstream ;
|
|
|
|
using std::ios ;
|
|
|
|
using std::string ;
|
|
|
|
using std::stringstream;
|
|
|
|
using std::vector ;
|
|
|
|
|
|
|
|
|
|
|
|
string vertexShaderCode ;
|
|
|
|
string fragmentShaderCode ;
|
|
|
|
|
|
|
|
ifstream vertexShaderFileStream ;
|
|
|
|
ifstream fragmentShaderFileStream;
|
|
|
|
|
|
|
|
vertexShaderFileStream .open(_vertexShaderFilePath);
|
|
|
|
fragmentShaderFileStream.open(_fragmentShaderFilePath);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (vertexShaderFileStream.is_open() and fragmentShaderFileStream.is_open())
|
|
|
|
{
|
|
|
|
stringstream vertSourceStrStream;
|
|
|
|
stringstream fragSourceStrStream;
|
|
|
|
|
|
|
|
vertSourceStrStream << vertexShaderFileStream .rdbuf();
|
|
|
|
fragSourceStrStream << fragmentShaderFileStream.rdbuf();
|
|
|
|
|
|
|
|
vertexShaderFileStream .close();
|
|
|
|
fragmentShaderFileStream.close();
|
|
|
|
|
|
|
|
vertexShaderCode = vertSourceStrStream.str();
|
|
|
|
fragmentShaderCode = fragSourceStrStream.str();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Impossible to open% s.Are you in the right directory ? Don't forget to read the FAQ !");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RawString<const char> vertexSourcePtr = vertexShaderCode .c_str();
|
|
|
|
RawString<const char> fragmentSourcePtr = fragmentShaderCode.c_str();
|
|
|
|
|
|
|
|
cout << "Compiling shader: " << _vertexShaderFilePath << endl;
|
|
|
|
|
|
|
|
ID<Shader> vertexShader = CreateShader(EShaderType::Vertex);
|
|
|
|
|
|
|
|
BindShaderSource(vertexShader, 1, Address(vertexSourcePtr), NULL);
|
|
|
|
CompileShader (vertexShader );
|
|
|
|
|
|
|
|
cout << "Compiling shader: " << _fragmentShaderFilePath << endl;
|
|
|
|
|
|
|
|
ID<Shader> fragmentShader = CreateShader(EShaderType::Fragment);
|
|
|
|
|
|
|
|
BindShaderSource(fragmentShader, 1, Address(fragmentSourcePtr), NULL);
|
|
|
|
CompileShader (fragmentShader );
|
|
|
|
|
|
|
|
|
|
|
|
cout << "Making Shader Program and Linking..." << endl;
|
|
|
|
|
|
|
|
ID<ShaderProgram> generatedProgram;
|
|
|
|
|
|
|
|
MakeShaderProgram(generatedProgram, vertexShader, fragmentShader);
|
|
|
|
|
|
|
|
DeleteShader(vertexShader );
|
|
|
|
DeleteShader(fragmentShader);
|
|
|
|
|
|
|
|
return generatedProgram;
|
|
|
|
}
|
|
|
|
catch (const std::runtime_error _error)
|
|
|
|
{
|
|
|
|
ErrorRuntime(_error);
|
|
|
|
|
|
|
|
Exit(ExitCode::Failed);
|
|
|
|
}
|
2020-02-12 00:29:35 -08:00
|
|
|
}
|
|
|
|
|
2020-02-13 22:13:16 -08:00
|
|
|
sfn LoadRawShader()
|
|
|
|
{
|
|
|
|
ID<Shader> VertexShader ;
|
|
|
|
ID<Shader> FragmentShader;
|
2020-02-12 00:29:35 -08:00
|
|
|
|
2020-02-14 23:21:27 -08:00
|
|
|
MakeShader(VertexShader , EShaderType::Vertex , 1, Address(DGL::RawVertextShaderSource ), NULL);
|
|
|
|
MakeShader(FragmentShader, EShaderType::Fragment, 1, Address(DGL::RawFragmentShaderSource), NULL);
|
|
|
|
|
|
|
|
DGL::MakeShaderProgram(RawShader, VertexShader, FragmentShader);
|
2020-02-13 22:13:16 -08:00
|
|
|
|
2020-02-14 23:21:27 -08:00
|
|
|
DGL::DeleteShader(VertexShader );
|
|
|
|
DGL::DeleteShader(FragmentShader);
|
2020-02-13 22:13:16 -08:00
|
|
|
|
2020-02-14 23:21:27 -08:00
|
|
|
return;
|
2020-02-13 22:13:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
sfn LoadSimpleShader()
|
|
|
|
{
|
|
|
|
SimpleShader = LoadShaders("SimpleVertexShader.vert", "SimpleFragmentShader.frag");
|
2020-02-14 23:21:27 -08:00
|
|
|
|
|
|
|
return;
|
2020-02-13 22:13:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
sfn LoadSimpleShader_Transformed()
|
|
|
|
{
|
|
|
|
SimpleShader_Transformed = LoadShaders("SimpleTransform.vert", "SingleColor.frag");
|
|
|
|
|
2020-02-14 23:21:27 -08:00
|
|
|
ScreenSpaceVarID = DGL::GetUniformVariable(DGL::SimpleShader_Transformed, "modelViewProjection");
|
|
|
|
|
|
|
|
return;
|
2020-02-13 22:13:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
sfn LoadDefaultShaders()
|
|
|
|
{
|
|
|
|
LoadRawShader ();
|
|
|
|
LoadSimpleShader ();
|
|
|
|
LoadSimpleShader_Transformed();
|
2020-02-14 23:21:27 -08:00
|
|
|
|
|
|
|
return;
|
2020-02-13 22:13:16 -08:00
|
|
|
}
|
2020-02-12 00:29:35 -08:00
|
|
|
|
|
|
|
sfn MakeShader
|
|
|
|
(
|
|
|
|
Ref(ID<Shader>) _shaderIDHolder ,
|
|
|
|
EShaderType _typeOfShader ,
|
|
|
|
uInt64 _numberOfStringElements,
|
|
|
|
ptr<RawString<const gChar>> _sourceCode ,
|
|
|
|
ptr< const gInt > _lengthsOfStrings
|
|
|
|
)
|
2020-02-13 22:13:16 -08:00
|
|
|
-> void
|
2020-02-12 00:29:35 -08:00
|
|
|
{
|
|
|
|
_shaderIDHolder = CreateShader(_typeOfShader);
|
|
|
|
|
|
|
|
BindShaderSource(_shaderIDHolder, _numberOfStringElements, _sourceCode, _lengthsOfStrings);
|
|
|
|
|
|
|
|
CompileShader(_shaderIDHolder);
|
2020-02-14 23:21:27 -08:00
|
|
|
|
|
|
|
return;
|
2020-02-12 00:29:35 -08:00
|
|
|
}
|
|
|
|
|
2020-02-13 22:13:16 -08:00
|
|
|
sfn MakeShaderProgram(Ref(ID<ShaderProgram>) _shaderProgramIDHolder, const ID<Shader> _vertexShader, const ID<Shader> _fragShader) -> void
|
2020-02-12 00:29:35 -08:00
|
|
|
{
|
|
|
|
_shaderProgramIDHolder = CreateShaderProgram();
|
|
|
|
|
2020-02-13 22:13:16 -08:00
|
|
|
AttachShader(_shaderProgramIDHolder, _vertexShader);
|
|
|
|
AttachShader(_shaderProgramIDHolder, _fragShader );
|
2020-02-12 00:29:35 -08:00
|
|
|
|
2020-02-13 22:13:16 -08:00
|
|
|
LinkProgramShader(_shaderProgramIDHolder);
|
2020-02-12 00:29:35 -08:00
|
|
|
|
2020-02-13 22:13:16 -08:00
|
|
|
gInt Result = false;
|
2020-02-12 00:29:35 -08:00
|
|
|
|
2020-02-13 22:13:16 -08:00
|
|
|
QueryShaderProgram(_shaderProgramIDHolder, EShaderProgramInfo::LinkStatus, Address(Result));
|
2020-02-12 00:29:35 -08:00
|
|
|
|
2020-02-13 22:13:16 -08:00
|
|
|
if (!Result)
|
|
|
|
{
|
|
|
|
gInt infoLogLength;
|
|
|
|
|
|
|
|
QueryShaderProgram(_shaderProgramIDHolder, EShaderProgramInfo::InfoLogLength, Address(infoLogLength));
|
|
|
|
|
|
|
|
if (infoLogLength > 0)
|
|
|
|
{
|
|
|
|
std::vector<char> ErrorMessage(infoLogLength + 1);
|
|
|
|
|
|
|
|
GetShaderProgramInfo(_shaderProgramIDHolder, infoLogLength, NULL, Address(ErrorMessage.at(0)));
|
|
|
|
|
|
|
|
throw std::runtime_error(Address(ErrorMessage.at(0)));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw std::runtime_error("ShaderProgram compilation failed and did not get a proper info log.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DetachShader(_shaderProgramIDHolder, _vertexShader);
|
|
|
|
DetachShader(_shaderProgramIDHolder, _fragShader );
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sfn SetUniformVariable_MatrixVariableArray(const ID<Matrix> _matrixID, const gSize _numMatricies, const EBool _shouldTransposeValues, ptr<const float> _dataPtr)
|
|
|
|
{
|
|
|
|
glUniformMatrix4fv(_matrixID, _numMatricies, GLenum(_shouldTransposeValues), _dataPtr);
|
2020-02-14 23:21:27 -08:00
|
|
|
|
|
|
|
return;
|
2020-02-13 22:13:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
sfn UseProgramShader(ID<ShaderProgram> _shaderProgramToUse)
|
|
|
|
{
|
|
|
|
glUseProgram(_shaderProgramToUse);
|
|
|
|
|
|
|
|
return;
|
2020-02-12 00:29:35 -08:00
|
|
|
}
|
|
|
|
}
|