Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "vgui_matrix_state.h"
00010 #include <vcl_cstdlib.h>
00011 #include <vcl_iostream.h>
00012 #include <vnl/vnl_matrix_fixed.h>
00013 #include <vnl/vnl_matlab_print.h>
00014 #include <vgui/vgui_gl.h>
00015
00016
00017 vgui_matrix_state::vgui_matrix_state(bool save_now_restore_on_destroy)
00018 {
00019 restore_on_destroy = save_now_restore_on_destroy;
00020 if (save_now_restore_on_destroy)
00021 save();
00022 }
00023
00024 vgui_matrix_state::~vgui_matrix_state()
00025 {
00026 if (restore_on_destroy)
00027 restore();
00028 }
00029
00030 void vgui_matrix_state::save()
00031 {
00032 glGetDoublev(GL_PROJECTION_MATRIX,P);
00033 glGetDoublev(GL_MODELVIEW_MATRIX, M);
00034 }
00035
00036 void vgui_matrix_state::restore() const
00037 {
00038 glMatrixMode(GL_PROJECTION);
00039 glLoadMatrixd(P);
00040
00041 glMatrixMode(GL_MODELVIEW);
00042 glLoadMatrixd(M);
00043 }
00044
00045
00046 void vgui_matrix_state::print(vcl_ostream &s)
00047 {
00048 vnl_matlab_print_format_push(vnl_matlab_print_format_long);
00049 vnl_matlab_print(s, projection_matrix(), "GL_PROJECTION_MATRIX");
00050 vnl_matlab_print(s, modelview_matrix (), "GL_MODELVIEW_MATRIX" );
00051 vnl_matlab_print_format_pop();
00052 s << vcl_endl;
00053 }
00054
00055
00056 void vgui_matrix_state::identity_gl_matrices()
00057 {
00058 glMatrixMode(GL_PROJECTION);
00059 glLoadIdentity();
00060
00061 glMatrixMode(GL_MODELVIEW);
00062 glLoadIdentity();
00063 }
00064
00065 void vgui_matrix_state::clear_gl_matrices()
00066 {
00067 vnl_matrix_fixed<double,4,4> empty(0.0);
00068
00069
00070 glMatrixMode(GL_PROJECTION);
00071 glLoadMatrixd(empty.data_block());
00072
00073
00074 glMatrixMode(GL_MODELVIEW);
00075 glLoadMatrixd(empty.data_block());
00076 }
00077
00078 bool vgui_matrix_state::gl_matrices_are_cleared()
00079 {
00080 vnl_matrix_fixed<double,4,4> empty(0.0);
00081 vnl_matrix_fixed<double,4,4> Pt;
00082 vnl_matrix_fixed<double,4,4> Mt;
00083
00084 glGetDoublev(GL_PROJECTION_MATRIX,Pt.data_block());
00085 glGetDoublev(GL_MODELVIEW_MATRIX,Mt.data_block());
00086
00087 return Pt == empty && Mt == empty;
00088 }
00089
00090
00091 vnl_matrix_fixed<double,4,4> vgui_matrix_state::projection_matrix()
00092 {
00093 vnl_matrix_fixed<double,4,4> P;
00094 glGetDoublev(GL_PROJECTION_MATRIX,P.data_block());
00095 P.inplace_transpose();
00096
00097 return P;
00098 }
00099
00100 vnl_matrix_fixed<double,4,4> vgui_matrix_state::modelview_matrix()
00101 {
00102 vnl_matrix_fixed<double,4,4> M;
00103 glGetDoublev(GL_MODELVIEW_MATRIX,M.data_block());
00104 M.inplace_transpose();
00105
00106 return M;
00107 }
00108
00109 vnl_matrix_fixed<double,4,4> vgui_matrix_state::total_transformation()
00110 {
00111 return projection_matrix()*modelview_matrix();
00112 }
00113
00114
00115 void vgui_matrix_state::premultiply(vnl_matrix_fixed<double,4,4> const& M,GLenum matrix)
00116 {
00117
00118 vnl_matrix_fixed<double,4,4> P;
00119 if (matrix==GL_PROJECTION || matrix==GL_PROJECTION_MATRIX)
00120 glGetDoublev(GL_PROJECTION_MATRIX,P.data_block());
00121 else if (matrix==GL_MODELVIEW || matrix==GL_MODELVIEW_MATRIX )
00122 glGetDoublev(GL_MODELVIEW_MATRIX,P.data_block());
00123 else
00124 vcl_abort();
00125 P.inplace_transpose();
00126
00127
00128 if (matrix==GL_PROJECTION || matrix==GL_PROJECTION_MATRIX)
00129 glMatrixMode(GL_PROJECTION);
00130 else if (matrix==GL_MODELVIEW || matrix==GL_MODELVIEW_MATRIX )
00131 glMatrixMode(GL_MODELVIEW);
00132 glLoadMatrixd( (M*P).transpose().data_block() );
00133 }
00134
00135
00136
00137 void vgui_matrix_state::postmultiply(vnl_matrix_fixed<double,4,4> const& M,GLenum matrix)
00138 {
00139
00140 if (matrix==GL_PROJECTION || matrix==GL_PROJECTION_MATRIX)
00141 glMatrixMode(GL_PROJECTION);
00142 else if (matrix==GL_MODELVIEW || matrix==GL_MODELVIEW_MATRIX )
00143 glMatrixMode(GL_MODELVIEW);
00144
00145 glMultMatrixd( M.transpose().data_block() );
00146 }