Commit 963a6859 authored by Jan Möbius's avatar Jan Möbius
Browse files

Merge branch 'StateMatrixUpdate' into 'master'

avoid expensive matrix update

profiling showed that update_matrices has a huge impact on performance. It's better to avoid updating the forward and backward transformations and computing them on the fly instead.

See merge request !30
parents 9fce2fcf a6b11fa8
Pipeline #74 skipped
...@@ -340,8 +340,6 @@ void GLState::reset_projection() ...@@ -340,8 +340,6 @@ void GLState::reset_projection()
glLoadIdentity(); glLoadIdentity();
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
} }
update_matrices();
} }
...@@ -360,8 +358,6 @@ void GLState::set_projection(const GLMatrixd& _m, const GLMatrixd& _inv_m) ...@@ -360,8 +358,6 @@ void GLState::set_projection(const GLMatrixd& _m, const GLMatrixd& _inv_m)
glLoadMatrixd(projection_.get_raw_data()); glLoadMatrixd(projection_.get_raw_data());
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
} }
update_matrices();
} }
...@@ -378,8 +374,6 @@ void GLState::reset_modelview() ...@@ -378,8 +374,6 @@ void GLState::reset_modelview()
makeCurrent(); makeCurrent();
glLoadIdentity(); glLoadIdentity();
} }
update_matrices();
} }
...@@ -396,8 +390,6 @@ void GLState::set_modelview(const GLMatrixd& _m, const GLMatrixd& _inv_m) ...@@ -396,8 +390,6 @@ void GLState::set_modelview(const GLMatrixd& _m, const GLMatrixd& _inv_m)
makeCurrent(); makeCurrent();
glLoadMatrixd(modelview_.get_raw_data()); glLoadMatrixd(modelview_.get_raw_data());
} }
update_matrices();
} }
...@@ -421,8 +413,6 @@ void GLState::ortho( double _left, double _right, ...@@ -421,8 +413,6 @@ void GLState::ortho( double _left, double _right,
glOrtho(_left, _right, _bottom, _top, _n, _f); glOrtho(_left, _right, _bottom, _top, _n, _f);
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
} }
update_matrices();
} }
...@@ -446,8 +436,6 @@ void GLState::frustum( double _left, double _right, ...@@ -446,8 +436,6 @@ void GLState::frustum( double _left, double _right,
glFrustum(_left, _right, _bottom, _top, _n, _f); glFrustum(_left, _right, _bottom, _top, _n, _f);
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
} }
update_matrices();
} }
...@@ -470,8 +458,6 @@ void GLState::perspective( double _fovY, double _aspect, ...@@ -470,8 +458,6 @@ void GLState::perspective( double _fovY, double _aspect,
glLoadMatrixd(projection_.data()); glLoadMatrixd(projection_.data());
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
} }
update_matrices();
} }
...@@ -517,8 +503,6 @@ void GLState::viewport( int _left, int _bottom, ...@@ -517,8 +503,6 @@ void GLState::viewport( int _left, int _bottom,
makeCurrent(); makeCurrent();
glViewport(_left, _bottom, _width, _height); glViewport(_left, _bottom, _width, _height);
} }
update_matrices();
} }
...@@ -537,8 +521,6 @@ void GLState::lookAt( const Vec3d& _eye, ...@@ -537,8 +521,6 @@ void GLState::lookAt( const Vec3d& _eye,
makeCurrent(); makeCurrent();
glLoadMatrixd(modelview_.data()); glLoadMatrixd(modelview_.data());
} }
update_matrices();
} }
...@@ -564,8 +546,6 @@ void GLState::translate( double _x, double _y, double _z, ...@@ -564,8 +546,6 @@ void GLState::translate( double _x, double _y, double _z,
makeCurrent(); makeCurrent();
glLoadMatrixd(modelview_.get_raw_data()); glLoadMatrixd(modelview_.get_raw_data());
} }
update_matrices();
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
...@@ -597,8 +577,6 @@ void GLState::rotate( double _angle, double _x, double _y, double _z, ...@@ -597,8 +577,6 @@ void GLState::rotate( double _angle, double _x, double _y, double _z,
makeCurrent(); makeCurrent();
glLoadMatrixd(modelview_.get_raw_data()); glLoadMatrixd(modelview_.get_raw_data());
} }
update_matrices();
} }
...@@ -624,8 +602,6 @@ void GLState::scale( double _sx, double _sy, double _sz, ...@@ -624,8 +602,6 @@ void GLState::scale( double _sx, double _sy, double _sz,
makeCurrent(); makeCurrent();
glLoadMatrixd(modelview_.get_raw_data()); glLoadMatrixd(modelview_.get_raw_data());
} }
update_matrices();
} }
...@@ -651,34 +627,18 @@ void GLState::mult_matrix( const GLMatrixd& _m, const GLMatrixd& _inv_m, ...@@ -651,34 +627,18 @@ void GLState::mult_matrix( const GLMatrixd& _m, const GLMatrixd& _inv_m,
makeCurrent(); makeCurrent();
glLoadMatrixd(modelview_.get_raw_data()); glLoadMatrixd(modelview_.get_raw_data());
} }
update_matrices();
} }
//-----------------------------------------------------------------------------
void GLState::update_matrices(bool _changedModelView,
bool _changedProjection,
bool _changedViewport)
{
forward_projection_ = window2viewport_;
forward_projection_ *= projection_;
forward_projection_ *= modelview_;
backward_projection_ = inverse_modelview_;
backward_projection_ *= inverse_projection_;
backward_projection_ *= inverse_window2viewport_;
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
Vec3d GLState::project(const Vec3d& _point) const Vec3d GLState::project(const Vec3d& _point) const
{ {
return forward_projection_.transform_point(_point); Vec3d t = modelview_.transform_point(_point);
t = projection_.transform_point(t);
return window2viewport_.transform_point(t);
} }
...@@ -687,7 +647,9 @@ Vec3d GLState::project(const Vec3d& _point) const ...@@ -687,7 +647,9 @@ Vec3d GLState::project(const Vec3d& _point) const
Vec3d GLState::unproject(const Vec3d& _winPoint) const Vec3d GLState::unproject(const Vec3d& _winPoint) const
{ {
return backward_projection_.transform_point(_winPoint); Vec3d t = inverse_window2viewport_.transform_point(_winPoint);
t = inverse_projection_.transform_point(t);
return inverse_modelview_.transform_point(t);
} }
...@@ -1027,8 +989,6 @@ void GLState::pop_projection_matrix() ...@@ -1027,8 +989,6 @@ void GLState::pop_projection_matrix()
stack_projection_.pop(); stack_projection_.pop();
stack_inverse_projection_.pop(); stack_inverse_projection_.pop();
update_matrices();
if (updateGL_ && compatibilityProfile_) if (updateGL_ && compatibilityProfile_)
{ {
makeCurrent(); makeCurrent();
...@@ -1047,8 +1007,6 @@ void GLState::push_modelview_matrix() ...@@ -1047,8 +1007,6 @@ void GLState::push_modelview_matrix()
stack_modelview_.push(modelview_); stack_modelview_.push(modelview_);
stack_inverse_modelview_.push(inverse_modelview_); stack_inverse_modelview_.push(inverse_modelview_);
update_matrices();
if (updateGL_ && compatibilityProfile_) if (updateGL_ && compatibilityProfile_)
{ {
makeCurrent(); makeCurrent();
...@@ -1068,8 +1026,6 @@ void GLState::pop_modelview_matrix() ...@@ -1068,8 +1026,6 @@ void GLState::pop_modelview_matrix()
stack_modelview_.pop(); stack_modelview_.pop();
stack_inverse_modelview_.pop(); stack_inverse_modelview_.pop();
update_matrices();
if (updateGL_ && compatibilityProfile_) if (updateGL_ && compatibilityProfile_)
{ {
makeCurrent(); makeCurrent();
......
...@@ -801,8 +801,8 @@ public: ...@@ -801,8 +801,8 @@ public:
} }
/// get forward projection matrix /// get forward projection matrix
const GLMatrixd& forward_projection() const { GLMatrixd forward_projection() const {
return forward_projection_; return window2viewport_ * projection_ * modelview_;
} }
/// get inverse projection matrix /// get inverse projection matrix
...@@ -1148,12 +1148,6 @@ public: ...@@ -1148,12 +1148,6 @@ public:
private: //-------------------------------------------------------------------- private: //--------------------------------------------------------------------
// update forward projection and backward projection matrices
void update_matrices(bool _changedModelView = true,
bool _changedProjection = true,
bool _changedViewport = true);
// matrix stacks // matrix stacks
std::stack<GLMatrixd> stack_projection_, std::stack<GLMatrixd> stack_projection_,
stack_modelview_, stack_modelview_,
...@@ -1166,9 +1160,7 @@ private: //-------------------------------------------------------------------- ...@@ -1166,9 +1160,7 @@ private: //--------------------------------------------------------------------
modelview_, modelview_,
inverse_modelview_, inverse_modelview_,
window2viewport_, window2viewport_,
inverse_window2viewport_, inverse_window2viewport_;
forward_projection_,
backward_projection_;
// viewport // viewport
int left_, bottom_, width_, height_; int left_, bottom_, width_, height_;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment