Change Encyclo button name and macros icon
[ryzomcore.git] / nel / src / 3d / driver / opengl / driver_opengl_matrix.cpp
blob392aa3f5d95156cc0cb49bbb3464b4a74ed231d6
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "stdopengl.h"
18 #include "driver_opengl.h"
20 #ifdef DEBUG_NEW
21 #define new DEBUG_NEW
22 #endif
24 namespace NL3D {
26 #ifdef NL_STATIC
27 #ifdef USE_OPENGLES
28 namespace NLDRIVERGLES {
29 #else
30 namespace NLDRIVERGL {
31 #endif
32 #endif
34 // ***************************************************************************
35 void CDriverGL::setFrustum(float left, float right, float bottom, float top, float znear, float zfar, bool perspective)
37 H_AUTO_OGL(CDriverGL_setFrustum);
39 glMatrixMode(GL_PROJECTION);
40 glLoadIdentity();
42 if (perspective)
44 glFrustum(left,right,bottom,top,znear,zfar);
46 else
48 glOrtho(left,right,bottom,top,znear,zfar);
51 _ProjMatDirty = true;
53 // Backup znear and zfar for zbias setup
54 _OODeltaZ = 1 / (zfar - znear);
56 glMatrixMode(GL_MODELVIEW);
59 // ***************************************************************************
61 void CDriverGL::setFrustumMatrix(CMatrix &frustumMatrix)
63 H_AUTO_OGL(CDriverGL_setFrustum)
64 glMatrixMode(GL_PROJECTION);
66 glLoadMatrixf(((GLfloat*)frustumMatrix.get()));
68 glMatrixMode(GL_MODELVIEW);
71 // ***************************************************************************
73 CMatrix CDriverGL::getFrustumMatrix()
75 H_AUTO_OGL(CDriverGL_getFrustum)
77 glMatrixMode(GL_PROJECTION);
79 CMatrix frustumMatrix;
80 float frustum[16];
81 glGetFloatv(GL_PROJECTION_MATRIX, ((GLfloat*)&frustum));
82 frustumMatrix.set(frustum);
84 glMatrixMode(GL_MODELVIEW);
86 return frustumMatrix;
89 // ***************************************************************************
90 void CDriverGL::setupViewMatrixEx(const CMatrix& mtx, const CVector &cameraPos)
92 H_AUTO_OGL(CDriverGL_setupViewMatrixEx)
93 _UserViewMtx= mtx;
95 // Setup the matrix to transform the CScene basis in openGL basis.
96 CMatrix changeBasis;
97 CVector I(1,0,0);
98 CVector J(0,0,-1);
99 CVector K(0,1,0);
101 changeBasis.identity();
102 changeBasis.setRot(I,J,K, true);
103 _ViewMtx=changeBasis*mtx;
104 // Reset the viewMtx position.
105 _ViewMtx.setPos(CVector::Null);
106 _PZBCameraPos= cameraPos;
108 // Anything that depend on the view martix must be updated.
109 _LightSetupDirty= true;
110 _ModelViewMatrixDirty= true;
111 _RenderSetupDirty= true;
112 // All lights must be refresh.
113 for(uint i=0;i<MaxLight;i++)
114 _LightDirty[i]= true;
116 _SpecularTexMtx = _ViewMtx;
117 _SpecularTexMtx.setPos(CVector(0.0f,0.0f,0.0f));
118 _SpecularTexMtx.invert();
119 _SpecularTexMtx = changeBasis * _SpecularTexMtx;
123 // ***************************************************************************
124 void CDriverGL::setupViewMatrix(const CMatrix& mtx)
126 H_AUTO_OGL(CDriverGL_setupViewMatrix)
127 _UserViewMtx= mtx;
129 // Setup the matrix to transform the CScene basis in openGL basis.
130 CMatrix changeBasis;
131 CVector I(1,0,0);
132 CVector J(0,0,-1);
133 CVector K(0,1,0);
135 changeBasis.identity();
136 changeBasis.setRot(I,J,K, true);
137 _ViewMtx=changeBasis*mtx;
138 // Just set the PZBCameraPos to 0.
139 _PZBCameraPos= CVector::Null;
141 // Anything that depend on the view martix must be updated.
142 _LightSetupDirty= true;
143 _ModelViewMatrixDirty= true;
144 _RenderSetupDirty= true;
145 // All lights must be refresh.
146 for(uint i=0;i<MaxLight;i++)
147 _LightDirty[i]= true;
149 _SpecularTexMtx = _ViewMtx;
150 _SpecularTexMtx.setPos(CVector(0.0f,0.0f,0.0f));
151 _SpecularTexMtx.invert();
152 _SpecularTexMtx = changeBasis * _SpecularTexMtx;
156 // ***************************************************************************
157 CMatrix CDriverGL::getViewMatrix(void) const
159 H_AUTO_OGL(CDriverGL_getViewMatrix)
160 return _UserViewMtx;
163 // ***************************************************************************
164 void CDriverGL::setupModelMatrix(const CMatrix& mtx)
166 H_AUTO_OGL(CDriverGL_setupModelMatrix)
167 // profiling
168 _NbSetupModelMatrixCall++;
171 // Dirt flags.
172 _ModelViewMatrixDirty= true;
173 _RenderSetupDirty= true;
176 // Put the matrix in the opengl eye space, and store it.
177 CMatrix mat= mtx;
178 // remove first the _PZBCameraPos
179 mat.setPos(mtx.getPos() - _PZBCameraPos);
180 _ModelViewMatrix= _ViewMtx*mat;
183 // ***************************************************************************
184 void CDriverGL::doRefreshRenderSetup()
186 H_AUTO_OGL(CDriverGL_doRefreshRenderSetup)
187 // Check if the light setup has been modified first
188 if (_LightSetupDirty)
189 // Recompute light setup
190 cleanLightSetup ();
192 // Check light setup is good
193 nlassert (_LightSetupDirty==false);
196 // Check if must update the modelViewMatrix
197 if( _ModelViewMatrixDirty )
199 // By default, the first model matrix is active
200 glLoadMatrixf( _ModelViewMatrix.get() );
201 // enable normalize if matrix has scale.
202 enableGlNormalize( _ModelViewMatrix.hasScalePart() || _ForceNormalize );
203 // clear.
204 _ModelViewMatrixDirty= false;
207 // render setup is cleaned.
208 _RenderSetupDirty= false;
211 #ifdef NL_STATIC
212 } // NLDRIVERGL/ES
213 #endif
215 } // NL3D