From: Mikko Rasa Date: Thu, 13 Nov 2014 21:43:05 +0000 (+0200) Subject: Add a new transform API to Renderer. X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=6db6b7e49848e6bbc14f3ec8113ee5a941e4af07;p=libs%2Fgl.git Add a new transform API to Renderer. Accessing the matrix stack directly is unsafe as it allows the caller to push and pop on it independently of Renderer states. --- diff --git a/demos/cubemap.cpp b/demos/cubemap.cpp index 4679590d..2a0bc02c 100644 --- a/demos/cubemap.cpp +++ b/demos/cubemap.cpp @@ -116,19 +116,19 @@ int main() renderer.set_material(&material); renderer.set_shader_program(&shprog, &shdata); renderer.set_texture(&texture); - renderer.matrix_stack() *= GL::Matrix::translation(0, 0, -7); + renderer.transform(GL::Matrix::translation(0, 0, -7)); { GL::Renderer::Push _push(renderer); - renderer.matrix_stack() *= GL::Matrix::translation(-2, 0, 0); - renderer.matrix_stack() *= GL::Matrix::rotation(angle/2.3, 0, 1, 0); - renderer.matrix_stack() *= GL::Matrix::rotation(angle, 1, 0.25, 0); + renderer.transform(GL::Matrix::translation(-2, 0, 0)); + renderer.transform(GL::Matrix::rotation(angle/2.3, 0, 1, 0)); + renderer.transform(GL::Matrix::rotation(angle, 1, 0.25, 0)); box.draw(renderer); } { GL::Renderer::Push _push(renderer); - renderer.matrix_stack() *= GL::Matrix::translation(2, 0, 0); - renderer.matrix_stack() *= GL::Matrix::rotation(-angle/2.3, 0, 1, 0); - renderer.matrix_stack() *= GL::Matrix::rotation(angle, 1, 0.25, 0); + renderer.transform(GL::Matrix::translation(2, 0, 0)); + renderer.transform(GL::Matrix::rotation(-angle/2.3, 0, 1, 0)); + renderer.transform(GL::Matrix::rotation(angle, 1, 0.25, 0)); cylinder.draw(renderer); } } diff --git a/demos/shaders.cpp b/demos/shaders.cpp index 5a268fec..6b7336fe 100644 --- a/demos/shaders.cpp +++ b/demos/shaders.cpp @@ -109,10 +109,10 @@ int main() renderer.add_shader_data(progdata); for(unsigned i=0; i<12; ++i) { - GL::MatrixStack::Push push(renderer.matrix_stack()); + GL::Renderer::Push push(renderer); 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); + renderer.transform(GL::Matrix::translation(-3.3+(i%4)*2.2, 0, -3.5+(i/4)*3.0)); + renderer.transform(GL::Matrix::rotation(angle, 0, 0, 1)); mesh.draw(renderer); } } diff --git a/source/animatedobject.cpp b/source/animatedobject.cpp index 4be198a8..3a1d9d83 100644 --- a/source/animatedobject.cpp +++ b/source/animatedobject.cpp @@ -43,7 +43,7 @@ void AnimatedObject::set_pose_matrix(unsigned link, const Matrix &m) void AnimatedObject::setup_render(Renderer &renderer, const Tag &) const { - renderer.matrix_stack() *= matrix; + renderer.transform(matrix); if(shdata) renderer.add_shader_data(*shdata); } diff --git a/source/renderer.cpp b/source/renderer.cpp index 184d3269..c558757f 100644 --- a/source/renderer.cpp +++ b/source/renderer.cpp @@ -61,6 +61,16 @@ void Renderer::begin(const Camera *c) } } +void Renderer::set_matrix(const Matrix &matrix) +{ + mtx_stack.load(matrix); +} + +void Renderer::transform(const Matrix &matrix) +{ + mtx_stack *= matrix; +} + void Renderer::set_texture(const Texture *t) { state->texture = t; diff --git a/source/renderer.h b/source/renderer.h index 10d1fb3f..18d3a41a 100644 --- a/source/renderer.h +++ b/source/renderer.h @@ -118,8 +118,18 @@ public: times without an intervening end(). */ void begin(const Camera *); + /** Deprecated as unsafe. Use set_matrix() or transform() instead. */ MatrixStack &matrix_stack() { return mtx_stack; } + /** Replaces the Renderer's modelview matrix. */ + void set_matrix(const Matrix &); + + /** Applies a transform to the Renderer's modelview matrix. */ + void transform(const Matrix &); + + /** Returns the current modelview matrix. */ + const Matrix &get_matrix() const { return mtx_stack.top(); } + const Camera *get_camera() const { return camera; } void set_texture(const Texture *);