Tester for loading document in XML format
[ipdf/code.git] / src / shaderprogram.cpp
1 #include "shaderprogram.h"
2
3 using namespace IPDF;
4
5 ShaderProgram::~ShaderProgram()
6 {
7         for(auto shader : m_shaders)
8         {
9                 glDetachShader(m_program, shader.obj);
10                 glDeleteShader(shader.obj);
11         }
12
13         if (m_program)
14                 glDeleteProgram(m_program);
15 }
16
17 void ShaderProgram::LazyCreateProgram()
18 {
19         if (!m_program)
20         {
21                 m_program = glCreateProgram();
22         }
23 }
24
25 bool ShaderProgram::AttachShader(const char *src, GLenum type)
26 {
27         GLuint shader_obj = glCreateShader(type);
28         glShaderSource(shader_obj, 1, &src, 0);
29         glCompileShader(shader_obj);
30
31         m_shaders.push_back(Shader{type, shader_obj});
32         LazyCreateProgram();
33         glAttachShader(m_program, shader_obj);
34         return true;
35 }
36
37 bool ShaderProgram::AttachVertexProgram(const char *src)
38 {
39         return AttachShader(src, GL_VERTEX_SHADER);
40 }
41
42 bool ShaderProgram::AttachFragmentProgram(const char *src)
43 {
44         return AttachShader(src, GL_FRAGMENT_SHADER);
45 }
46
47 bool ShaderProgram::Link()
48 {
49         glLinkProgram(m_program);
50         return true;
51 }
52
53 const void ShaderProgram::Use() const
54 {
55         glUseProgram(m_program);
56 }
57
58 const GLint ShaderProgram::GetUniformLocation(const char *name) const
59 {
60         return glGetUniformLocation(m_program, name);
61 }

UCC git Repository :: git.ucc.asn.au