]> git.tdb.fi Git - libs/gl.git/commitdiff
Add a new transform API to Renderer.
authorMikko Rasa <tdb@tdb.fi>
Thu, 13 Nov 2014 21:43:05 +0000 (23:43 +0200)
committerMikko Rasa <tdb@tdb.fi>
Thu, 13 Nov 2014 21:43:05 +0000 (23:43 +0200)
Accessing the matrix stack directly is unsafe as it allows the caller to
push and pop on it independently of Renderer states.

demos/cubemap.cpp
demos/shaders.cpp
source/animatedobject.cpp
source/renderer.cpp
source/renderer.h

index 4679590d3e65057076e5d8d7b0e749611c2f40ee..2a0bc02c6d1ffe83313837e7ff1778c78a160854 100644 (file)
@@ -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);
                        }
                }
index 5a268fec0bc94b581d61d5cb2fc9636bc2a751ee..6b7336fe6c50307f28459add241df02eaa844c43 100644 (file)
@@ -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);
                        }
                }
index 4be198a87fec6739957642e485db91baee036263..3a1d9d836ffea98989b79936f87a8ebe8034e20b 100644 (file)
@@ -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);
 }
index 184d326928381722e5766eed46e030b572278771..c558757f885654e0646f6392d9866b4bf52b205e 100644 (file)
@@ -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;
index 10d1fb3f13c0f2c2f8d9b539a5b90d724d72b506..18d3a41a7bf49dcf19737ce53a01f5a08cfc93e3 100644 (file)
@@ -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 *);