00001 #include <wx/wx.h>
00002 #include <wx/button.h>
00003
00004 #include "vuSpectral.h"
00005 #include "vuColourRGBa.h"
00006 #include "vuMatrix.h"
00007 #include "vuArcBall.h"
00008 #include "vuPreviewWin.h"
00009
00010
00011
00012
00013
00014 enum
00015 {
00016 idCANVAS,
00017 idRENDER,
00018 idLOADSETUP,
00019 idLGTINT,
00020 idLGTCOL,
00021 idLIGHTPOS,
00022 };
00023
00024 BEGIN_EVENT_TABLE(vuSpectral, vuBasicUtility)
00025 EVT_COMMAND_SCROLL(idLGTINT, vuSpectral::OnSlideLight)
00026 EVT_COMMAND_SCROLL(idLGTCOL, vuSpectral::OnSlideLight)
00027 EVT_CHECKBOX(vuSpectral::idDOSPECULAR, vuSpectral::handleGUIevent)
00028 EVT_CHECKBOX(vuSpectral::idDRAWPREV, vuSpectral::handleGUIevent)
00029 EVT_BUTTON (idRENDER, vuSpectral::OnButtonRender)
00030 EVT_BUTTON (idLOADSETUP, vuSpectral::OnButtonLoadSetup)
00031 EVT_BUTTON (idLIGHTPOS, vuSpectral::OnButtonLightPos)
00032 END_EVENT_TABLE();
00033
00034
00035
00036
00037
00038 vuSpectral::vuSpectral() : m_TFunc(32), m_TFuncDlg(this, m_TFunc),
00039 m_LightDial(this, m_TFunc)
00040 {
00041 m_Data = NULL;
00042 }
00043
00044
00045
00046
00047
00048 vuSpectral::~vuSpectral()
00049 {
00050 if (m_Data != 0) delete m_Data;
00051 }
00052
00053
00054
00055
00056
00057 const char* vuSpectral::getFileType()
00058 {
00059 return "11121";
00060 }
00061
00062
00063
00064
00065
00066 bool vuSpectral::init(const char* DataFile)
00067 {
00068
00069 SetTitle("Spectral Volume Rendering");
00070 CreateStatusBar();
00071
00072
00073
00074 m_Data = new vu1112112;
00075 m_Data->setFileName(DataFile);
00076
00077
00078 m_TFunc.addOpacity(1,0.01);
00079 m_TFunc.addOpacity(11,0.01);
00080 m_TFunc.addOpacity(12,1);
00081 m_TFunc.addOpacity(32,1);
00082 m_TFunc.addColour(1,vuColourRGBa(0.f));
00083 m_TFunc.addColour(11,vuColourRGBa(1.f));
00084 m_TFunc.addColour(32,vuColourRGBa(0.f,0.f,0.6f));
00085 m_TFunc.setOpacitySmoothing(0);
00086 m_TFunc.setColourSmoothing(0);
00087 m_TFunc.generateFunction();
00088 m_Data->setTransferFunc(m_TFunc);
00089
00090
00091 bool success = m_Data->read();
00092 if (success)
00093 {
00094 m_glCanvas->SetSize(512,512);
00095 Fit();
00096 m_Preview->attachCamera(&m_Data->getCamera());
00097 m_Preview->setCubeSize(m_Data->getDim1Size(),m_Data->getDim2Size(),m_Data->getDim3Size());
00098 }
00099 else
00100 {
00101 wxMessageDialog dlg(this,m_Data->getErrorMessage(),"vuSpectralVR",wxOK);
00102 dlg.ShowModal();
00103 }
00104
00105 return success;
00106 }
00107
00108
00109
00110
00111
00112 void vuSpectral::addRight(wxSizer *sizer)
00113 {
00114
00115 sizer->Add( new wxButton(this, idRENDER, "Render"),
00116 0,
00117 wxALL,
00118 10 );
00119 sizer->Add( new wxButton(this, idLIGHTPOS, "Place Light"),
00120 0, wxALL, 10 );
00121
00122 wxCheckBox *CBdoSpec = new wxCheckBox(this,idDOSPECULAR,"do specular");
00123 CBdoSpec->SetValue(false);
00124 if(m_Data) m_Data->setDoSpecular(CBdoSpec->GetValue());
00125 wxCheckBox *CBdrawPrev = new wxCheckBox(this,idDRAWPREV,"draw preview");
00126 CBdrawPrev->SetValue(true);
00127 if(m_Data) m_Data->setDrawPreview(CBdrawPrev->GetValue());
00128 sizer->Add( CBdrawPrev,0,wxALL|wxALIGN_LEFT,1);
00129 sizer->Add( CBdoSpec,0,wxALL|wxALIGN_LEFT,1);
00130
00131 m_Preview = new vuPreviewWin(this,200,200);
00132 sizer->Add( m_Preview,
00133 1,
00134 wxALL,
00135 10 );
00136
00137 }
00138
00139
00140
00141
00142
00143 void vuSpectral::OnSlideLight( wxScrollEvent& event)
00144 {
00145
00146
00147 m_glCanvas->redraw();
00148 }
00149
00150
00151
00152
00153
00154 void vuSpectral::OnButtonRender( wxCommandEvent& event)
00155 {
00156 m_DrawPreview = false;
00157 m_Data->doRefresh();
00158 m_glCanvas->redraw();
00159 }
00160
00161
00162
00163
00164
00165
00166 void vuSpectral::OnButtonLoadSetup( wxCommandEvent& event)
00167 {
00168 wxFileDialog fd(this,"Choose a file","","","*.txt");
00169 if(fd.ShowModal() == wxID_OK)
00170 {
00171
00172 cout<<"loading... "<<flush;
00173 try {
00174 if(m_Data->load_scene(fd.GetPath()))
00175 cout<<"successful."<<endl;
00176 else
00177 cout<<"failed."<<endl;
00178 } catch (char *msg) {
00179 printf("%s\n",msg);
00180 }
00181 }
00182 }
00183
00184 void vuSpectral::notifyDataChanged()
00185 {
00186 m_glCanvas->redraw();
00187 }
00188
00189
00190
00191
00192
00193 bool vuSpectral::glInit(void)
00194 {
00195 if (m_Data == 0) return false;
00196
00197
00198
00199
00200
00201
00202
00203 glEnable(GL_DEPTH_TEST);
00204 glDepthFunc(GL_LESS);
00205 m_Data->initOpenGL();
00206
00207 return true;
00208 };
00209
00210
00211
00212
00213
00214
00215 void vuSpectral::glRender()
00216 {
00217 bool preview = false;
00218 if(m_TFuncDlg.isUpdated() || m_LightDial.isUpdated()) {
00219 if(!m_TFuncDlg.isUpdated())
00220 m_TFuncDlg.updateSliders();
00221 else m_LightDial.updateSliders();
00222 m_TFuncDlg.unsetUpdated();
00223 vuColour31a light(m_TFunc.getLight());
00224
00225 m_Data->setLight(light);
00226 } else preview = m_DrawPreview;
00227 m_Data->setTransferFunc(m_TFunc);
00228
00229 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
00230 glMatrixMode(GL_PROJECTION);
00231 glLoadIdentity();
00232 glOrtho(0,m_glCanvas->getWidth(),
00233 0,m_glCanvas->getHeight(),-1,1);
00234 glMatrixMode(GL_MODELVIEW);
00235 glLoadIdentity();
00236
00237 wxStopWatch watch;
00238 watch.Start();
00239
00240 if(preview)
00241 m_Data->preview();
00242 else
00243 m_Data->render();
00244
00245 watch.Pause();
00246 SetStatusText(wxString("Render Time: ") + vuString(watch.Time()).c_str() + "ms");
00247 m_DrawPreview = true;
00248 };
00249
00250
00251
00252
00253
00254 void vuSpectral::glResize()
00255 {
00256 m_Data->setImageSize(m_glCanvas->getWidth(),m_glCanvas->getHeight());
00257 }
00258
00259
00260
00261
00262
00263 void vuSpectral::onMouse(wxMouseEvent &ev)
00264 {
00265 if(!m_Data) return;
00266
00267 if (ev.LeftDClick()) {
00268 m_TFuncDlg.Show(true);
00269 }
00270 else if (ev.RightDClick()) {
00271 m_LightDial.Show(true);
00272 }
00273 }
00274
00275 vuCamera* vuSpectral::getCamera ()
00276 {
00277 return m_Data->getCameraPtr ();
00278 }
00279
00280 vuImage* vuSpectral::getCurrentImage ()
00281
00282 {
00283 return m_Data->getImage ();
00284
00285 }
00286
00287 void vuSpectral::DrawAgain()
00288
00289 {
00290 m_Data->doRefresh();
00291 m_glCanvas->redraw();
00292 }
00293
00294 void vuSpectral::DrawFromImage ()
00295
00296 {
00297
00298
00299 m_glCanvas->redraw ();
00300 }
00301
00302 void vuSpectral::handleGUIevent(wxCommandEvent& ev)
00303 {
00304 switch(ev.GetId()) {
00305 case idDOSPECULAR:
00306 m_Data->setDoSpecular(ev.IsChecked());
00307 break;
00308 case idDRAWPREV:
00309 m_Data->setDrawPreview(ev.IsChecked());
00310 break;
00311 }
00312 }
00313
00314 void vuSpectral::OnButtonLightPos(wxCommandEvent& ev) {
00315 vuVector v(m_Data->getCenter());
00316 v-=getCamera()->getPosition();
00317 v.makeUnit();
00318 v*=-1.0f;
00319 m_Data->setLightDir(v);
00320 }
00321
00322 vu1 *vuSpectral::getVolume()
00323 {
00324 return (vu1*)m_Data;
00325 }