2020-02-12 00:29:35 -08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
//DGL
|
2020-02-17 18:10:15 -08:00
|
|
|
#include "DGL_Enum.hpp"
|
2020-02-20 22:18:07 -08:00
|
|
|
#include "DGL_Types.hpp"
|
2020-02-17 18:10:15 -08:00
|
|
|
#include "DGL_Buffers.hpp"
|
2020-02-13 22:13:16 -08:00
|
|
|
#include "DGL_Space.hpp"
|
2020-02-12 00:29:35 -08:00
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
// Non-Standard C++
|
|
|
|
#include "Cpp_Alias.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
|
|
|
{
|
2020-02-13 22:13:16 -08:00
|
|
|
// Forward Declarations
|
|
|
|
|
2020-02-20 22:18:07 -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 , ro ptr<RawString<ro gChar>> _sourceCode, ro ptr<gInt> _lengthsOfStrings) -> void;
|
|
|
|
sfn MakeShaderProgram(Ref(ID<ShaderProgram>) _shaderProgramIDHolder,ro ID<Shader> _vertexShader , ro 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;
|
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
glGetProgramiv(_shaderToQueryForUniforms, GL_ACTIVE_UNIFORMS, Address(uniforms));
|
2020-02-13 22:13:16 -08:00
|
|
|
|
2020-02-15 19:32:26 -08:00
|
|
|
for (int uniformIndex = 0; uniformIndex < uniforms; uniformIndex++)
|
2020-02-13 22:13:16 -08:00
|
|
|
{
|
|
|
|
int name_len = -1, num = -1;
|
|
|
|
|
|
|
|
GLenum type = GL_ZERO;
|
|
|
|
|
|
|
|
char name[100];
|
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
glGetActiveUniform(_shaderToQueryForUniforms, GLuint(uniformIndex), sizeof(name) - 1, Address(name_len), Address(num), Address(type), name);
|
2020-02-13 22:13:16 -08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
sfn BindShaderSource(ID<Shader> _shader, gSize _numberOfStringElements, ro ptr<RawString<ro gChar>> _sourceCode, ro ptr<gInt> _lengthsOfStrings)
|
2020-02-12 00:29:35 -08:00
|
|
|
{
|
2020-02-20 22:18:07 -08:00
|
|
|
glShaderSource(_shader, _numberOfStringElements, _sourceCode, _lengthsOfStrings); //Address(_sourceCode), Address(_lengthsOfStrings));
|
2020-02-12 00:29:35 -08:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
sfn CompileShader(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;
|
|
|
|
}
|
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
sfn GetShaderInfo(ID<Shader> _shader, gSize _logLength, ro ptr<gSize> _infoLengthRef, RawString<gChar> _infoLogRef) -> void
|
2020-02-13 22:13:16 -08:00
|
|
|
{
|
|
|
|
glGetShaderInfoLog(_shader, _logLength, _infoLengthRef, _infoLogRef);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
sfn GetShaderProgramInfo(ID<ShaderProgram> _shaderProgram, gSize _logLength, ro ptr<gSize> _infoLengthRef, RawString<gChar> _infoLogRef) -> void
|
2020-02-13 22:13:16 -08:00
|
|
|
{
|
|
|
|
glGetProgramInfoLog(_shaderProgram, _logLength, _infoLengthRef, _infoLogRef);
|
2020-02-14 23:21:27 -08:00
|
|
|
|
|
|
|
return;
|
2020-02-13 22:13:16 -08:00
|
|
|
}
|
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
sfn GetUniformVariable(const ID<ShaderProgram> _programID, ro RawString<const char> _nameOfVariable) -> ID<Matrix>
|
2020-02-13 22:13:16 -08:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
sfn LoadShaders(RawString<ro char> _vertexShaderFilePath, RawString<ro char> _fragmentShaderFilePath) -> ID<ShaderProgram>
|
2020-02-13 22:13:16 -08:00
|
|
|
{
|
2020-02-20 22:18:07 -08:00
|
|
|
using std::vector;
|
2020-02-13 22:13:16 -08:00
|
|
|
|
|
|
|
|
|
|
|
string vertexShaderCode ;
|
|
|
|
string fragmentShaderCode ;
|
|
|
|
|
|
|
|
ifstream vertexShaderFileStream ;
|
|
|
|
ifstream fragmentShaderFileStream;
|
|
|
|
|
|
|
|
vertexShaderFileStream .open(_vertexShaderFilePath);
|
|
|
|
fragmentShaderFileStream.open(_fragmentShaderFilePath);
|
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
if (vertexShaderFileStream.is_open() and fragmentShaderFileStream.is_open())
|
2020-02-13 22:13:16 -08:00
|
|
|
{
|
2020-02-20 22:18:07 -08:00
|
|
|
stringstream vertSourceStrStream;
|
|
|
|
stringstream fragSourceStrStream;
|
2020-02-13 22:13:16 -08:00
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
vertSourceStrStream << vertexShaderFileStream .rdbuf();
|
|
|
|
fragSourceStrStream << fragmentShaderFileStream.rdbuf();
|
2020-02-13 22:13:16 -08:00
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
vertexShaderFileStream .close();
|
|
|
|
fragmentShaderFileStream.close();
|
2020-02-13 22:13:16 -08:00
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
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 !");
|
|
|
|
}
|
2020-02-13 22:13:16 -08:00
|
|
|
|
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
RawString<const char> vertexSourcePtr = vertexShaderCode .c_str();
|
|
|
|
RawString<const char> fragmentSourcePtr = fragmentShaderCode.c_str();
|
2020-02-13 22:13:16 -08:00
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
cout << "Compiling shader: " << _vertexShaderFilePath << endl;
|
2020-02-13 22:13:16 -08:00
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
ID<Shader> vertexShader = CreateShader(EShaderType::Vertex);
|
2020-02-13 22:13:16 -08:00
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
BindShaderSource(vertexShader, 1, Address(vertexSourcePtr), NULL);
|
|
|
|
CompileShader (vertexShader );
|
2020-02-13 22:13:16 -08:00
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
cout << "Compiling shader: " << _fragmentShaderFilePath << endl;
|
2020-02-13 22:13:16 -08:00
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
ID<Shader> fragmentShader = CreateShader(EShaderType::Fragment);
|
2020-02-13 22:13:16 -08:00
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
BindShaderSource(fragmentShader, 1, Address(fragmentSourcePtr), NULL);
|
|
|
|
CompileShader (fragmentShader );
|
2020-02-13 22:13:16 -08:00
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
|
|
|
|
cout << "Making Shader Program and Linking..." << endl;
|
2020-02-13 22:13:16 -08:00
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
ID<ShaderProgram> generatedProgram;
|
2020-02-13 22:13:16 -08:00
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
MakeShaderProgram(generatedProgram, vertexShader, fragmentShader);
|
2020-02-13 22:13:16 -08:00
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
DeleteShader(vertexShader );
|
|
|
|
DeleteShader(fragmentShader);
|
2020-02-13 22:13:16 -08:00
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
return generatedProgram;
|
2020-02-12 00:29:35 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
sfn MakeShader
|
|
|
|
(
|
2020-02-20 22:18:07 -08:00
|
|
|
Ref(ID<Shader> ) _shaderIDHolder ,
|
|
|
|
EShaderType _typeOfShader ,
|
|
|
|
gSize _numberOfStringElements,
|
|
|
|
ro ptr<RawString<ro gChar>> _sourceCode ,
|
|
|
|
ro ptr< gInt > _lengthsOfStrings
|
2020-02-12 00:29:35 -08:00
|
|
|
)
|
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-20 22:18:07 -08:00
|
|
|
sfn MakeShaderProgram(Ref(ID<ShaderProgram>) _shaderProgramIDHolder, ID<Shader> _vertexShader, 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)));
|
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
cout << (char*)&ErrorMessage[0] << endl;
|
|
|
|
|
2020-02-13 22:13:16 -08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-02-15 19:32:26 -08:00
|
|
|
// MVA: MatrixVariableArray
|
2020-02-20 22:18:07 -08:00
|
|
|
sfn SetUniformVariable_MVA(ID<Matrix> _matrixID, gSize _numMatricies, gBoolean _shouldTransposeValues, ro Ref(gFloat) _dataPtr)
|
|
|
|
{
|
|
|
|
glUniformMatrix4fv(_matrixID, _numMatricies, _shouldTransposeValues, Address(_dataPtr));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sfn SetUniformVariable_Vector3(ID<Vec3> _ID, gSize _num, ro Ref(gFloat) _dataPtr)
|
2020-02-13 22:13:16 -08:00
|
|
|
{
|
2020-02-20 22:18:07 -08:00
|
|
|
glUniform3fv(_ID, _num, Address(_dataPtr));
|
2020-02-14 23:21:27 -08:00
|
|
|
|
|
|
|
return;
|
2020-02-13 22:13:16 -08:00
|
|
|
}
|
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
sfn SetUniformVariable_Float(ID<gFloat> _ID, gFloat _data)
|
2020-02-16 22:28:24 -08:00
|
|
|
{
|
2020-02-20 22:18:07 -08:00
|
|
|
glUniform1f(_ID, _data);
|
2020-02-16 22:28:24 -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
|
|
|
}
|
2020-02-15 19:32:26 -08:00
|
|
|
|
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
|
|
|
|
// Shader Class
|
|
|
|
|
|
|
|
//class Shader_PhongBasic
|
|
|
|
//{
|
|
|
|
//public:
|
|
|
|
// Shader_PhongBasic()
|
|
|
|
// {
|
|
|
|
// LoadShader();
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
// sfn LoadShader()
|
|
|
|
// {
|
|
|
|
// shaderID = LoadShaders("PhongShader_Deprecated.vert", "PhongShader_Deprecated.frag");
|
|
|
|
|
|
|
|
// inverseModelSpaceID = GetUniformVariable(shaderID, "InverseModelSpace");
|
|
|
|
// modelSpaceID = GetUniformVariable(shaderID, "ModelSpace" );
|
|
|
|
// projectionID = GetUniformVariable(shaderID, "Projection" );
|
|
|
|
// viewportID = GetUniformVariable(shaderID, "Viewport" );
|
|
|
|
|
|
|
|
// objectColorID = GetUniformVariable(shaderID, "ObjectColor" );
|
|
|
|
// lightColorID = GetUniformVariable(shaderID, "LightColor" );
|
|
|
|
// lightPositionID = GetUniformVariable(shaderID, "LightPosition");
|
|
|
|
// viewPositionID = GetUniformVariable(shaderID, "ViewPosition" );
|
|
|
|
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// sfn Use
|
|
|
|
// (
|
|
|
|
// ro Ref(CoordSpace) _projection ,
|
|
|
|
// ro Ref(CoordSpace) _viewport ,
|
|
|
|
// ro Ref(CoordSpace) _objectTransform ,
|
|
|
|
// ro Ref(Vector3 ) _objectColor ,
|
|
|
|
// ro Ref(Vector3 ) _lightPosition ,
|
|
|
|
// ro Ref(Vector3 ) _lightColor ,
|
|
|
|
// ro Ref(Vector3 ) _viewPosition
|
|
|
|
// )
|
|
|
|
// {
|
|
|
|
// CoordSpace inverseTransform = Inverse(_viewport * _objectTransform);
|
|
|
|
|
|
|
|
// UseProgramShader(shaderID);
|
|
|
|
|
|
|
|
// SetUniformVariable_MVA(inverseModelSpaceID, 1, false, inverseTransform[0][0]);
|
|
|
|
// SetUniformVariable_MVA(modelSpaceID , 1, false, _objectTransform[0][0]);
|
|
|
|
// SetUniformVariable_MVA(projectionID , 1, false, _projection [0][0]);
|
|
|
|
// SetUniformVariable_MVA(viewportID , 1, false, _viewport [0][0]);
|
|
|
|
|
|
|
|
// SetUniformVariable_Vector3(lightPositionID, 1, _lightPosition[0]);
|
|
|
|
|
|
|
|
// SetUniformVariable_Vector3(objectColorID , 1, _objectColor [0]);
|
|
|
|
// SetUniformVariable_Vector3(lightColorID , 1, _lightColor [0]);
|
|
|
|
// SetUniformVariable_Vector3(viewPositionID, 1, _viewPosition[0]);
|
|
|
|
|
|
|
|
// EnableVertexAttributeArray(0);
|
|
|
|
// EnableVertexAttributeArray(1);
|
|
|
|
// //EnableVertexAttributeArray(2);
|
|
|
|
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// sfn Stop()
|
|
|
|
// {
|
|
|
|
// //DisableVertexAttributeArray(2);
|
|
|
|
// DisableVertexAttributeArray(1);
|
|
|
|
// DisableVertexAttributeArray(0);
|
|
|
|
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// Declarations
|
|
|
|
|
|
|
|
//private:
|
|
|
|
|
|
|
|
// ID<ShaderProgram> shaderID;
|
|
|
|
|
|
|
|
// ID<CoordSpace> modelSpaceID, inverseModelSpaceID, viewportID, projectionID;
|
|
|
|
|
|
|
|
// ID<Vec3> lightPositionID, viewPositionID, objectColorID, lightColorID;
|
|
|
|
|
|
|
|
// gInt vertexIndexID, normalIndexID;
|
|
|
|
//};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Old
|
|
|
|
|
|
|
|
// 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);"
|
|
|
|
|
|
|
|
"}";
|
|
|
|
|
|
|
|
|
|
|
|
// Default Shaders
|
|
|
|
|
|
|
|
ID<ShaderProgram> RawShader ,
|
|
|
|
SimpleShader ;
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-02-15 19:32:26 -08:00
|
|
|
namespace SS_Transformed
|
|
|
|
{
|
|
|
|
ID<ShaderProgram> Shader;
|
|
|
|
|
|
|
|
ID<CoordSpace> ScreenSpaceVarID;
|
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
sfn UpdateShader(ro Ref(CoordSpace) _screenSpace)
|
2020-02-15 19:32:26 -08:00
|
|
|
{
|
2020-02-20 22:18:07 -08:00
|
|
|
SetUniformVariable_MVA(ScreenSpaceVarID, 1, false, _screenSpace[0][0]);
|
2020-02-15 19:32:26 -08:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sfn LoadShader()
|
|
|
|
{
|
|
|
|
Shader = LoadShaders("SimpleTransform.vert", "SingleColor.frag");
|
|
|
|
|
|
|
|
ScreenSpaceVarID = DGL::GetUniformVariable(Shader, "ScreenSpaceTransform");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-16 22:28:24 -08:00
|
|
|
namespace Basic_LightingShader
|
|
|
|
{
|
|
|
|
ID<ShaderProgram> Shader;
|
|
|
|
|
|
|
|
ID<CoordSpace> ScreenSpaceVarID;
|
|
|
|
|
|
|
|
ID<Vec3> ObjectColorID, LightColorID;
|
|
|
|
|
|
|
|
sfn LoadShader()
|
|
|
|
{
|
|
|
|
Shader = LoadShaders("SimpleTransform.vert", "BasicLighting.frag");
|
|
|
|
|
|
|
|
ScreenSpaceVarID = GetUniformVariable(Shader, "ScreenSpaceTransform");
|
|
|
|
|
|
|
|
ObjectColorID = GetUniformVariable(Shader, "ObjectColor");
|
|
|
|
LightColorID = GetUniformVariable(Shader, "LightColor" );
|
|
|
|
}
|
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
sfn Stop()
|
|
|
|
{
|
|
|
|
DisableVertexAttributeArray(0);
|
|
|
|
DisableVertexAttributeArray(1);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-17 18:10:15 -08:00
|
|
|
sfn Use(Ref(CoordSpace) _cubeTransform, Ref(Vector3) _objectColor, Ref(Vector3) _lightColor)
|
2020-02-16 22:28:24 -08:00
|
|
|
{
|
|
|
|
UseProgramShader(Shader);
|
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
SetUniformVariable_MVA(ScreenSpaceVarID, 1, false, _cubeTransform[0][0]);
|
2020-02-16 22:28:24 -08:00
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
SetUniformVariable_Vector3(ObjectColorID, 1, _objectColor[0]);
|
|
|
|
SetUniformVariable_Vector3(LightColorID , 1, _lightColor [0]);
|
2020-02-17 18:10:15 -08:00
|
|
|
|
|
|
|
EnableVertexAttributeArray(0);
|
|
|
|
EnableVertexAttributeArray(1);
|
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
return;
|
2020-02-16 22:28:24 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Basic_LampShader
|
|
|
|
{
|
|
|
|
ID<ShaderProgram> Shader;
|
|
|
|
|
|
|
|
ID<CoordSpace> ScreenSpaceVarID;
|
|
|
|
|
|
|
|
sfn LoadShader()
|
|
|
|
{
|
|
|
|
Shader = LoadShaders("SimpleTransform.vert", "BasicLamp.frag");
|
|
|
|
|
|
|
|
ScreenSpaceVarID = GetUniformVariable(Shader, "ScreenSpaceTransform");
|
2020-02-20 22:18:07 -08:00
|
|
|
|
|
|
|
return;
|
2020-02-16 22:28:24 -08:00
|
|
|
}
|
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
sfn Use(ro Ref(CoordSpace) _lampTransform)
|
2020-02-16 22:28:24 -08:00
|
|
|
{
|
|
|
|
UseProgramShader(Shader);
|
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
SetUniformVariable_MVA(ScreenSpaceVarID, 1, false, _lampTransform[0][0]);
|
2020-02-17 18:10:15 -08:00
|
|
|
|
|
|
|
EnableVertexAttributeArray(0);
|
|
|
|
EnableVertexAttributeArray(1);
|
2020-02-20 22:18:07 -08:00
|
|
|
|
|
|
|
return;
|
2020-02-17 18:10:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
sfn Stop()
|
|
|
|
{
|
|
|
|
DisableVertexAttributeArray(0);
|
|
|
|
DisableVertexAttributeArray(1);
|
2020-02-20 22:18:07 -08:00
|
|
|
|
|
|
|
return;
|
2020-02-16 22:28:24 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
struct Material_Phong
|
|
|
|
{
|
|
|
|
VecColor Color ;
|
|
|
|
gFloat Ambience;
|
|
|
|
gFloat Diffuse ;
|
|
|
|
gFloat Specular;
|
|
|
|
};
|
|
|
|
|
2020-02-16 22:28:24 -08:00
|
|
|
namespace PhongShader
|
|
|
|
{
|
|
|
|
ID<ShaderProgram> ShaderID;
|
|
|
|
|
2020-02-17 18:10:15 -08:00
|
|
|
ID<CoordSpace> ModelSpaceID, InverseModelSpaceID, ViewportID, ProjectionID;
|
2020-02-16 22:28:24 -08:00
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
ID<Vec3> LightPositionID, ObjectColorID, LightColorID;
|
2020-02-16 22:28:24 -08:00
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
ID<gFloat> AmbientStrengthID, DiffuseStrengthID, SpecularStrengthID;
|
|
|
|
|
|
|
|
gInt VertexIndexID, NormalIndexID;
|
2020-02-16 22:28:24 -08:00
|
|
|
|
|
|
|
sfn LoadShader()
|
|
|
|
{
|
|
|
|
ShaderID = LoadShaders("PhongShader.vert", "PhongShader.frag");
|
|
|
|
|
2020-02-17 18:10:15 -08:00
|
|
|
InverseModelSpaceID = GetUniformVariable(ShaderID, "InverseModelSpace");
|
|
|
|
ModelSpaceID = GetUniformVariable(ShaderID, "ModelSpace" );
|
|
|
|
ProjectionID = GetUniformVariable(ShaderID, "Projection" );
|
|
|
|
ViewportID = GetUniformVariable(ShaderID, "Viewport" );
|
2020-02-16 22:28:24 -08:00
|
|
|
|
2020-02-17 18:10:15 -08:00
|
|
|
ObjectColorID = GetUniformVariable(ShaderID, "ObjectColor" );
|
|
|
|
LightColorID = GetUniformVariable(ShaderID, "LightColor" );
|
|
|
|
LightPositionID = GetUniformVariable(ShaderID, "LightPosition");
|
2020-02-20 22:18:07 -08:00
|
|
|
|
|
|
|
AmbientStrengthID = GetUniformVariable(ShaderID, "AmbientStrength" );
|
|
|
|
DiffuseStrengthID = GetUniformVariable(ShaderID, "DiffuseStrength" );
|
|
|
|
SpecularStrengthID = GetUniformVariable(ShaderID, "SpecularStrength");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sfn Use_Old
|
|
|
|
(
|
|
|
|
ro Ref(CoordSpace) _projection ,
|
|
|
|
ro Ref(CoordSpace) _viewport ,
|
|
|
|
ro Ref(CoordSpace) _objectTransform ,
|
|
|
|
ro Ref(Vector3 ) _objectColor ,
|
|
|
|
ro Ref(Vector3 ) _lightPosition ,
|
|
|
|
ro Ref(Vector3 ) _lightColor ,
|
|
|
|
ro Ref(Vector3 ) _viewPosition
|
|
|
|
)
|
|
|
|
{
|
|
|
|
CoordSpace inverseTransform = Inverse(_viewport * _objectTransform);
|
|
|
|
|
|
|
|
UseProgramShader(ShaderID);
|
|
|
|
|
|
|
|
SetUniformVariable_MVA(InverseModelSpaceID, 1, false, inverseTransform[0][0]);
|
|
|
|
SetUniformVariable_MVA(ModelSpaceID , 1, false, _objectTransform[0][0]);
|
|
|
|
SetUniformVariable_MVA(ProjectionID , 1, false, _projection [0][0]);
|
|
|
|
SetUniformVariable_MVA(ViewportID , 1, false, _viewport [0][0]);
|
|
|
|
|
|
|
|
SetUniformVariable_Vector3(LightPositionID, 1, _lightPosition[0]);
|
|
|
|
|
|
|
|
SetUniformVariable_Vector3(ObjectColorID , 1, _objectColor [0]);
|
|
|
|
SetUniformVariable_Vector3(LightColorID , 1, _lightColor [0]);
|
|
|
|
|
|
|
|
EnableVertexAttributeArray(0);
|
|
|
|
EnableVertexAttributeArray(1);
|
|
|
|
|
|
|
|
return;
|
2020-02-16 22:28:24 -08:00
|
|
|
}
|
|
|
|
|
2020-02-18 11:37:17 -08:00
|
|
|
sfn Use
|
2020-02-16 22:28:24 -08:00
|
|
|
(
|
2020-02-20 22:18:07 -08:00
|
|
|
ro Ref(CoordSpace ) _projection ,
|
|
|
|
ro Ref(CoordSpace ) _viewport ,
|
|
|
|
ro Ref(CoordSpace ) _objectTransform ,
|
|
|
|
ro Ref(Vector3 ) _lightPosition ,
|
|
|
|
ro Ref(Vector3 ) _lightColor ,
|
|
|
|
ro Ref(Material_Phong) _material
|
2020-02-16 22:28:24 -08:00
|
|
|
)
|
|
|
|
{
|
2020-02-18 11:37:17 -08:00
|
|
|
CoordSpace inverseTransform = Inverse(_viewport * _objectTransform);
|
2020-02-17 18:10:15 -08:00
|
|
|
|
2020-02-16 22:28:24 -08:00
|
|
|
UseProgramShader(ShaderID);
|
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
SetUniformVariable_MVA(InverseModelSpaceID, 1, false, inverseTransform[0][0]);
|
|
|
|
SetUniformVariable_MVA(ModelSpaceID , 1, false, _objectTransform[0][0]);
|
|
|
|
SetUniformVariable_MVA(ProjectionID , 1, false, _projection [0][0]);
|
|
|
|
SetUniformVariable_MVA(ViewportID , 1, false, _viewport [0][0]);
|
|
|
|
|
|
|
|
SetUniformVariable_Vector3(LightPositionID, 1, _lightPosition[0]);
|
2020-02-16 22:28:24 -08:00
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
SetUniformVariable_Vector3(ObjectColorID , 1, _material.Color[0]);
|
|
|
|
SetUniformVariable_Vector3(LightColorID , 1, _lightColor [0]);
|
2020-02-16 22:28:24 -08:00
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
SetUniformVariable_Float(AmbientStrengthID , _material.Ambience);
|
|
|
|
SetUniformVariable_Float(DiffuseStrengthID , _material.Diffuse );
|
|
|
|
SetUniformVariable_Float(SpecularStrengthID, _material.Specular);
|
2020-02-17 18:10:15 -08:00
|
|
|
|
|
|
|
EnableVertexAttributeArray(0);
|
|
|
|
EnableVertexAttributeArray(1);
|
2020-02-20 22:18:07 -08:00
|
|
|
|
|
|
|
return;
|
2020-02-17 18:10:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
sfn Stop()
|
|
|
|
{
|
|
|
|
DisableVertexAttributeArray(1);
|
|
|
|
DisableVertexAttributeArray(0);
|
2020-02-20 22:18:07 -08:00
|
|
|
|
|
|
|
return;
|
2020-02-16 22:28:24 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 19:32:26 -08:00
|
|
|
sfn LoadRawShader()
|
|
|
|
{
|
2020-02-20 22:18:07 -08:00
|
|
|
ID<Shader> VertexShader = 0;
|
|
|
|
ID<Shader> FragmentShader = 0;
|
2020-02-15 19:32:26 -08:00
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
MakeShader(VertexShader , EShaderType::Vertex , 1, Address(RawVertextShaderSource ), NULL);
|
|
|
|
MakeShader(FragmentShader, EShaderType::Fragment, 1, Address(RawFragmentShaderSource), NULL);
|
2020-02-15 19:32:26 -08:00
|
|
|
|
|
|
|
MakeShaderProgram(RawShader, VertexShader, FragmentShader);
|
|
|
|
|
2020-02-20 22:18:07 -08:00
|
|
|
DeleteShader(VertexShader );
|
2020-02-15 19:32:26 -08:00
|
|
|
DeleteShader(FragmentShader);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sfn LoadSimpleShader()
|
|
|
|
{
|
|
|
|
SimpleShader = LoadShaders("SimpleVertexShader.vert", "SimpleFragmentShader.frag");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sfn LoadDefaultShaders()
|
|
|
|
{
|
2020-02-16 22:28:24 -08:00
|
|
|
LoadRawShader ();
|
|
|
|
LoadSimpleShader();
|
|
|
|
SS_Transformed ::LoadShader ();
|
|
|
|
Basic_LampShader ::LoadShader ();
|
|
|
|
Basic_LightingShader::LoadShader ();
|
|
|
|
PhongShader ::LoadShader ();
|
2020-02-15 19:32:26 -08:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2020-02-12 00:29:35 -08:00
|
|
|
}
|