00001 #include "SIGEL_Visualisation/SIG_Visualisation.h" 00002 00003 #include <GL/gl.h> 00004 #include <GL/glu.h> 00005 00006 namespace SIGEL_Visualisation 00007 { 00008 00009 SIG_Visualisation::SIG_Visualisation() 00010 : viewSettings(), 00011 floatingTexts(), 00012 ambientSceneColor( 3 ) 00013 { 00014 ambientSceneColor( 1 ) = ambientSceneColor( 2 ) = ambientSceneColor( 3 ) = 1; 00015 00016 glEnable(GL_DEPTH_TEST); 00017 glEnable(GL_NORMALIZE); 00018 00019 glClearColor(1, 1, 1, 1); 00020 glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE ); 00021 00022 glEnable( GL_LIGHT0 ); 00023 00024 glCullFace( GL_BACK ); 00025 00026 GLfloat const diffuseLightIntensity = 1; 00027 00028 QArray< GLfloat > diffuseLightColor( 4 ); 00029 for (int i=0; i<3; i++) 00030 diffuseLightColor[i] = diffuseLightIntensity; 00031 diffuseLightColor[3] = 1; 00032 00033 glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuseLightColor.data() ); 00034 00035 glLightf( GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.4 ); 00036 00037 QArray< GLfloat > fogColor( 4 ); 00038 fogColor.fill( 1 ); 00039 00040 glEnable( GL_FOG ); 00041 glFogi( GL_FOG_MODE, GL_LINEAR ); 00042 glFogf( GL_FOG_START, 24 ); 00043 glFogf( GL_FOG_END, 30 ); 00044 glFogfv( GL_FOG_COLOR, fogColor.data() ); 00045 00046 updateAspectRatio(); 00047 }; 00048 00049 void SIG_Visualisation::visualize() 00050 { 00051 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 00052 00053 DL_vector finalEyePoint; 00054 if (viewSettings.relativeEyePoint) 00055 { 00056 finalEyePoint.assign( &viewSettings.lookPoint ); 00057 finalEyePoint.plusis( &viewSettings.eyePoint ); 00058 } 00059 else 00060 { 00061 finalEyePoint.assign( &viewSettings.eyePoint ); 00062 }; 00063 00064 glMatrixMode(GL_MODELVIEW); 00065 glLoadIdentity(); 00066 00067 gluLookAt( static_cast<GLdouble>(finalEyePoint.get(0)), 00068 static_cast<GLdouble>(finalEyePoint.get(1)), 00069 static_cast<GLdouble>(finalEyePoint.get(2)), 00070 static_cast<GLdouble>(viewSettings.lookPoint.get(0)), 00071 static_cast<GLdouble>(viewSettings.lookPoint.get(1)), 00072 static_cast<GLdouble>(viewSettings.lookPoint.get(2)), 00073 static_cast<GLdouble>(viewSettings.up.get(0)), 00074 static_cast<GLdouble>(viewSettings.up.get(1)), 00075 static_cast<GLdouble>(viewSettings.up.get(2)) ); 00076 00077 QArray< GLfloat > lightPos( 4 ); 00078 00079 lightPos[0] = static_cast<GLfloat>(finalEyePoint.get(0)); 00080 lightPos[1] = static_cast<GLfloat>(finalEyePoint.get(1)); 00081 lightPos[2] = static_cast<GLfloat>(finalEyePoint.get(2)); 00082 lightPos[3] = 1; 00083 glLightfv( GL_LIGHT0, GL_POSITION, lightPos.data() ); 00084 00085 switch (viewSettings.renderMode) 00086 { 00087 case SIG_ViewSettings::wireFrame: 00088 glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); 00089 glShadeModel( GL_FLAT ); 00090 glDisable( GL_LIGHTING ); 00091 glDisable( GL_COLOR_MATERIAL ); 00092 glDisable( GL_CULL_FACE ); 00093 break; 00094 case SIG_ViewSettings::flatShaded: 00095 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); 00096 glShadeModel( GL_FLAT ); 00097 glEnable( GL_LIGHTING ); 00098 glEnable( GL_COLOR_MATERIAL ); 00099 glEnable( GL_CULL_FACE ); 00100 break; 00101 case SIG_ViewSettings::garoudShaded: 00102 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); 00103 glShadeModel( GL_SMOOTH ); 00104 glEnable( GL_LIGHTING ); 00105 glEnable( GL_COLOR_MATERIAL ); 00106 glEnable( GL_CULL_FACE ); 00107 break; 00108 }; 00109 }; 00110 00111 void SIG_Visualisation::setAmbientSceneColor( double red, 00112 double green, 00113 double blue ) 00114 { 00115 ambientSceneColor( 1 ) = red; 00116 ambientSceneColor( 2 ) = green; 00117 ambientSceneColor( 3 ) = blue; 00118 00119 QArray< GLfloat > lightModelAmbientColor( 4 ); 00120 00121 lightModelAmbientColor[0] = red; 00122 lightModelAmbientColor[1] = green; 00123 lightModelAmbientColor[2] = blue; 00124 lightModelAmbientColor[3] = 1; 00125 00126 glLightModelfv( GL_LIGHT_MODEL_AMBIENT, lightModelAmbientColor.data() ); 00127 }; 00128 00129 void SIG_Visualisation::updateAspectRatio() 00130 { 00131 GLdouble const fovy = 100; 00132 GLdouble const near = 0.1; 00133 GLdouble const far = 30; 00134 GLdouble const aspectRatio = static_cast<GLdouble>(viewSettings.aspectRatio); 00135 00136 glMatrixMode(GL_PROJECTION); 00137 glLoadIdentity(); 00138 gluPerspective( fovy, aspectRatio, near, far ); 00139 }; 00140 00141 SIG_Visualisation::~SIG_Visualisation() 00142 { }; 00143 00144 }