/** * Set the material for this state. **/ static void state_do_material_activate(material_t mat) { size_t i, j; current_material = mat; if (! current_state) return; state_sync_mat_backlog(current_state); if (! current_state->shader) return; for (i = 0; i < current_state->num_materials; i++) { if (current_state->materials[i].mat != current_material && current_state->materials[i].mat != NO_MATERIAL) continue; for (j = 0; j < current_state->materials[i].num_uniforms; j++) shader_set_uniform(current_state->shader, current_state->materials[i] .uniforms[j]); } }
/** * Set a uniform that is applied when this state is entered. **/ void state_set_uniform(state_t *state, material_t mat, uniform_type_t type, ...) { size_t i; uniform_t *uniform; struct material *material; va_list ap; state_sync_mat_backlog(state); if (mat != NO_MATERIAL && ! material_is_allocd(mat)) errx(1, "Must set uniforms for NO_MATERIAL" " or a valid material"); va_start(ap, type); uniform = uniform_vcreate(type, ap); va_end(ap); for (i = 0; i < state->num_materials; i++) if (state->materials[i].mat == mat) break; if (i == state->num_materials) { state->materials = vec_expand(state->materials, state->num_materials); state->num_materials++; state->materials[i].uniforms = NULL; state->materials[i].num_uniforms = 0; state->materials[i].mat = mat; } material = &state->materials[i]; for (i = 0; i < material->num_uniforms; i++) { if (strcmp(material->uniforms[i]->name, uniform->name)) continue; uniform_ungrab(material->uniforms[i]); material->uniforms[i] = uniform; return; } material->uniforms = vec_expand(material->uniforms, material->num_uniforms); material->uniforms[material->num_uniforms++] = uniform; if (state == current_state && mat == current_material) shader_set_uniform(state->shader, uniform); }
void shader_commit_uniform(struct shader* self, GLint program) { for (int i = 0; i < BUILT_IN_UNIFORM_MAX; ++i) { shader_set_uniform(self, program, i, self->uniform_buffer_objects[i].v); } }
void render_set_unfiorm(struct render* self, const char* name, enum UNIFORM_TYPE uniform_type, float* v) { s_assert(self->cur_program); GLint location = shader_get_uniform(self->shader, self->cur_program, name); shader_set_uniform(self->shader, location, uniform_type, v); }