ddnet/src/engine/client/opengl_sl_program.cpp

139 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);
}
2017-09-13 18:33:58 +00:00
bool CGLSLProgram::AddShader(CGLSL* pShader)
{
if (pShader->IsLoaded())
{
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())
{
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;
2017-09-13 18:33:58 +00:00
if (!m_IsLinked)
{
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;
}
}
2020-08-19 05:05:51 +00:00
void CGLSLProgram::SetUniformVec4(int Loc, int Count, const float *pValues)
2017-09-13 18:33:58 +00:00
{
2020-08-19 05:05:51 +00:00
glUniform4fv(Loc, Count, pValues);
}
2020-08-19 05:05:51 +00:00
void CGLSLProgram::SetUniformVec2(int Loc, int Count, const float *pValues)
2017-09-13 18:33:58 +00:00
{
2020-08-19 05:05:51 +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);
}
2017-09-13 18:33:58 +00:00
int CGLSLProgram::GetUniformLoc(const char* Name)
{
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
}