00001
00002 #include "TransferFunctionTexture.h"
00003 #include <algorithm>
00004
00005 #include "utility.h"
00006 #include "Colours.h"
00007
00008 using namespace Colours;
00009
00010
00011 GLuint TransferFunctionTexture::bottomWindowTransferFunctionTextureID = -1;
00012 GLuint TransferFunctionTexture::renderWindowTransferFunctionTextureID = -1;
00013
00014 RGBA_Colour TransferFunctionTexture::transferFunctionTexture[transferFunctionTextureWidth];
00015
00016
00017
00018 void TransferFunctionTexture::initTransferFunctionTexture(int windowID, Window window)
00019 {
00020 int initialWindow = glutGetWindow();
00021 glutSetWindow(windowID);
00022
00023 GLuint* textureIDPointer = NULL;
00024
00025 if(window == WINDOW_BOTTOM)
00026 textureIDPointer = &bottomWindowTransferFunctionTextureID;
00027 if(window == WINDOW_RENDERING)
00028 textureIDPointer = &renderWindowTransferFunctionTextureID;
00029
00030
00031 if(textureIDPointer != NULL && windowID != GLuint(-1))
00032 {
00033 glGenTextures(1, textureIDPointer);
00034
00035 get_errors();
00036
00037 if(*textureIDPointer != GLuint(-1))
00038 {
00039 get_errors();
00040 glBindTexture(GL_TEXTURE_1D, *textureIDPointer);
00041
00042 glTexParameteri(GL_TEXTURE_1D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
00043 glTexParameteri(GL_TEXTURE_1D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
00044 glTexParameteri(GL_TEXTURE_1D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
00045
00046 glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, transferFunctionTextureWidth, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
00047
00048 get_errors();
00049 }
00050 }
00051
00052 glutSetWindow(initialWindow);
00053 }
00054
00055
00056 void TransferFunctionTexture::refreshTransferTextureDataWithControlPoints()
00057 {
00058 std::vector<TFControlPoint>::iterator currentLeft = TransferFunctionGUI::controlPoints.begin();
00059 std::vector<TFControlPoint>::iterator currentRight = (TransferFunctionGUI::controlPoints.begin() + 1);
00060 std::vector<TFControlPoint>::iterator controlPointsEnd = TransferFunctionGUI::controlPoints.end();
00061
00062
00063 for(int x = 0; x < transferFunctionTextureWidth; ++x)
00064 {
00065
00066 float xRel = (x / (float) transferFunctionTextureWidth);
00067
00068
00069 while(currentRight != controlPointsEnd && xRel > currentRight->m_pos.x)
00070 {
00071 ++currentRight;
00072 ++currentLeft;
00073 }
00074
00075
00076 if(currentRight == controlPointsEnd && xRel > currentLeft->m_pos.x)
00077 {
00078 float interPolBalance = (xRel - currentLeft->m_pos.x) / (float)(1.f - currentLeft->m_pos.x);
00079 GLubyte leftAlpha = (GLubyte)(255 * currentLeft->m_pos.y);
00080 GLubyte transparency = linearInterpolationAlpha
00081 (interPolBalance,
00082 leftAlpha,
00083 (GLubyte)0
00084 );
00085 transferFunctionTexture[x] = RGBA_Colour(toRGB(currentLeft->getColourLab()), transparency);
00086 }
00087
00088 else if((currentLeft != controlPointsEnd) && xRel < currentLeft->m_pos.x)
00089 {
00090 float interPolBalance = (currentLeft->m_pos.x - xRel) / currentLeft->m_pos.x;
00091 GLubyte leftAlpha = (GLubyte)(255 * currentLeft->m_pos.y);
00092 GLubyte transparency = linearInterpolationAlpha
00093 (interPolBalance,
00094 leftAlpha,
00095 (GLubyte)0
00096 );
00097 transferFunctionTexture[x] = RGBA_Colour(toRGB(currentLeft->getColourLab()), transparency);
00098 }
00099 else
00100 {
00101 float interPolBalance = TransferFunctionGUI::calcInterpolationValueBetweenControlPoints((int)(xRel * TransferFunctionGUI::g_iTransferFuncPickerWidth), currentLeft, currentRight);
00102 GLubyte transparency = linearInterpolationAlpha
00103 (interPolBalance,
00104 (GLubyte)(255 * currentLeft->m_pos.y),
00105 (GLubyte)(255 * currentRight->m_pos.y)
00106 );
00107 RGB_Colour& rgbResult = TransferFunctionGUI::interpolateControlPointColours(interPolBalance, currentLeft, currentRight);
00108 transferFunctionTexture[x].setColour(rgbResult, transparency);
00109 }
00110 }
00111 }
00112
00113
00114
00115 void TransferFunctionTexture::refreshTransferFunctionTextureData()
00116 {
00117
00118 if(TransferFunctionGUI::controlPoints.size() == 0)
00119 {
00120 for(int x = 0; x < transferFunctionTextureWidth; ++x)
00121 transferFunctionTexture[x] = RGBA_Colour((GLubyte)0, (GLubyte)0, (GLubyte)0, (GLubyte)0);
00122 }
00123 else
00124 {
00125 refreshTransferTextureDataWithControlPoints();
00126 }
00127 }
00128
00129
00130 void TransferFunctionTexture::copyDataToTransferFunctionTexture(int windowID, Window window)
00131 {
00132 int initialWindow = glutGetWindow();
00133 glutSetWindow(windowID);
00134
00135 GLuint* textureIDPointer = NULL;
00136
00137 if(window == WINDOW_BOTTOM)
00138 textureIDPointer = &bottomWindowTransferFunctionTextureID;
00139 if(window == WINDOW_RENDERING)
00140 textureIDPointer = &renderWindowTransferFunctionTextureID;
00141
00142 if(textureIDPointer != NULL && windowID != GLuint(-1) && *textureIDPointer != GLuint(-1))
00143 {
00144 glBindTexture(GL_TEXTURE_1D, *textureIDPointer);
00145 glTexSubImage1D(GL_TEXTURE_1D, 0, 0, transferFunctionTextureWidth, GL_RGBA, GL_UNSIGNED_BYTE, transferFunctionTexture);
00146 get_errors();
00147 }
00148
00149 glutSetWindow(initialWindow);
00150 }