| 1 | #include <list> |
| 2 |
|
| 3 | #include "ZydecoCommon.hpp" |
| 4 | #include "GLRenderObject.hpp" |
| 5 | #include "GLProgram.hpp" |
| 6 | #include "GLTexture.hpp" |
| 7 |
|
| 8 |
|
| 9 | static Logger LOGGER("GLRenderObject"); |
| 10 |
|
| 11 | // static initialize |
| 12 | std::map<uint64_t, std::list<GLRenderObject*>> GLRenderObject::s_renderObjects = {}; |
| 13 |
|
| 14 |
|
| 15 | GLRenderObject::GLRenderObject(std::string gl_program_name, uint64_t render_order): |
| 16 | m_renderOrder(render_order), |
| 17 | m_renderEnabled(true) |
| 18 | { |
| 19 | if (gl_program_name != "") |
| 20 | { |
| 21 | m_glProgram = GLProgram::GetGLProgram(gl_program_name); |
| 22 | if (m_glProgram == nullptr) { ZydecoFault("GLRenderObject(): Program '{}' is nonexistent", gl_program_name); } |
| 23 | } |
| 24 |
|
| 25 | s_renderObjects.insert(std::pair<uint64_t, std::list<GLRenderObject*>>(render_order, {})); |
| 26 | s_renderObjects.at(render_order).push_back(this); |
| 27 | } |
| 28 |
|
| 29 | GLRenderObject::~GLRenderObject() |
| 30 | { |
| 31 | s_renderObjects.at(m_renderOrder).remove(this); |
| 32 | if (s_renderObjects.at(m_renderOrder).empty()) |
| 33 | { |
| 34 | s_renderObjects.erase(m_renderOrder); |
| 35 | } |
| 36 | } |
| 37 |
|
| 38 | void GLRenderObject::RenderEnable() |
| 39 | { |
| 40 | m_renderEnabled = true; |
| 41 | } |
| 42 |
|
| 43 | void GLRenderObject::RenderDisable() |
| 44 | { |
| 45 | m_renderEnabled = false; |
| 46 | } |
| 47 |
|
| 48 | void GLRenderObject::AddTexture(uint64_t texture_unit, GLTexture *texture) |
| 49 | { |
| 50 | m_textures[texture_unit] = texture; |
| 51 | } |
| 52 |
|
| 53 | void GLRenderObject::GLTargetSetup() |
| 54 | { |
| 55 | glUseProgram(m_glProgram->GetGLProgramID()); |
| 56 |
|
| 57 | for (std::pair<std::string, glUniform> uniform : m_uniforms) |
| 58 | { |
| 59 | int location = glGetUniformLocation(m_glProgram->GetGLProgramID(), uniform.first.c_str()); |
| 60 | if (location == -1) |
| 61 | { |
| 62 | // LOGGER.Log(Logger::WARNING, "Render(): Uniform '{}' does not exist for program '{}'", uniform.first, m_glProgram->GetGLProgramName()); |
| 63 | continue; |
| 64 | } |
| 65 |
|
| 66 | if (uniform.second.type == glUniformType::FLOAT) |
| 67 | { |
| 68 | switch (uniform.second.quantity) |
| 69 | { |
| 70 | case 1: glUniform1f(location, *(float*)(uniform.second.data[0])); break; |
| 71 | case 2: glUniform2f(location, *(float*)(uniform.second.data[0]), *(float*)(uniform.second.data[1])); break; |
| 72 | case 3: glUniform3f(location, *(float*)(uniform.second.data[0]), *(float*)(uniform.second.data[1]), *(float*)(uniform.second.data[2])); break; |
| 73 | 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; |
| 74 | } |
| 75 | } |
| 76 | else if (uniform.second.type == glUniformType::DOUBLE) |
| 77 | { |
| 78 | switch (uniform.second.quantity) |
| 79 | { |
| 80 | case 1: glUniform1d(location, *(double*)(uniform.second.data[0])); break; |
| 81 | case 2: glUniform2d(location, *(double*)(uniform.second.data[0]), *(double*)(uniform.second.data[1])); break; |
| 82 | case 3: glUniform3d(location, *(double*)(uniform.second.data[0]), *(double*)(uniform.second.data[1]), *(double*)(uniform.second.data[2])); break; |
| 83 | case 4: glUniform4d(location, *(double*)(uniform.second.data[0]), *(double*)(uniform.second.data[1]), *(double*)(uniform.second.data[2]), *(double*)(uniform.second.data[3])); break; |
| 84 | } |
| 85 | } |
| 86 | else if (uniform.second.type == glUniformType::INT) |
| 87 | { |
| 88 | switch (uniform.second.quantity) |
| 89 | { |
| 90 | case 1: glUniform1i(location, *(int*)(uniform.second.data[0])); break; |
| 91 | case 2: glUniform2i(location, *(int*)(uniform.second.data[0]), *(int*)(uniform.second.data[1])); break; |
| 92 | case 3: glUniform3i(location, *(int*)(uniform.second.data[0]), *(int*)(uniform.second.data[1]), *(int*)(uniform.second.data[2])); break; |
| 93 | 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; |
| 94 | } |
| 95 | } |
| 96 | else if (uniform.second.type == glUniformType::UINT) |
| 97 | { |
| 98 | switch (uniform.second.quantity) |
| 99 | { |
| 100 | case 1: glUniform1ui(location, *(unsigned int*)(uniform.second.data[0])); break; |
| 101 | case 2: glUniform2ui(location, *(unsigned int*)(uniform.second.data[0]), *(unsigned int*)(uniform.second.data[1])); break; |
| 102 | case 3: glUniform3ui(location, *(unsigned int*)(uniform.second.data[0]), *(unsigned int*)(uniform.second.data[1]), *(unsigned int*)(uniform.second.data[2])); break; |
| 103 | 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; |
| 104 | } |
| 105 | } |
| 106 | else if (uniform.second.type == glUniformType::MAT4) |
| 107 | { |
| 108 | glUniformMatrix4fv(location, uniform.second.quantity, false, (float*)(uniform.second.data[0]) ); |
| 109 | // glUniformMatrix4fv(location, uniform.second.quantity, false, &((*(glm::mat4*)(uniform.second.data[0]))[0][0])); |
| 110 | } |
| 111 | } |
| 112 |
|
| 113 | for (std::pair<uint64_t, GLTexture*> texture : m_textures) |
| 114 | { |
| 115 | texture.second->Bind(texture.first); |
| 116 | } |
| 117 | } |
| 118 |
|
| 119 | std::map<uint64_t, std::list<GLRenderObject*>>& GLRenderObject::GetRenderObjects() |
| 120 | { |
| 121 | return s_renderObjects; |
| 122 | } |
| 123 |
|