00001 #include "SIGEL_Visualisation/SIG_EnvironmentRenderer.h" 00002 00003 #include "SIGEL_Tools/SIG_TypeConverter.h" 00004 00005 #include <cmath> 00006 00007 using namespace SIGEL_Tools; 00008 00009 namespace SIGEL_Visualisation 00010 { 00011 00012 SIG_EnvironmentRenderer::SIG_EnvironmentRenderer(SIGEL_Environment::SIG_Environment const &environment) 00013 : SIG_Renderer( 2, 0), 00014 environment(environment), 00015 lookPoint(0, 0, 0), 00016 fieldEdgeLength( 1 ), 00017 renderMode( SIG_ViewSettings::wireFrame ), 00018 showPlane( true ), 00019 showGrid( false ) 00020 { 00021 robotPathPoints.setAutoDelete( true ); 00022 00023 DL_matrix idRotation; 00024 idRotation.makeone(); 00025 00026 SIG_VisualSceneObject *plane = new SIG_VisualSceneObject( 0, "Plane" ); 00027 00028 double planeLevel = environment.getYPlaneLevel(); 00029 00030 plane->setPosition( DL_vector(0, planeLevel, 0) ); 00031 plane->setRotation( idRotation ); 00032 00033 sceneObjects.insert( 0, plane ); 00034 00035 SIG_VisualSceneObject *grid = new SIG_VisualSceneObject( 1, "Grid" ); 00036 00037 grid->setColor( DL_vector( 1, 0, 0 ) ); 00038 00039 grid->setPosition( DL_vector(0, planeLevel, 0) ); 00040 grid->setRotation( idRotation ); 00041 00042 sceneObjects.insert( 1, grid ); 00043 00044 buildDisplayLists(); 00045 }; 00046 00047 SIG_EnvironmentRenderer::~SIG_EnvironmentRenderer() 00048 { }; 00049 00050 void SIG_EnvironmentRenderer::setPlaneColor( double red, 00051 double green, 00052 double blue ) 00053 { 00054 sceneObjects[ 0 ]->setColor( DL_vector( red, 00055 green, 00056 blue ) ); 00057 }; 00058 00059 void SIG_EnvironmentRenderer::buildDisplayLists() 00060 { 00061 GLuint planeListIndex = displayListsOffset; 00062 buildPlane( planeListIndex ); 00063 00064 GLuint gridListIndex = displayListsOffset + 1; 00065 00066 buildGrid( gridListIndex ); 00067 }; 00068 00069 void SIG_EnvironmentRenderer::buildPlane(GLuint number) 00070 { 00071 GLint const noOfFields = 70; 00072 00073 GLint const minX = - fieldEdgeLength * noOfFields; 00074 GLint const maxX = fieldEdgeLength * noOfFields; 00075 GLint const minZ = - fieldEdgeLength * noOfFields; 00076 GLint const maxZ = fieldEdgeLength * noOfFields; 00077 00078 glNewList(number, GL_COMPILE); 00079 00080 glFrontFace( GL_CW ); 00081 00082 glNormal3i( 0, 1, 0 ); 00083 00084 for (GLint x=minX; x<maxX; x+=fieldEdgeLength) 00085 { 00086 glBegin(GL_QUAD_STRIP); 00087 for (int z=minZ; z<=maxZ; z+=fieldEdgeLength) 00088 { 00089 glVertex3i( x, 0, z ); 00090 glVertex3i( x + fieldEdgeLength, 0, z ); 00091 }; 00092 glEnd(); 00093 }; 00094 00095 glEndList(); 00096 }; 00097 00098 void SIG_EnvironmentRenderer::buildGrid(GLuint number) 00099 { 00100 GLint const noOfFields = 70; 00101 00102 GLint const minX = - fieldEdgeLength * noOfFields; 00103 GLint const maxX = fieldEdgeLength * noOfFields; 00104 GLint const minZ = - fieldEdgeLength * noOfFields; 00105 GLint const maxZ = fieldEdgeLength * noOfFields; 00106 00107 glNewList(number, GL_COMPILE); 00108 00109 glFrontFace( GL_CW ); 00110 00111 glNormal3i( 0, 1, 0 ); 00112 00113 glBegin( GL_LINES ); 00114 for (GLdouble x=minX; x<=maxX; x+=fieldEdgeLength) 00115 { 00116 glVertex3d( x, 0.02, minZ ); 00117 glVertex3d( x, 0.02, maxZ ); 00118 }; 00119 00120 for (GLdouble z=minZ; z<=maxZ; z+=fieldEdgeLength) 00121 { 00122 glVertex3d( minX, 0.02, z ); 00123 glVertex3d( maxX, 0.02, z ); 00124 }; 00125 glEnd(); 00126 00127 glEndList(); 00128 }; 00129 00130 void SIG_EnvironmentRenderer::renderRobotPath() 00131 { 00132 if (robotPathPoints.count() >= 2) 00133 { 00134 glLineWidth( 2 ); 00135 glColor3d( 1, 1, 0 ); 00136 00137 glBegin( GL_LINE_STRIP ); 00138 DL_vector *actPoint = robotPathPoints.first(); 00139 while (actPoint) 00140 { 00141 glVertex3d( GLdouble( actPoint->x ), 00142 GLdouble( actPoint->y ), 00143 GLdouble( actPoint->z ) ); 00144 00145 actPoint = robotPathPoints.next(); 00146 }; 00147 glEnd(); 00148 00149 glLineWidth( 1 ); 00150 }; 00151 }; 00152 00153 void SIG_EnvironmentRenderer::render() 00154 { 00155 GLdouble xPos = static_cast< GLdouble >( std::floor( lookPoint.get( 0 ) / fieldEdgeLength ) * fieldEdgeLength ); 00156 GLdouble zPos = static_cast< GLdouble >( std::floor( lookPoint.get( 2 ) / fieldEdgeLength ) * fieldEdgeLength ); 00157 00158 glMatrixMode( GL_MODELVIEW ); 00159 glPushMatrix(); 00160 glTranslated( xPos, 00161 0, 00162 zPos ); 00163 00164 renderSceneObjects(); 00165 00166 glPopMatrix(); 00167 00168 if (showRobotPath) 00169 renderRobotPath(); 00170 }; 00171 00172 QString SIG_EnvironmentRenderer::exportToPovray() 00173 { 00174 QString resultString; 00175 QTextStream stream( &resultString, IO_WriteOnly ); 00176 00177 int xPos = static_cast< int >( std::floor( lookPoint.get( 0 ) / fieldEdgeLength ) * fieldEdgeLength ); 00178 int zPos = - static_cast< int >( std::floor( lookPoint.get( 2 ) / fieldEdgeLength ) * fieldEdgeLength ); 00179 00180 stream << "union {\n" 00181 << exportSceneObjectsToPovray() 00182 << "translate <" 00183 << xPos 00184 << ",0," 00185 << zPos 00186 << ">\n" 00187 << "}\n" 00188 << "\n"; 00189 00190 if (showRobotPath) 00191 if (robotPathPoints.count() >= 2) 00192 { 00193 DL_vector *prevPoint = robotPathPoints.first(); 00194 DL_vector *actPoint = robotPathPoints.next(); 00195 00196 while (actPoint) 00197 { 00198 NEWMAT::ColumnVector base = SIG_TypeConverter::sigelToPovray() 00199 * SIG_TypeConverter::toColumnVector( *prevPoint ); 00200 NEWMAT::ColumnVector cap = SIG_TypeConverter::sigelToPovray() 00201 * SIG_TypeConverter::toColumnVector( *actPoint ); 00202 00203 stream << "cylinder {\n" 00204 << " " 00205 << vectorToPovray( base ) 00206 << ", " 00207 << vectorToPovray( cap ) 00208 << ", 0.02\n" 00209 << " open\n" 00210 << " pigment { rgb <1,1,0> }\n" 00211 << " finish { ambient rgb <1,1,0>\n" 00212 << " diffuse 1 }\n" 00213 << "}\n"; 00214 00215 prevPoint = actPoint; 00216 actPoint = robotPathPoints.next(); 00217 }; 00218 00219 stream << "\n"; 00220 }; 00221 00222 return resultString; 00223 }; 00224 00225 QString SIG_EnvironmentRenderer::createPovrayDeclarations() 00226 { 00227 QString declarationsString; 00228 QTextStream stream( &declarationsString, IO_WriteOnly ); 00229 00230 NEWMAT::ColumnVector planeColorVector = SIG_TypeConverter::toColumnVector( sceneObjects[ 0 ]->getColor() ); 00231 00232 stream << "#declare Plane = plane {\n" 00233 << " <0,1,0>, 0\n" 00234 << " texture {\n" 00235 << " pigment { rgb " 00236 << vectorToPovray( planeColorVector ) 00237 << " }\n" 00238 << " finish { ambient rgb " 00239 << vectorToPovray( planeColorVector ) 00240 << "\n" 00241 << " diffuse 1 }\n" 00242 << " }\n" 00243 << " }\n" 00244 << "\n"; 00245 00246 int const noOfFields = 70; 00247 00248 int const minX = - fieldEdgeLength * noOfFields; 00249 int const maxX = fieldEdgeLength * noOfFields; 00250 int const minZ = - fieldEdgeLength * noOfFields; 00251 int const maxZ = fieldEdgeLength * noOfFields; 00252 00253 stream << "#declare Grid = union {\n"; 00254 00255 for (int x=minX; x<=maxX; x+=fieldEdgeLength) 00256 { 00257 stream << " cylinder {\n" 00258 << " <" 00259 << x 00260 << ",0.01," 00261 << minZ 00262 << ">, <" 00263 << x 00264 << ",0.01," 00265 << maxZ 00266 << ">, 0.02\n" 00267 << " }\n"; 00268 }; 00269 00270 for (int z=minZ; z<=maxZ; z+=fieldEdgeLength) 00271 { 00272 stream << " cylinder {\n" 00273 << " <" 00274 << minX 00275 << ",0.01," 00276 << z 00277 << ">, <" 00278 << maxX 00279 << ",0.01," 00280 << z 00281 << ">, 0.02\n" 00282 << " }\n"; 00283 }; 00284 00285 stream << "\n" 00286 << " pigment { rgb <1,0,0> }\n" 00287 << " finish { ambient rgb <1,0,0>\n" 00288 << " diffuse 1 }\n" 00289 << "}\n"; 00290 00291 return declarationsString; 00292 }; 00293 00294 void SIG_EnvironmentRenderer::setLookPoint( DL_vector newPosition ) 00295 { 00296 lookPoint = newPosition; 00297 }; 00298 00299 void SIG_EnvironmentRenderer::setRenderMode( SIG_ViewSettings::renderModeType newRenderMode ) 00300 { 00301 renderMode = newRenderMode; 00302 00303 setShowGrid( showGrid ); 00304 }; 00305 00306 void SIG_EnvironmentRenderer::setShowPlane( bool newShowPlane ) 00307 { 00308 showPlane = newShowPlane; 00309 00310 sceneObjects[ 0 ]->setVisible( showPlane ); 00311 }; 00312 00313 void SIG_EnvironmentRenderer::setShowGrid( bool newShowGrid ) 00314 { 00315 showGrid = newShowGrid; 00316 00317 sceneObjects[ 1 ]->setVisible( showGrid ); 00318 }; 00319 00320 void SIG_EnvironmentRenderer::setShowRobotPath( bool newShowRobotPath ) 00321 { 00322 showRobotPath = newShowRobotPath; 00323 }; 00324 00325 void SIG_EnvironmentRenderer::addRobotPathPoint( DL_vector newPoint ) 00326 { 00327 DL_vector *newPointObject = new DL_vector( newPoint ); 00328 00329 robotPathPoints.append( newPointObject ); 00330 }; 00331 }