libmoldeo (Moldeo 1.0 Core)  1.0
libmoldeo is the group of objects and functions that executes the basic operations of Moldeo 1.0 Platform.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
moGLManager.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 
3  moGLManager.cpp
4 
5  ****************************************************************************
6  * *
7  * This source is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This code is distributed in the hope that it will be useful, but *
13  * WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
15  * General Public License for more details. *
16  * *
17  * A copy of the GNU General Public License is available on the World *
18  * Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
19  * obtain it by writing to the Free Software Foundation, *
20  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  * *
22  ****************************************************************************
23 
24  Copyright(C) 2006 Fabricio Costa
25 
26  Authors:
27  Fabricio Costa
28 
29 
30 *******************************************************************************/
31 
32 #include "moGLManager.h"
33 #include "moRenderManager.h"
34 
35 #ifdef MO_LINUXX
36 extern "C"
37 {
38  #include "gtk/gtk.h"
39  #include "gdk/gdk.h"
40  #include "gdk/gdkx.h"
41 }
42 #endif
43 
44 
46  0.0f,0.0f,0.0f,0.0f,
47  0.0f,0.0f,0.0f,0.0f,
48  0.0f,0.0f,0.0f,0.0f,
49  0.0f,0.0f,0.0f,0.0f) );
51  1.0f,0.0f,0.0f,0.0f,
52  0.0f,1.0f,0.0f,0.0f,
53  0.0f,0.0f,1.0f,0.0f,
54  0.0f,0.0f,0.0f,1.0f));
55 
56 moGLMatrixf::moGLMatrixf( const moGLMatrixf& rkM ) : moMatrix4f( rkM.GetPointer() ) {
57  //moMatrix4f *m = (moMatrix4f *) rkM;
58 /* SetRow( 0, rkM.GetRow(0));
59  SetRow( 1, rkM.GetRow(1));
60  SetRow( 2, rkM.GetRow(2));
61  SetRow( 3, rkM.GetRow(3));
62  */
63 }
64 
65 
66 
69 
70  SetRow( 0, rkM.GetRow(0));
71  SetRow( 1, rkM.GetRow(1));
72  SetRow( 2, rkM.GetRow(2));
73  SetRow( 3, rkM.GetRow(3));
74  return *this;
75 }
76 
77 
80  (*this) = moMatrix4f::IDENTITY;
81  return (*this);
82 }
83 
86  (*this) = moMatrix4f::ZERO;
87  return (*this);
88 }
89 
90 
92 moGLMatrixf::MakePerspective( float fovy, float aspect, float znear, float zfar ) {
93 
94  float ymax, xmax;
95  ymax = znear * tanf ( 0.5f * fovy * moMathf::DEG_TO_RAD );
96  xmax = ymax * aspect;
97  MakeFrustrum( -xmax, xmax, -ymax, ymax, znear, zfar );
98  return (*this);
99 }
100 
102 moGLMatrixf::MakeLookAt( float eyeX, float eyeY, float eyeZ, float centerX, float centerY, float centerZ, float upX, float upY, float upZ ) {
103 //moGLMatrixf::MakeLookAt( float eyeX, float eyeY, float eyeZ, float centerX, float centerY, float centerZ, float upX, float upY, float upZ ) {
104  moVector3f center3D( centerX, centerY, centerZ );
105  moVector3f eyePosition3D( eyeX, eyeY, eyeZ );
106  moVector3f upVector3D( upX, upY, upZ );
107  moGLMatrixf& me(*this);
108  moVector3f direction3D;
109  moGLMatrixf matrix2, resultMatrix;
110  //------------------
111  direction3D.X() = center3D.X() - eyePosition3D.X();
112  direction3D.Y()= center3D.Y() - eyePosition3D.Y();
113  direction3D.Z() = center3D.Z() - eyePosition3D.Z();
114  direction3D.Normalize();
115 
116  /*
117  //------------------
118  //Side = forward x up
119  ComputeNormalOfPlane(side, forward, upVector3D);
120  NormalizeVector(side);
121  */
122  moVector3f side3D, up;
123  side3D = direction3D.UnitCross( upVector3D );
124 
125 
126 /*
127  //------------------
128  //Recompute up as: up = side x forward
129  ComputeNormalOfPlane(up, side, forward);
130 */
131  up = side3D.Cross( direction3D );
132 
133  /*
134  //------------------
135  matrix2[0] = side[0];
136  matrix2[4] = side[1];
137  matrix2[8] = side[2];
138  matrix2[12] = 0.0;
139  //------------------
140  matrix2[1] = up[0];
141  matrix2[5] = up[1];
142  matrix2[9] = up[2];
143  matrix2[13] = 0.0;
144  //------------------
145  matrix2[2] = -forward[0];
146  matrix2[6] = -forward[1];
147  matrix2[10] = -forward[2];
148  matrix2[14] = 0.0;
149  //------------------
150  matrix2[3] = matrix2[7] = matrix2[11] = 0.0;
151  matrix2[15] = 1.0;
152  */
153 
154  matrix2[0][0] = side3D.X();
155  matrix2[1][0] = side3D.Y();
156  matrix2[2][0] = side3D.Z();
157  matrix2[3][0] = 0.0;
158 
159  matrix2[0][1] = up.X();
160  matrix2[1][1] = up.Y();
161  matrix2[2][1] = up.Z();
162  matrix2[3][1] = 0.0;
163 
164  matrix2[0][2] = -direction3D.X();
165  matrix2[1][2] = -direction3D.Y();
166  matrix2[2][2] = -direction3D.Z();
167  matrix2[3][2] = 0.0;
168 
169  matrix2[0][3] = matrix2[1][3] = matrix2[2][3] = 0.0f;
170  matrix2[3][3] = 1.0;
171  //------------------
172 /*
173  MultiplyMatrices4by4OpenGL_FLOAT(resultMatrix, matrix, matrix2);
174  glhTranslatef2(resultMatrix,
175  -eyePosition3D[0], -eyePosition3D[1], -eyePosition3D[2]);
176 */
177 
178  me = me * matrix2;
179  me.Translate( -eyePosition3D.X(), -eyePosition3D.Y(), -eyePosition3D.Z() );
180 
181 
182  return (*this);
183 }
184 
186 moGLMatrixf::MakeFrustrum( float left, float right, float bottom, float top, float znear, float zfar ) {
187  float r_l = right - left;
188  float t_b = top - bottom;
189  float f_n = zfar - znear;
190  float A = (right + left)/r_l;
191  float B = (top + bottom)/t_b;
192  float C = -(zfar + znear)/f_n;
193  float D = -2*(zfar*znear)/f_n;
194 
195  MakeIdentity();
196  moGLMatrixf& Me( *this );
197  moGLMatrixf Result = moMatrix4f::IDENTITY;
198  Result.SetRow( 0, moVector4f( 2.0 * znear / r_l, 0.0, A, 0.0 ) );
199  Result.SetRow( 1, moVector4f( 0.0, 2.0 * znear / t_b, B, 0.0 ) );
200  Result.SetRow( 2, moVector4f( 0.0, 0.0, C, D ) );
201  Result.SetRow( 3, moVector4f( 0.0, 0.0, -1.0, 0.0 ) );
202 
203  Me = Me * (Result.Transpose());
204  return (*this);
205 }
206 
207 
212 moGLMatrixf::MakeOrthographic( float left, float right, float bottom, float top, float znear, float zfar ) {
213  float r_l = right - left;
214  float t_b = top - bottom;
215  float f_n = zfar - znear;
216  float tx = -(right + left)/r_l;
217  float ty = -(top + bottom)/t_b;
218  float tz = -(zfar + znear)/f_n;
219  MakeIdentity();
220  moGLMatrixf& Me( *this );
221  moGLMatrixf Result = moMatrix4f::IDENTITY;
222  Result.SetRow( 0, moVector4f( 2.0 / r_l, 0.0, 0.0, tx ) );
223  Result.SetRow( 1, moVector4f( 0.0, 2.0 / t_b, 0.0, ty ) );
224  Result.SetRow( 2, moVector4f( 0.0, 0.0, -2.0 / f_n, tz ) );
225 
226  Me = Me * (Result.Transpose());
227  return (*this);
228 }
229 
231 moGLMatrixf::Translate( float x, float y, float z ) {
232 
233  //moVector3f tr = moVector3f( x, y, z);
234  moGLMatrixf& Me( *this );
235  moGLMatrixf Result;
236  Result.MakeIdentity();
237  //const moGLMatrixf& m
238 
239  Result[0][3] = x;
240  Result[1][3] = y;
241  Result[2][3] = z;
242 
243  Me = Me*Result.Transpose();
244 
245  return (*this);
246 }
247 
249 moGLMatrixf::Rotate( float angle, float rx, float ry, float rz ) {
250 
251  moGLMatrixf& Me( *this );
252  moGLMatrixf Result;
253  Result.MakeIdentity();
254 
255  moVector3f rotAxe3D( rx, ry, rz );
256  rotAxe3D.Normalize();
257  rx = rotAxe3D.X();
258  ry = rotAxe3D.Y();
259  rz = rotAxe3D.Z();
260  float c = moMathf::Cos( angle );
261  float s = moMathf::Sin( angle );
262 
263  if (rz!=0.0 && rx==0.0 && ry==0.0) {
264 
265  Result[0][0] = moMathf::Cos( angle );
266  Result[0][1] = -moMathf::Sin( angle );
267  Result[1][0] = moMathf::Sin( angle );
268  Result[1][1] = moMathf::Cos( angle );
269  Me = Me*Result.Transpose();
270 
271  } else {
272  Result[0][0] = rx*rx*(1-c) + c;
273  Result[0][1] = rx*ry*(1-c) - rz*s;
274  Result[0][2] = rx*rz*(1-c) + ry*s;
275  Result[0][3] = 0.0;
276 
277  Result[1][0] = rx*ry*(1-c) + rz*s;
278  Result[1][1] = ry*ry*(1-c) + c;
279  Result[1][2] = ry*rz*(1-c) - rx*s;
280  Result[1][3] = 0.0;
281 
282  Result[2][0] = rx*rz*(1-c) - ry*s;
283  Result[2][1] = ry*rz*(1-c) + rx*s;
284  Result[2][2] = rz*rz*(1-c) + c;
285  Result[2][3] = 0.0;
286 
287  Result[3][0] = 0.0;
288  Result[3][1] = 0.0;
289  Result[3][2] = 0.0;
290  Result[3][3] = 1.0;
291  Me = Me*Result.Transpose();
292  }
293 
294  return (*this);
295 }
296 
298 moGLMatrixf::Scale( float sx, float sy, float sz ) {
299 
300  moGLMatrixf& Me( *this );
301  moGLMatrixf Result = moMatrix4f::IDENTITY;
302  Result[0][0] = sx;
303  Result[1][1] = sy;
304  Result[2][2] = sz;
305  Me = Me*Result.Transpose();
306 
307  return (*this);
308 }
309 
310 moText
312  moText JSON = "[";
313  moText comma="",nline="";
314  for(int j=0;j<4; j++) {
315  JSON+= nline;
316  for(int i=0;i<4; i++) {
317  JSON+= comma + FloatToStr( (*this)[j][i] );
318  comma=",";
319  }
320  nline="\n";
321  }
322  JSON+= "]";
323  return JSON;
324 }
325 
326 
327 
328 
330 {
331 
334 
335  SetName("glmanager");
336  SetLabelName("glmanager");
337 
338  m_Context = NULL;
339  m_DisplayServer = NULL;
340  m_DisplayScreen = NULL;
341  m_DisplayWindow = NULL;
342 
343  m_gpu_vendor_code = 0;
344  m_gpu_vendor_string = "undefined";
345  m_gpu_renderer_string = "undefined";
346 
347  m_current_fbo = m_previous_fbo = 0;
348 
349  m_current_read_buffer = m_current_draw_buffer = 0;
350  m_previous_read_buffer = m_previous_draw_buffer = 0;
351  m_bFrameBufferObjectActive = false;
352 }
353 
355 {
356  Finish();
357 }
358 
360 {
361  QueryGPUVendorString();
362  m_current_fbo = 0;
363  m_bFrameBufferObjectActive = false;
364 
365  m_Context = NULL;
366  m_DisplayServer = NULL;
367  m_DisplayScreen = NULL;
368  m_DisplayWindow = NULL;
369  m_gl_version = "none";
370  m_gl_major_version = 0;
371  m_gl_minor_version = 0;
372 
373 #ifndef OPENGLESV2
374  glGetIntegerv(GL_DRAW_BUFFER, &m_current_draw_buffer);
375  glGetIntegerv(GL_READ_BUFFER, &m_current_read_buffer);
376 
377 
378  m_gl_version = (char*) glGetString(GL_VERSION);
379  glGetIntegerv(GL_MAJOR_VERSION, &m_gl_major_version);
380  glGetIntegerv(GL_MINOR_VERSION, &m_gl_minor_version);
381 
382  if (m_gl_major_version==0) {
383  std::string major = (char*)m_gl_version;
384  major.substr( 0, major.find(".") );
385  m_gl_major_version = (int) atoi(major.c_str() );
386  }
387 
388  if (m_gl_minor_version==0) {
389  std::string minor = (char*)m_gl_version;
390  minor.substr( minor.find(".")+1, 1 );
391  m_gl_minor_version = (int) atoi(minor.c_str() );
392  }
393 
394  MODebug2->Message("moGLManager::Init > GL VERSION: "+m_gl_version );
395  MODebug2->Message("moGLManager::Init > GL_MAJOR_VERSION: "+IntToStr(m_gl_major_version) );
396  MODebug2->Message("moGLManager::Init > GL_MINOR_VERSION: "+IntToStr(m_gl_minor_version) );
397  MODebug2->Message("moGLManager::Init > GPU VENDOR STRING is "+m_gpu_vendor_string );
398  MODebug2->Message("moGLManager::Init > GPU RENDERER STRING is "+m_gpu_renderer_string );
399 
400 #endif
401  return true;
402 }
403 
405 {
406  return true;
407 }
408 
410 
411  m_bFrameBufferObjectActive = active;
412 
413 }
414 
416 {
417  GLuint errnum;
418  moText errstr;
419  MOboolean error = false;
420 
421  return false;
422 
423  while ((errnum = glGetError()))
424  {
425  //GL_NO_ERROR
426  //GL_INVALID_ENUM
427 
428  error = true;
429  errstr = moText(" GL error code:") + IntToStr(errnum) + moText(" message > ");
430 #ifndef OPENGLESV2
431  errstr+= (char *)gluErrorString(errnum);
432 #endif
433  if (p_location != moText("")) errstr += moText(" at ") + moText(p_location);
434  moDebugManager::Error("moGLManager::CheckErrors > errors: " + errstr);
435  }
436  return error;
437 }
438 
439 void moGLManager::QueryGPUVendorString()
440 {
441  char vendor[80];
442  char *glvendor = NULL;
443  glvendor = (char*)glGetString(GL_VENDOR);
444  if (glvendor!=NULL) {
445  strcpy(vendor, glvendor);
446  m_gpu_vendor_string = vendor;
447 
448  if (strstr(vendor, "NVIDIA") != NULL) m_gpu_vendor_code = MO_GPU_NV;
449  else if (strstr(vendor, "ATI") != NULL) m_gpu_vendor_code = MO_GPU_ATI;
450  else if (strstr(vendor, "INTEL") != NULL) m_gpu_vendor_code = MO_GPU_INTEL;
451  else m_gpu_vendor_code = MO_GPU_OTHER;
452  } else m_gpu_vendor_code = MO_GPU_OTHER;
453 
454  glvendor = (char*)glGetString(GL_RENDERER);
455  if (glvendor!=NULL) {
456  strcpy(vendor, glvendor);
457  m_gpu_renderer_string = vendor;
458  }
459 
460 }
461 
462 void moGLManager::SetPerspectiveView( MOint p_width, MOint p_height, double fovy, double aspect, double znear, double zfar )
463 {
464  glViewport(0, 0, p_width, p_height);
465  m_Viewport = moGLViewport( p_width, p_height );
466  if (aspect==0.0) {
467  aspect = m_Viewport.GetProportion();
468  }
469  m_ProjectionMatrix.MakePerspective( fovy, aspect, znear, zfar );
470  //m_ProjectionMatrix.MakePerspectiveProjection( moVector3f( 0.0, 1.0, 0.0 ), moVector3f(0.0, 0.0, 0.0), moVector3f(0.0, 0.0, -10.0) );
471 
472  //m_ProjectionMatrix.MakePerspective( );
473 // float* pfv = m_ProjectionMatrix[0];
474 
475 #ifndef OPENGLESV2
476 
477 /*
478  glMatrixMode(GL_PROJECTION);
479  glLoadIdentity();
480 
481  gluPerspective(60.0f, m_Viewport.GetProportion(), 0.01f, 1000.0f);
482 */
483 
484  glMatrixMode(GL_PROJECTION);
485  glLoadIdentity();
486  glLoadMatrixf( m_ProjectionMatrix.GetPointer() );
487 
488 /*
489  glMatrixMode(GL_MODELVIEW);
490  glLoadIdentity();
491 */
492 #endif
493 }
494 
496  SetPerspectiveView( p_width, p_height, 60.0f, moDisplay(p_width,p_height).Proportion(), 0.01f, 1000.0f );
497 }
498 
500  SetOrthographicView( p_width,
501  p_height,
502  -0.5,
503  0.5,
504  moDisplay( p_width, p_height ).HeightToProportion(-0.5),
505  moDisplay( p_width, p_height ).HeightToProportion(0.5)
506  );
507 }
508 
509 void moGLManager::SetOrthographicView(MOint p_width, MOint p_height, float left, float right, float bottom, float top, float znear, float zfar)
510 {
511 #ifndef OPENGLESV2
512  glMatrixMode(GL_PROJECTION);
513  glLoadIdentity();
514 #endif
515 
516  m_ProjectionMatrix.MakeOrthographic( left, right, bottom, top, znear, zfar );
517 
518  if (p_width!=0 || p_height!=0) {
519  glViewport(0, 0, p_width, p_height);
520  m_Viewport = moGLViewport( p_width, p_height );
521 
522 #ifndef OPENGLESV2
523  //gluOrtho2D(0.0, p_width, 0.0, p_height);
524  glMatrixMode(GL_PROJECTION);
525  glLoadMatrixf( m_ProjectionMatrix.GetPointer() );
526 #endif
527  } else {
528  float prop;
529  if (m_pResourceManager) {
532  }
533  if ( p_width == 0 || p_height == 0 ) { p_width = 1; p_height = 1; prop = 1.0; }
534  else {
535  prop = (float) p_height / (float) p_width;
536  }
537  glViewport( 0, 0, p_width, p_height );
538  m_Viewport = moGLViewport( p_width, p_height );
539 #ifndef OPENGLESV2
540  //glOrtho( -0.5, 0.5, -0.5*prop, 0.5*prop, -1, 1);
541  glMatrixMode(GL_PROJECTION);
542  glLoadMatrixf( m_ProjectionMatrix.GetPointer() );
543 #endif
544  // Set Up An Ortho Screen
545  }
546 #ifndef OPENGLESV2
547  glMatrixMode(GL_MODELVIEW);
548  glLoadIdentity();
549 #endif
550 
551 }
552 
553 
554 void moGLManager::LookAt( float eyeX, float eyeY, float eyeZ, float centerX, float centerY, float centerZ, float upX, float upY, float upZ ) {
555 
556  moGLMatrixf Lat;
557  Lat.MakeIdentity();
558  Lat.MakeLookAt( eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ );
559 
560  //m_ModelMatrix.MakeIdentity();
561  //m_ModelMatrix.MakeLookAt( eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ );
562  //m_ProjectionMatrix.MakeIdentity();
563  m_ProjectionMatrix = Lat*m_ProjectionMatrix;
564 /*
565  MODebug2->Message("moGLManager::LookAt > LookAt Matrix:" );
566  MODebug2->Message( Lat.ToJSON() );
567 */
568 #ifndef OPENGLESV2
569  glMatrixMode(GL_PROJECTION);
570  glLoadIdentity();
571  glLoadMatrixf( m_ProjectionMatrix.GetPointer() );
572  //gluLookAt( eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ);
573  /*glMatrixMode(GL_MODELVIEW);
574  glLoadIdentity();*/
575 #endif
576 }
577 
578 
580 {
581  MOint render_mode = 0;
582 #ifndef OPENGLESV2
583  glGetIntegerv(GL_RENDER_MODE, &render_mode);
584 #endif
585  return render_mode;
586 }
587 
589 {
590  // Valid modes are GL_RENDER, GL_SELECT and GL_FEEDBACK.
591 #ifndef OPENGLESV2
592  glRenderMode(p_mode);
593 #endif
594 }
595 
596 // Setting up rendering parameters commonly used in moldeo effects.
598 {
599  glEnable(GL_DEPTH_TEST); // Enables Depth Testing.
600  glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do.
601  glEnable(GL_BLEND); // Enables blending.
602  glEnable(GL_TEXTURE_2D);
603 #ifndef OPENGLESV2
604  // Enables texturing.
605  glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); // Polygon full filling mode, front and back
606 #endif
607 }
608 
609 // Sets the variables changed by SetMoldeoGLState to the default GL settings.
611 {
612  glDisable(GL_DEPTH_TEST); // Disables Depth Testing.
613  glDisable(GL_BLEND); // Disables blending.
614  glDisable(GL_TEXTURE_2D); // Disables texturing.
615 }
616 
618 {
619 #ifndef OPENGLESV2
620  glPixelStorei(GL_PACK_SWAP_BYTES, GL_FALSE);
621  glPixelStorei(GL_PACK_LSB_FIRST, GL_FALSE);
622  glPixelStorei(GL_PACK_ROW_LENGTH, 0);
623  glPixelStorei(GL_PACK_SKIP_ROWS, 0);
624  glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
625 
626  glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_FALSE);
627  glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
628  glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
629  glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
630  glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
631 #endif
632 
633  glPixelStorei(GL_PACK_ALIGNMENT, 4);
634  glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
635 }
636 
638 {
639  // For more info about the GL attributes, see apendix B of the Red Book:
640  // http://cs-sdl.sourceforge.net/index.php/Red_Book_Appendix_B
641 
642  SaveFramebuffer();
643 #ifndef OPENGLESV2
644  glPushAttrib(GL_ALL_ATTRIB_BITS);
645 #endif
646  SaveGLMatrices();
647 }
648 
650 {
651 #ifndef OPENGLESV2
652  glMatrixMode(GL_MODELVIEW);
653  glPushMatrix();
654  glMatrixMode(GL_PROJECTION);
655  glPushMatrix();
656  glMatrixMode(GL_TEXTURE);
657  glPushMatrix();
658 #endif
659 }
660 
662 {
663 #ifndef OPENGLESV2
664  glPushAttrib(GL_VIEWPORT_BIT);
665 #endif
666  SaveGLMatrices();
667 }
668 
670 {
671  m_saved_fbo = m_current_fbo;
672  m_saved_read_buffer = m_current_read_buffer;
673  m_saved_draw_buffer = m_current_draw_buffer;
674 }
675 
677 {
680 #ifndef OPENGLESV2
681  glPopAttrib();
682 #endif
683 }
684 
686 {
687 #ifndef OPENGLESV2
688  glMatrixMode(GL_PROJECTION);
689  glPopMatrix();
690  glMatrixMode(GL_MODELVIEW);
691  glPopMatrix();
692  glMatrixMode(GL_TEXTURE);
693  glPopMatrix();
694 #endif
695 }
696 
698 {
700 #ifndef OPENGLESV2
701  glPopAttrib();
702 #endif
703 }
704 
706 {
707  m_current_fbo = m_saved_fbo;
708  if (m_bFrameBufferObjectActive) glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_current_fbo);
709 
710  m_current_read_buffer = m_saved_read_buffer;
711 #ifndef OPENGLESV2
712  if (m_bFrameBufferObjectActive) glReadBuffer(m_current_read_buffer);
713 #endif
714  m_current_draw_buffer = m_saved_draw_buffer;
715 #ifndef OPENGLESV2
716  if (m_bFrameBufferObjectActive) glDrawBuffer(m_current_draw_buffer);
717 #endif
718 }
719 
721 {
722  moTexParam result;
723 #ifndef OPENGLESV2
724  if (m_gpu_vendor_code == MO_GPU_NV)
725  {
726  result.target = GL_TEXTURE_RECTANGLE_NV;
727  if (p_16bits)
728  if (p_num_components == 4) result.internal_format = GL_FLOAT_RGBA16_NV;
729  else result.internal_format = GL_FLOAT_R16_NV;
730  else
731  if (p_num_components == 4) result.internal_format = GL_FLOAT_RGBA32_NV;
732  else result.internal_format = GL_FLOAT_R32_NV;
733  }
734  else if (m_gpu_vendor_code == MO_GPU_ATI)
735  {
736  result.target = GL_TEXTURE_RECTANGLE_ARB;
737  if (p_16bits)
738  if (p_num_components == 4) result.internal_format = GL_RGBA_FLOAT16_ATI;
739  else result.internal_format = GL_LUMINANCE_FLOAT16_ATI;
740  else
741  if (p_num_components == 4) result.internal_format = GL_RGBA_FLOAT32_ATI;
742  else result.internal_format = GL_LUMINANCE_FLOAT32_ATI;
743  }
744  else
745 #endif
746  {
747  result.target = GL_TEXTURE_RECTANGLE_ARB;
748  if (p_16bits)
749  if (p_num_components == 4) result.internal_format = GL_RGBA16F_ARB;
750  else result.internal_format = GL_LUMINANCE16F_ARB;
751  else
752  if (p_num_components == 4) result.internal_format = GL_RGBA32F_ARB;
753  else result.internal_format = GL_LUMINANCE32F_ARB;
754  }
755 
756  // FP textures don't have filtering nor wrapping.
757  result.mag_filter = GL_NEAREST;
758  result.min_filter = GL_NEAREST;
759  result.wrap_s = GL_CLAMP_TO_EDGE;
760  result.wrap_t = GL_CLAMP_TO_EDGE;
761 
762  return result;
763 }
764 
765 MOboolean moGLManager::RectTexture(GLenum p_target) const
766 {
767  return
768 #ifndef OPENGLESV2
769  (p_target == GL_TEXTURE_RECTANGLE_NV) ||
770 #endif
771  (p_target == GL_TEXTURE_RECTANGLE_ARB);
772 }
773 
774 MOboolean moGLManager::FPTexture(GLint p_internal_format)
775 {
776  return
777 #ifndef OPENGLESV2
778  (p_internal_format == GL_FLOAT_RGBA16_NV) ||
779  (p_internal_format == GL_FLOAT_RGBA32_NV) ||
780  (p_internal_format == GL_FLOAT_R16_NV) ||
781  (p_internal_format == GL_FLOAT_R32_NV) ||
782  (p_internal_format == GL_RGBA_FLOAT16_ATI) ||
783  (p_internal_format == GL_RGBA_FLOAT32_ATI) ||
784  (p_internal_format == GL_LUMINANCE_FLOAT16_ATI) ||
785  (p_internal_format == GL_LUMINANCE_FLOAT32_ATI) ||
786 #endif
787  (p_internal_format == GL_RGBA16F_ARB) ||
788  (p_internal_format == GL_RGBA32F_ARB) ||
789  (p_internal_format == GL_LUMINANCE16F_ARB) ||
790  (p_internal_format == GL_LUMINANCE32F_ARB);
791 }
792 
794 {
795  return (p_min_filter == GL_NEAREST_MIPMAP_NEAREST) ||
796  (p_min_filter == GL_LINEAR_MIPMAP_NEAREST) ||
797  (p_min_filter == GL_NEAREST_MIPMAP_LINEAR) ||
798  (p_min_filter == GL_LINEAR_MIPMAP_LINEAR);
799 }
800 
802 {
803  m_current_fbo = m_fbo;
804  if (m_bFrameBufferObjectActive) glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_current_fbo);
805 }
806 
808 {
809  m_current_read_buffer = p_buffer;
810 #ifndef OPENGLESV2
811  if (m_bFrameBufferObjectActive) glReadBuffer(m_current_read_buffer);
812 #endif
813 }
814 
816 {
817  m_current_draw_buffer = p_buffer;
818 #ifndef OPENGLESV2
819  if (m_bFrameBufferObjectActive) glDrawBuffer(m_current_draw_buffer);
820 #endif
821 }
822 
824 {
825  m_previous_fbo = m_current_fbo;
826  m_previous_read_buffer = m_current_read_buffer;
827  m_previous_draw_buffer = m_current_draw_buffer;
828 }
829 
831 {
832  SetCurrentFBO(m_previous_fbo);
833  SetCurrentReadBuffer(m_previous_read_buffer);
834  SetCurrentDrawBuffer(m_previous_draw_buffer);
835 }
836 
837 
838 int
839 moGLManager::CreateContext( int p_width, int p_height ) {
840 
841  MODebug2->Message( "moGLManager::CreateContext > p_width: " + IntToStr(p_width) + " p_height: " + IntToStr(p_height) );
842 
843  #ifdef MO_WIN
844 
845  GLuint PixelFormat;
846  PIXELFORMATDESCRIPTOR pfd;
847  hDC = GetDC(NULL);
848 
849 
850  memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
851 
852  pfd.nSize = sizeof(pfd);
853  pfd.nVersion = 1;
854  pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
855  pfd.iPixelType = PFD_TYPE_RGBA;
856  pfd.cColorBits = 32;
857  pfd.cDepthBits = 16;
858  pfd.cStencilBits = 8;
859  pfd.iLayerType = PFD_MAIN_PLANE;
860 
861  PixelFormat = ChoosePixelFormat(hDC, &pfd);
862  SetPixelFormat(hDC, PixelFormat, &pfd);
863 
864  // HGLRC
865  this->m_Context = (moGLContext) wglCreateContext(hDC);
866  wglMakeCurrent(hDC, (HGLRC) this->m_Context );
867 
868  #endif
869 
870  #ifdef MO_LINUXX
871 
872  /*
873  If direct is True,
874  then a direct-rendering context is created if the
875  implementation supports direct rendering, if the connection is to an X
876  server that is local, and if a direct-rendering context is available. (An implementation may return an indirect context when share_list is True.)
877  If share_list is False,
878  then a rendering context that renders through the X server is always created.
879  Direct rendering provides a performance advantage in some implementations.
880  However, direct-rendering contexts cannot be shared outside a single process,
881  and they may be unable to render to GLX pixmaps.
882  */
883 
884  int m_glxVersion = 0;
885  int glxMajorVer, glxMinorVer;
886  Display *XServerDisplay=NULL;
887  GdkDisplay *XGdkDisplay=NULL;
888  GLXFBConfig *fbc = NULL;
889  GLXPbuffer OffScreenBuffer = 0;
890  int nscreens = 0;
891 
892  //XServerDisplay = GDK_DISPLAY();
893  //XServerDisplay = gdk_display_get_default();
894  MODebug2->Message( "moGLManager::CreateContext > getting X server display" );
895  //XServerDisplay = gdk_x11_get_default_xdisplay();
896  if (m_DisplayServer==NULL) {
897  XServerDisplay = XOpenDisplay(NULL);
898  m_DisplayServer = (void*)XServerDisplay;
899  } else {
900  XServerDisplay = (Display *)m_DisplayServer;
901  MODebug2->Message("moGLManager::CreateContext > display server connection already opened.");
902  }
903  //XServerDisplay = GDK_DISPLAY();
904  // gdk_display_get_screen ()
905  // gdk_display_get_n_screens ()
906 
907  if (XServerDisplay) {
908 
909  MODebug2->Message( "moGLManager::CreateContext > X server display OK." );
910 
911  XGdkDisplay = gdk_x11_lookup_xdisplay( XServerDisplay );
912  if (!XGdkDisplay) XGdkDisplay = gdk_display_get_default ();
913 
914  if (XGdkDisplay) {
915  MODebug2->Error("moGLManager::CreateContext > X Gdk Display OK! : " + IntToStr((MOulong)XGdkDisplay));
916  nscreens = gdk_display_get_n_screens(XGdkDisplay);
917  } else MODebug2->Error("moGLManager::CreateContext > X Gdk Display not found : " + IntToStr((MOulong)XGdkDisplay));
918 
919  MODebug2->Message("moGLManager::CreateContext > X Server Display found: " + IntToStr((MOulong)XServerDisplay) + " screens:" + IntToStr(nscreens) );
920 
921  } else {
922  MODebug2->Error("moGLManager::CreateContext > X Server Display not found : " + IntToStr((MOulong)XServerDisplay));
923  return false;
924  }
925 
926  bool ok = glXQueryVersion( XServerDisplay, &glxMajorVer, &glxMinorVer);
927 
928  if (!ok)
929  m_glxVersion = 10; // 1.0 by default
930  else
931  m_glxVersion = glxMajorVer*10 + glxMinorVer;
932 
933  MODebug2->Message("moGLManager::CreateContext > GLX Version (10=1.0,13=1.3,...): " + IntToStr(m_glxVersion) + "=" + FloatToStr( (double)(m_glxVersion)/(double) 10.0) );
934 
935  if ( m_glxVersion >= 13)
936  {
937  // GLX >= 1.3
938  //GLXFBConfig *fbc = gc->m_fbc;
939 
940  int nelements = 0;
941  int attrib_list[] =
942  {
943  GLX_RENDER_TYPE, GLX_RGBA_BIT,
944  GLX_DOUBLEBUFFER, 1,
945  GLX_RED_SIZE, 8,
946  GLX_GREEN_SIZE, 8,
947  GLX_BLUE_SIZE, 8,
948  GLX_ALPHA_SIZE, 8,
949  GLX_DEPTH_SIZE, 16,
950  GLX_STENCIL_SIZE, 8,
951  0
952  };
953 
954 
955  fbc = glXChooseFBConfig( XServerDisplay,
956  DefaultScreen(XServerDisplay)/*screen number*/,
957  attrib_list,
958  &nelements );
959 
960 
961  if (fbc) {
962  MODebug2->Message( "moGLManager::CreateContext > FBConfig OK!");
963  } else {
964  MODebug2->Error( "moGLManager::CreateContext > No FBConfig");
965  return false;
966  }
967 
968  this->m_Context = (moGLContext) glXCreateNewContext( XServerDisplay /*XServer Display*/,
969  fbc[0] /*attribute list FB Config match*/,
970  GLX_RGBA_TYPE /*type of rendering*/,
971  NULL /*shared context for display lists*/,
972  GL_TRUE /*direct rendering to graphics system*/);
973  }
974  else
975  {
976  // GLX <= 1.2
977  int attrib_list_vi[] =
978  {
979  GLX_RGBA,
980  GLX_DOUBLEBUFFER,
981  GLX_RED_SIZE, 8,
982  GLX_GREEN_SIZE, 8,
983  GLX_BLUE_SIZE, 8,
984  GLX_ALPHA_SIZE, 8,
985  GLX_DEPTH_SIZE, 16,
986  GLX_STENCIL_SIZE, 8,
987  0
988  };
989 
990  XVisualInfo *vi = glXChooseVisual( XServerDisplay,
991  DefaultScreen(XServerDisplay), /*screen number*/
992  attrib_list_vi );
993 
994  this->m_Context = (moGLContext) glXCreateContext( XServerDisplay,
995  vi,
996  NULL,
997  GL_TRUE );
998  }
999 
1000  if (this->m_Context) {
1001 
1002  MODebug2->Message("moGLManager::CreateContext > creating offscreen buffer.");
1003 
1004  if ( m_glxVersion >= 13 ) {
1005  // GLX >= 1.3
1006  int pbuffer_attrib[] = {
1007  GLX_PBUFFER_WIDTH, p_width,
1008  GLX_PBUFFER_HEIGHT, p_height,
1009  GLX_LARGEST_PBUFFER, 0,
1010  GLX_PRESERVED_CONTENTS, 0,
1011  0 };
1012  OffScreenBuffer = glXCreatePbuffer( XServerDisplay, fbc[0], pbuffer_attrib );
1013 
1014  this->m_DisplayScreen = (void*) OffScreenBuffer;
1015 
1016  if (OffScreenBuffer) {
1017  MODebug2->Message("moGLManager::CreateContext > Offscreen buffer OK! GLPXBuffer: " + IntToStr( (MOulong) OffScreenBuffer ) );
1018  } else {
1019  MODebug2->Error("moGLManager::CreateContext > Offscreen buffer BAD... : " + IntToStr( (MOulong) OffScreenBuffer ) );
1020  return false;
1021  }
1022  if ( glXMakeContextCurrent( XServerDisplay, OffScreenBuffer, OffScreenBuffer, (GLXContext) this->m_Context ) ) {
1023  MODebug2->Message("moGLManager::CreateContext > Making Context current OK!");
1024  }
1025  } else {
1026  // GLX <= 1.2
1027  //glXMakeCurrent( XServerDisplay, GDK_WINDOW_XWINDOW(window), m_glContext );
1028  }
1029 
1030  }
1031 
1032  #endif
1033 
1034  #ifdef MO_MAC
1035  //CGL
1036  CGLCreateContext
1037 
1038 
1039  //COCOA
1040  //NSOpenGLView
1041 
1042  //CARBON
1043  //aglCreateContext
1044 
1045  #endif
1046 
1047 
1048 
1049  return (this->m_Context == NULL);
1050 }
1051 /*
1052 int
1053 moGLManager::CreateOffscreen( int p_width, int p_height, int ) {
1054 
1055 }
1056 */
1057 
1060  return this->m_Context;
1061 }
1062 
1063 
1066  return this->m_DisplayServer;
1067 }
1068 
1071  return this->m_DisplayScreen;
1072 }
1073 
1076  return this->m_DisplayWindow;
1077 }
1078 
1079 
1080 void
1082  m_Context = p_Context;
1083 }
1084 
1085 moGLMatrixf&
1087  return m_ModelMatrix;
1088 }
1089 
1090 moGLMatrixf&
1092 
1093  m_ModelMatrix = p_mat4;
1094  return m_ModelMatrix;
1095 }
1096 
1097 moGLMatrixf&
1099  return m_ProjectionMatrix;
1100 }
1101 
1102 moGLMatrixf&
1104 
1105  m_ProjectionMatrix = p_mat4;
1106  return m_ProjectionMatrix;
1107 }
1108 
1109 
void SetDefaultPixelStorageModes()
moGLMatrixf & MakeLookAt(float eyeX=0.0, float eyeY=0.0, float eyeZ=-10.0, float centerX=0.0, float centerY=0.0, float centerZ=0.0, float upX=0.0, float upY=0.0, float upZ=1.0)
moDisplayScreen GetDisplayScreen()
moGLMatrixf & Scale(float sx, float sy, float sz)
void SetPerspectiveView(MOint p_width, MOint p_height, double fovy=60.0, double aspect=1.0, double znear=0.1, double zfar=4000.0)
GLint mag_filter
Definition: moTypes.h:548
moGLMatrixf & MakePerspective(float fovy, float aspect, float zNear, float zFar)
Definition: moGLManager.cpp:92
Parámetros internos de una textura.
Definition: moTypes.h:543
The Blue component of a color.
Definition: moOGLFT.h:88
#define MOulong
Definition: moTypes.h:392
void SetDefaultOrthographicView(MOint p_width=0, MOint p_height=0)
moGLMatrixf & GetModelMatrix()
void LookAt(float eyeX=0.0, float eyeY=0.0, float eyeZ=-10.0, float centerX=0.0, float centerY=0.0, float centerZ=0.0, float upX=0.0, float upY=0.0, float upZ=1.0)
void Error(moText p_text)
Anuncia y registra un error.
Definition: moAbstract.cpp:79
GLint internal_format
Definition: moTypes.h:546
void SetName(const moText &p_name)
moGLMatrixf & SetModelMatrix(const moGLMatrixf &p_mat4)
moVector4< MOfloat > moVector4f
#define MO_GPU_NV
Definition: moGLManager.h:35
void SaveFramebuffer()
void RestoreFBOState()
virtual ~moGLManager()
LIBMOLDEO_API moText0 FloatToStr(double a)
Definition: moText.cpp:1134
moRenderManager * GetRenderMan()
#define MOboolean
Definition: moTypes.h:385
Matrices para transformaciones en Open GL.
Definition: moGLManager.h:71
static void Error(moText p_text)
Anuncia un error.
moDisplayWindow GetDisplayWindow()
void SaveView()
static const moGLMatrixf ZERO
Definition: moGLManager.h:103
void * moGLContext
Definition: moGLManager.h:49
void SetResourceType(moResourceType p_restype)
static Real Sin(Real fValue)
Definition: moMath.h:260
MOboolean RectTexture(GLenum p_target) const
Real Z() const
Definition: moMathVector3.h:77
#define MO_GPU_INTEL
Definition: moGLManager.h:37
moGLMatrixf & operator=(const moGLMatrixf &rkM)
Definition: moGLManager.cpp:68
moGLMatrixf(bool bZero=true)
Definition: moGLManager.h:74
moGLMatrixf & Rotate(float angle, float vx, float vy, float vz)
GLint wrap_t
Definition: moTypes.h:550
MOboolean MipMapTexture(GLint p_min_filter)
Real X() const
Definition: moMathVector3.h:73
MOboolean CheckErrors(moText p_location)
void SetRow(int iRow, const moVector3< Real > &rkV)
Definition: moMathMatrix.h:895
void RestoreView()
clase de para manejar textos
Definition: moText.h:75
void SaveFBOState()
moResourceManager * m_pResourceManager
Puntero al administrador de recursos.
moGLMatrixf & MakeZero()
Definition: moGLManager.cpp:85
MOint RenderHeight() const
Dispositivo de entrada/salida, típicamente, interfaces humanas de IO y datos ( teclado, mouse, tableta, tcp, udp, serial )
Definition: moTypes.h:532
moGLMatrixf & SetProjectionMatrix(const moGLMatrixf &p_mat4)
moGLMatrixf & MakeIdentity()
Definition: moGLManager.cpp:79
moGLMatrixf & MakeFrustrum(float left=-1.0, float right=1.0, float bottom=-1.0, float top=1.0, float near=0.0001, float far=1000.0f)
#define MOushort
Definition: moTypes.h:402
void SetCurrentDrawBuffer(MOint p_buffer)
moGLMatrixf & Translate(float x, float y, float z)
moText0 moText
Definition: moText.h:291
void SetCurrentReadBuffer(MOint p_buffer)
void * moDisplayScreen
Definition: moGLManager.h:51
MOint GetRenderMode()
MOboolean FPTexture(GLint p_internal_format)
void SetCurrentFBO(MOuint m_fbo)
#define MOint
Definition: moTypes.h:388
void RestoreGLMatrices()
void SaveGLState()
#define MO_GPU_ATI
Definition: moGLManager.h:36
void * moDisplayWindow
Definition: moGLManager.h:52
void SetDefaultPerspectiveView(MOint p_width, MOint p_height)
void SetContext(moGLContext p_Context)
static const moGLMatrixf IDENTITY
Definition: moGLManager.h:104
The Alpha (or transparency) of a color.
Definition: moOGLFT.h:89
void SetFrameBufferObjectActive(bool active=true)
moGLMatrixf & GetProjectionMatrix()
#define MO_GPU_OTHER
Definition: moGLManager.h:38
void RestoreGLState()
Real Normalize()
float GetProportion()
Definition: moGLManager.h:141
void SetLabelName(const moText &p_labelname)
moTexParam BuildFPTexParam(MOboolean p_16bits=true, MOushort p_num_components=4)
GLint wrap_s
Definition: moTypes.h:549
moText ToJSON() const
virtual MOboolean Finish()
MOint RenderWidth() const
static moDebug * MODebug2
Clase de impresión de errores para depuración
Definition: moAbstract.h:225
void SetRenderMode(MOint p_mode)
moGLMatrixf & MakeOrthographic(float left=-1.0, float right=1.0, float bottom=-1.0, float top=1.0, float near=0.0001, float far=1000.0f)
moMatrix4< MOfloat > moMatrix4f
static Real Cos(Real fValue)
Definition: moMath.h:160
void SetMoldeoGLState()
moDisplayServer GetDisplayServer()
moVector3 Cross(const moVector3 &rkV) const
Real Y() const
Definition: moMathVector3.h:75
moGLContext GetContext()
GLenum target
Definition: moTypes.h:545
GLint min_filter
Definition: moTypes.h:547
#define MOuint
Definition: moTypes.h:387
LIBMOLDEO_API moText0 IntToStr(int a)
Definition: moText.cpp:1070
void RestoreFramebuffer()
void SetDefaultGLState()
int CreateContext(int p_width, int p_height)
void * moDisplayServer
Definition: moGLManager.h:50
const MOlong DEG_TO_RAD
Definition: moMath.cpp:49
void Message(moText p_text)
Anuncia un mensaje al usuario además de guardarlo en el log de texto.
Definition: moAbstract.cpp:114
void SetOrthographicView(MOint p_width=0, MOint p_height=0, float left=0.0, float right=1.0, float bottom=0.0, float top=1.0, float znear=-1.0, float zfar=1.0)
virtual MOboolean Init()
void SetType(moMoldeoObjectType p_type)
void SaveGLMatrices()
moVector3 UnitCross(const moVector3 &rkV) const