00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include <structureColoring/gui/StructureGL.h>
00037 #include <GL/glu.h>
00038
00039
00040
00041 void StructureGL::flipToViewport(Vec3& vec, const Vec3& viewport) {
00042 if (viewport.dot(vec) > 0) {
00043 vec *= -1.f;
00044 }
00045 }
00046
00047
00048
00049 StructureGL::StructureGL(QWidget* parent, const QGLWidget* shareWidget, Qt::WindowFlags f) :
00050 QGLWidget(parent, shareWidget, f), mForward(false), mReverse(false), mLeft(false), mRight(false), mUp(false), mDown(
00051 false), mTurnLeft(false), mTurnRight(false) {
00052 initialize();
00053 }
00054
00055
00056
00057 StructureGL::StructureGL(QGLContext* context, QWidget* parent, const QGLWidget* shareWidget, Qt::WindowFlags f) :
00058 QGLWidget(context, parent, shareWidget, f), mForward(false), mReverse(false), mLeft(false), mRight(false), mUp(false),
00059 mDown(false), mTurnLeft(false), mTurnRight(false) {
00060 initialize();
00061 }
00062
00063
00064
00065 StructureGL::StructureGL(const QGLFormat& format, QWidget* parent, const QGLWidget* shareWidget, Qt::WindowFlags f) :
00066 QGLWidget(format, parent, shareWidget, f), mForward(false), mReverse(false), mLeft(false), mRight(false), mUp(false),
00067 mDown(false), mTurnLeft(false), mTurnRight(false) {
00068 initialize();
00069 }
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 void StructureGL::initialize() {
00080 connect(&mMoveTimer, SIGNAL(timeout()), this, SLOT(moveTimeout()));
00081 mMoveTimer.setSingleShot(false);
00082 mTranslationSpeed = 1.f;
00083 mRotationSpeed = M_PI_2;
00084 mMouseView = false;
00085 mShowPoints = false;
00086 mShowPlanes = true;
00087 mShowCylinders = true;
00088 mPointCloud = PointCloudPtr(new PointCloud());
00089 mUnsegPointCloud = PointCloudPtr(new PointCloud());
00090 mLight = true;
00091 mBumpMaps = false;
00092 mHeightMaps = false;
00093 mShowUnsegmentedPoints = true;
00094 }
00095
00096
00097
00098 void StructureGL::initializeGL() {
00099 glClearColor(1.f, 1.f, 1.f, 0.f);
00100 glClearDepth(1.f);
00101 glDisable(GL_CULL_FACE);
00102 glEnable(GL_DEPTH_TEST);
00103 glDepthFunc(GL_LEQUAL);
00104 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
00105 glShadeModel(GL_SMOOTH);
00106 glPointSize(2.f);
00107 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
00108 glDisable(GL_BLEND);
00109 glEnable(GL_TEXTURE_2D);
00110
00111
00112
00113
00114
00115 glGenTextures(1, &mNormalisationCubeMap);
00116 glBindTexture(GL_TEXTURE_CUBE_MAP, mNormalisationCubeMap);
00117 generateNormalisationCubeMap();
00118 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00119 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00120 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
00121 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
00122 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
00123 initLight();
00124 }
00125
00126
00127
00128 void StructureGL::initLight() {
00129 GLfloat ambient_matrl[] = { 1.0f, 1.0f, 1.0f, 1.0f };
00130 GLfloat diffuse_matrl[] = { 1.0f, 1.0f, 1.0f, 1.0f };
00131 glMaterialfv(GL_FRONT, GL_AMBIENT, ambient_matrl);
00132 glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse_matrl);
00133 GLfloat lightAmbient[] = { 0.5f, 0.5f, 0.5f, 1.0f };
00134 GLfloat lightDiffuse[] = { 1.f, 1.f, 1.f, 1.f };
00135 mLightPos0[0] = 0.f;
00136 mLightPos0[1] = -2.0f;
00137 mLightPos0[2] = 0.f;
00138 mLightPos0[3] = 1.f;
00139
00140 glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
00141 glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse);
00142 glLightfv(GL_LIGHT0, GL_POSITION, mLightPos0);
00143
00144 glEnable(GL_COLOR_MATERIAL);
00145 glEnable(GL_LIGHT0);
00146 GLfloat ambient_lightModel[] = { 0.25f, 0.25f, 0.25f, 1.0f };
00147 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient_lightModel);
00148
00149 }
00150
00151
00152
00153 void StructureGL::resizeGL(int width, int height) {
00154 glViewport(0, 0, width, height);
00155 glMatrixMode(GL_MODELVIEW);
00156 glLoadIdentity();
00157 glMatrixMode(GL_PROJECTION);
00158 glLoadIdentity();
00159 setPerspective(45., 1. * width / height, 0.01, 327.68);
00160 glGetDoublev(GL_PROJECTION_MATRIX, mProjM);
00161 glGetDoublev(GL_MODELVIEW_MATRIX, mModelM);
00162 glGetIntegerv(GL_VIEWPORT, mViewP);
00163 update();
00164 }
00165
00166
00167
00168 void StructureGL::paintCoordinateSystemGL() {
00169 glBegin(GL_LINES);
00170 glColor3f(1.0, 0, 0);
00171 glVertex3f(0, 0, 0);
00172 glVertex3f(-0.1, 0, 0);
00173 glColor3f(0, 1.0, 0);
00174 glVertex3f(0, 0, 0);
00175 glVertex3f(0, 0.1, 0);
00176 glColor3f(0, 0, 1.0);
00177 glVertex3f(0, 0, 0);
00178 glVertex3f(0, 0, -0.1);
00179 glEnd();
00180 }
00181
00182
00183
00184 void StructureGL::paintPlanes() {
00185 if (mPlanePatchesMutex.tryLock(10)) {
00186 if (mPlanePatches.size() > 0) {
00187 unsigned int pi = 0;
00188 glEnable(GL_ALPHA_TEST);
00189 glAlphaFunc(GL_GREATER, 0.5);
00190 for (PlanePatches::const_iterator pit = mPlanePatches.begin(); pit != mPlanePatches.end(); ++pit) {
00191 glColor4f(1.f, 1.f, 1.f, 1.f);
00192 const PlanePatch& pp = **pit;
00193 if (pi >= mPlaneTextureIDs.size()) {
00194 TexID texID = 0;
00195 glGenTextures(1, &texID);
00196 mPlaneTextureIDs.push_back(texID);
00197 glBindTexture(GL_TEXTURE_2D, texID);
00198 const cv::Mat& texMap = pp.getTextureMap();
00199 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00200 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
00201
00202 gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, texMap.cols, texMap.rows, GL_RGBA, GL_FLOAT,
00203 texMap.data);
00204 }
00205 Points vertices = pp.getBRVertices();
00206 Vec3 ppNormal = pp.getPlane3D().getPlaneNormal();
00207 Vec3 vp(-mViewpoint.getZ(), -mViewpoint.getX(), mViewpoint.getY());
00208 flipToViewport(ppNormal, vp);
00209 glActiveTexture(GL_TEXTURE0);
00210 glEnable(GL_TEXTURE_2D);
00211 glBindTexture(GL_TEXTURE_2D, mPlaneTextureIDs[pi]);
00212 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
00213 glBegin(GL_QUADS);
00214 glNormal3f(-ppNormal(1), ppNormal(2), -ppNormal(0));
00215 glTexCoord2f(0, 0);
00216 glVertex3f(-vertices[0](1), vertices[0](2), -vertices[0](0));
00217 glTexCoord2f(0, pp.getTextureHeightRatio());
00218 glVertex3f(-vertices[1](1), vertices[1](2), -vertices[1](0));
00219 glTexCoord2f(pp.getTextureWidthRatio(), pp.getTextureHeightRatio());
00220 glVertex3f(-vertices[2](1), vertices[2](2), -vertices[2](0));
00221 glTexCoord2f(pp.getTextureWidthRatio(), 0);
00222 glVertex3f(-vertices[3](1), vertices[3](2), -vertices[3](0));
00223 glEnd();
00224 ++pi;
00225 }
00226 glDisable(GL_ALPHA_TEST);
00227 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
00228 glDisable(GL_TEXTURE_2D);
00229 }
00230 mPlanePatchesMutex.unlock();
00231 }
00232 }
00233
00234
00235
00236 void StructureGL::paintBMPlanes() {
00237 const GLfloat& light_x = mLightPos0[0];
00238 const GLfloat& light_y = mLightPos0[1];
00239 const GLfloat& light_z = mLightPos0[2];
00240 if (mPlanePatchesMutex.tryLock(10)) {
00241 if (mPlanePatches.size() > 0) {
00242 unsigned int pi = 0;
00243 glEnable(GL_ALPHA_TEST);
00244 glAlphaFunc(GL_GREATER, 0.5);
00245 for (PlanePatches::const_iterator pit = mPlanePatches.begin(); pit != mPlanePatches.end(); ++pit) {
00246 const PlanePatch& pp = **pit;
00247 glColor4f(1.f, 1.f, 1.f, 1.f);
00248 if (pi >= mPlaneTextureIDs.size()) {
00249 TexID texID = 0;
00250 glGenTextures(1, &texID);
00251 mPlaneTextureIDs.push_back(texID);
00252 glBindTexture(GL_TEXTURE_2D, texID);
00253 const cv::Mat& texMap = pp.getTextureMap();
00254 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00255 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
00256 gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, texMap.cols, texMap.rows, GL_RGBA, GL_FLOAT, texMap.data);
00257 }
00258 if (pi >= mNormalMapIDs.size()) {
00259 TexID texID = 0;
00260 glGenTextures(1, &texID);
00261 mNormalMapIDs.push_back(texID);
00262 glBindTexture(GL_TEXTURE_2D, texID);
00263 const cv::Mat& texMap = pp.getNormalMap();
00264 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00265 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
00266 gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, texMap.cols, texMap.rows, GL_RGB, GL_FLOAT, texMap.data);
00267 }
00268 Points vertices = pp.getBRVertices();
00269 Vec3 ppNormal = pp.getPlane3D().getPlaneNormal();
00270 Vec3 vp(-mViewpoint.getZ(), -mViewpoint.getX(), mViewpoint.getY());
00271 flipToViewport(ppNormal, vp);
00272
00273
00274
00275
00276 glActiveTexture(GL_TEXTURE0);
00277 glEnable(GL_TEXTURE_CUBE_MAP);
00278 glBindTexture(GL_TEXTURE_CUBE_MAP, mNormalisationCubeMap);
00279 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
00280 glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE) ;
00281 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE) ;
00282
00283
00284
00285
00286
00287
00288 glActiveTexture(GL_TEXTURE1);
00289 glEnable(GL_TEXTURE_2D);
00290 glBindTexture(GL_TEXTURE_2D, mNormalMapIDs[pi]);
00291 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
00292 glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_DOT3_RGB);
00293 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS);
00294 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_TEXTURE);
00295
00296
00297
00298
00299 glActiveTexture(GL_TEXTURE2);
00300 glEnable(GL_TEXTURE_2D);
00301 glBindTexture(GL_TEXTURE_2D, mPlaneTextureIDs[pi]);
00302 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
00303
00304 GLfloat vertex_to_light_x, vertex_to_light_y, vertex_to_light_z;
00305 glBegin(GL_QUADS);
00306 for(unsigned int i = 0; i < 4; i++){
00307 const float& current_vertex_x = -vertices[i](1);
00308 const float& current_vertex_y = vertices[i](2);
00309 const float& current_vertex_z = -vertices[i](0);
00310 float current_texcoord_s = 0;
00311 if (i > 1) current_texcoord_s += pp.getTextureWidthRatio();
00312 float current_texcoord_t = 0;
00313 if (i == 1 || i == 2) current_texcoord_t += pp.getTextureHeightRatio();
00314 vertex_to_light_x = current_vertex_x - light_x;
00315 vertex_to_light_y = current_vertex_y - light_y;
00316 vertex_to_light_z = current_vertex_z - light_z;
00317
00318
00319
00320
00321 glMultiTexCoord3f(GL_TEXTURE0, vertex_to_light_x, vertex_to_light_y, vertex_to_light_z);
00322
00323 glMultiTexCoord2f(GL_TEXTURE1, current_texcoord_s, current_texcoord_t);
00324 glMultiTexCoord2f(GL_TEXTURE2, current_texcoord_s, current_texcoord_t);
00325 glVertex3f(current_vertex_x, current_vertex_y, current_vertex_z);
00326 }
00327 glEnd();
00328 ++pi;
00329 }
00330 }
00331 glDisable( GL_ALPHA_TEST);
00332 glActiveTexture( GL_TEXTURE2 );
00333 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
00334 glDisable( GL_TEXTURE_2D);
00335 glActiveTexture( GL_TEXTURE1 );
00336 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
00337 glDisable(GL_TEXTURE_2D);
00338 glActiveTexture( GL_TEXTURE0 );
00339 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
00340 glDisable(GL_TEXTURE_CUBE_MAP);
00341 mPlanePatchesMutex.unlock();
00342 }
00343 }
00344
00345
00346
00347 void StructureGL::paintHeightMapPlanes() {
00348 if (mPlanePatchesMutex.tryLock(10)) {
00349 if (mPlanePatches.size() > 0) {
00350 unsigned int pi = 0;
00351 glEnable(GL_ALPHA_TEST);
00352 glAlphaFunc(GL_GREATER, 0.5);
00353 for (PlanePatches::const_iterator pit = mPlanePatches.begin(); pit != mPlanePatches.end(); ++pit) {
00354 const PlanePatch& pp = **pit;
00355 glColor4f(1.f, 1.f, 1.f, 1.f);
00356 if (pi >= mPlaneAlphaIDs.size()) {
00357 TexID texID = 0;
00358 glGenTextures(1, &texID);
00359 mPlaneAlphaIDs.push_back(texID);
00360 glBindTexture(GL_TEXTURE_2D, texID);
00361 const cv::Mat& texMap = pp.getTextureMap();
00362 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00363 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
00364 gluBuild2DMipmaps(GL_TEXTURE_2D, GL_ALPHA, texMap.cols, texMap.rows, GL_RGBA, GL_FLOAT, texMap.data);
00365 }
00366 if (pi >= mHeightMapIDs.size()) {
00367 TexID texID = 0;
00368 glGenTextures(1, &texID);
00369 mHeightMapIDs.push_back(texID);
00370 glBindTexture(GL_TEXTURE_2D, texID);
00371 const cv::Mat& texMap = pp.getHeightMap();
00372 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00373 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
00374 gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, texMap.cols, texMap.rows, GL_LUMINANCE, GL_FLOAT, texMap.data);
00375 }
00376 Points vertices = pp.getBRVertices();
00377 Vec3 ppNormal = pp.getPlane3D().getPlaneNormal();
00378 Vec3 vp(-mViewpoint.getZ(), -mViewpoint.getX(), mViewpoint.getY());
00379 flipToViewport(ppNormal, vp);
00380
00381 glActiveTexture(GL_TEXTURE0);
00382 glEnable(GL_TEXTURE_2D);
00383 glBindTexture(GL_TEXTURE_2D, mHeightMapIDs[pi]);
00384 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
00385
00386 glActiveTexture(GL_TEXTURE1);
00387 glEnable(GL_TEXTURE_2D);
00388 glBindTexture(GL_TEXTURE_2D, mPlaneAlphaIDs[pi]);
00389 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
00390
00391 glBegin(GL_QUADS);
00392 glNormal3f(-ppNormal(1), ppNormal(2), -ppNormal(0));
00393 glMultiTexCoord2f(GL_TEXTURE0, 0, 0);
00394 glMultiTexCoord2f(GL_TEXTURE1, 0, 0);
00395 glVertex3f(-vertices[0](1), vertices[0](2), -vertices[0](0));
00396 glMultiTexCoord2f(GL_TEXTURE0, 0, pp.getTextureHeightRatio());
00397 glMultiTexCoord2f(GL_TEXTURE1, 0, pp.getTextureHeightRatio());
00398 glVertex3f(-vertices[1](1), vertices[1](2), -vertices[1](0));
00399 glMultiTexCoord2f(GL_TEXTURE0, pp.getTextureWidthRatio(), pp.getTextureHeightRatio());
00400 glMultiTexCoord2f(GL_TEXTURE1, pp.getTextureWidthRatio(), pp.getTextureHeightRatio());
00401 glVertex3f(-vertices[2](1), vertices[2](2), -vertices[2](0));
00402 glMultiTexCoord2f(GL_TEXTURE0, pp.getTextureWidthRatio(), 0);
00403 glMultiTexCoord2f(GL_TEXTURE1, pp.getTextureWidthRatio(), 0);
00404 glVertex3f(-vertices[3](1), vertices[3](2), -vertices[3](0));
00405 glEnd();
00406 ++pi;
00407 }
00408 }
00409 glDisable(GL_ALPHA_TEST);
00410 glActiveTexture(GL_TEXTURE1);
00411 glDisable(GL_TEXTURE_2D);
00412 glActiveTexture(GL_TEXTURE0);
00413 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
00414 glDisable(GL_TEXTURE_2D);
00415 mPlanePatchesMutex.unlock();
00416 }
00417 }
00418
00419
00420
00421 void StructureGL::paintCylinders(){
00422 if(mCylinderPatchesMutex.tryLock(10)){
00423 float rgb[3] = {0.f, 0.f, 1.f};
00424 unsigned int ci=0;
00425 for(CylinderPatches::const_iterator cit = mCylinderPatches.begin(); cit != mCylinderPatches.end(); ++cit){
00426 getColorByIndex(rgb, ci, mCylinderPatches.size());
00427 const CylinderPatch& cp = **cit;
00428 glColor3f(rgb[1],rgb[0],rgb[2]);
00429 glDisable(GL_TEXTURE_2D);
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441 const Points& vertices3 = cp.getBBVertices();
00442 glBegin(GL_QUAD_STRIP);
00443 glVertex3f(-vertices3[0](1),vertices3[0](2),-vertices3[0](0));
00444 glVertex3f(-vertices3[1](1),vertices3[1](2),-vertices3[1](0));
00445 glVertex3f(-vertices3[2](1),vertices3[2](2),-vertices3[2](0));
00446 glVertex3f(-vertices3[3](1),vertices3[3](2),-vertices3[3](0));
00447 glVertex3f(-vertices3[4](1),vertices3[4](2),-vertices3[4](0));
00448 glVertex3f(-vertices3[5](1),vertices3[5](2),-vertices3[5](0));
00449 glVertex3f(-vertices3[6](1),vertices3[6](2),-vertices3[6](0));
00450 glVertex3f(-vertices3[7](1),vertices3[7](2),-vertices3[7](0));
00451 glEnd();
00452 glBegin(GL_QUAD_STRIP);
00453 glVertex3f(-vertices3[4](1),vertices3[4](2),-vertices3[4](0));
00454 glVertex3f(-vertices3[2](1),vertices3[2](2),-vertices3[2](0));
00455 glVertex3f(-vertices3[6](1),vertices3[6](2),-vertices3[6](0));
00456 glVertex3f(-vertices3[0](1),vertices3[0](2),-vertices3[0](0));
00457 glVertex3f(-vertices3[7](1),vertices3[7](2),-vertices3[7](0));
00458 glVertex3f(-vertices3[1](1),vertices3[1](2),-vertices3[1](0));
00459 glVertex3f(-vertices3[5](1),vertices3[5](2),-vertices3[5](0));
00460 glVertex3f(-vertices3[3](1),vertices3[3](2),-vertices3[3](0));
00461 glEnd();
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509 }
00510 mCylinderPatchesMutex.unlock();
00511 }
00512 }
00513
00514
00515
00516 void StructureGL::paintPointsGL() {
00517 if (mPointCloudMutex.tryLock(50)) {
00518 if (mPointCloud != NULL) {
00519 glBegin(GL_POINTS);
00520 for (unsigned int i = 0; i < mPointCloud->points.size(); i++) {
00521 PointT p = mPointCloud->points[i];
00522 int rgb = *reinterpret_cast<int*> (&p.rgb);
00523 glColor3ub((rgb >> 16) & 0xff, (rgb >> 8) & 0xff, rgb & 0xff);
00524 glVertex3f(-p.y, p.z, -p.x);
00525 }
00526 glEnd();
00527 }
00528 mPointCloudMutex.unlock();
00529 }
00530 }
00531
00532
00533
00534 void StructureGL::paintUnsegPointsGL() {
00535 if (mPointCloudMutex.tryLock(50)) {
00536 if (mUnsegPointCloud != NULL) {
00537 glBegin(GL_POINTS);
00538 for (unsigned int i = 0; i < mUnsegPointCloud->points.size(); i++) {
00539 PointT p = mUnsegPointCloud->points[i];
00540 int rgb = *reinterpret_cast<int*> (&p.rgb);
00541 glColor3ub((rgb >> 16) & 0xff, (rgb >> 8) & 0xff, rgb & 0xff);
00542 glVertex3f(-p.y, p.z, -p.x);
00543 }
00544 glEnd();
00545 }
00546 mPointCloudMutex.unlock();
00547 }
00548 }
00549
00550
00551
00552 void StructureGL::paintGL() {
00553 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00554 glMatrixMode(GL_MODELVIEW);
00555 glLoadIdentity();
00556 mViewpoint.applyGL();
00557 glGetDoublev(GL_MODELVIEW_MATRIX, mModelM);
00558 if (mLight)
00559 glEnable(GL_LIGHTING);
00560 else
00561 glDisable(GL_LIGHTING);
00562
00563 glLightfv(GL_LIGHT0, GL_POSITION, mLightPos0);
00564
00565 paintCoordinateSystemGL();
00566 if (mShowPoints)
00567 paintPointsGL();
00568 if (mShowUnsegmentedPoints)
00569 paintUnsegPointsGL();
00570 if (mShowPlanes) {
00571 if (mBumpMaps)
00572 paintBMPlanes();
00573 else if (mHeightMaps)
00574 paintHeightMapPlanes();
00575 else
00576 paintPlanes();
00577 }
00578 if (mShowCylinders) paintCylinders();
00579
00580 GLenum errCode;
00581 const GLubyte *errString;
00582 if ((errCode = glGetError()) != GL_NO_ERROR) {
00583 errString = gluErrorString(errCode);
00584 std::cerr << "OpenGL Error (paintGL): " << errString << std::endl;
00585 exit(1);
00586 }
00587
00588 if (!mTexIDsToRemove.empty()){
00589 removedMarkedTextures();
00590 }
00591 }
00592
00593
00594
00595 void StructureGL::moveTimeout() {
00596 std::clock_t tick = clock();
00597 float dt = (float) (mLastTick - tick) / (float) CLOCKS_PER_SEC;
00598 float forward = 0.f, up = 0.f, side = 0.f, turn = 0.f;
00599 if (mForward)
00600 forward -= 1.f;
00601 if (mReverse)
00602 forward += 1.f;
00603 if (mLeft)
00604 side += 1.f;
00605 if (mRight)
00606 side -= 1.f;
00607 if (mUp)
00608 up -= 1.f;
00609 if (mDown)
00610 up += 1.f;
00611 if (mTurnLeft)
00612 turn -= 1.f;
00613 if (mTurnRight)
00614 turn += 1.f;
00615 forward *= mTranslationSpeed;
00616 up *= mTranslationSpeed;
00617 side *= mTranslationSpeed;
00618 turn *= mRotationSpeed;
00619 mViewpoint.setVelocity(forward, up, side, turn);
00620 mViewpoint.applyVelocity(dt);
00621 mLastTick = tick;
00622 updateGL();
00623 }
00624
00625
00626
00627 void StructureGL::setPerspective(double fovy, double aspect, double zNear, double zFar) {
00628 gluPerspective(fovy, aspect, zNear, zFar);
00629 }
00630
00631
00632
00633 void StructureGL::keyPressEvent(QKeyEvent* event) {
00634 if (!QApplication::keyboardModifiers()) {
00635 switch (event->key()) {
00636 case Qt::Key_Space:
00637 mUp = true;
00638 mDown = false;
00639 event->accept();
00640 break;
00641 case Qt::Key_A:
00642 mLeft = true;
00643 mRight = false;
00644 event->accept();
00645 break;
00646 case Qt::Key_B:
00647 mBumpMaps = !mBumpMaps;
00648 mHeightMaps = false;
00649 resetTextures(mPlaneTextureIDs);
00650 event->accept();
00651 update();
00652 break;
00653 case Qt::Key_C:
00654 mDown = true;
00655 mUp = false;
00656 event->accept();
00657 break;
00658 case Qt::Key_D:
00659 mRight = true;
00660 mLeft = false;
00661 event->accept();
00662 break;
00663 case Qt::Key_H:
00664 mHeightMaps = !mHeightMaps;
00665 mBumpMaps = false;
00666 resetTextures(mPlaneTextureIDs);
00667 event->accept();
00668 update();
00669 break;
00670 case Qt::Key_L:
00671 mLight = !mLight;
00672 event->accept();
00673 update();
00674 break;
00675 case Qt::Key_P:
00676 if (mShowPoints){
00677 mShowPoints = false;
00678 mShowPlanes = true;
00679 } else if (mShowPlanes){
00680 mShowPlanes = false;
00681 mShowPoints = true;
00682 } else {
00683 mShowPoints = true;
00684 }
00685 event->accept();
00686 update();
00687 break;
00688 case Qt::Key_S:
00689 mReverse = true;
00690 mForward = false;
00691 event->accept();
00692 break;
00693 case Qt::Key_U:
00694 mShowUnsegmentedPoints = !mShowUnsegmentedPoints;
00695 event->accept();
00696 break;
00697 case Qt::Key_W:
00698 mForward = true;
00699 mReverse = false;
00700 event->accept();
00701 break;
00702 default:
00703 event->ignore();
00704 break;
00705 }
00706 } else
00707 event->ignore();
00708 if ((mForward || mReverse || mLeft || mRight || mUp || mDown || mTurnLeft || mTurnRight) && !mMoveTimer.isActive()) {
00709 mLastTick = clock();
00710 mMoveTimer.start(0);
00711 }
00712 }
00713
00714
00715
00716 void StructureGL::keyReleaseEvent(QKeyEvent* event) {
00717 if (!event->isAutoRepeat()) {
00718 switch (event->key()) {
00719 case Qt::Key_W:
00720 mForward = false;
00721 mReverse = false;
00722 event->accept();
00723 break;
00724 case Qt::Key_S:
00725 mReverse = false;
00726 mForward = false;
00727 event->accept();
00728 break;
00729 case Qt::Key_A:
00730 mLeft = false;
00731 mRight = false;
00732 event->accept();
00733 break;
00734 case Qt::Key_D:
00735 mRight = false;
00736 mLeft = false;
00737 event->accept();
00738 break;
00739 case Qt::Key_Space:
00740 mUp = false;
00741 mDown = false;
00742 event->accept();
00743 break;
00744 case Qt::Key_C:
00745 mDown = false;
00746 mUp = false;
00747 event->accept();
00748 break;
00749 default:
00750 event->ignore();
00751 break;
00752 }
00753 } else
00754 event->ignore();
00755 if (!(mForward || mReverse || mLeft || mRight || mUp || mDown || mTurnLeft || mTurnRight) && mMoveTimer.isActive()) {
00756 mMoveTimer.stop();
00757 }
00758 }
00759
00760
00761
00762 void StructureGL::mousePressEvent(QMouseEvent* event) {
00763 if (event->button() == Qt::RightButton) {
00764 mMouseView = true;
00765 mStartPos = event->pos();
00766 mYawStart = mViewpoint.getYaw();
00767 mPitchStart = mViewpoint.getPitch();
00768 event->accept();
00769 } else
00770 event->ignore();
00771 }
00772
00773
00774
00775 void StructureGL::mouseReleaseEvent(QMouseEvent* event) {
00776 if (event->button() == Qt::RightButton) {
00777 mMouseView = false;
00778 event->accept();
00779 } else
00780 event->ignore();
00781 }
00782
00783
00784
00785 void StructureGL::mouseMoveEvent(QMouseEvent* event) {
00786 if (mMouseView) {
00787 static const float LOOK_SENSITIVITY = -0.01f;
00788 static const float FREE_TURN_SENSITIVITY = -0.01f;
00789
00790 float turn = FREE_TURN_SENSITIVITY * (event->pos().x() - mStartPos.x());
00791 float tilt = LOOK_SENSITIVITY * (event->pos().y() - mStartPos.y());
00792 mViewpoint.setYaw(mYawStart + turn);
00793 mViewpoint.setPitch(mPitchStart + tilt);
00794 updateGL();
00795 event->accept();
00796 }
00797 if (event->buttons() & Qt::LeftButton)
00798 event->ignore();
00799 }
00800
00801
00802
00803 void StructureGL::wheelEvent(QWheelEvent* event) {
00804 static const float WHEEL_SENSITIVITY = 0.01f;
00805 mViewpoint.translate(0.0f, event->delta() * WHEEL_SENSITIVITY * mTranslationSpeed / 15.0f, 0.0f);
00806 updateGL();
00807 }
00808
00809
00810
00811 void StructureGL::getColorByIndex(float *rgb, unsigned int index, unsigned int total) {
00812 float t = static_cast<float> (index) / static_cast<float> (total);
00813
00814 if (t == 1.0) {
00815 rgb[0] = 1.0f;
00816 rgb[1] = 0.0f;
00817 rgb[2] = 0.0f;
00818 } else if (t < 1.0 && t >= 0.83333) {
00819 rgb[0] = 1.0f;
00820 rgb[1] = 1.0f - (t - 0.83333) / 0.16666;
00821 rgb[2] = 0.0f;
00822 } else if (t < 0.83333 && t >= 0.66666) {
00823 rgb[0] = (t - 0.66666) / 0.16666;
00824 rgb[1] = 1.0f;
00825 rgb[2] = 0.0f;
00826 } else if (t < 0.66666 && t >= 0.5) {
00827 rgb[0] = 0.0f;
00828 rgb[1] = 1.0f;
00829 rgb[2] = 1.0f - (t - 0.5) / 0.16666;
00830 } else if (t < 0.5 && t >= 0.33333) {
00831 rgb[0] = 0.0f;
00832 rgb[1] = 1.0f - (t - 0.33333) / 0.16666;
00833 rgb[2] = 1.0f;
00834 } else if (t < 0.33333 && t >= 0.16666) {
00835 rgb[0] = (t - 0.16666) / 0.16666;
00836 rgb[1] = 0.0f;
00837 rgb[2] = 1.0f;
00838 } else {
00839 rgb[0] = 1.0f;
00840 rgb[1] = 0.0f;
00841 rgb[2] = 1.0f - t / 0.16666;
00842 }
00843 }
00844
00845
00846
00847 void StructureGL::generateNormalisationCubeMap() {
00848
00849
00850
00851 unsigned char * data = new unsigned char[32 * 32 * 3];
00852 if (!data) {
00853
00854 return;
00855 }
00856
00857 int size = 32;
00858 float offset = 0.5f;
00859 float halfSize = 16.0f;
00860 Vec3 tempVector;
00861 unsigned char * bytePtr;
00862
00863
00864
00865 bytePtr = data;
00866
00867 for (int j = 0; j < size; j++) {
00868 for (int i = 0; i < size; i++) {
00869
00870 tempVector.x() = halfSize;
00871 tempVector.y() = - (j + offset - halfSize);
00872 tempVector.z() = - (i + offset - halfSize);
00873
00874 tempVector.normalize();
00875 packTo01(tempVector);
00876 bytePtr[0] = (unsigned char) (tempVector.x() * 255);
00877 bytePtr[1] = (unsigned char) (tempVector.y() * 255);
00878 bytePtr[2] = (unsigned char) (tempVector.z() * 255);
00879 bytePtr += 3;
00880 }
00881 }
00882
00883 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGBA8, size, size, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
00884
00885
00886 bytePtr = data;
00887
00888 for (int j = 0; j < size; j++) {
00889 for (int i = 0; i < size; i++) {
00890
00891 tempVector.x() = (i + offset - halfSize);
00892 tempVector.y() = halfSize;
00893 tempVector.z() = (j + offset - halfSize);
00894
00895 tempVector.normalize();
00896 packTo01(tempVector);
00897 bytePtr[0] = (unsigned char) (tempVector.x() * 255);
00898 bytePtr[1] = (unsigned char) (tempVector.y() * 255);
00899 bytePtr[2] = (unsigned char) (tempVector.z() * 255);
00900 bytePtr += 3;
00901 }
00902 }
00903
00904 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGBA8, size, size, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
00905
00906
00907 bytePtr = data;
00908
00909 for (int j = 0; j < size; j++) {
00910 for (int i = 0; i < size; i++) {
00911
00912 tempVector.x() = (i + offset - halfSize);
00913 tempVector.y() = - (j + offset - halfSize);
00914 tempVector.z() = halfSize;
00915
00916 tempVector.normalize();
00917 packTo01(tempVector);
00918 bytePtr[0] = (unsigned char) (tempVector.x() * 255);
00919 bytePtr[1] = (unsigned char) (tempVector.y() * 255);
00920 bytePtr[2] = (unsigned char) (tempVector.z() * 255);
00921 bytePtr += 3;
00922 }
00923 }
00924
00925 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGBA8, size, size, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
00926
00927
00928 bytePtr = data;
00929
00930 for (int j = 0; j < size; j++) {
00931 for (int i = 0; i < size; i++) {
00932
00933 tempVector.x() = - halfSize;
00934 tempVector.y() = - (j + offset - halfSize);
00935 tempVector.z() = (i + offset - halfSize);
00936
00937 tempVector.normalize();
00938 packTo01(tempVector);
00939 bytePtr[0] = (unsigned char) (tempVector.x() * 255);
00940 bytePtr[1] = (unsigned char) (tempVector.y() * 255);
00941 bytePtr[2] = (unsigned char) (tempVector.z() * 255);
00942 bytePtr += 3;
00943 }
00944 }
00945
00946 glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGBA8, size, size, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
00947
00948
00949 bytePtr = data;
00950
00951 for (int j = 0; j < size; j++) {
00952 for (int i = 0; i < size; i++) {
00953
00954 tempVector.x() = (i + offset - halfSize);
00955 tempVector.y() = - halfSize;
00956 tempVector.z() = - (j + offset - halfSize);
00957
00958 tempVector.normalize();
00959 packTo01(tempVector);
00960 bytePtr[0] = (unsigned char) (tempVector.x() * 255);
00961 bytePtr[1] = (unsigned char) (tempVector.y() * 255);
00962 bytePtr[2] = (unsigned char) (tempVector.z() * 255);
00963 bytePtr += 3;
00964 }
00965 }
00966
00967 glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGBA8, size, size, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
00968
00969
00970 bytePtr = data;
00971
00972 for (int j = 0; j < size; j++) {
00973 for (int i = 0; i < size; i++) {
00974
00975 tempVector.x() = - (i + offset - halfSize);
00976 tempVector.y() = - (j + offset - halfSize);
00977 tempVector.z() = - halfSize;
00978
00979 tempVector.normalize();
00980 packTo01(tempVector);
00981 bytePtr[0] = (unsigned char) (tempVector.x() * 255);
00982 bytePtr[1] = (unsigned char) (tempVector.y() * 255);
00983 bytePtr[2] = (unsigned char) (tempVector.z() * 255);
00984 bytePtr += 3;
00985 }
00986 }
00987
00988 glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGBA8, size, size, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
00989
00990
00991 delete[] data;
00992
00993 return;
00994 }
00995
00996
00997 void StructureGL::resetAllPlaneTextures(){
00998 resetTextures(mPlaneTextureIDs);
00999 resetTextures(mNormalMapIDs);
01000 resetTextures(mHeightMapIDs);
01001 resetTextures(mPlaneAlphaIDs);
01002 }
01003
01004
01005
01006 void StructureGL::resetTextures(TexIDs& textureIDs){
01007 if(mPlaneTextureMutex.tryLock()){
01008 for (TexIDs::const_iterator tex_it = textureIDs.begin(); tex_it != textureIDs.end(); ++tex_it){
01009 mTexIDsToRemove.push_back(*tex_it);
01010 }
01011 textureIDs.clear();
01012 mPlaneTextureMutex.unlock();
01013 }
01014 }
01015
01016
01017
01018 void StructureGL::removedMarkedTextures(){
01019 if(mPlaneTextureMutex.tryLock(10)){
01020 for(TexIDs::const_iterator tex_it = mTexIDsToRemove.begin(); tex_it != mTexIDsToRemove.end(); ++tex_it){
01021 glDeleteTextures(1, &*tex_it);
01022 }
01023 mTexIDsToRemove.clear();
01024 mPlaneTextureMutex.unlock();
01025 }
01026 }
01027
01028
01029
01030 void StructureGL::Viewpoint::translate(float forward, float up, float sideways) {
01031 mZ -= forward * cos(mYaw) + sideways * sin(mYaw);
01032 mX -= forward * sin(mYaw) - sideways * cos(mYaw);
01033 mY += up;
01034 }
01035
01036
01037
01038 void StructureGL::Viewpoint::rotate(float yaw, float pitch) {
01039 mYaw += yaw;
01040 mPitch += pitch;
01041 if (mPitch < -M_PI_2)
01042 mPitch = -M_PI_2;
01043 if (mPitch > M_PI_2)
01044 mPitch = M_PI_2;
01045 }
01046
01047
01048
01049 void StructureGL::Viewpoint::applyGL() {
01050 glRotatef(-180.0f * mYaw / M_PI, 0.0f, 1.0f, 0.0f);
01051 glRotatef(-180.0f * mPitch / M_PI, cos(mYaw), 0.0f, -sin(mYaw));
01052 glTranslatef(-mX, -mY, -mZ);
01053 }
01054
01055
01056
01057 void StructureGL::Viewpoint::applyVelocity(float dt) {
01058 rotate(mVelTurn * dt, 0.0f);
01059 translate(mVelForward * dt, mVelUp * dt, mVelSide * dt);
01060 }
01061