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