Visualization Library v1.0.3A lightweight C++ OpenGL middleware for 2D/3D graphics |
[Download] [Tutorials] [All Classes] [Grouped Classes] |
00001 /**************************************************************************************/ 00002 /* */ 00003 /* Visualization Library */ 00004 /* http://visualizationlibrary.org */ 00005 /* */ 00006 /* Copyright (c) 2005-2011, Michele Bosi */ 00007 /* All rights reserved. */ 00008 /* */ 00009 /* Redistribution and use in source and binary forms, with or without modification, */ 00010 /* are permitted provided that the following conditions are met: */ 00011 /* */ 00012 /* - Redistributions of source code must retain the above copyright notice, this */ 00013 /* list of conditions and the following disclaimer. */ 00014 /* */ 00015 /* - Redistributions in binary form must reproduce the above copyright notice, this */ 00016 /* list of conditions and the following disclaimer in the documentation and/or */ 00017 /* other materials provided with the distribution. */ 00018 /* */ 00019 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */ 00020 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */ 00021 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */ 00022 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */ 00023 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */ 00024 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */ 00025 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */ 00026 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */ 00027 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */ 00028 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ 00029 /* */ 00030 /**************************************************************************************/ 00031 00032 #include <vlGraphics/ProjViewTransfCallback.hpp> 00033 #include <vlCore/Transform.hpp> 00034 #include <vlGraphics/Camera.hpp> 00035 #include <vlGraphics/GLSL.hpp> 00036 00037 using namespace vl; 00038 00039 //------------------------------------------------------------------------------ 00040 // ProjViewTransfCallbackStandard 00041 //------------------------------------------------------------------------------ 00042 void ProjViewTransfCallback::updateMatrices(bool cam_changed, bool transf_changed, const GLSLProgram* glsl_program, const Camera* camera, const Transform* transform) 00043 { 00044 // Once you opt-in for using VL substitutes for matrix variables you should not use the GL fixed-function ones such as: 00045 // gl_ModelViewMatrix, gl_ProjectionMatrix, gl_ModelViewProjectionMatrix and gl_NormalMatrix 00046 // see http://www.opengl.org/registry/doc/GLSLangSpec.Full.1.10.59.pdf pag 45 00047 00048 // Also, don't use the fixed function pipeline if we don't have it! 00049 00050 bool use_fixed_function_matrices = Has_Fixed_Function_Pipeline; 00051 00052 // projection matrix 00053 if ( cam_changed ) 00054 { 00055 if ( glsl_program && glsl_program->vl_ProjectionMatrix() != -1 ) 00056 { 00057 use_fixed_function_matrices = false; 00058 #if VL_PIPELINE_PRECISION == 1 00059 glUniformMatrix4fv( glsl_program->vl_ProjectionMatrix(), 1, GL_FALSE, camera->projectionMatrix().ptr() ); VL_CHECK_OGL(); 00060 #elif VL_PIPELINE_PRECISION == 2 00061 glUniformMatrix4fv( glsl_program->vl_ProjectionMatrix(), 1, GL_FALSE, ((fmat4)camera->projectionMatrix()).ptr() ); VL_CHECK_OGL(); 00062 #endif 00063 } 00064 00065 if ( use_fixed_function_matrices ) 00066 { 00067 // this updates 00068 glMatrixMode(GL_PROJECTION); VL_CHECK_OGL(); 00069 VL_glLoadMatrix( camera->projectionMatrix().ptr() ); VL_CHECK_OGL(); 00070 } 00071 } 00072 00073 // model + view transform 00074 if ( cam_changed || transf_changed ) 00075 { 00076 // compute the modelview matrix to send to GL 00077 mat4 modelview; 00078 if ( transform ) 00079 modelview = camera->viewMatrix() * transform->worldMatrix(); 00080 else 00081 modelview = camera->viewMatrix(); 00082 00083 if ( glsl_program ) 00084 { 00085 // update vl_ModelViewMatrix if used 00086 if ( glsl_program->vl_ModelViewMatrix() != -1 ) 00087 { 00088 use_fixed_function_matrices = false; 00089 #if VL_PIPELINE_PRECISION == 1 00090 glUniformMatrix4fv( glsl_program->vl_ModelViewMatrix(), 1, GL_FALSE, modelview.ptr() ); VL_CHECK_OGL(); 00091 #elif VL_PIPELINE_PRECISION == 2 00092 glUniformMatrix4fv( glsl_program->vl_ModelViewMatrix(), 1, GL_FALSE, ((fmat4)modelview).ptr() ); VL_CHECK_OGL(); 00093 #endif 00094 } 00095 00096 // update vl_ModelViewProjectionMatrix if used 00097 if ( glsl_program->vl_ModelViewProjectionMatrix() != -1 ) 00098 { 00099 use_fixed_function_matrices = false; 00100 #if VL_PIPELINE_PRECISION == 1 00101 glUniformMatrix4fv( glsl_program->vl_ModelViewProjectionMatrix(), 1, GL_FALSE, (camera->projectionMatrix() * modelview).ptr() ); VL_CHECK_OGL(); 00102 #elif VL_PIPELINE_PRECISION == 2 00103 glUniformMatrix4fv( glsl_program->vl_ModelViewProjectionMatrix(), 1, GL_FALSE, ((fmat4)(camera->projectionMatrix() * modelview)).ptr() ); VL_CHECK_OGL(); 00104 #endif 00105 } 00106 00107 // update vl_NormalMatrix if used 00108 if ( glsl_program->vl_NormalMatrix() != -1 ) 00109 { 00110 use_fixed_function_matrices = false; 00111 // transpose of the inverse of the upper leftmost 3x3 of vl_ModelViewMatrix 00112 mat4 normalmtx = modelview.as3x3(); 00113 normalmtx.invert(); 00114 normalmtx.transpose(); 00115 #if VL_PIPELINE_PRECISION == 1 00116 glUniformMatrix4fv( glsl_program->vl_NormalMatrix(), 1, GL_FALSE, normalmtx.ptr() ); VL_CHECK_OGL(); 00117 #elif VL_PIPELINE_PRECISION == 2 00118 glUniformMatrix4fv( glsl_program->vl_NormalMatrix(), 1, GL_FALSE, ((fmat4)normalmtx).ptr() ); VL_CHECK_OGL(); 00119 #endif 00120 } 00121 } 00122 00123 if( use_fixed_function_matrices ) 00124 { 00125 glMatrixMode(GL_MODELVIEW); VL_CHECK_OGL(); 00126 VL_glLoadMatrix( modelview.ptr() ); VL_CHECK_OGL(); 00127 } 00128 } 00129 } 00130 //------------------------------------------------------------------------------