Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

Diff Revisions 167 vs 168 for /trunk/Source Code/Hardware/Video/OpenGL/gluOverrides.cpp

Diff revisions: vs.
  @@ -10,186 +10,252 @@
10 10
11 11 namespace Divide {
12 12 namespace GL {
13 - /*-----------Context Management----*/
14 - bool _applicationClosing = false;
15 - bool _contextAvailable = false;
16 - bool _useDebugOutputCallback = false;
17 - GLFWwindow* _mainWindow = NULL;
18 - GLFWwindow* _loaderWindow = NULL;
19 - /*----------- GLU overrides ------*/
20 - ///Matrix management
21 - MATRIX_MODE _currentMatrixMode;
22 - matrixStack _viewMatrix;
23 - matrixStack _projectionMatrix;
24 - matrixStack _textureMatrix;
25 - glm::mat4 _MVPCachedMatrix;
26 - glm::mat4 _MVCachedMatrix;
27 - glm::mat4 _identityMatrix;
28 - glm::mat4 _biasMatrix;
29 - glm::mat4 _modelMatrix;
30 - vector3Stack _currentEyeVector;
31 - vector3Stack _currentLookAtVector;
32 -
33 - bool _MDirty = true;
34 - bool _VDirty = true;
35 - bool _PDirty = true;
36 - bool _isUniformScaled = true;
37 - bool _resetModelMatrixFlag = true;
38 -
39 - void _matrixMode(const MATRIX_MODE& mode) {
40 - _currentMatrixMode = mode;
41 - }
42 -
43 - void _initStacks(){
44 - _currentEyeVector.push(glm::vec3());
45 - _currentLookAtVector.push(glm::vec3());
46 - _projectionMatrix.push(_identityMatrix);
47 - _viewMatrix.push(_identityMatrix);
48 - _textureMatrix.push(_identityMatrix);
49 - _biasMatrix = glm::mat4(0.5, 0.0, 0.0, 0.0,
50 - 0.0, 0.5, 0.0, 0.0,
51 - 0.0, 0.0, 0.5, 0.0,
52 - 0.5, 0.5, 0.5, 1.0);
53 - }
54 -
55 - void _LookAt(const glm::vec3& eye,const glm::vec3& lookAt,const glm::vec3& up, bool invertx, bool inverty){
56 - _matrixMode(VIEW_MATRIX);
57 - _currentEyeVector.top() = eye;
58 - _currentLookAtVector.top() = lookAt;
59 - _viewMatrix.top() = glm::lookAt(_currentEyeVector.top(),_currentLookAtVector.top(),up);
60 - if(invertx) glm::scale(_viewMatrix.top(),glm::vec3(-1,1,1));
61 - GL_API::getInstance().updateViewMatrix();
62 - _VDirty = true;
63 - }
64 -
65 - void _ortho(GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat zNear, GLfloat zFar){
66 - _matrixMode(PROJECTION_MATRIX);
67 - _projectionMatrix.top() = glm::ortho(left,right,bottom,top,zNear,zFar);
68 - GL_API::getInstance().updateProjectionMatrix();
69 - _PDirty = true;
70 - }
71 -
72 - void _perspective(GLfloat fovy, GLfloat aspect, GLfloat zNear, GLfloat zFar){
73 - _matrixMode(PROJECTION_MATRIX);
74 - _projectionMatrix.top() = glm::perspective(fovy,aspect,zNear,zFar);
75 - GL_API::getInstance().updateProjectionMatrix();
76 - _PDirty = true;
77 - }
78 -
79 - void _resetModelMatrix(bool force){
80 - _resetModelMatrixFlag = !force;
13 + ///Used for anaglyph rendering
14 + struct CameraFrustum {
15 + GLdouble leftfrustum;
16 + GLdouble rightfrustum;
17 + GLdouble bottomfrustum;
18 + GLdouble topfrustum;
19 + GLfloat modeltranslation;
20 + } _leftCam, _rightCam;
21 +
22 + /*-----------Object Management----*/
23 + GLuint _invalidObjectID = std::numeric_limits<U32>::max();
24 +
25 + /*-----------Context Management----*/
26 + bool _applicationClosing = false;
27 + bool _contextAvailable = false;
28 + bool _useDebugOutputCallback = false;
29 + GLFWwindow* _mainWindow = NULL;
30 + GLFWwindow* _loaderWindow = NULL;
31 +
32 + /*----------- GLU overrides ------*/
33 + ///Matrix management
34 + MATRIX_MODE _currentMatrixMode;
35 + matrixStack _viewMatrix;
36 + matrixStack _projectionMatrix;
37 + matrixStack _textureMatrix;
38 + viewportStack _viewport;
39 + glm::mat4 _MVPCachedMatrix;
40 + glm::mat4 _MVCachedMatrix;
41 + glm::mat4 _identityMatrix;
42 + glm::mat4 _biasMatrix;
43 + glm::mat4 _modelMatrix;
44 + vector3Stack _currentViewDirection;
45 +
46 + bool _MDirty = true;
47 + bool _VDirty = true;
48 + bool _PDirty = true;
49 + bool _isUniformScaled = true;
50 + bool _resetModelMatrixFlag = true;
51 + GLfloat _anaglyphIOD = -0.01f;
52 +
53 + void _initStacks(){
54 + _currentViewDirection.push(vec3<F32>());
55 + _projectionMatrix.push(_identityMatrix);
56 + _viewMatrix.push(_identityMatrix);
57 + _textureMatrix.push(_identityMatrix);
58 + _viewport.push(vec4<U32>(0,0,0,0));
59 + _biasMatrix = glm::mat4(0.5, 0.0, 0.0, 0.0,
60 + 0.0, 0.5, 0.0, 0.0,
61 + 0.0, 0.0, 0.5, 0.0,
62 + 0.5, 0.5, 0.5, 1.0);
63 + }
81 64
82 - if(force){
83 - _modelMatrix = _identityMatrix;
84 - _isUniformScaled = _MDirty = true;
65 + void _LookAt(const GLfloat* viewMatrix, const vec3<F32>& viewDirection){
66 + _matrixMode(VIEW_MATRIX);
67 + _currentViewDirection.top() = viewDirection;
68 + _viewMatrix.top() = glm::make_mat4(viewMatrix);
69 + GL_API::getInstance().updateViewMatrix();
70 + _VDirty = true;
85 71 }
86 - }
87 72
88 - void _setModelMatrix(const mat4<F32>& matrix,bool uniform){
89 - _modelMatrix = glm::make_mat4(matrix.mat);
90 - _isUniformScaled = uniform;
91 - _resetModelMatrixFlag = false;
92 - _MDirty = true;
93 - }
94 -
95 - void _pushMatrix(){
96 - if(_currentMatrixMode == PROJECTION_MATRIX) _projectionMatrix.push(_projectionMatrix.top());
97 - else if(_currentMatrixMode == VIEW_MATRIX){
98 - _viewMatrix.push(_viewMatrix.top());
99 - _currentEyeVector.push(_currentEyeVector.top());
100 - _currentLookAtVector.push(_currentLookAtVector.top());
101 - }
102 - else if(_currentMatrixMode == TEXTURE_MATRIX) _textureMatrix.push(_textureMatrix.top());
103 - else assert(_currentMatrixMode == -1);
104 - }
105 -
106 - void _popMatrix(){
107 - if(_currentMatrixMode == PROJECTION_MATRIX){
108 - _projectionMatrix.pop();
73 + void _ortho(GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat zNear, GLfloat zFar){
74 + _matrixMode(PROJECTION_MATRIX);
75 + _projectionMatrix.top() = glm::ortho(left,right,bottom,top,zNear,zFar);
76 + GL_API::getInstance().updateProjectionMatrix();
109 77 _PDirty = true;
78 + }
79 +
80 + void _perspective(GLfloat fovy, GLfloat aspect, GLfloat zNear, GLfloat zFar){
81 + _matrixMode(PROJECTION_MATRIX);
82 + _projectionMatrix.top() = glm::perspective(fovy,aspect,zNear,zFar);
110 83 GL_API::getInstance().updateProjectionMatrix();
111 - }else if(_currentMatrixMode == VIEW_MATRIX){
112 - _viewMatrix.pop();
113 - _currentEyeVector.pop();
114 - _currentLookAtVector.pop();
115 - GL_API::getInstance().updateViewMatrix();
116 - _VDirty = true;
117 - }else if(_currentMatrixMode == TEXTURE_MATRIX){
118 - _textureMatrix.pop();
119 - }else{
120 - assert(_currentMatrixMode == -1);
121 - }
122 - }
123 -
124 - void _loadIdentity(){
125 - if(_currentMatrixMode == PROJECTION_MATRIX){
126 - _projectionMatrix.top() = _identityMatrix;
127 84 _PDirty = true;
85 + }
86 +
87 + void _anaglyph(GLfloat IOD, GLdouble zNear, GLdouble zFar, GLfloat aspect, GLfloat fovy, bool rightFrustum){
88 + if(!FLOAT_COMPARE(_anaglyphIOD,IOD)){
89 + static const GLdouble DTR = 0.0174532925;
90 + static const GLdouble screenZ = 10.0;
91 +
92 + //sets top of frustum based on fovy and near clipping plane
93 + GLdouble top = zNear*tan(DTR*fovy/2);
94 + GLdouble right = aspect*top;
95 + //sets right of frustum based on aspect ratio
96 + GLdouble frustumshift = (IOD/2)*zNear/screenZ;
97 +
98 + _leftCam.topfrustum = top;
99 + _leftCam.bottomfrustum = -top;
100 + _leftCam.leftfrustum = -right + frustumshift;
101 + _leftCam.rightfrustum = right + frustumshift;
102 + _leftCam.modeltranslation = IOD/2;
103 +
104 + _rightCam.topfrustum = top;
105 + _rightCam.bottomfrustum = -top;
106 + _rightCam.leftfrustum = -right - frustumshift;
107 + _rightCam.rightfrustum = right - frustumshift;
108 + _rightCam.modeltranslation = -IOD/2;
109 +
110 + _anaglyphIOD = IOD;
111 + }
112 +
113 + _matrixMode(PROJECTION_MATRIX);
114 +
115 + CameraFrustum& tempCamera = rightFrustum ? _rightCam : _leftCam;
116 +
117 + _projectionMatrix.top() = glm::frustum(tempCamera.leftfrustum,
118 + tempCamera.rightfrustum,
119 + tempCamera.bottomfrustum,
120 + tempCamera.topfrustum,
121 + zNear,
122 + zFar);
123 + //translate to cancel parallax
124 + glm::translate(_projectionMatrix.top(), glm::vec3(tempCamera.modeltranslation, 0.0, 0.0));
125 +
128 126 GL_API::getInstance().updateProjectionMatrix();
129 - }else if(_currentMatrixMode == VIEW_MATRIX){
130 - _viewMatrix.top() = _identityMatrix;
131 - _currentEyeVector.top() = glm::vec3();
132 - _currentLookAtVector.top() = glm::vec3();
133 - GL_API::getInstance().updateViewMatrix();
134 - _VDirty = true;
135 - }else if(_currentMatrixMode == TEXTURE_MATRIX){
136 - _textureMatrix.top() = _identityMatrix;
137 - }else{
138 - assert(_currentMatrixMode == -1);
139 - }
140 - }
141 -
142 - void _queryMatrix(const MATRIX_MODE& mode, mat4<GLfloat>& mat){
143 - if(mode == VIEW_MATRIX) mat.set(glm::value_ptr(_viewMatrix.top()));
144 - else if(mode == PROJECTION_MATRIX) mat.set(glm::value_ptr(_projectionMatrix.top()));
145 - else if(mode == TEXTURE_MATRIX) mat.set(glm::value_ptr(_textureMatrix.top()));
146 - else assert(mode == -1);
147 - }
148 -
149 - void _queryMatrix(const EXTENDED_MATRIX& mode, mat4<GLfloat>& mat){
150 - assert(mode != NORMAL_MATRIX /*|| mode != ... */);
151 -
152 - switch(mode){
153 - case MODEL_MATRIX:{ //check if we need to reset our model matrix
154 - if(_resetModelMatrixFlag) _resetModelMatrix(true);
155 - mat.set(glm::value_ptr(_modelMatrix));
156 - }break;
157 - case MV_MATRIX:{
158 - _clean(); //refresh cache
159 - mat.set(glm::value_ptr(_MVCachedMatrix));
160 - }break;
161 - case MV_INV_MATRIX:{
162 - _clean(); //refresh cache
163 - mat.set(glm::value_ptr(glm::inverse(_MVCachedMatrix)));
164 - }break;
165 - case MVP_MATRIX:{
166 - _clean(); //refresh cache
167 - mat.set(glm::value_ptr(_MVPCachedMatrix));
168 - }break;
169 - case BIAS_MATRIX:
170 - mat.set(glm::value_ptr(_biasMatrix));
171 - break;
172 - };
173 - }
174 -
175 - void _queryMatrix(const EXTENDED_MATRIX& mode, mat3<GLfloat>& mat){
176 - assert(mode == NORMAL_MATRIX /*|| mode == ... */);
177 - _clean();
178 - // Normal Matrix
179 - _isUniformScaled ? mat.set(glm::value_ptr(glm::mat3(_MVCachedMatrix))) :
180 - mat.set(glm::value_ptr(glm::inverseTranspose(glm::mat3(_MVCachedMatrix))));
181 - }
127 + _PDirty = true;
128 +
129 + _matrixMode(VIEW_MATRIX);
130 + }
182 131
183 - void _clean(){
184 - if(_resetModelMatrixFlag) _resetModelMatrix(true);
132 + void _resetModelMatrix(bool force){
133 + _resetModelMatrixFlag = !force;
185 134
186 - bool MVDirty = (_MDirty || _VDirty);
135 + if(!force)
136 + return;
137 +
138 + _modelMatrix = _identityMatrix;
139 + _isUniformScaled = _MDirty = true;
140 + }
187 141
188 - if(MVDirty) _MVCachedMatrix = _viewMatrix.top() * _modelMatrix;
189 - if(_PDirty || MVDirty) _MVPCachedMatrix = _projectionMatrix.top() * _MVCachedMatrix;
142 + void _setModelMatrix(const mat4<F32>& matrix, bool uniform){
143 + _modelMatrix = glm::make_mat4(matrix.mat);
144 + _isUniformScaled = uniform;
145 + _resetModelMatrixFlag = false;
146 + _MDirty = true;
147 + }
148 +
149 + void _pushMatrix(){
150 + if(_currentMatrixMode == PROJECTION_MATRIX) {
151 + _projectionMatrix.push(_projectionMatrix.top());
152 + }else if(_currentMatrixMode == VIEW_MATRIX){
153 + _viewMatrix.push(_viewMatrix.top());
154 + _currentViewDirection.push(_currentViewDirection.top());
155 + }else if(_currentMatrixMode == TEXTURE_MATRIX) {
156 + _textureMatrix.push(_textureMatrix.top());
157 + }else {
158 + assert(_currentMatrixMode == -1);
159 + }
160 + }
161 +
162 + void _popMatrix(){
163 + if(_currentMatrixMode == PROJECTION_MATRIX){
164 + _projectionMatrix.pop();
165 + _PDirty = true;
166 + GL_API::getInstance().updateProjectionMatrix();
167 + }else if(_currentMatrixMode == VIEW_MATRIX){
168 + _viewMatrix.pop();
169 + _currentViewDirection.pop();
170 + GL_API::getInstance().updateViewMatrix();
171 + _VDirty = true;
172 + }else if(_currentMatrixMode == TEXTURE_MATRIX){
173 + _textureMatrix.pop();
174 + }else{
175 + assert(_currentMatrixMode == -1);
176 + }
177 + }
178 +
179 + void _loadIdentity(){
180 + if(_currentMatrixMode == PROJECTION_MATRIX){
181 + _projectionMatrix.top() = _identityMatrix;
182 + _PDirty = true;
183 + GL_API::getInstance().updateProjectionMatrix();
184 + }else if(_currentMatrixMode == VIEW_MATRIX){
185 + _viewMatrix.top() = _identityMatrix;
186 + _currentViewDirection.top() = vec3<F32>();
187 + GL_API::getInstance().updateViewMatrix();
188 + _VDirty = true;
189 + }else if(_currentMatrixMode == TEXTURE_MATRIX){
190 + _textureMatrix.top() = _identityMatrix;
191 + }else{
192 + assert(_currentMatrixMode == -1);
193 + }
194 + }
195 +
196 + void _queryMatrix(const MATRIX_MODE& mode, mat4<GLfloat>& mat){
197 + if(mode == VIEW_MATRIX)
198 + mat.set(glm::value_ptr(_viewMatrix.top()));
199 + else if(mode == PROJECTION_MATRIX)
200 + mat.set(glm::value_ptr(_projectionMatrix.top()));
201 + else if(mode == TEXTURE_MATRIX)
202 + mat.set(glm::value_ptr(_textureMatrix.top()));
203 + else
204 + assert(mode == -1);
205 + }
206 +
207 + void _queryMatrix(const EXTENDED_MATRIX& mode, mat4<GLfloat>& mat){
208 + assert(mode != NORMAL_MATRIX /*|| mode != ... */);
209 + const GLfloat* matrixValue = NULL;
210 +
211 + switch(mode){
212 + case MODEL_MATRIX:{ //check if we need to reset our model matrix
213 + if(_resetModelMatrixFlag)
214 + _resetModelMatrix(true);
215 + matrixValue = glm::value_ptr(_modelMatrix);
216 + }break;
217 + case MV_MATRIX:{
218 + _clean(); //refresh cache
219 + matrixValue = glm::value_ptr(_MVCachedMatrix);
220 + }break;
221 + case MV_INV_MATRIX:{
222 + _clean(); //refresh cache
223 + matrixValue = glm::value_ptr(glm::inverse(_MVCachedMatrix));
224 + }break;
225 + case MVP_MATRIX:{
226 + _clean(); //refresh cache
227 + matrixValue = glm::value_ptr(_MVPCachedMatrix);
228 + }break;
229 + case BIAS_MATRIX:
230 + matrixValue = glm::value_ptr(_biasMatrix);
231 + break;
232 + };
233 +
234 + assert(matrixValue != NULL);
235 + mat.set(matrixValue);
236 + }
237 +
238 + void _queryMatrix(const EXTENDED_MATRIX& mode, mat3<GLfloat>& mat){
239 + assert(mode == NORMAL_MATRIX /*|| mode == ... */);
240 + _clean();
241 + // Normal Matrix
242 + _isUniformScaled ? mat.set(glm::value_ptr(glm::mat3(_MVCachedMatrix))) :
243 + mat.set(glm::value_ptr(glm::inverseTranspose(glm::mat3(_MVCachedMatrix))));
244 + }
245 +
246 + void _clean(){
247 + if(_resetModelMatrixFlag)
248 + _resetModelMatrix(true);
249 +
250 + bool MVDirty = (_MDirty || _VDirty);
251 +
252 + if(MVDirty)
253 + _MVCachedMatrix = _viewMatrix.top() * _modelMatrix;
254 + if(_PDirty || MVDirty)
255 + _MVPCachedMatrix = _projectionMatrix.top() * _MVCachedMatrix;
190 256
191 257
192 - _MDirty = _VDirty = _PDirty = false;
193 - }
194 - }//namespace GL
258 + _MDirty = _VDirty = _PDirty = false;
259 + }
260 + }//namespace GL
195 261 }// namespace Divide