1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
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.
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"
28 namespace NLDRIVERGLES
{
30 namespace NLDRIVERGL
{
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
);
44 glFrustum(left
,right
,bottom
,top
,znear
,zfar
);
48 glOrtho(left
,right
,bottom
,top
,znear
,zfar
);
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
;
81 glGetFloatv(GL_PROJECTION_MATRIX
, ((GLfloat
*)&frustum
));
82 frustumMatrix
.set(frustum
);
84 glMatrixMode(GL_MODELVIEW
);
89 // ***************************************************************************
90 void CDriverGL::setupViewMatrixEx(const CMatrix
& mtx
, const CVector
&cameraPos
)
92 H_AUTO_OGL(CDriverGL_setupViewMatrixEx
)
95 // Setup the matrix to transform the CScene basis in openGL basis.
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
)
129 // Setup the matrix to transform the CScene basis in openGL basis.
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
)
163 // ***************************************************************************
164 void CDriverGL::setupModelMatrix(const CMatrix
& mtx
)
166 H_AUTO_OGL(CDriverGL_setupModelMatrix
)
168 _NbSetupModelMatrixCall
++;
172 _ModelViewMatrixDirty
= true;
173 _RenderSetupDirty
= true;
176 // Put the matrix in the opengl eye space, and store it.
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
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
);
204 _ModelViewMatrixDirty
= false;
207 // render setup is cleaned.
208 _RenderSetupDirty
= false;