Visualization Library 2.0.0-b3

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
ProjViewTransfCallback.cpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2011, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
33 #include <vlCore/Transform.hpp>
34 #include <vlGraphics/Camera.hpp>
35 #include <vlGraphics/GLSL.hpp>
36 
37 using namespace vl;
38 
39 //------------------------------------------------------------------------------
40 // ProjViewTransfCallbackStandard
41 //------------------------------------------------------------------------------
42 void ProjViewTransfCallback::updateMatrices(bool cam_changed, bool transf_changed, const GLSLProgram* glsl_program, const Camera* camera, const Transform* transform)
43 {
44  VL_CHECK_OGL();
45  // Once you opt-in for using VL substitutes for matrix variables you should not use the GL fixed-function ones such as:
46  // gl_ModelViewMatrix, gl_ProjectionMatrix, gl_ModelViewProjectionMatrix and gl_NormalMatrix
47  // see http://www.opengl.org/registry/doc/GLSLangSpec.Full.1.10.59.pdf pag 45
48 
49  // projection matrix
50  if ( cam_changed )
51  {
52  if ( glsl_program && glsl_program->vl_ProjectionMatrix() != -1 )
53  {
54 #if VL_PIPELINE_PRECISION == 1
55  glUniformMatrix4fv( glsl_program->vl_ProjectionMatrix(), 1, GL_FALSE, camera->projectionMatrix().ptr() ); VL_CHECK_OGL();
56 #elif VL_PIPELINE_PRECISION == 2
57  glUniformMatrix4fv( glsl_program->vl_ProjectionMatrix(), 1, GL_FALSE, ((fmat4)camera->projectionMatrix()).ptr() ); VL_CHECK_OGL();
58 #endif
59  }
60 
62  {
63  // this updates
64  glMatrixMode( GL_PROJECTION ); VL_CHECK_OGL();
65  VL_glLoadMatrix( camera->projectionMatrix().ptr() ); VL_CHECK_OGL();
66  }
67  }
68 
69  // model + view transform
70  if ( cam_changed || transf_changed )
71  {
72  // compute the modelview matrix to send to GL
73  mat4 modelview;
74  if ( transform )
75  modelview = camera->viewMatrix() * transform->worldMatrix();
76  else
77  modelview = camera->viewMatrix();
78 
79  if ( glsl_program )
80  {
81  // update vl_ModelViewMatrix if used
82  if ( glsl_program->vl_ModelViewMatrix() != -1 )
83  {
84 #if VL_PIPELINE_PRECISION == 1
85  glUniformMatrix4fv( glsl_program->vl_ModelViewMatrix(), 1, GL_FALSE, modelview.ptr() ); VL_CHECK_OGL();
86 #elif VL_PIPELINE_PRECISION == 2
87  glUniformMatrix4fv( glsl_program->vl_ModelViewMatrix(), 1, GL_FALSE, ((fmat4)modelview).ptr() ); VL_CHECK_OGL();
88 #endif
89  }
90 
91  // update vl_ModelViewProjectionMatrix if used
92  if ( glsl_program->vl_ModelViewProjectionMatrix() != -1 )
93  {
94 #if VL_PIPELINE_PRECISION == 1
95  glUniformMatrix4fv( glsl_program->vl_ModelViewProjectionMatrix(), 1, GL_FALSE, (camera->projectionMatrix() * modelview).ptr() ); VL_CHECK_OGL();
96 #elif VL_PIPELINE_PRECISION == 2
97  glUniformMatrix4fv( glsl_program->vl_ModelViewProjectionMatrix(), 1, GL_FALSE, ((fmat4)(camera->projectionMatrix() * modelview)).ptr() ); VL_CHECK_OGL();
98 #endif
99  }
100 
101  // update vl_NormalMatrix if used
102  if ( glsl_program->vl_NormalMatrix() != -1 )
103  {
104  // transpose of the inverse of the upper leftmost 3x3 of vl_ModelViewMatrix
105  mat3 normalmtx = modelview.get3x3().invert().transpose();
106 #if VL_PIPELINE_PRECISION == 1
107  glUniformMatrix3fv( glsl_program->vl_NormalMatrix(), 1, GL_FALSE, normalmtx.ptr() ); VL_CHECK_OGL();
108 #elif VL_PIPELINE_PRECISION == 2
109  glUniformMatrix3fv( glsl_program->vl_NormalMatrix(), 1, GL_FALSE, ((fmat4)normalmtx).ptr() ); VL_CHECK_OGL();
110 #endif
111  }
112 
113  // update vl_WorldMatrix if used
114  if ( transf_changed && glsl_program->vl_WorldMatrix() != - 1 ) {
115  mat4 world_matrix = transform ? transform->worldMatrix() : mat4::getIdentity();
116  #if VL_PIPELINE_PRECISION == 1
117  glUniformMatrix4fv( glsl_program->vl_WorldMatrix(), 1, GL_FALSE, world_matrix.ptr() ); VL_CHECK_OGL();
118  #elif VL_PIPELINE_PRECISION == 2
119  glUniformMatrix4fv( glsl_program->vl_WorldMatrix(), 1, GL_FALSE, ((fmat4)world_matrix).ptr() ); VL_CHECK_OGL();
120  #endif
121  }
122  }
123 
125  {
126  glMatrixMode( GL_MODELVIEW ); VL_CHECK_OGL();
127  VL_glLoadMatrix( modelview.ptr() ); VL_CHECK_OGL();
128  }
129  }
130 }
131 //------------------------------------------------------------------------------
const mat4 & viewMatrix() const
Returns the Camera&#39;s view matrix (inverse of the modeling matrix).
Definition: Camera.hpp:161
Implements a 4x4 matrix transform used to define the position and orientation of an Actor...
Definition: Transform.hpp:72
int vl_ProjectionMatrix() const
Returns the binding location of the vl_ProjectionMatrix uniform variable or -1 if no such variable is...
Definition: GLSL.hpp:499
Wraps a GLSL program to which you can bind vertex, fragment and geometry shaders. ...
Definition: GLSL.hpp:233
static Matrix4 getIdentity()
Definition: Matrix4.hpp:419
Visualization Library main namespace.
int vl_WorldMatrix() const
Returns the binding location of the vl_WorldMatrix uniform variable or -1 if no such variable is used...
Definition: GLSL.hpp:490
The Matrix3 class is a template class that implements a generic 3x3 matrix, see also vl::dmat3...
Definition: Matrix3.hpp:48
int vl_ModelViewMatrix() const
Returns the binding location of the vl_ModelViewMatrix uniform variable or -1 if no such variable is ...
Definition: GLSL.hpp:494
Matrix3< T_Scalar > get3x3() const
Definition: Matrix4.hpp:323
T_Scalar * ptr()
Definition: Matrix3.hpp:301
int vl_NormalMatrix() const
Returns the binding location of the vl_NormalMatrix uniform variable or -1 if no such variable is use...
Definition: GLSL.hpp:507
const mat4 & worldMatrix() const
Returns the world matrix used for rendering.
Definition: Transform.hpp:168
#define VL_CHECK_OGL()
Definition: OpenGL.hpp:156
virtual void updateMatrices(bool cam_changed, bool transf_changed, const GLSLProgram *glsl_program, const Camera *camera, const Transform *transform)
Update matrices of the current GLSLProgram, if glsl_program == NULL then fixed function pipeline is a...
T_Scalar * ptr()
Definition: Matrix4.hpp:340
const mat4 & projectionMatrix() const
The Camera&#39;s projection matrix.
Definition: Camera.hpp:175
bool Has_Fixed_Function_Pipeline
OpenGL: true if !Is_OpenGL_Forward_Compatible && !Is_OpenGL_Core_Profile OpenGL ES 1: always true Ope...
Definition: OpenGL.cpp:63
Matrix4< float > fmat4
A 4x4 matrix using float precision.
Definition: Matrix4.hpp:1229
Represents a virtual camera defining, among other things, the point of view from which scenes can be ...
Definition: Camera.hpp:50
int vl_ModelViewProjectionMatrix() const
Returns the binding location of the vl_ModelViewProjectionMatrix uniform variable or -1 if no such va...
Definition: GLSL.hpp:502