00001
00002
00003
00004
00005
00006
00007 #include "glew.h"
00008 #include "gl/glu.h"
00009 #pragma comment(lib,"OpenGL32.lib")
00010 #pragma comment(lib,"glu32.lib")
00011 #pragma comment(lib,"glew32.lib")
00012
00013 #include <QLayout>
00014
00015 #include "CTransferProperty.h"
00016 #include "Definitions.h"
00017 #include "Memory/mmgr.h"
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 CTransferProperty::CTransferProperty(const char * pName,const char * pID)
00029 : CShaderProperty(pName,pID)
00030 {
00031 pTransferWdgt = new CTransferWdt();
00032 connect(pTransferWdgt,SIGNAL(Dirty(int)),this,SLOT(MarkDirty(int)));
00033 connect(pTransferWdgt,SIGNAL(Dirty(int)),this,SLOT(MarkDirty(int)));
00034
00035 pTransferWdgt->setVisible(false);
00036
00037 txTransfer = 0;
00038 transferSize = QSize(0,0);
00039
00040 dirty = false;
00041 }
00042
00043
00044 CTransferProperty::~CTransferProperty(void)
00045 {
00046 if(glIsTexture(txTransfer)){
00047 glDeleteTextures(1,&txTransfer);
00048 }
00049 pTransferWdgt = NULL;
00050 }
00051
00052
00053 void CTransferProperty::Attach( QLayout * insertLayout )
00054 {
00055 insertLayout->addWidget(pTransferWdgt);
00056 }
00057
00058
00059 void CTransferProperty::Show()
00060 {
00061 pTransferWdgt->show();
00062 }
00063
00064
00065 void CTransferProperty::OpenState(QSettings &openFile )
00066 {
00067 openFile.beginGroup(propertyName);
00068
00069
00070 if(!openFile.contains("propertyID")){
00071 SaveDefaultState(openFile);
00072 }
00073
00074
00075 openFile.beginGroup("Histogram");
00076 pTransferWdgt->SetHistogramQuality(openFile.value("HistQuality").toInt());
00077 pTransferWdgt->SetHistogramSize(openFile.value("HistSize").toInt());
00078 openFile.endGroup();
00079
00080
00081 pTransferWdgt->ClearColorStops(SELECT_COLOR);
00082 pTransferWdgt->ClearColorStops(SELECT_ALPHA);
00083 QGradientStops stopList;
00084 QGradientStop stop;
00085
00086 int sizeC = openFile.beginReadArray("TransferColorStops");
00087 for(int i = 0 ; i < sizeC ; i ++)
00088 {
00089 openFile.setArrayIndex(i);
00090 stop.first = openFile.value("stop").toDouble();
00091 stop.second = openFile.value("color").value<QColor>();
00092 stopList.push_back(stop);
00093 }
00094 openFile.endArray();
00095 pTransferWdgt->SetColorStops(stopList,SELECT_COLOR);
00096
00097
00098 stopList.clear();
00099 int sizeA = openFile.beginReadArray("TransferAlphaStops");
00100 for(int i = 0 ; i < sizeA ; i ++)
00101 {
00102 openFile.setArrayIndex(i);
00103 stop.first = openFile.value("stop").toDouble();
00104 stop.second = openFile.value("color").value<QColor>();
00105 stopList.push_back(stop);
00106 }
00107 openFile.endArray();
00108 pTransferWdgt->SetColorStops(stopList,SELECT_ALPHA);
00109
00110 openFile.endGroup();
00111 }
00112
00113
00114 void CTransferProperty::SaveState(QSettings &saveFile )
00115 {
00116 saveFile.beginGroup(propertyName);
00117
00118
00119 saveFile.setValue("propertyID",propertyID);
00120
00121
00122 saveFile.beginGroup("Histogram");
00123 saveFile.setValue("HistQuality",pTransferWdgt->GetHistogramQuality());
00124 saveFile.setValue("HistSize",pTransferWdgt->GetHistogramSize());
00125 saveFile.endGroup();
00126
00127
00128 QGradientStops & stoplistC = pTransferWdgt->GetColorStops(SELECT_COLOR);
00129
00130 saveFile.beginWriteArray("TransferColorStops");
00131 for(int i = 0 ; i < stoplistC.size() ; i ++)
00132 {
00133 saveFile.setArrayIndex(i);
00134 saveFile.setValue("stop",stoplistC.at(i).first);
00135 saveFile.setValue("color",stoplistC.at(i).second);
00136 }
00137 saveFile.endArray();
00138
00139
00140 QGradientStops & stoplistA = pTransferWdgt->GetColorStops(SELECT_ALPHA);
00141
00142 saveFile.beginWriteArray("TransferAlphaStops");
00143 for(int i = 0 ; i < stoplistA.size() ; i ++)
00144 {
00145 saveFile.setArrayIndex(i);
00146 saveFile.setValue("stop",stoplistA.at(i).first);
00147 saveFile.setValue("color",stoplistA.at(i).second);
00148 }
00149 saveFile.endArray();
00150
00151 saveFile.endGroup();
00152 }
00153
00154
00155 void CTransferProperty::SaveDefaultState(QSettings &saveFile )
00156 {
00157
00158 saveFile.setValue("propertyID",propertyID);
00159
00160
00161 saveFile.beginGroup("Histogram");
00162 saveFile.setValue("HistQuality",saveFile.value("HistQuality",1));
00163 saveFile.setValue("HistSize",saveFile.value("HistSize",1024));
00164 saveFile.endGroup();
00165
00166
00167 saveFile.beginWriteArray("TransferColorStops");
00168 saveFile.setArrayIndex(0);
00169 saveFile.setValue("stop",saveFile.value("stop",0.0));
00170 saveFile.setValue("color",saveFile.value("color",QColor("magenta")));
00171 saveFile.setArrayIndex(1);
00172 saveFile.setValue("stop",saveFile.value("stop",1.0));
00173 saveFile.setValue("color",saveFile.value("color",QColor("yellow")));
00174 saveFile.endArray();
00175
00176
00177 saveFile.beginWriteArray("TransferAlphaStops");
00178 saveFile.setArrayIndex(0);
00179 saveFile.setValue("stop",saveFile.value("stop",0.0));
00180 saveFile.setValue("color",saveFile.value("color",QColor("black")));
00181 saveFile.setArrayIndex(1);
00182 saveFile.setValue("stop",saveFile.value("stop",1.0));
00183 saveFile.setValue("color",saveFile.value("color",QColor("white")));
00184 saveFile.endArray();
00185 }
00186
00187
00188
00189
00190
00191 void CTransferProperty::UpdateShader(uint programID,int texUnit,int texNum)
00192 {
00193 glActiveTexture(texUnit);
00194
00195 if(dirty){
00196 CreateTransferTexture();
00197 dirty = false;
00198 }
00199
00200 glBindTexture(GL_TEXTURE_1D,txTransfer);
00201 GLint loc = glGetUniformLocation(programID,propertyID);
00202 glUniform1i(loc,texNum);
00203 }
00204
00205
00206
00207
00208
00209
00210 bool CTransferProperty::CreateTransferTexture()
00211 {
00212 if(txTransfer){
00213 if(pTransferWdgt->GetTransferSize() == transferSize){
00214 if(pTransferWdgt->GetTransferSize().height()==1){
00215 glBindTexture(GL_TEXTURE_1D,txTransfer);
00216 glTexSubImage1D(GL_TEXTURE_1D,0,0,transferSize.width(),GL_RGBA,GL_UNSIGNED_BYTE,pTransferWdgt->GetTransfer());
00217 }else{
00218 glBindTexture(GL_TEXTURE_2D,txTransfer);
00219 glTexSubImage2D(GL_TEXTURE_2D,0,0,0,transferSize.width(),transferSize.height(),
00220 GL_RGBA,GL_UNSIGNED_BYTE,pTransferWdgt->GetTransfer());
00221
00222 }
00223 return true;
00224 }else{
00225 glDeleteTextures(1,&txTransfer);
00226 }
00227 }
00228
00229 if(pTransferWdgt->GetTransferSize().height()==1)
00230 {
00231 glGenTextures(1,&txTransfer);
00232 glBindTexture(GL_TEXTURE_1D,txTransfer);
00233 glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00234 glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00235 glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
00236 glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
00237 glTexImage1D(GL_TEXTURE_1D,0,GL_RGBA,pTransferWdgt->GetTransferSize().width(),
00238 0,GL_RGBA,GL_UNSIGNED_BYTE,pTransferWdgt->GetTransfer());
00239 }
00240 else
00241 {
00242 glGenTextures(1,&txTransfer);
00243 glBindTexture(GL_TEXTURE_2D,txTransfer);
00244 glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00245 glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00246 glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
00247 glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
00248 glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,pTransferWdgt->GetTransferSize().width(),
00249 pTransferWdgt->GetTransferSize().height(),0,GL_RGBA,GL_UNSIGNED_BYTE,pTransferWdgt->GetTransfer());
00250 }
00251
00252
00253 transferSize = pTransferWdgt->GetTransferSize();
00254
00255 return glIsTexture(txTransfer);
00256 }