From b23bfbdcd8f9fd2492666f2ab4936121cd7a8a81 Mon Sep 17 00:00:00 2001 From: Kienan Stewart Date: Wed, 12 Feb 2020 21:30:38 -0500 Subject: [PATCH] Use the default shader program for the triangle render --- src/client/client_imgui.cpp | 4 ++++ src/common/shaderloader.hpp | 27 +++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/client/client_imgui.cpp b/src/client/client_imgui.cpp index 834dc0c..e399384 100644 --- a/src/client/client_imgui.cpp +++ b/src/client/client_imgui.cpp @@ -129,6 +129,9 @@ int main(int argc, char** argv) // Render game geometries // @see https://blog.conan.io/2019/06/26/An-introduction-to-the-Dear-ImGui-library.html // 1st attribute buffer : vertices + GLuint p = find_shader_program_by_name("default", &shader_program_registry); + printf("Using program %d\n", p); + glUseProgram(p); glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); glVertexAttribPointer( @@ -142,6 +145,7 @@ int main(int argc, char** argv) // Draw the triangle ! glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle glDisableVertexAttribArray(0); + glUseProgram(0); // Start the Dear ImGui frame ImGui_ImplOpenGL3_NewFrame(); diff --git a/src/common/shaderloader.hpp b/src/common/shaderloader.hpp index 9ee2393..77b5cca 100644 --- a/src/common/shaderloader.hpp +++ b/src/common/shaderloader.hpp @@ -34,7 +34,7 @@ class RegisteredShaderProgram { public: RegisteredShaderProgram(char *name, GLuint programID) { int name_size = strlen(name); - char *n = (char*) calloc(name_size, sizeof(char)); + char *n = (char*) calloc(name_size + 1, sizeof(char)); strncpy(n, name, name_size); this->programName = n; this->programID = programID; @@ -42,8 +42,30 @@ public: ~RegisteredShaderProgram() { free(this->programName); } + char* get_program_name() { + return this->programName; + } + GLuint get_program_id() { + return this->programID; + } }; +GLuint find_shader_program_by_name(const char *name, std::vector *r) { + GLuint programID = 0; + for(auto i = r->begin() ; i != r->end() ; ++i) { + if (strlen(name) != strlen(i->get_program_name())) { + // String lengths aren't the same, so carry on. + continue; + } + if(strncmp(i->get_program_name(), name, strlen(name)) == 0) { + programID = i->get_program_id(); + } else { + continue; + } + } + return programID; +} + struct ShaderRegistry { struct RegisteredShader *list = 0; int size = 0; @@ -241,7 +263,8 @@ int loadShaderProgramsFromFile(const char* file_path, struct ShaderRegistry *sha // Add to program_registry if (InfoLogLength == 0) { - program_registry->push_back(RegisteredShaderProgram(name, program)); + auto p = new RegisteredShaderProgram(name, program); + program_registry->push_back(*p); } free(program_list_copy); free(line_copy);