1 | #include <list> |
2 |
|
3 | #include "ZydecoCommon.hpp" |
4 | #include "GLRenderObject.hpp" |
5 | #include "GLProgram.hpp" |
6 |
|
7 |
|
8 | static Logger LOGGER("GLRenderObject"); |
9 |
|
10 | // static initialize |
11 | std::map<uint64_t, std::list<GLRenderObject*>> GLRenderObject::s_renderObjects = {}; |
12 |
|
13 |
|
14 | GLRenderObject::GLRenderObject(std::string gl_program_name, uint64_t render_priority): |
15 | m_renderPriority(render_priority), |
16 | m_renderEnabled(true) |
17 | { |
18 | m_glProgram = GLProgram::GetGLProgram(gl_program_name); |
19 | if (m_glProgram == nullptr) { ZydecoFault("GLRenderObject(): Program '{}' is nonexistent", gl_program_name); } |
20 |
|
21 | s_renderObjects.insert(std::pair<uint64_t, std::list<GLRenderObject*>>(render_priority, {})); |
22 | s_renderObjects.at(render_priority).push_back(this); |
23 | } |
24 |
|
25 | GLRenderObject::~GLRenderObject() |
26 | { |
27 | s_renderObjects.at(m_renderPriority).remove(this); |
28 | if (s_renderObjects.at(m_renderPriority).empty()) |
29 | { |
30 | s_renderObjects.erase(m_renderPriority); |
31 | } |
32 | } |
33 |
|
34 | void GLRenderObject::RenderEnable() |
35 | { |
36 | m_renderEnabled = true; |
37 | } |
38 |
|
39 | void GLRenderObject::RenderDisable() |
40 | { |
41 | m_renderEnabled = false; |
42 | } |
43 |
|
44 | void GLRenderObject::GLTargetSetup() |
45 | { |
46 | glUseProgram(m_glProgram->GetGLProgramID()); |
47 |
|
48 | for (std::pair<std::string, glUniform> uniform : m_uniforms) |
49 | { |
50 | int location = glGetUniformLocation(m_glProgram->GetGLProgramID(), uniform.first.c_str()); |
51 | if (location == -1) |
52 | { |
53 | LOGGER.Log(Logger::ERROR, "Render(): Uniform '{}' does not exist for program '{}'", uniform.first, m_glProgram->GetGLProgramName()); |
54 | continue; |
55 | } |
56 |
|
57 | if (uniform.second.type == glUniformType::FLOAT) |
58 | { |
59 | switch (uniform.second.quantity) |
60 | { |
61 | case 1: glUniform1f(location, *(float*)(uniform.second.data[0])); break; |
62 | case 2: glUniform2f(location, *(float*)(uniform.second.data[0]), *(float*)(uniform.second.data[1])); break; |
63 | case 3: glUniform3f(location, *(float*)(uniform.second.data[0]), *(float*)(uniform.second.data[1]), *(float*)(uniform.second.data[2])); break; |
64 | case 4: glUniform4f(location, *(float*)(uniform.second.data[0]), *(float*)(uniform.second.data[1]), *(float*)(uniform.second.data[2]), *(float*)(uniform.second.data[3])); break; |
65 | } |
66 | } |
67 | else if (uniform.second.type == glUniformType::INT) |
68 | { |
69 | switch (uniform.second.quantity) |
70 | { |
71 | case 1: glUniform1i(location, *(int*)(uniform.second.data[0])); break; |
72 | case 2: glUniform2i(location, *(int*)(uniform.second.data[0]), *(int*)(uniform.second.data[1])); break; |
73 | case 3: glUniform3i(location, *(int*)(uniform.second.data[0]), *(int*)(uniform.second.data[1]), *(int*)(uniform.second.data[2])); break; |
74 | case 4: glUniform4i(location, *(int*)(uniform.second.data[0]), *(int*)(uniform.second.data[1]), *(int*)(uniform.second.data[2]), *(int*)(uniform.second.data[3])); break; |
75 | } |
76 | } |
77 | else if (uniform.second.type == glUniformType::UINT) |
78 | { |
79 | switch (uniform.second.quantity) |
80 | { |
81 | case 1: glUniform1ui(location, *(unsigned int*)(uniform.second.data[0])); break; |
82 | case 2: glUniform2ui(location, *(unsigned int*)(uniform.second.data[0]), *(unsigned int*)(uniform.second.data[1])); break; |
83 | case 3: glUniform3ui(location, *(unsigned int*)(uniform.second.data[0]), *(unsigned int*)(uniform.second.data[1]), *(unsigned int*)(uniform.second.data[2])); break; |
84 | case 4: glUniform4ui(location, *(unsigned int*)(uniform.second.data[0]), *(unsigned int*)(uniform.second.data[1]), *(unsigned int*)(uniform.second.data[2]), *(unsigned int*)(uniform.second.data[3])); break; |
85 | } |
86 | } |
87 | } |
88 | } |
89 |
|
90 | std::map<uint64_t, std::list<GLRenderObject*>>& GLRenderObject::GetRenderObjects() |
91 | { |
92 | return s_renderObjects; |
93 | } |
94 |
|