00001 #define AUX
00002
00003 #include <assert.h>
00004 #include "IGS_Controller.h"
00005 #include <stdio.h>
00006 #include "opengl/glos.h"
00007 #include <gl/gl.h>
00008 #include <gl/glu.h>
00009 #ifndef NOAUX
00010 #include <gl/glaux.h>
00011 #endif
00012 #include "synchronization/guid.h"
00013 #include "synchronization/semaphore.h"
00014
00015 int gGlGuid = GuidGenerator::GenerateNewGuid();
00016 Semaphore gGlSemaphore( gGlGuid );
00017
00018 float gDefaultClearR = 0.8f;
00019 float gDefaultClearG = 0.8f;
00020 float gDefaultClearB = 1.0f;
00021 float gDefaultClearA = 1.0f;
00022
00023
00024
00025
00026
00027
00028
00029 IGS_Controller::IGS_Controller():
00030 mCameraEye( 0, 0, 1 ),
00031 mCameraTarget( 0, 0, 0 ),
00032 mCameraUp( 0, 1, 0 ),
00033 mColorDefault( NULL ),
00034 mFrameNumber( 0 ),
00035 mGlClearBits( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ),
00036 mHdc( NULL ),
00037 mHwnd( NULL ),
00038 mSettingAlphaBlendingOn( true ),
00039 mSettingAnimationOn( true ),
00040 m_SettingAntiAliasingOn( true ),
00041 mSettingDebugMessageLevel( 0 ),
00042 mSettingDepthTestingOn( true ),
00043 mSettingDoubleBufferingOn( true ),
00044 mSettingLightingEnabled( false ),
00045 mSettingNormalsRelativeToCamera( true ),
00046 m_SettingPointSize( 5.0 ),
00047 mSettingRenderNormals( false ),
00048 mSettingRenderNormalsAsColors( false ),
00049 mSettingRenderSilhouettes( true ),
00050 mSettingRenderStyle( G_COLOR ),
00051 mSettingShadingModel( GL_SMOOTH ),
00052 mSettingSilhouetteEdgeWidth( 2.0 ),
00053 mSettingTexturingOn( false ),
00054 m_SettingWindowXmin( -0.1 ),
00055 m_SettingWindowXmax( 1.1 ),
00056 m_SettingWindowYmin( -0.1 ),
00057 m_SettingWindowYmax( 1.1 ),
00058 m_SettingWindowZmin( -1000.0 ),
00059 m_SettingWindowZmax( 1000.0 ),
00060 mStateScreenCaptureDone( false ),
00061 mWindowResolutionX( 100 ),
00062 mWindowResolutionY( 100 )
00063 {
00064
00065 this->SetRenderStyle( this->mSettingRenderStyle );
00066
00067
00068 mGbufferColor.SetFormat( GL_RGB );
00069 mGbufferDepth.SetFormat( GL_DEPTH );
00070
00071
00072 mGbufferColor.SetResolution( this->mWindowResolutionX, this->mWindowResolutionY );
00073 mGbufferDepth.SetResolution( this->mWindowResolutionX, this->mWindowResolutionY );
00074
00075
00076 this->mColorDefault = new unsigned char[ 3 ];
00077 mColorDefault[ 0 ] = 255;
00078 mColorDefault[ 1 ] = 255;
00079 mColorDefault[ 2 ] = 255;
00080
00081
00082 mColorClear[ 0 ] = gDefaultClearR;
00083 mColorClear[ 1 ] = gDefaultClearG;
00084 mColorClear[ 2 ] = gDefaultClearB;
00085 mColorClear[ 3 ] = gDefaultClearA;
00086 }
00087
00088
00089
00090
00091
00092
00093 IGS_Controller::~IGS_Controller()
00094 {
00095 delete[] this->mColorDefault;
00096 }
00097
00098
00099
00100
00101
00102
00103 void IGS_Controller::BeginFrame() const
00104 {
00105
00106
00107 static int frameCount = 0;
00108 frameCount++;
00109 GLenum errorcode = glGetError();
00110
00111
00112
00113 #ifdef NOAUX
00114
00115 this->mHdc = ::GetDC( this->mHwnd );
00116 static bool alreadyCurrent = false;
00117 if( !alreadyCurrent )
00118 {
00119 BOOL success = wglMakeCurrent ( this->mHdc, this->mHglrc );
00120 assert( success != FALSE ) ;
00121 alreadyCurrent = true;
00122 }
00123
00124 #endif
00125
00126 this->mFrameNumber++;
00127 if( mSettingDebugMessageLevel > 0 )
00128 {
00129 ::printf( "Beginning Frame %d\n", this->FrameNumber() );
00130 }
00131
00132
00133 ::glViewport( 0, 0, mWindowResolutionX, mWindowResolutionY );
00134 errorcode = ::glGetError();
00135 IJG_Assert( errorcode == GL_NO_ERROR );
00136
00137
00138 ::glMatrixMode( GL_PROJECTION );
00139 errorcode = ::glGetError();
00140 assert( errorcode == GL_NO_ERROR );
00141
00142 ::glLoadIdentity();
00143 errorcode = ::glGetError();
00144 assert( errorcode == GL_NO_ERROR );
00145
00146 ::glOrtho( m_SettingWindowXmin, m_SettingWindowXmax, m_SettingWindowYmin, m_SettingWindowYmax, m_SettingWindowZmin, m_SettingWindowZmax );
00147 errorcode = ::glGetError();
00148 assert( errorcode == GL_NO_ERROR );
00149
00150 ::glMatrixMode( GL_MODELVIEW );
00151 errorcode = ::glGetError();
00152 assert( errorcode == GL_NO_ERROR );
00153
00154 ::glLoadIdentity();
00155 errorcode = ::glGetError();
00156 assert( errorcode == GL_NO_ERROR );
00157
00158 ::gluLookAt
00159 (
00160 this->mCameraEye[ 0 ], this->mCameraEye[ 1 ], this->mCameraEye[ 2 ],
00161 this->mCameraTarget[ 0 ], this->mCameraTarget[ 1 ], this->mCameraTarget[ 2 ],
00162 this->mCameraUp[ 0 ], this->mCameraUp[ 1 ], this->mCameraUp[ 2 ]
00163 );
00164 errorcode = ::glGetError();
00165 assert( errorcode == GL_NO_ERROR );
00166
00167 ::glShadeModel( this->mSettingShadingModel );
00168 errorcode = ::glGetError();
00169 assert( errorcode == GL_NO_ERROR );
00170
00171
00172 if( this->mSettingAlphaBlendingOn == true )
00173 {
00174 ::glEnable( GL_BLEND );
00175 errorcode = ::glGetError();
00176 assert( errorcode == GL_NO_ERROR );
00177
00178 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
00179 errorcode = ::glGetError();
00180 assert( errorcode == GL_NO_ERROR );
00181 }
00182 else
00183 {
00184 ::glDisable( GL_BLEND );
00185 errorcode = ::glGetError();
00186 assert( errorcode == GL_NO_ERROR );
00187 }
00188
00189
00190 if( mSettingDepthTestingOn )
00191 {
00192 ::glEnable( GL_DEPTH_TEST );
00193 errorcode = ::glGetError();
00194 assert( errorcode == GL_NO_ERROR );
00195
00196 ::glDepthFunc( GL_LEQUAL );
00197 errorcode = ::glGetError();
00198 assert( errorcode == GL_NO_ERROR );
00199 }
00200 else
00201 {
00202 ::glDisable( GL_DEPTH_TEST );
00203 errorcode = ::glGetError();
00204 assert( errorcode == GL_NO_ERROR );
00205 }
00206
00207
00208 if( this->mSettingTexturingOn == true )
00209 {
00210 ::glEnable( GL_TEXTURE_2D );
00211 errorcode = ::glGetError();
00212 assert( errorcode == GL_NO_ERROR );
00213 }
00214 else
00215 {
00216 ::glDisable( GL_TEXTURE_2D );
00217 errorcode = ::glGetError();
00218 assert( errorcode == GL_NO_ERROR );
00219 }
00220
00221 glClearColor
00222 (
00223 this->mColorClear[ 0 ],
00224 this->mColorClear[ 1 ],
00225 this->mColorClear[ 2 ],
00226 this->mColorClear[ 3 ]
00227 );
00228 ::glClear( this->mGlClearBits );
00229 errorcode = ::glGetError();
00230 assert( errorcode == GL_NO_ERROR );
00231
00232 ::glColor3ubv( this->mColorDefault );
00233 errorcode = ::glGetError();
00234 assert( errorcode == GL_NO_ERROR );
00235
00236
00237 this->DoLighting();
00238
00239
00240 ::glPointSize( m_SettingPointSize );
00241
00242 if( m_SettingAntiAliasingOn == true )
00243 {
00244 ::glEnable( GL_POINT_SMOOTH );
00245 errorcode = ::glGetError();
00246 assert( errorcode == GL_NO_ERROR );
00247
00248 ::glEnable( GL_LINE_SMOOTH );
00249 errorcode = ::glGetError();
00250 assert( errorcode == GL_NO_ERROR );
00251 }
00252 }
00253
00254
00255
00256
00257
00258
00259 Segment IGS_Controller::ConvertMouseClick( const int x, const int y ) const
00260 {
00261
00262
00263
00264 RECT rect;
00265 GetWindowRect( mHwnd, &rect );
00266
00267
00268
00269
00270 int xOffset = x;
00271 int yOffset = ( rect.bottom - rect.top ) - y;
00272 double xPercent = xOffset / double( mWindowResolutionX );
00273 double yPercent = yOffset / double( mWindowResolutionY );
00274 double xPos = xPercent * ( m_SettingWindowXmax - m_SettingWindowXmin ) + m_SettingWindowXmin;
00275 double yPos = yPercent * ( m_SettingWindowYmax - m_SettingWindowYmin ) + m_SettingWindowYmin;
00276 Segment returnMe( mCameraEye, Vector4( xPos, yPos, 0 ) );
00277 return returnMe;
00278 }
00279
00280
00281
00282
00283
00284
00285
00286 #ifndef NOAUX
00287 void IGS_Controller::CreateInitialWindow() const
00288 {
00289
00290 if( this->mSettingDoubleBufferingOn == true )
00291 {
00292 auxInitDisplayMode (AUX_DOUBLE | AUX_RGB );
00293 }
00294 else
00295 {
00296 auxInitDisplayMode ( AUX_RGB );
00297 }
00298
00299
00300 auxInitPosition( 0, 0, this->mWindowResolutionX, this->mWindowResolutionY );
00301 auxInitWindow ("VRML Loader");
00302 }
00303 #endif
00304
00305
00306
00307
00308
00309
00310 void IGS_Controller::DoLighting() const
00311 {
00312 if( this->mSettingLightingEnabled == true )
00313 {
00314
00315 ::glEnable( GL_LIGHTING );
00316 ::glEnable( GL_LIGHT0 );
00317 ::glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, 1 );
00318 float ambient[] = { 1.0, 0.0, 0.0 };
00319 glLightfv( GL_LIGHT0, GL_AMBIENT, ambient );
00320
00321
00322 float mAmbient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
00323 float mDiffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
00324 float mSpecular[] = { 0.2f, 0.2f, 0.2f, 1.0f };
00325 glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, mAmbient );
00326 glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, mAmbient );
00327 glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, mSpecular );
00328 glMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, 5.0 );
00329
00330
00331 }
00332 else
00333 {
00334 ::glDisable( GL_LIGHTING );
00335 }
00336 }
00337
00338
00339
00340
00341
00342
00343 void IGS_Controller::EndFrame() const
00344 {
00345 if( this->mSettingDoubleBufferingOn == true )
00346 {
00347 #ifndef NOAUX
00348 auxSwapBuffers();
00349 #else
00350
00351 BOOL swapSuccess = wglSwapLayerBuffers( this->mHdc, WGL_SWAP_MAIN_PLANE );
00352 assert( swapSuccess == TRUE );
00353 #endif
00354 }
00355
00356 if( this->mSettingAnimationOn == true )
00357 {
00358 HWND handle = NULL;
00359 #ifndef NOAUX
00360 handle = auxGetHWND();
00361 #else
00362 handle = this->mHwnd;
00363 #endif
00364 ::InvalidateRect( handle, NULL, FALSE );
00365 }
00366
00367 #ifdef NOAUX
00368
00369 ::ReleaseDC( this->mHwnd, this->mHdc );
00370 this->mHdc = NULL;
00371 #endif
00372
00373 if( mSettingDebugMessageLevel > 0 )
00374 {
00375 ::printf( "Ending Frame %d\n", this->FrameNumber() );
00376 }
00377
00378
00379 GLenum errorcode;
00380 errorcode = ::glGetError();
00381 assert( errorcode == GL_NO_ERROR );
00382 }
00383
00384
00385
00386
00387
00388
00389 unsigned int IGS_Controller::FrameNumber() const
00390 {
00391 return this->mFrameNumber;
00392 }
00393
00394
00395
00396
00397
00398
00399 Vector4 IGS_Controller::GetCameraDirection() const
00400 {
00401 return ( this->mCameraEye - this->mCameraTarget );
00402 }
00403
00404
00405
00406
00407
00408
00409 enumGBufferTypes IGS_Controller::GetRenderStyle() const
00410 {
00411 return this->mSettingRenderStyle;
00412 }
00413
00414
00415
00416
00417
00418
00419 float IGS_Controller::GetSilhouetteEdgeWidth() const
00420 {
00421 return this->mSettingSilhouetteEdgeWidth;
00422 }
00423
00424
00425
00426
00427
00428
00429 double IGS_Controller::GetWindowXMax() const
00430 {
00431 return this->m_SettingWindowXmax;
00432 }
00433
00434
00435
00436
00437
00438
00439 double IGS_Controller::GetWindowXMin() const
00440 {
00441 return this->m_SettingWindowXmin;
00442 }
00443
00444
00445
00446
00447
00448
00449 double IGS_Controller::GetWindowYMax() const
00450 {
00451 return this->m_SettingWindowYmax;
00452 }
00453
00454
00455
00456
00457
00458
00459 double IGS_Controller::GetWindowYMin() const
00460 {
00461 return this->m_SettingWindowYmin;
00462 }
00463
00464
00465
00466
00467
00468
00469 void IGS_Controller::Process()
00470 {
00471 assert( false );
00472 }
00473
00474
00475
00476
00477
00478
00479 void IGS_Controller::RegisterInitialWindow( HWND hwnd )
00480 {
00481 this->mHwnd = hwnd;
00482 PIXELFORMATDESCRIPTOR pfd =
00483 {
00484 sizeof( PIXELFORMATDESCRIPTOR ),
00485 1,
00486 PFD_DRAW_TO_WINDOW |
00487 PFD_DOUBLEBUFFER |
00488 PFD_SUPPORT_OPENGL |
00489 PFD_GENERIC_ACCELERATED |
00490
00491 PFD_TYPE_RGBA,
00492 24,
00493 0, 0, 0, 0, 0, 0,
00494 0, 0, 0, 0, 0, 0, 0,
00495 32,
00496 0, 0,
00497 PFD_MAIN_PLANE,
00498 0,
00499 0, 0, 0
00500 };
00501
00502 HDC cdc = ::GetDC( hwnd );
00503 assert( cdc != NULL );
00504
00505 int pixelFormat = ::ChoosePixelFormat( cdc, &pfd );
00506 assert( pixelFormat != 0 );
00507
00508 BOOL success = ::SetPixelFormat( cdc, pixelFormat, &pfd );
00509 assert( success == TRUE );
00510
00511 mHglrc = ::wglCreateContext ( cdc );
00512 assert( mHglrc != NULL ) ;
00513
00514 int result = ::ReleaseDC( hwnd, cdc ) ;
00515 assert( result == 1 );
00516 }
00517
00518
00519
00520
00521
00522
00523 void IGS_Controller::ScreenCapture()
00524 {
00525 #ifdef NOAUX
00526 this->mHdc = ::GetDC( this->mHwnd );
00527 BOOL success = wglMakeCurrent ( this->mHdc, this->mHglrc );
00528
00529
00530
00531
00532 assert( success != FALSE ) ;
00533 #endif
00534
00535
00536 this->mGbufferColor.ScreenCapture();
00537 this->mGbufferDepth.ScreenCapture();
00538 this->mStateScreenCaptureDone = true;
00539 }
00540
00541
00542
00543
00544
00545
00546 void IGS_Controller::SetCameraLookat( const Vector4& vector )
00547 {
00548 this->mCameraTarget = vector;
00549 }
00550
00551
00552
00553
00554
00555
00556 void IGS_Controller::SetCameraPosition( const Vector4& vector )
00557 {
00558 this->mCameraEye = vector;
00559 }
00560
00561
00562
00563
00564
00565
00566 void IGS_Controller::SetClearColor( IGS_Color& color )
00567 {
00568 this->mColorClear[ 0 ] = color.r;
00569 this->mColorClear[ 1 ] = color.g;
00570 this->mColorClear[ 2 ] = color.b;
00571 this->mColorClear[ 3 ] = 1.0;
00572 }
00573
00574
00575
00576
00577
00578
00579 void IGS_Controller::SetRenderSilhouettes( bool onOff )
00580 {
00581 this->mSettingRenderSilhouettes = onOff;
00582 }
00583
00584
00585
00586
00587
00588
00589 void IGS_Controller::SetRenderStyle( enumGBufferTypes style )
00590 {
00591 this->mSettingRenderStyle = style;
00592
00593
00594 this->mSettingLightingEnabled = false;
00595 this->mSettingRenderNormals = false;
00596 this->mSettingRenderNormalsAsColors = false;
00597 this->mSettingNormalsRelativeToCamera = false;
00598
00599 this->mColorClear[ 0 ] = gDefaultClearR;
00600 this->mColorClear[ 1 ] = gDefaultClearG;
00601 this->mColorClear[ 2 ] = gDefaultClearB;
00602 this->mColorClear[ 3 ] = gDefaultClearA;
00603
00604 switch( style )
00605 {
00606 case G_PHOTOREALISTIC:
00607 {
00608 this->mSettingLightingEnabled = true;
00609 this->mSettingRenderNormals = true;
00610 break;
00611 }
00612 case G_COLOR:
00613 {
00614 break;
00615 }
00616 case G_DEPTH:
00617 {
00618 break;
00619 }
00620 case G_NORMAL:
00621 {
00622 this->mSettingRenderNormalsAsColors = true;
00623 break;
00624 }
00625 case G_NORMAL_WRT_CAMERA:
00626 {
00627 this->mSettingRenderNormalsAsColors = true;
00628 this->mSettingNormalsRelativeToCamera = true;
00629 break;
00630 }
00631 case G_UV:
00632 {
00633 break;
00634 }
00635 case G_SILHOUETTE_ONLY:
00636 {
00637 this->mColorDefault[ 0 ] = 255;
00638 this->mColorDefault[ 1 ] = 255;
00639 this->mColorDefault[ 2 ] = 255;
00640 this->mColorDefault[ 3 ] = 255;
00641
00642 this->mColorClear[ 0 ] = 1.0;
00643 this->mColorClear[ 1 ] = 1.0;
00644 this->mColorClear[ 2 ] = 1.0;
00645 this->mColorClear[ 3 ] = 1.0;
00646
00647 break;
00648 }
00649 default:
00650 {
00651 assert( false );
00652 break;
00653 }
00654 }
00655 }
00656
00657
00658
00659
00660
00661 void IGS_Controller::SetSilhouetteEdgeWidth( const float width )
00662 {
00663 this->mSettingSilhouetteEdgeWidth = width;
00664 }
00665
00666
00667
00668
00669
00670
00671 bool IGS_Controller::SettingLightingEnabled() const
00672 {
00673 return this->mSettingLightingEnabled;
00674 }
00675
00676
00677
00678
00679
00680
00681 bool IGS_Controller::SettingNormalsRelativeToCamera() const
00682 {
00683 return this->mSettingNormalsRelativeToCamera;
00684 }
00685
00686
00687
00688
00689
00690
00691 bool IGS_Controller::SettingRenderNormals() const
00692 {
00693 return this->mSettingRenderNormals;
00694 }
00695
00696
00697
00698
00699
00700
00701 bool IGS_Controller::SettingRenderNormalsAsColors() const
00702 {
00703 return this->mSettingRenderNormalsAsColors;
00704 }
00705
00706
00707
00708
00709
00710
00711 bool IGS_Controller::SettingRenderSilhouettes() const
00712 {
00713 return this->mSettingRenderSilhouettes;
00714 }
00715
00716 void IGS_Controller::SetWindowRect
00717 (
00718 const double xMin,
00719 const double xMax,
00720 const double yMin,
00721 const double yMax,
00722 const double zMin,
00723 const double zMax
00724 )
00725 {
00726 this->m_SettingWindowXmin = xMin;
00727 this->m_SettingWindowXmax = xMax;
00728 this->m_SettingWindowYmin = yMin;
00729 this->m_SettingWindowYmax = yMax;
00730 this->m_SettingWindowZmin = zMin;
00731 this->m_SettingWindowZmax = zMax;
00732 }
00733
00734
00735
00736
00737
00738
00739 void IGS_Controller::SetWindowResolution( const int xres, const int yres )
00740 {
00741 this->mWindowResolutionX = xres;
00742 this->mWindowResolutionY = yres;
00743
00744
00745 mGbufferColor.SetResolution( this->mWindowResolutionX, this->mWindowResolutionY );
00746 mGbufferDepth.SetResolution( this->mWindowResolutionX, this->mWindowResolutionY );
00747 }