2017-09-02 13:24:07 +00:00
|
|
|
#include "opengl_sl_program.h"
|
|
|
|
#include "opengl_sl.h"
|
|
|
|
#include <base/system.h>
|
|
|
|
|
2017-09-13 18:33:58 +00:00
|
|
|
void CGLSLProgram::CreateProgram()
|
|
|
|
{
|
2017-09-02 13:24:07 +00:00
|
|
|
m_ProgramID = glCreateProgram();
|
|
|
|
}
|
|
|
|
|
2017-09-13 18:33:58 +00:00
|
|
|
void CGLSLProgram::DeleteProgram()
|
|
|
|
{
|
2017-09-02 13:24:07 +00:00
|
|
|
if (!m_IsLinked) return;
|
|
|
|
m_IsLinked = false;
|
|
|
|
glDeleteProgram(m_ProgramID);
|
|
|
|
}
|
|
|
|
|
2017-09-13 18:33:58 +00:00
|
|
|
bool CGLSLProgram::AddShader(CGLSL* pShader)
|
|
|
|
{
|
|
|
|
if (pShader->IsLoaded())
|
|
|
|
{
|
2017-09-02 13:24:07 +00:00
|
|
|
glAttachShader(m_ProgramID, pShader->GetShaderID());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-13 18:33:58 +00:00
|
|
|
void CGLSLProgram::DetachShader(CGLSL* pShader)
|
|
|
|
{
|
|
|
|
if (pShader->IsLoaded())
|
|
|
|
{
|
2017-09-14 17:34:14 +00:00
|
|
|
DetachShaderByID(pShader->GetShaderID());
|
2017-09-02 13:24:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-14 17:34:14 +00:00
|
|
|
void CGLSLProgram::DetachShaderByID(GLuint ShaderID)
|
|
|
|
{
|
|
|
|
glDetachShader(m_ProgramID, ShaderID);
|
|
|
|
}
|
|
|
|
|
2017-09-13 18:33:58 +00:00
|
|
|
void CGLSLProgram::LinkProgram()
|
|
|
|
{
|
2017-09-02 13:24:07 +00:00
|
|
|
glLinkProgram(m_ProgramID);
|
|
|
|
int LinkStatus;
|
|
|
|
glGetProgramiv(m_ProgramID, GL_LINK_STATUS, &LinkStatus);
|
|
|
|
m_IsLinked = LinkStatus == GL_TRUE;
|
2017-09-13 18:33:58 +00:00
|
|
|
if (!m_IsLinked)
|
|
|
|
{
|
2017-09-02 13:24:07 +00:00
|
|
|
char sInfoLog[1024];
|
|
|
|
char sFinalMessage[1536];
|
|
|
|
int iLogLength;
|
|
|
|
glGetProgramInfoLog(m_ProgramID, 1024, &iLogLength, sInfoLog);
|
|
|
|
str_format(sFinalMessage, 1536, "Error! Shader program wasn't linked! The linker returned:\n\n%s", sInfoLog);
|
2017-09-27 13:01:38 +00:00
|
|
|
dbg_msg("GLSL Program", "%s", sFinalMessage);
|
2017-09-02 13:24:07 +00:00
|
|
|
}
|
2017-09-14 17:34:14 +00:00
|
|
|
|
|
|
|
//detach all shaders attached to this program
|
|
|
|
DetachAllShaders();
|
|
|
|
}
|
|
|
|
|
2017-09-27 12:52:06 +00:00
|
|
|
void CGLSLProgram::DetachAllShaders()
|
|
|
|
{
|
2017-09-14 17:34:14 +00:00
|
|
|
GLuint aShaders[100];
|
|
|
|
GLsizei ReturnedCount = 0;
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
glGetAttachedShaders(m_ProgramID, 100, &ReturnedCount, aShaders);
|
|
|
|
|
|
|
|
if(ReturnedCount > 0)
|
|
|
|
{
|
|
|
|
for(GLsizei i = 0; i < ReturnedCount; ++i)
|
|
|
|
{
|
|
|
|
DetachShaderByID(aShaders[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ReturnedCount < 100) break;
|
|
|
|
}
|
2017-09-02 13:24:07 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 18:33:58 +00:00
|
|
|
void CGLSLProgram::SetUniformVec4(int Loc, int Count, const float* Value)
|
|
|
|
{
|
2017-09-02 13:24:07 +00:00
|
|
|
glUniform4fv(Loc, Count, Value);
|
|
|
|
}
|
|
|
|
|
2017-09-13 18:33:58 +00:00
|
|
|
void CGLSLProgram::SetUniformVec2(int Loc, int Count, const float* Value)
|
|
|
|
{
|
2017-09-12 18:09:40 +00:00
|
|
|
glUniform2fv(Loc, Count, Value);
|
|
|
|
}
|
|
|
|
|
2017-09-13 18:33:58 +00:00
|
|
|
void CGLSLProgram::SetUniform(int Loc, const int Value)
|
|
|
|
{
|
2017-09-02 13:24:07 +00:00
|
|
|
glUniform1i(Loc, Value);
|
|
|
|
}
|
|
|
|
|
2017-09-13 18:33:58 +00:00
|
|
|
void CGLSLProgram::SetUniform(int Loc, const unsigned int Value)
|
|
|
|
{
|
2017-09-02 13:24:07 +00:00
|
|
|
glUniform1ui(Loc, Value);
|
|
|
|
}
|
|
|
|
|
2017-09-13 18:33:58 +00:00
|
|
|
void CGLSLProgram::SetUniform(int Loc, const float Value)
|
|
|
|
{
|
2017-09-02 13:24:07 +00:00
|
|
|
glUniform1f(Loc, Value);
|
|
|
|
}
|
|
|
|
|
2017-09-13 18:33:58 +00:00
|
|
|
void CGLSLProgram::SetUniform(int Loc, const bool Value)
|
|
|
|
{
|
2017-09-02 13:24:07 +00:00
|
|
|
glUniform1i(Loc, (int)Value);
|
|
|
|
}
|
|
|
|
|
2017-09-13 18:33:58 +00:00
|
|
|
int CGLSLProgram::GetUniformLoc(const char* Name)
|
|
|
|
{
|
2017-09-02 13:24:07 +00:00
|
|
|
return glGetUniformLocation(m_ProgramID, Name);
|
|
|
|
}
|
|
|
|
|
2017-09-13 18:33:58 +00:00
|
|
|
void CGLSLProgram::UseProgram()
|
|
|
|
{
|
2017-09-02 13:24:07 +00:00
|
|
|
if(m_IsLinked) glUseProgram(m_ProgramID);
|
|
|
|
}
|
|
|
|
|
2017-09-13 18:33:58 +00:00
|
|
|
GLuint CGLSLProgram::GetProgramID()
|
|
|
|
{
|
2017-09-02 13:24:07 +00:00
|
|
|
return m_ProgramID;
|
|
|
|
}
|
|
|
|
|
2017-09-13 18:33:58 +00:00
|
|
|
CGLSLProgram::CGLSLProgram()
|
|
|
|
{
|
2017-09-02 13:24:07 +00:00
|
|
|
m_IsLinked = false;
|
|
|
|
}
|
|
|
|
|
2017-09-13 18:33:58 +00:00
|
|
|
CGLSLProgram::~CGLSLProgram()
|
|
|
|
{
|
2017-09-02 13:24:07 +00:00
|
|
|
DeleteProgram();
|
2017-10-20 07:31:42 +00:00
|
|
|
}
|