Use the default shader program for the triangle render

This commit is contained in:
Kienan Stewart 2020-02-12 21:30:38 -05:00
parent 30b00fc2c8
commit b23bfbdcd8
2 changed files with 29 additions and 2 deletions

View File

@ -129,6 +129,9 @@ int main(int argc, char** argv)
// Render game geometries // Render game geometries
// @see https://blog.conan.io/2019/06/26/An-introduction-to-the-Dear-ImGui-library.html // @see https://blog.conan.io/2019/06/26/An-introduction-to-the-Dear-ImGui-library.html
// 1st attribute buffer : vertices // 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); glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer( glVertexAttribPointer(
@ -142,6 +145,7 @@ int main(int argc, char** argv)
// Draw the triangle ! // Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle
glDisableVertexAttribArray(0); glDisableVertexAttribArray(0);
glUseProgram(0);
// Start the Dear ImGui frame // Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplOpenGL3_NewFrame();

View File

@ -34,7 +34,7 @@ class RegisteredShaderProgram {
public: public:
RegisteredShaderProgram(char *name, GLuint programID) { RegisteredShaderProgram(char *name, GLuint programID) {
int name_size = strlen(name); 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); strncpy(n, name, name_size);
this->programName = n; this->programName = n;
this->programID = programID; this->programID = programID;
@ -42,8 +42,30 @@ public:
~RegisteredShaderProgram() { ~RegisteredShaderProgram() {
free(this->programName); 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<RegisteredShaderProgram> *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 ShaderRegistry {
struct RegisteredShader *list = 0; struct RegisteredShader *list = 0;
int size = 0; int size = 0;
@ -241,7 +263,8 @@ int loadShaderProgramsFromFile(const char* file_path, struct ShaderRegistry *sha
// Add to program_registry // Add to program_registry
if (InfoLogLength == 0) { 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(program_list_copy);
free(line_copy); free(line_copy);