Shader::Shader(const std::string &vertexPath, const std::string &fragmentPath) { GLuint vshader = compile_shader(GL_VERTEX_SHADER, read_text_file(vertexPath)); GLuint fshader = compile_shader(GL_FRAGMENT_SHADER, read_text_file(fragmentPath)); GLuint shader = link_shader(vshader, fshader); glDeleteShader(vshader); glDeleteShader(fshader); this->program = shader; }
static bool load_shaders_file(char const *file, uint32_t *dst) { uint32_t shaders[g_shader_t.length]; int tmp; bool success; t_list lines; if ((tmp = open(file, O_RDONLY)) < 0) return (false); ft_bzero(shaders, sizeof(uint32_t[g_shader_t.length])); lines = LIST(t_sub); success = read_shader(tmp, &lines, shaders, g_shader_t.all); close(tmp); ft_listremove_next(&lines, LIST_IT(&lines), -1); success = success && link_shader(shaders, dst); tmp = -1; while (++tmp < g_shader_t.length) if (shaders[tmp] > 0) glDeleteShader(shaders[tmp]); return (success); }
Material * getDefaultMaterial(void) { Material *ret = alloc(1, Material); #include "shader.h" ret->vsh = compile_shader(vertex_shader, GL_VERTEX_SHADER); ret->fsh = compile_shader(fragment_shader, GL_FRAGMENT_SHADER); assert(ret->vsh != 0); assert(ret->fsh != 0); ret->program = link_shader(ret->vsh, ret->fsh); assert(ret->program != 0); for(const char *s : { "position", "colour"}) { BindPoint bp = { .name = strdup(s), .location = glGetAttribLocation(ret->program, s), .type = 0, }; ret->attributes.push_back(bp); } for(const char *s : { "projection", "modelview"}) { BindPoint bp = { .name = strdup(s), .location = glGetUniformLocation(ret->program, s), .type = 0, }; ret->uniforms.push_back(bp); } return ret; } void dumpMaterial(Material *mat) { std::for_each(mat->attributes.begin(), mat->attributes.end(), [](BindPoint &bp){ printf("Attribute : %s\n", bp.name); printf(" Location: %d\n", bp.location); printf(" Type : %d\n", bp.type); }); std::for_each(mat->uniforms.begin(), mat->uniforms.end(), [](BindPoint &bp){ printf("Uniform : %s\n", bp.name); printf(" Location: %d\n", bp.location); printf(" Type : %d\n", bp.type); }); } BindPoint * getAttribute(Material *m, const char *name) { for(auto attr = m->attributes.begin(); attr != m->attributes.end(); ++attr) { if (strcmp(attr->name, name) == 0) return &(*attr); } return NULL; } BindPoint * getUniform(Material *m, const char *name) { for(auto uni = m->uniforms.begin(); uni != m->uniforms.end(); ++uni) { if (strcmp(uni->name, name) == 0) return &(*uni); } return NULL; }