![]() |
TAPs 0.7.7.3
|
00001 /****************************************************************************** 00002 TAPsOpenGLCtrlTransferFn.cpp 00003 00004 Transfer Fn Control via OpenGL 00005 00006 SUKITTI PUNAK (05/02/2007) 00007 UPDATE (05/09/2007) 00008 ******************************************************************************/ 00009 #include "TAPsOpenGLCtrlTransferFn.hpp" 00010 // Using Inclusion Model (i.e. definitions are included in declarations) 00011 // (this name.cpp is included in name.hpp) 00012 // Each friend is defined directly inside its declaration. 00013 00014 BEGIN_NAMESPACE_TAPs__OpenGL 00015 //============================================================================= 00016 // Constructor(s) and Destructor 00017 //----------------------------------------------------------------------------- 00018 OpenGLCtrlTransferFn::OpenGLCtrlTransferFn () 00019 : OpenGLCtrl(), m_RGBA_1DTexture( 0 ) 00020 { 00021 Default(); 00022 } 00023 //----------------------------------------------------------------------------- 00024 //OpenGLCtrlTransferFn::OpenGLCtrlTransferFn ( 00025 //{ 00026 //} 00027 //----------------------------------------------------------------------------- 00028 OpenGLCtrlTransferFn::~OpenGLCtrlTransferFn () 00029 { 00030 glDeleteTextures( 1, &m_RGBA_1DTexture ); 00031 } 00032 //----------------------------------------------------------------------------- 00033 //============================================================================= 00034 00035 //============================================================================= 00036 // Get/Set Function(s) 00037 //----------------------------------------------------------------------------- 00038 void OpenGLCtrlTransferFn::Default () 00039 { 00040 SetSize( 256, 256 ); 00041 m_BorderSize = 8; 00042 m_BackgroundColor[0] = 0.7; 00043 m_BackgroundColor[1] = 0.7; 00044 m_BackgroundColor[2] = 0.7; 00045 m_BackgroundColor[3] = 0.3; 00046 // 00047 SetButtons(); 00048 // 00049 SetDefaultLUT(); 00050 // 00051 SetHistogramColor( 0.9f, 0.6f, 0.0f, 0.4f ); 00052 // 00053 GLubyte zero[256]; 00054 for ( int i = 0; i < 256; ++i ) { 00055 zero[i] = 0; 00056 } 00057 LoadFreqsToHistogram( zero ); 00058 SetClickXY( 0, 0 ); // for setting the click dot 00059 // but this one make red button's status true 00060 m_ButtonRed.SetStatus( false ); // so set red button to false 00061 } 00062 //----------------------------------------------------------------------------- 00063 void OpenGLCtrlTransferFn::SetStartPos ( int x, int y ) 00064 { 00065 //x = x; 00066 //m_xyLowerLeftPt[0] = x; 00067 //m_xyLowerLeftPt[1] = y; 00068 // 00069 //int X = m_xyLowerLeftPt[0]; 00070 m_ButtonRed.SetStartPos( x+10, y ); 00071 m_ButtonGreen.SetStartPos( x+70, y ); 00072 m_ButtonBlue.SetStartPos( x+130, y ); 00073 m_ButtonAlpha.SetStartPos( x+190, y ); 00074 // 00075 y += 30; 00076 OpenGLCtrl::SetStartPos( x, y ); 00077 } 00078 //----------------------------------------------------------------------------- 00079 void OpenGLCtrlTransferFn::SetButtons () 00080 { 00081 // 00082 m_ButtonRed.SetText( "Red" ); 00083 m_ButtonGreen.SetText( "Green" ); 00084 m_ButtonBlue.SetText( "Blue" ); 00085 m_ButtonAlpha.SetText( "Alpha" ); 00086 // 00087 m_ButtonRed.SetBackgroundActColor( 1, 0, 0, 0.8 ); 00088 m_ButtonGreen.SetBackgroundActColor( 0, 1, 0, 0.7 ); 00089 m_ButtonBlue.SetBackgroundActColor( 0.1, 0.1, 1, 0.9 ); 00090 m_ButtonAlpha.SetBackgroundActColor( 1, 1, 1, 0.7 ); 00091 // 00092 m_ButtonRed.SetForegroundActColor( 0.1, 0, 0, 0.7 ); 00093 m_ButtonGreen.SetForegroundActColor( 0, 0.1, 0, 0.7 ); 00094 m_ButtonBlue.SetForegroundActColor( 0.8, 0.8, 1, 0.7 ); 00095 m_ButtonAlpha.SetForegroundActColor( 0, 0, 0, 0.7 ); 00096 // 00097 m_ButtonRed.SetBackgroundColor( 0.15, 0, 0, 0.7 ); 00098 m_ButtonGreen.SetBackgroundColor( 0, 0.1, 0, 0.7 ); 00099 m_ButtonBlue.SetBackgroundColor( 0, 0, 0.3, 0.7 ); 00100 m_ButtonAlpha.SetBackgroundColor( 0, 0, 0, 0.7 ); 00101 // 00102 m_ButtonRed.SetForegroundColor( 1, 0, 0, 0.7 ); 00103 m_ButtonGreen.SetForegroundColor( 0, 0.7, 0, 0.7 ); 00104 m_ButtonBlue.SetForegroundColor( 0, 0, 1, 0.7 ); 00105 m_ButtonAlpha.SetForegroundColor( 0.6, 0.6, 0.6, 0.7 ); 00106 } 00107 //----------------------------------------------------------------------------- 00108 void OpenGLCtrlTransferFn::SetDefaultLUT () 00109 { 00110 for ( int i = 0; i < 256; ++i ) { 00111 m_RGBA[i][0] = m_RGBA[i][1] = m_RGBA[i][2] = m_RGBA[i][3] = i; 00112 } 00113 if ( m_RGBA_1DTexture != 0 ) { 00114 glDeleteTextures( 1, &m_RGBA_1DTexture ); 00115 } 00116 { 00117 //glEnable( GL_TEXTURE_1D ); 00118 glGenTextures( 1, &m_RGBA_1DTexture ); 00119 glBindTexture( GL_TEXTURE_1D, m_RGBA_1DTexture ); 00120 glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP ); 00121 glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); 00122 glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); 00123 glTexImage1D( GL_TEXTURE_1D, 0, GL_RGBA, 256, 0, GL_RGBA, 00124 GL_UNSIGNED_BYTE, m_RGBA ); 00125 glBindTexture( GL_TEXTURE_1D, 0 ); 00126 } 00127 } 00128 //----------------------------------------------------------------------------- 00129 void OpenGLCtrlTransferFn::SetLUTToRampFn () 00130 { 00131 for ( int i = 0; i < 256; ++i ) { 00132 m_RGBA[i][0] = m_RGBA[i][1] = m_RGBA[i][2] = m_RGBA[i][3] = i; 00133 } 00134 UpdateRGBA(); 00135 } 00136 //----------------------------------------------------------------------------- 00137 void OpenGLCtrlTransferFn::SetLUTToRandomFn () 00138 { 00139 srand ( time(NULL) ); 00140 for ( int i = 0; i < 256; ++i ) { 00141 m_RGBA[i][0] = rand() % 256; 00142 m_RGBA[i][1] = rand() % 256; 00143 m_RGBA[i][2] = rand() % 256; 00144 m_RGBA[i][3] = rand() % 256; 00145 } 00146 UpdateRGBA(); 00147 } 00148 //----------------------------------------------------------------------------- 00149 void OpenGLCtrlTransferFn::SetOpacity ( float alpha ) 00150 { 00151 OpenGLCtrl::SetOpacity( alpha ); 00152 m_ButtonRed.SetOpacity( alpha ); 00153 m_ButtonGreen.SetOpacity( alpha ); 00154 m_ButtonBlue.SetOpacity( alpha ); 00155 m_ButtonAlpha.SetOpacity( alpha ); 00156 } 00157 //----------------------------------------------------------------------------- 00158 bool OpenGLCtrlTransferFn::IsInside ( int x, int y ) 00159 { 00160 return 00161 m_xyLowerLeftPt[0]-m_BorderSize <= x && x <= m_xyLowerLeftPt[0]+m_DrawSizeXY[0]+m_BorderSize 00162 && m_xyLowerLeftPt[1]-m_BorderSize <= y && y <= m_xyLowerLeftPt[1]+m_DrawSizeXY[1]+m_BorderSize; 00163 } 00164 //----------------------------------------------------------------------------- 00165 bool OpenGLCtrlTransferFn::IsInsidePlusBorder ( int x, int y ) 00166 { 00167 return 00168 m_xyLowerLeftPt[0]-m_BorderSize <= x && x <= m_xyLowerLeftPt[0]+m_DrawSizeXY[0]+m_BorderSize 00169 && m_xyLowerLeftPt[1]-m_BorderSize-30 <= y && y <= m_xyLowerLeftPt[1]+m_DrawSizeXY[1]+m_BorderSize; 00170 } 00171 //----------------------------------------------------------------------------- 00172 void OpenGLCtrlTransferFn::UpdateRGBA () 00173 { 00174 //=============================================================== 00175 // Write Transfer Function Look Up Table (Texture) to GPU 00176 //--------------------------------------------------------------- 00177 glBindBuffer( GL_PIXEL_UNPACK_BUFFER, 0 ); 00178 glBindTexture( GL_TEXTURE_1D, m_RGBA_1DTexture ); 00179 //------------------------------------------- 00180 glTexImage1D( GL_TEXTURE_1D, 0, GL_RGBA, 256, 0, GL_RGBA, 00181 GL_UNSIGNED_BYTE, m_RGBA ); 00182 TAPs::OpenGL::Fn::CHECK_GL_ERROR(); 00183 //------------------------------------------- 00184 glBindTexture( GL_TEXTURE_1D, 0 ); 00185 //--------------------------------------------------------------- 00186 //=============================================================== 00187 } 00188 //----------------------------------------------------------------------------- 00189 template <typename T> 00190 void OpenGLCtrlTransferFn::LoadDataFrom ( T data[256*4], bool isDataNormalized ) 00191 { 00192 //-- Data is normalized [0.0,1.0] 00193 if ( isDataNormalized ) { 00194 int idx = 0; 00195 for( i = 0; i < 256; ++i ) { 00196 m_RGBA[idx] = (GLubyte) ( data[idx++] * 255 ); 00197 m_RGBA[idx] = (GLubyte) ( data[idx++] * 255 ); 00198 m_RGBA[idx] = (GLubyte) ( data[idx++] * 255 ); 00199 m_RGBA[idx] = (GLubyte) ( data[idx++] * 255 ); 00200 } 00201 } 00202 //-- Data is unnormalized [0,255] 00203 else { 00204 int idx = 0; 00205 for( i = 0; i < 256; ++i ) { 00206 m_RGBA[idx] = data[idx++]; 00207 m_RGBA[idx] = data[idx++]; 00208 m_RGBA[idx] = data[idx++]; 00209 m_RGBA[idx] = data[idx++]; 00210 } 00211 } 00212 } 00213 //----------------------------------------------------------------------------- 00214 template <typename T> 00215 void OpenGLCtrlTransferFn::LoadFreqsToHistogram ( T freqs[256] ) 00216 { 00217 // Find max frequency and total frequencies 00218 int i = 0; 00219 m_Histogram.totalFreqs = 00220 m_Histogram.maxFreq = freqs[i]; 00221 for ( i = 1; i < 256; ++i ) { 00222 m_Histogram.totalFreqs += freqs[i]; 00223 if ( static_cast<unsigned long int>( freqs[i] ) > m_Histogram.maxFreq ) { 00224 m_Histogram.maxFreq = freqs[i]; 00225 } 00226 } 00227 // Record normalized frequencies 00228 double normalizationFactor = (double) m_Histogram.maxFreq / 255.0; 00229 if ( normalizationFactor > 0 ) { 00230 for( i = 0; i < 256; ++i ) { 00231 m_Histogram.normalizedFreqs[i] = 00232 static_cast<GLubyte>( (double) freqs[i] / normalizationFactor ); 00233 //std::cout << i << ":\t" << (int)m_Histogram.normalizedFreqs[i] << "\t--> " << freqs[i] << "\n"; 00234 } 00235 //std::cout << "normalizationFactor: " << normalizationFactor << "\n"; 00236 } 00237 else { 00238 for( i = 0; i < 256; ++i ) { 00239 m_Histogram.normalizedFreqs[i] = 0.0; 00240 //std::cout << "i:\t" << (int)m_Histogram.normalizedFreqs[i] << "\t--> " << freqs[i] << "\n"; 00241 } 00242 } 00243 //std::cout << "Max Freq: " << m_Histogram.maxFreq << "\n"; 00244 //std::cout << "Tot Freq: " << m_Histogram.totalFreqs << "\n"; 00245 } 00246 //----------------------------------------------------------------------------- 00247 void OpenGLCtrlTransferFn::GetHistogramColor ( 00248 float & r, float & g, float & b, float & a ) const 00249 { 00250 r = m_Histogram.histogramColor[0]; 00251 g = m_Histogram.histogramColor[1]; 00252 b = m_Histogram.histogramColor[2]; 00253 a = m_Histogram.histogramColor[3]; 00254 } 00255 //----------------------------------------------------------------------------- 00256 void OpenGLCtrlTransferFn::SetHistogramColor ( 00257 float r, float g, float b, float a ) 00258 { 00259 m_Histogram.histogramColor[0] = r; 00260 m_Histogram.histogramColor[1] = g; 00261 m_Histogram.histogramColor[2] = b; 00262 m_Histogram.histogramColor[3] = a; 00263 } 00264 //----------------------------------------------------------------------------- 00265 //============================================================================= 00266 00267 //============================================================================= 00268 // Operation(s) 00269 //----------------------------------------------------------------------------- 00270 bool OpenGLCtrlTransferFn::SetClickXY ( int x, int y ) 00271 { 00272 // 00273 if ( !IsInsidePlusBorder( x, y ) ) return false; 00274 // 00275 // Check Button Click 00276 if ( GetRedButton().IsInside( x, y ) ) { 00277 GetRedButton().ToggleStatus(); 00278 return true; 00279 } 00280 if ( GetGreenButton().IsInside( x, y ) ) { 00281 GetGreenButton().ToggleStatus(); 00282 return true; 00283 } 00284 if ( GetBlueButton().IsInside( x, y ) ) { 00285 GetBlueButton().ToggleStatus(); 00286 return true; 00287 } 00288 if ( GetAlphaButton().IsInside( x, y ) ) { 00289 GetAlphaButton().ToggleStatus(); 00290 return true; 00291 } 00292 // 00293 x -= m_xyLowerLeftPt[0]; 00294 y -= m_xyLowerLeftPt[1]; 00295 // 00296 if ( x < 0 ) x = 0; 00297 else if ( x > 255 ) x = 255; 00298 if ( y < 0 ) y = 0; 00299 else if ( y > 255 ) y = 255; 00300 // 00301 m_clickX = m_old_x = x; 00302 m_clickY = m_old_y = y; 00303 // 00304 return true; 00305 } 00306 //----------------------------------------------------------------------------- 00307 bool OpenGLCtrlTransferFn::ChangeLUTValues ( int x, int y ) 00308 { 00309 // 00310 if ( !IsInside( x, y ) ) return false; 00311 // 00312 x -= m_xyLowerLeftPt[0]; 00313 y -= m_xyLowerLeftPt[1]; 00314 // 00315 if ( x < 0 ) x = 0; 00316 else if ( x > 255 ) x = 255; 00317 if ( 0 > y ) y = 0; 00318 else if ( 255 < y ) y = 255; 00319 // 00320 // Forward Changing 00321 if ( x >= m_old_x+1 ) { 00322 float yStep = (float)( y - m_old_y ) / (float)( x - m_old_x ); 00323 float yVal = m_old_y; 00324 for ( int i = m_old_x; i <= x; ++i ) { 00325 if ( m_ButtonRed.GetStatus() ) { 00326 m_RGBA[i][0] = (unsigned int)yVal; 00327 } 00328 if ( m_ButtonGreen.GetStatus() ) { 00329 m_RGBA[i][1] = (unsigned int)yVal; 00330 } 00331 if ( m_ButtonBlue.GetStatus() ) { 00332 m_RGBA[i][2] = (unsigned int)yVal; 00333 } 00334 if ( m_ButtonAlpha.GetStatus() ) { 00335 m_RGBA[i][3] = (unsigned int)yVal; 00336 } 00337 yVal += yStep; 00338 } 00339 } 00340 // 00341 // Backward Changing 00342 else { 00343 float yStep = (float)( m_old_y - y ) / (float)( x - m_old_x ); 00344 float yVal = m_old_y; 00345 for ( int i = m_old_x; i >= x; --i ) { 00346 if ( m_ButtonRed.GetStatus() ) { 00347 m_RGBA[i][0] = (unsigned int)yVal; 00348 } 00349 if ( m_ButtonGreen.GetStatus() ) { 00350 m_RGBA[i][1] = (unsigned int)yVal; 00351 } 00352 if ( m_ButtonBlue.GetStatus() ) { 00353 m_RGBA[i][2] = (unsigned int)yVal; 00354 } 00355 if ( m_ButtonAlpha.GetStatus() ) { 00356 m_RGBA[i][3] = (unsigned int)yVal; 00357 } 00358 yVal += yStep; 00359 } 00360 } 00361 // 00362 m_old_x = x; 00363 m_old_y = y; 00364 UpdateRGBA(); 00365 // 00366 return true; 00367 } 00368 //----------------------------------------------------------------------------- 00369 //============================================================================= 00370 00371 00372 //============================================================================= 00373 // Draw Function(s) 00374 //----------------------------------------------------------------------------- 00375 void OpenGLCtrlTransferFn::Draw () 00376 { 00377 DrawBackgroundScale(); 00378 DrawHistogram (); 00379 DrawIntensities(); 00380 DrawColors(); 00381 m_ButtonRed.Draw(); 00382 m_ButtonGreen.Draw(); 00383 m_ButtonBlue.Draw(); 00384 m_ButtonAlpha.Draw(); 00385 } 00386 //----------------------------------------------------------------------------- 00387 void OpenGLCtrlTransferFn::DrawHistogram () 00388 { 00389 GLfloat xStep = (m_DrawSizeXY[0]-1) / 255.0; 00390 GLfloat yStep = (m_DrawSizeXY[1]-1) / 255.0; 00391 // 00392 // Histogram 00393 GLfloat X = m_xyLowerLeftPt[0]; 00394 glColor4fv( m_Histogram.histogramColor ); 00395 glLineWidth( 1 ); 00396 glBegin( GL_LINES ); 00397 for ( int i = 0; i < 256; ++i ) { 00398 glVertex2i( X, m_xyLowerLeftPt[1] ); 00399 glVertex2i( X, m_xyLowerLeftPt[1] + m_Histogram.normalizedFreqs[i]*yStep ); 00400 X += xStep; 00401 } 00402 glEnd(); 00403 } 00404 //----------------------------------------------------------------------------- 00405 void OpenGLCtrlTransferFn::DrawColors () 00406 { 00407 glEnable( GL_TEXTURE_1D ); 00408 glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE ); 00409 //glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL ); 00410 glBindTexture( GL_TEXTURE_1D, m_RGBA_1DTexture ); 00411 glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP ); 00412 glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); 00413 glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); 00414 //------------------------------------------- 00415 int startX = m_xyLowerLeftPt[0]; 00416 int endX = startX + m_DrawSizeXY[0]; 00417 int startY = m_xyLowerLeftPt[1] + m_DrawSizeXY[1] + m_BorderSize; 00418 int w, h; 00419 m_ButtonRed.GetSize( w, h ); 00420 int endY = startY + h; 00421 glDisable( GL_TEXTURE_1D ); 00422 glBegin( GL_QUADS ); 00423 int midY = ( endY + startY ) / 2; 00424 glColor4f( 0, 0, 0, 1 ); 00425 glVertex2i( startX, midY ); 00426 glVertex2i( startX, startY ); 00427 glVertex2i( endX, startY ); 00428 glVertex2i( endX, midY ); 00429 // 00430 glColor4f( 1, 1, 1, 1 ); 00431 glVertex2i( startX, endY ); 00432 glVertex2i( startX, midY ); 00433 glVertex2i( endX, midY ); 00434 glVertex2i( endX, endY ); 00435 glEnd(); 00436 // 00437 glEnable( GL_TEXTURE_1D ); 00438 glBegin( GL_QUADS ); 00439 glTexCoord1i( 0 ); 00440 glVertex2i( startX, endY ); 00441 glVertex2i( startX, startY ); 00442 glTexCoord1i( 1 ); 00443 glVertex2i( endX, startY ); 00444 glVertex2i( endX, endY ); 00445 glEnd(); 00446 //------------------------------------------- 00447 glBindTexture( GL_TEXTURE_1D, 0 ); 00448 glDisable( GL_TEXTURE_1D ); 00449 } 00450 //----------------------------------------------------------------------------- 00451 void OpenGLCtrlTransferFn::DrawIntensities () 00452 { 00456 //glBindBuffer( GL_PIXEL_PACK_BUFFER, 0 ); 00457 //glBindTexture( GL_TEXTURE_1D, m_RGBA_1DTexture ); 00459 //glGetTexImage( 00460 // GL_TEXTURE_1D, 00461 // 0, 00462 // GL_RGBA, 00463 // GL_UNSIGNED_BYTE, 00464 // m_RGBA 00465 //); 00466 //TAPs::OpenGL::Fn::CHECK_GL_ERROR(); 00468 //glBindTexture( GL_TEXTURE_1D, 0 ); 00471 00472 //=============================================================== 00473 // Draw Mark Point 00474 //--------------------------------------------------------------- 00475 glPointSize( 5 ); 00476 glColor4f( 0.5, 0.5, 0.0, 0.7 ); 00477 glBegin( GL_POINTS ); 00478 glVertex2i( m_clickX + m_xyLowerLeftPt[0], m_clickY + m_xyLowerLeftPt[1] ); 00479 glEnd(); 00480 //--------------------------------------------------------------- 00481 //=============================================================== 00482 00483 //=============================================================== 00484 // Draw RGBA Lines 00485 //--------------------------------------------------------------- 00486 GLfloat xStep = (m_DrawSizeXY[0]-1) / 255.0; 00487 GLfloat yStep = (m_DrawSizeXY[1]-1) / 255.0; 00488 // 00489 // 00490 // Red 00491 GLfloat X = m_xyLowerLeftPt[0]; 00492 glColor4f( 1, 0, 0, 0.8 ); 00493 if ( m_ButtonRed.GetStatus() ) glLineWidth( 3 ); 00494 else glLineWidth( 1 ); 00495 glBegin( GL_LINE_STRIP ); 00496 for ( int i = 0; i < 256; ++i ) { 00497 glVertex2i( X, m_xyLowerLeftPt[1] + m_RGBA[i][0]*yStep + 2 ); 00498 X += xStep; 00499 } 00500 glEnd(); 00501 // 00502 // Green 00503 X = m_xyLowerLeftPt[0]; 00504 glColor4f( 0, 1, 0, 0.7 ); 00505 if ( m_ButtonGreen.GetStatus() ) glLineWidth( 3 ); 00506 else glLineWidth( 1 ); 00507 glBegin( GL_LINE_STRIP ); 00508 for ( int i = 0; i < 256; ++i ) { 00509 glVertex2f( X, m_xyLowerLeftPt[1] + m_RGBA[i][1]*yStep + 1 ); 00510 X += xStep; 00511 } 00512 glEnd(); 00513 // 00514 // Blue 00515 X = m_xyLowerLeftPt[0]; 00516 glColor4f( 0, 0, 1, 0.9 ); 00517 if ( m_ButtonBlue.GetStatus() ) glLineWidth( 3 ); 00518 else glLineWidth( 1 ); 00519 glBegin( GL_LINE_STRIP ); 00520 for ( int i = 0; i < 256; ++i ) { 00521 glVertex2f( X, m_xyLowerLeftPt[1] + m_RGBA[i][2]*yStep ); 00522 X += xStep; 00523 } 00524 glEnd(); 00525 // 00526 // Alpha 00527 X = m_xyLowerLeftPt[0]; 00528 glColor4f( 0.25, 0.25, 0.25, 0.9 ); 00529 if ( m_ButtonAlpha.GetStatus() ) glLineWidth( 3 ); 00530 else glLineWidth( 1 ); 00531 glBegin( GL_LINE_STRIP ); 00532 for ( int i = 0; i < 256; ++i ) { 00533 glVertex2f( X, m_xyLowerLeftPt[1] + m_RGBA[i][3]*yStep - 1 ); 00534 X += xStep; 00535 } 00536 glEnd(); 00537 //--------------------------------------------------------------- 00538 //=============================================================== 00539 } 00540 //----------------------------------------------------------------------------- 00541 void OpenGLCtrlTransferFn::DrawBackgroundScale () 00542 { 00543 DrawBackgroundAndBorder(); 00544 //----------------------------------------------------- 00545 // Draw Big Scale Ticks 00546 { 00547 float stepX = (m_DrawSizeXY[0]) / 36.0; 00548 float stepY = (m_DrawSizeXY[1]) / 36.0; 00549 float startX = m_xyLowerLeftPt[0]; 00550 float startY = m_xyLowerLeftPt[1]; 00551 // Long Tick Length 00552 float endX0 = m_xyLowerLeftPt[0] + m_DrawSizeXY[0]; 00553 float endY0 = m_xyLowerLeftPt[1] + m_DrawSizeXY[1]; 00554 // Middle Tick Length 00555 float midTickLenX = m_DrawSizeXY[0]/28.0; 00556 float midTickLenY = m_DrawSizeXY[0]/28.0; 00557 float endX1 = m_xyLowerLeftPt[0] + midTickLenX; 00558 float endY1 = m_xyLowerLeftPt[1] + midTickLenY; 00559 float strX1 = endX0 - midTickLenX; 00560 float strY1 = endY0 - midTickLenY; 00561 // Short Tick Length 00562 float shortTickLenX = m_DrawSizeXY[0]/52.0; 00563 float shortTickLenY = m_DrawSizeXY[0]/52.0; 00564 float endX2 = m_xyLowerLeftPt[0] + shortTickLenX; 00565 float endY2 = m_xyLowerLeftPt[1] + shortTickLenY; 00566 float strX2 = endX0 - shortTickLenX; 00567 float strY2 = endY0 - shortTickLenY; 00568 glBegin( GL_LINES ); 00569 // 00570 glColor4f( 0, 0, 0, m_BackgroundColor[3] ); 00571 for ( int i = 0; i < 9; ++i ) { 00572 // 00573 // Vertical Long Tick 00574 glVertex2f( startX, m_xyLowerLeftPt[1] ); 00575 glVertex2f( startX, endY0 ); 00576 startX += stepX; 00577 // Vertical Short Tick 00578 glVertex2f( startX, m_xyLowerLeftPt[1] ); 00579 glVertex2f( startX, endY2 ); 00580 // Vertical Short Tick 00581 glVertex2f( startX, strY2 ); 00582 glVertex2f( startX, endY0 ); 00583 startX += stepX; 00584 // Vertical Middle Tick 00585 glVertex2f( startX, m_xyLowerLeftPt[1] ); 00586 glVertex2f( startX, endY1 ); 00587 // Vertical Middle Tick 00588 glVertex2f( startX, strY1 ); 00589 glVertex2f( startX, endY0 ); 00590 startX += stepX; 00591 // Vertical Short Tick 00592 glVertex2f( startX, m_xyLowerLeftPt[1] ); 00593 glVertex2f( startX, endY2 ); 00594 // Vertical Short Tick 00595 glVertex2f( startX, strY2 ); 00596 glVertex2f( startX, endY0 ); 00597 startX += stepX; 00598 // 00599 // Horizontal Long Tick 00600 glVertex2f( m_xyLowerLeftPt[0], startY ); 00601 glVertex2f( endX0, startY ); 00602 startY += stepY; 00603 // Horizontal Short Tick 00604 glVertex2f( m_xyLowerLeftPt[0], startY ); 00605 glVertex2f( endX2, startY ); 00606 // Horizontal Short Tick 00607 glVertex2f( strX2, startY ); 00608 glVertex2f( endX0, startY ); 00609 startY += stepY; 00610 // Horizontal Middle Tick 00611 glVertex2f( m_xyLowerLeftPt[0], startY ); 00612 glVertex2f( endX1, startY ); 00613 // Horizontal Middle Tick 00614 glVertex2f( strX1, startY ); 00615 glVertex2f( endX0, startY ); 00616 startY += stepY; 00617 // Horizontal Short Tick 00618 glVertex2f( m_xyLowerLeftPt[0], startY ); 00619 glVertex2f( endX2, startY ); 00620 // Horizontal Short Tick 00621 glVertex2f( strX2, startY ); 00622 glVertex2f( endX0, startY ); 00623 startY += stepY; 00624 // 00625 } 00626 // Vertical Long Tick 00627 glVertex2f( startX, m_xyLowerLeftPt[1] ); 00628 glVertex2f( startX, endY0 ); 00629 // Horizontal Long Tick 00630 glVertex2f( m_xyLowerLeftPt[0], startY ); 00631 glVertex2f( endX0, startY ); 00632 // 00633 glEnd(); 00634 } 00635 } 00636 //----------------------------------------------------------------------------- 00637 //============================================================================= 00638 00639 //* 00640 //============================================================================= 00641 // Copy to/from 1D texture 00642 //----------------------------------------------------------------------------- 00643 bool OpenGLCtrlTransferFn::CopyTo1DTexture ( 00644 GLuint to1DTex, bool isDataNormalized ) const 00645 { 00646 //--------------------------------------------------------------- 00647 // DEBUG: Check texture compatability 00648 #ifdef TAPs_DEBUG_MODE 00649 //glGetTexLevelParameteriv( GL_TEXTURE_1D, ... 00650 // if ... return false 00651 #endif 00652 if ( to1DTex == NULL ) return false; 00653 //=============================================================== 00654 // Write Transfer Function Look Up Table (Texture) to GPU 00655 //--------------------------------------------------------------- 00656 // Normalized Data 00657 if ( isDataNormalized ) { 00658 GLfloat dataRGBA[256][4]; 00659 for ( int i = 0; i < 256; ++i ) { 00660 dataRGBA[i][0] = (GLfloat) ( m_RGBA[i][0] / 255.0f ); 00661 dataRGBA[i][1] = (GLfloat) ( m_RGBA[i][1] / 255.0f ); 00662 dataRGBA[i][2] = (GLfloat) ( m_RGBA[i][2] / 255.0f ); 00663 dataRGBA[i][3] = (GLfloat) ( m_RGBA[i][3] / 255.0f ); 00664 } 00665 //------------------------------------------- 00666 glBindBuffer( GL_PIXEL_UNPACK_BUFFER, 0 ); 00667 glBindTexture( GL_TEXTURE_1D, to1DTex ); 00668 //------------------------------------------- 00669 glTexImage1D( GL_TEXTURE_1D, 0, GL_RGBA, 256, 0, GL_RGBA, 00670 GL_FLOAT, dataRGBA ); 00671 TAPs::OpenGL::Fn::CHECK_GL_ERROR(); 00672 //------------------------------------------- 00673 glBindTexture( GL_TEXTURE_1D, 0 ); 00674 } 00675 //--------------------------------------------------------------- 00676 // Unnormalized Data 00677 else { 00678 glBindBuffer( GL_PIXEL_UNPACK_BUFFER, 0 ); 00679 glBindTexture( GL_TEXTURE_1D, to1DTex ); 00680 //------------------------------------------- 00681 glTexImage1D( GL_TEXTURE_1D, 0, GL_RGBA, 256, 0, GL_RGBA, 00682 GL_UNSIGNED_BYTE, m_RGBA ); 00683 TAPs::OpenGL::Fn::CHECK_GL_ERROR(); 00684 //------------------------------------------- 00685 glBindTexture( GL_TEXTURE_1D, 0 ); 00686 } 00687 //--------------------------------------------------------------- 00688 //=============================================================== 00689 /* 00690 // DEBUG 00691 for ( int i = 0; i < 6; ++i ) { 00692 std::cout << i << ": " 00693 << (int)m_RGBA[i][0] << " " 00694 << (int)m_RGBA[i][1] << " " 00695 << (int)m_RGBA[i][2] << " " 00696 << (int)m_RGBA[i][3] << "\n"; 00697 } 00698 std::cout << "-----\n"; 00699 for ( int i = 250; i < 256; ++i ) { 00700 std::cout << i << ": " 00701 << (int)m_RGBA[i][0] << " " 00702 << (int)m_RGBA[i][1] << " " 00703 << (int)m_RGBA[i][2] << " " 00704 << (int)m_RGBA[i][3] << "\n"; 00705 } 00706 //*/ 00707 //--------------------------------------------------------------- 00708 return true; 00709 } 00710 //----------------------------------------------------------------------------- 00711 bool OpenGLCtrlTransferFn::CopyFrom1DTexture ( 00712 GLuint from1DTex, bool isDataNormalized ) 00713 { 00714 //--------------------------------------------------------------- 00715 // DEBUG: Check texture compatability 00716 #ifdef TAPs_DEBUG_MODE 00717 //glGetTexLevelParameteriv( GL_TEXTURE_1D, ... 00718 // if ... return false 00719 #endif 00720 if ( from1DTex == NULL ) return false; 00721 //=============================================================== 00722 // Read Transfer Function Look Up Table (Texture) from GPU 00723 //--------------------------------------------------------------- 00724 // Normalized Data 00725 if ( isDataNormalized ) { 00726 GLfloat dataRGBA[256][4]; 00727 //------------------------------------------- 00728 glBindBuffer( GL_PIXEL_PACK_BUFFER, 0 ); 00729 glBindTexture( GL_TEXTURE_1D, from1DTex ); 00730 //--------------------------------------- 00731 glGetTexImage( 00732 GL_TEXTURE_1D, 00733 0, 00734 GL_RGBA, 00735 GL_FLOAT, 00736 dataRGBA 00737 ); 00738 TAPs::OpenGL::Fn::CHECK_GL_ERROR(); 00739 //--------------------------------------- 00740 glBindTexture( GL_TEXTURE_1D, 0 ); 00741 //--------------------------------------- 00742 for ( int i = 0; i < 256; ++i ) { 00743 m_RGBA[i][0] = (GLubyte) ( dataRGBA[i][0] * 255 ); 00744 m_RGBA[i][1] = (GLubyte) ( dataRGBA[i][1] * 255 ); 00745 m_RGBA[i][2] = (GLubyte) ( dataRGBA[i][2] * 255 ); 00746 m_RGBA[i][3] = (GLubyte) ( dataRGBA[i][3] * 255 ); 00747 } 00748 } 00749 //--------------------------------------------------------------- 00750 // Unnormalized Data 00751 else { 00752 glBindBuffer( GL_PIXEL_PACK_BUFFER, 0 ); 00753 glBindTexture( GL_TEXTURE_1D, from1DTex ); 00754 //--------------------------------------- 00755 glGetTexImage( 00756 GL_TEXTURE_1D, 00757 0, 00758 GL_RGBA, 00759 GL_UNSIGNED_BYTE, 00760 m_RGBA 00761 ); 00762 TAPs::OpenGL::Fn::CHECK_GL_ERROR(); 00763 //--------------------------------------- 00764 glBindTexture( GL_TEXTURE_1D, 0 ); 00765 } 00766 //--------------------------------------------------------------- 00767 //=============================================================== 00768 UpdateRGBA(); 00769 //--------------------------------------------------------------- 00770 return true; 00771 } 00772 //----------------------------------------------------------------------------- 00773 //============================================================================= 00774 //*/ 00775 00776 //----------------------------------------------------------------------------- 00777 //============================================================================= 00778 END_NAMESPACE_TAPs__OpenGL 00779 //----------------------------------------------------------------------------- 00780 //34567890123456789012345678901234567890123456789012345678901234567890123456789 00781 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----