From: Mikko Rasa Date: Tue, 28 Aug 2012 07:39:19 +0000 (+0300) Subject: Tweak the shader functions in Renderer X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=0c731643d6363eb4c492e836ffb919cb7c0a3035;p=libs%2Fgl.git Tweak the shader functions in Renderer Since ProgramData is no longer tied to a single Program, we can retain it over program changes. This opens up some new possibilities, in particular for Effects. Also rename set_shader to set_shader_program. --- diff --git a/demos/cubemap.cpp b/demos/cubemap.cpp index 1778fb7f..98d0ba24 100644 --- a/demos/cubemap.cpp +++ b/demos/cubemap.cpp @@ -112,7 +112,7 @@ int main() GL::Bind bind_depth(GL::DepthTest::lequal()); GL::Renderer renderer(0); renderer.set_material(&material); - renderer.set_shader(&shprog, &shdata); + renderer.set_shader_program(&shprog, &shdata); renderer.set_texture(&texture); renderer.matrix_stack() *= GL::Matrix::translation(0, 0, -7); { diff --git a/demos/shaders.cpp b/demos/shaders.cpp index ae5c98fb..27538e4e 100644 --- a/demos/shaders.cpp +++ b/demos/shaders.cpp @@ -112,10 +112,11 @@ int main() GL::Renderer renderer(0); renderer.set_material(&mat); renderer.set_texturing(&texturing); + renderer.add_shader_data(progdata); for(unsigned i=0; i<12; ++i) { GL::MatrixStack::Push push(renderer.matrix_stack()); - renderer.set_shader(programs[i], &progdata); + renderer.set_shader_program(programs[i]); renderer.matrix_stack() *= GL::Matrix::translation(-3.3+(i%4)*2.2, 0, -3.5+(i/4)*3.0); renderer.matrix_stack() *= GL::Matrix::rotation(angle, 0, 0, 1); mesh.draw(renderer); diff --git a/source/animatedobject.cpp b/source/animatedobject.cpp index eec54aa7..63a25112 100644 --- a/source/animatedobject.cpp +++ b/source/animatedobject.cpp @@ -45,7 +45,7 @@ void AnimatedObject::setup_render(Renderer &renderer, const Tag &) const { renderer.matrix_stack() *= matrix; if(shdata) - renderer.add_shader_data(shdata); + renderer.add_shader_data(*shdata); } } // namespace GL diff --git a/source/object.cpp b/source/object.cpp index 06062fc0..964099e5 100644 --- a/source/object.cpp +++ b/source/object.cpp @@ -77,7 +77,7 @@ void Object::render(Renderer &renderer, const Tag &tag) const return; Renderer::Push push(renderer); - renderer.set_shader(pass->get_shader_program(), pass->get_shader_data()); + renderer.set_shader_program(pass->get_shader_program(), pass->get_shader_data()); renderer.set_material(pass->get_material()); renderer.set_texturing(pass->get_texturing()); @@ -91,7 +91,7 @@ void Object::render(Renderer &renderer, const ObjectInstance &inst, const Tag &t return; Renderer::Push push(renderer); - renderer.set_shader(pass->get_shader_program(), pass->get_shader_data()); + renderer.set_shader_program(pass->get_shader_program(), pass->get_shader_data()); renderer.set_material(pass->get_material()); renderer.set_texturing(pass->get_texturing()); diff --git a/source/renderer.cpp b/source/renderer.cpp index ee845e08..c751b0e3 100644 --- a/source/renderer.cpp +++ b/source/renderer.cpp @@ -68,22 +68,17 @@ void Renderer::set_material(const Material *m) state->material = m; } -void Renderer::set_shader(const Program *p, const ProgramData *d) +void Renderer::set_shader_program(const Program *p, const ProgramData *d) { state->shprog = p; - if(d) - state->shdata.assign(1, d); - else - state->shdata.clear(); + if(p && d) + add_shader_data(*d); shdata_changed = true; } -void Renderer::add_shader_data(const ProgramData *d) +void Renderer::add_shader_data(const ProgramData &d) { - if(!state->shprog) - throw invalid_operation("Renderer::add_shader_data"); - - state->shdata.push_back(d); + state->shdata.push_back(&d); shdata_changed = true; } diff --git a/source/renderer.h b/source/renderer.h index dd54ca16..5536702b 100644 --- a/source/renderer.h +++ b/source/renderer.h @@ -85,8 +85,16 @@ public: void set_texture(const Texture *); void set_texturing(const Texturing *); void set_material(const Material *); - void set_shader(const Program *, const ProgramData *); - void add_shader_data(const ProgramData *); + + /** Sets the shader program to use. An initial set of data can be set as + well, with the same semantics as add_shader_data. */ + void set_shader_program(const Program *prog, const ProgramData *data = 0); + + /** Adds another set of data to be use with shader programs. The data is + independent of any shader program changes and remains in effect until the + Renderer state is popped. */ + void add_shader_data(const ProgramData &data); + void set_vertex_array(const VertexArray *); void set_element_buffer(const Buffer *); void set_winding_test(const WindingTest *);