ddnet/src/engine/client/opengl_sl_program.cpp

142 lines
2.6 KiB
C++
Raw Normal View History

#include "opengl_sl_program.h"
#include "opengl_sl.h"
#include <base/system.h>
2017-09-13 18:33:58 +00:00
void CGLSLProgram::CreateProgram()
{
m_ProgramID = glCreateProgram();
}
2017-09-13 18:33:58 +00:00
void CGLSLProgram::DeleteProgram()
{
if(!m_IsLinked)
return;
m_IsLinked = false;
glDeleteProgram(m_ProgramID);
}
bool CGLSLProgram::AddShader(CGLSL *pShader)
2017-09-13 18:33:58 +00:00
{
if(pShader->IsLoaded())
2017-09-13 18:33:58 +00:00
{
glAttachShader(m_ProgramID, pShader->GetShaderID());
return true;
}
return false;
}
void CGLSLProgram::DetachShader(CGLSL *pShader)
2017-09-13 18:33:58 +00:00
{
if(pShader->IsLoaded())
2017-09-13 18:33:58 +00:00
{
DetachShaderByID(pShader->GetShaderID());
}
}
void CGLSLProgram::DetachShaderByID(GLuint ShaderID)
{
glDetachShader(m_ProgramID, ShaderID);
}
2017-09-13 18:33:58 +00:00
void CGLSLProgram::LinkProgram()
{
glLinkProgram(m_ProgramID);
int LinkStatus;
glGetProgramiv(m_ProgramID, GL_LINK_STATUS, &LinkStatus);
m_IsLinked = LinkStatus == GL_TRUE;
if(!m_IsLinked)
2017-09-13 18:33:58 +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);
}
//detach all shaders attached to this program
DetachAllShaders();
}
2017-09-27 12:52:06 +00:00
void CGLSLProgram::DetachAllShaders()
{
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;
}
}
void CGLSLProgram::SetUniformVec4(int Loc, int Count, const float *pValues)
2017-09-13 18:33:58 +00:00
{
glUniform4fv(Loc, Count, pValues);
}
void CGLSLProgram::SetUniformVec2(int Loc, int Count, const float *pValues)
2017-09-13 18:33:58 +00:00
{
glUniform2fv(Loc, Count, pValues);
}
void CGLSLProgram::SetUniform(int Loc, int Count, const float *pValues)
{
glUniform1fv(Loc, Count, pValues);
}
2017-09-13 18:33:58 +00:00
void CGLSLProgram::SetUniform(int Loc, const int Value)
{
glUniform1i(Loc, Value);
}
2017-09-13 18:33:58 +00:00
void CGLSLProgram::SetUniform(int Loc, const unsigned int Value)
{
glUniform1ui(Loc, Value);
}
2017-09-13 18:33:58 +00:00
void CGLSLProgram::SetUniform(int Loc, const float Value)
{
glUniform1f(Loc, Value);
}
2017-09-13 18:33:58 +00:00
void CGLSLProgram::SetUniform(int Loc, const bool Value)
{
glUniform1i(Loc, (int)Value);
}
int CGLSLProgram::GetUniformLoc(const char *Name)
2017-09-13 18:33:58 +00:00
{
return glGetUniformLocation(m_ProgramID, Name);
}
2017-09-13 18:33:58 +00:00
void CGLSLProgram::UseProgram()
{
if(m_IsLinked)
glUseProgram(m_ProgramID);
}
2017-09-13 18:33:58 +00:00
GLuint CGLSLProgram::GetProgramID()
{
return m_ProgramID;
}
2017-09-13 18:33:58 +00:00
CGLSLProgram::CGLSLProgram()
{
m_IsLinked = false;
}
2017-09-13 18:33:58 +00:00
CGLSLProgram::~CGLSLProgram()
{
DeleteProgram();
2017-10-20 07:31:42 +00:00
}