Index

zydeco / master

Experiment in graphics programming, C++, OpenGL, simulation techniques.

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
1008 Dec 2023 15:215a717feFractal renderer update for articleJosh Stockin122G

Blob @ zydeco / src / render / gl / GLProgram.cpp

text/plain1737 bytesdownload raw
1#include <GL/gl3w.h>
2
3#include "ZydecoCommon.hpp"
4#include "GLProgram.hpp"
5#include "GLShader.hpp"
6
7
8static Logger LOGGER("GLProgram");
9
10
11// static initialize members
12std::map<std::string, GLProgram*> GLProgram::s_glPrograms {};
13
14
15GLProgram *GLProgram::GetGLProgram(std::string name)
16{
17 if (!s_glPrograms.count(name)) { ZydecoFault("GetGLProgram(): Program '{}' does not exist", name); }
18 return s_glPrograms.at(name);
19}
20
21GLProgram::GLProgram(std::string name):
22 m_glProgramId(-1),
23 m_glProgramName(name)
24{
25 LOGGER.Log(Logger::TRACE, "GLProgram(name: {})", name);
26
27 s_glPrograms.emplace(std::pair<std::string, GLProgram*>(m_glProgramName, this));
28}
29
30GLProgram::~GLProgram()
31{
32 LOGGER.Log(Logger::TRACE, "~GLProgram() for '{}'", m_glProgramName);
33 if (m_glProgramId != -1) { glDeleteProgram(m_glProgramId); }
34 s_glPrograms.erase(m_glProgramName);
35}
36
37void GLProgram::Generate(std::vector<GLShader*> shaders)
38{
39 uint64_t pid = glCreateProgram();
40
41 for (GLShader *p_shader : shaders)
42 {
43 glAttachShader(pid, p_shader->GetGLShaderID());
44 }
45
46 glLinkProgram(pid);
47
48 for (GLShader *p_shader : shaders)
49 {
50 glDetachShader(pid, p_shader->GetGLShaderID());
51 }
52
53 int success;
54 char infoLog[512];
55 glGetProgramiv(pid, GL_LINK_STATUS, &success);
56 if(!success)
57 {
58 glGetProgramInfoLog(pid, 512, NULL, infoLog);
59 ZydecoFault("Shader program linking failed:\n{}", infoLog);
60 return;
61 }
62 else
63 {
64 if (m_glProgramId != -1) { glDeleteProgram(m_glProgramId); }
65 m_glProgramId = pid;
66 }
67}
68
69uint64_t GLProgram::GetGLProgramID()
70{
71 return m_glProgramId;
72}
73
74std::string GLProgram::GetGLProgramName()
75{
76 return m_glProgramName;
77}
78