GLC_lib  2.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
glc_context.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 
3  This file is part of the GLC-lib library.
4  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
5  http://glc-lib.sourceforge.net
6 
7  GLC-lib is free software; you can redistribute it and/or modify
8  it under the terms of the GNU Lesser General Public License as published by
9  the Free Software Foundation; either version 3 of the License, or
10  (at your option) any later version.
11 
12  GLC-lib is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with GLC-lib; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21  *****************************************************************************/
23 
24 #include "glc_context.h"
25 #include "glc_contextmanager.h"
26 #include "shading/glc_shader.h"
27 
28 #include "glc_state.h"
29 
31 
32 GLC_Context::GLC_Context(const QGLFormat& format)
33 : QGLContext(format)
34 , m_CurrentMatrixMode()
35 , m_MatrixStackHash()
36 , m_ContextSharedData()
37 , m_UniformShaderData()
38 , m_LightingIsEnable()
39 {
40  qDebug() << "GLC_Context::GLC_Context";
42  init();
43 }
44 
46 {
47  qDebug() << "GLC_Context::~GLC_Context()";
49  QHash<GLenum, QStack<GLC_Matrix4x4>* >::iterator iStack= m_MatrixStackHash.begin();
50  while (iStack != m_MatrixStackHash.end())
51  {
52  delete iStack.value();
53  ++iStack;
54  }
55 }
56 
57 
59 // Get Functions
61 
63 {
64  return m_pCurrentContext;
65 }
66 
68 // OpenGL Functions
70 void GLC_Context::glcMatrixMode(GLenum mode)
71 {
72  Q_ASSERT(QGLContext::isValid());
73  Q_ASSERT((mode == GL_MODELVIEW) || (mode == GL_PROJECTION));
74 
75  m_CurrentMatrixMode= mode;
76 #ifdef GLC_OPENGL_ES_2
77 
78 #else
79  glMatrixMode(m_CurrentMatrixMode);
80 #endif
81 
82 }
83 
85 {
86  Q_ASSERT(QGLContext::isValid());
87  m_MatrixStackHash.value(m_CurrentMatrixMode)->top().setToIdentity();
88 
89 #ifdef GLC_OPENGL_ES_2
90  m_UniformShaderData.setModelViewProjectionMatrix(m_MatrixStackHash.value(GL_MODELVIEW)->top(), m_MatrixStackHash.value(GL_PROJECTION)->top());
91 #else
93  {
94  m_UniformShaderData.setModelViewProjectionMatrix(m_MatrixStackHash.value(GL_MODELVIEW)->top(), m_MatrixStackHash.value(GL_PROJECTION)->top());
95  }
96  glLoadIdentity();
97 #endif
98 
99 }
100 
102 {
103  Q_ASSERT(QGLContext::isValid());
105 
106 #ifndef GLC_OPENGL_ES_2
107  glPushMatrix();
108 #endif
109 
110 }
111 
113 {
114  Q_ASSERT(QGLContext::isValid());
116 
117 #ifdef GLC_OPENGL_ES_2
119 #else
121  {
123  }
124  glPopMatrix();
125 #endif
126 
127 }
128 
129 
131 {
132  m_MatrixStackHash.value(m_CurrentMatrixMode)->top()= matrix;
133 
134 #ifdef GLC_OPENGL_ES_2
135  m_UniformShaderData.setModelViewProjectionMatrix(m_MatrixStackHash.value(GL_MODELVIEW)->top(), m_MatrixStackHash.value(GL_PROJECTION)->top());
136 #else
138  {
139  m_UniformShaderData.setModelViewProjectionMatrix(m_MatrixStackHash.value(GL_MODELVIEW)->top(), m_MatrixStackHash.value(GL_PROJECTION)->top());
140  }
141  ::glLoadMatrixd(matrix.getData());
142 #endif
143 
144 }
145 
147 {
149  m_MatrixStackHash.value(m_CurrentMatrixMode)->top()= m_MatrixStackHash.value(m_CurrentMatrixMode)->top() * matrix;
150 #ifdef GLC_OPENGL_ES_2
151  m_UniformShaderData.setModelViewProjectionMatrix(m_MatrixStackHash.value(GL_MODELVIEW)->top(), m_MatrixStackHash.value(GL_PROJECTION)->top());
152 #else
154  {
155  m_UniformShaderData.setModelViewProjectionMatrix(m_MatrixStackHash.value(GL_MODELVIEW)->top(), m_MatrixStackHash.value(GL_PROJECTION)->top());
156  }
157  ::glMultMatrixd(matrix.getData());
158 #endif
159 
160 }
161 
162 void GLC_Context::glcScaled(double x, double y, double z)
163 {
164  GLC_Matrix4x4 scale;
165  scale.setMatScaling(x, y, z);
166  glcMultMatrix(scale);
167 }
168 
169 void GLC_Context::glcOrtho(double left, double right, double bottom, double top, double nearVal, double farVal)
170 {
171  GLC_Matrix4x4 orthoMatrix;
172  double* m= orthoMatrix.setData();
173 
174  const double tx= - (right + left) / (right - left);
175  const double ty= - (top + bottom) / (top - bottom);
176  const double tz= - (farVal + nearVal) / (farVal - nearVal);
177  m[0]= 2.0 / (right - left);
178  m[5]= 2.0 / (top - bottom);
179  m[10]= -2.0 / (farVal - nearVal);
180  m[12]= tx;
181  m[13]= ty;
182  m[14]= tz;
183 
184  glcMultMatrix(orthoMatrix);
185 }
186 
187 void GLC_Context::glcFrustum(double left, double right, double bottom, double top, double nearVal, double farVal)
188 {
189  GLC_Matrix4x4 perspMatrix;
190  double* m= perspMatrix.setData();
191 
192  const double a= (right + left) / (right - left);
193  const double b= (top + bottom) / (top - bottom);
194  const double c= - (farVal + nearVal) / (farVal - nearVal);
195  const double d= - (2.0 * farVal * nearVal) / (farVal - nearVal);
196 
197  m[0]= (2.0 * nearVal) / (right - left);
198  m[5]= (2.0 * nearVal) / (top - bottom);
199  m[8]= a;
200  m[9]= b;
201  m[10]= c;
202  m[11]= -1.0;
203  m[14]= d;
204  m[15]= 0.0;
205 
206  glcMultMatrix(perspMatrix);
207 }
208 
210 {
211  if (enable != m_LightingIsEnable.top())
212  {
213  m_LightingIsEnable.top()= enable;
214 
215 #ifdef GLC_OPENGL_ES_2
216 
218 #else
220  {
222  }
223  if (m_LightingIsEnable.top()) ::glEnable(GL_LIGHTING);
224  else ::glDisable(GL_LIGHTING);
225 #endif
226 
227  }
228 }
229 
231 // Set Functions
233 
235 {
236  QGLContext::makeCurrent();
237  if (!GLC_State::isValid())
238  {
239  GLC_State::init();
240  }
242  m_pCurrentContext= this;
243 }
244 
246 {
247  QGLContext::doneCurrent();
249  m_pCurrentContext= NULL;
250 }
251 
252 bool GLC_Context::chooseContext(const QGLContext* shareContext)
253 {
254  qDebug() << "GLC_Context::chooseContext";
255  const bool success= QGLContext::chooseContext(shareContext);
256  if (!success)
257  {
258  qDebug() << "enable to create context " << this;
259  }
260  else if (NULL != shareContext)
261  {
262  GLC_Context* pContext= const_cast<GLC_Context*>(dynamic_cast<const GLC_Context*>(shareContext));
263  Q_ASSERT(NULL != pContext);
265  }
266  else
267  {
268  m_ContextSharedData= QSharedPointer<GLC_ContextSharedData>(new GLC_ContextSharedData());
269  }
270 
271  return success;
272 }
273 
275 // Private services Functions
278 {
279  QStack<GLC_Matrix4x4>* pStack1= new QStack<GLC_Matrix4x4>();
280  pStack1->push(GLC_Matrix4x4());
281  m_MatrixStackHash.insert(GL_MODELVIEW, pStack1);
282 
283  QStack<GLC_Matrix4x4>* pStack2= new QStack<GLC_Matrix4x4>();
284  pStack2->push(GLC_Matrix4x4());
285  m_MatrixStackHash.insert(GL_PROJECTION, pStack2);
286 
287  m_LightingIsEnable.push(false);
288 }
289 

©2005-2013 Laurent Ribon