00001 #include "VolumeBuffer.h"
00002 #include <QtGui>
00003 #include <QtOpenGL>
00004
00005 #include <math.h>
00006
00007 #include "sliceWidget2.h"
00008 #include <GL/gl.h>
00009 #include <GL/glu.h>
00010 #include <GL/glut.h>
00011
00012 CGcontext s2cgContext;
00013
00014 #include <iostream>
00015 #include <fstream>
00016 using namespace std;
00017
00018 void s2cgErrorCallback()
00019 {
00020 CGerror lastError = cgGetError();
00021 if(lastError)
00022 {
00023 printf("%s\n", cgGetErrorString(lastError));
00024 printf("%s\n", cgGetLastListing(s2cgContext));
00025 exit(1);
00026 }
00027 }
00028
00029 sliceWidget2::sliceWidget2(QWidget *parent)
00030 : QGLWidget(parent)
00031 {
00032 setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
00033
00034 m_vb = new VolumeBuffer(GL_UNSIGNED_BYTE, 256, 256, 256, 0);
00035 s2cgContext = cgCreateContext();
00036 cgSetErrorCallback(s2cgErrorCallback);
00037
00038 m_tf_texture = new unsigned char [256*256*4];
00039
00040 m_current_slice = 0;
00041
00042 m_tfimg = QImage(256, 4, QImage::Format_ARGB32_Premultiplied);
00043 m_tfimg.fill(0);
00044 }
00045
00046 sliceWidget2::~sliceWidget2()
00047 {
00048 makeCurrent();
00049 delete [] m_tf_texture;
00050 }
00051
00052 QSize sliceWidget2::minimumSizeHint() const
00053 {
00054 return QSize(50, 50);
00055 }
00056
00057 QSize sliceWidget2::sizeHint() const
00058 {
00059 return QSize(256, 256);
00060 }
00061
00062 void sliceWidget2::initializeGL()
00063 {
00064
00065 glewInit();
00066
00067 qglClearColor(QColor::fromRgb(255,255,255));
00068 glShadeModel (GL_FLAT);
00069
00070 glEnable(GL_BLEND);
00071 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00072
00073 m_vr = new VolumeRender(s2cgContext,m_vb);
00074 }
00075
00076 void sliceWidget2::paintGL()
00077 {
00078 makeCurrent();
00079 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00080
00081 glLoadIdentity();
00082 glTranslated(0.0, 0.0, 0.0);
00083
00084 glEnable(GL_TEXTURE_1D);
00085
00086
00087
00088 deleteTexture(m_vr->m_tfid);
00089
00090
00091 glGenTextures(1, &(m_vr->m_tfid));
00092
00093 int iy = 0;
00094 int ix;
00095
00096 memset(m_tf_texture,0,256*4*sizeof(unsigned char));
00097
00098 ofstream tf_r_file;
00099 tf_r_file.open ("tf_red.txt");
00100 ofstream tf_g_file;
00101 tf_g_file.open ("tf_green.txt");
00102 ofstream tf_b_file;
00103 tf_b_file.open ("tf_blue.txt");
00104 ofstream tf_a_file;
00105 tf_a_file.open ("tf_alpha.txt");
00106
00107
00108
00109
00110 for (ix = 0; ix < 256; ix++)
00111 {
00112 QRgb tfc = m_tfimg.pixel(ix,iy);
00113
00114 m_tf_texture[ix*4 + 0] = (GLubyte) qRed(tfc);
00115 m_tf_texture[ix*4 + 1] = (GLubyte) qGreen(tfc);
00116 m_tf_texture[ix*4 + 2] = (GLubyte) qBlue(tfc);
00117 m_tf_texture[ix*4 + 3] = (GLubyte) qAlpha(tfc);
00118
00119
00120 tf_r_file << qRed(tfc) << " ";
00121 tf_g_file << qGreen(tfc) << " ";
00122 tf_b_file << qBlue(tfc) << " ";
00123 tf_a_file << qAlpha(tfc) << " ";
00124
00125 }
00126
00127 tf_r_file.close();
00128 tf_g_file.close();
00129 tf_b_file.close();
00130 tf_a_file.close();
00131
00132
00133 glBindTexture(GL_TEXTURE_1D,m_vr->m_tfid);
00134
00135
00136 glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00137 glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00138
00139 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
00140 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
00141
00142
00143 glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_tf_texture);
00144
00145
00146 glEnable(GL_TEXTURE_1D);
00147
00148
00149
00150 deleteTexture(m_vr->m_tfid_s1);
00151
00152
00153 glGenTextures(1, &(m_vr->m_tfid_s1));
00154
00155
00156 memset(m_tf_texture,0,256*4*sizeof(unsigned char));
00157
00158 tf_r_file.open ("tf_red_s1.txt");
00159 tf_g_file.open ("tf_green_s1.txt");
00160 tf_b_file.open ("tf_blue_s1.txt");
00161 tf_a_file.open ("tf_alpha_s1.txt");
00162
00163
00164
00165
00166 for (ix = 0; ix < 256; ix++)
00167 {
00168
00169 QRgb tfc = m_tfimg.pixel(ix,1);
00170
00171 m_tf_texture[ix*4 + 0] = (GLubyte) qRed(tfc);
00172 m_tf_texture[ix*4 + 1] = (GLubyte) qGreen(tfc);
00173 m_tf_texture[ix*4 + 2] = (GLubyte) qBlue(tfc);
00174 m_tf_texture[ix*4 + 3] = (GLubyte) qAlpha(tfc);
00175
00176
00177 tf_r_file << qRed(tfc) << " ";
00178 tf_g_file << qGreen(tfc) << " ";
00179 tf_b_file << qBlue(tfc) << " ";
00180 tf_a_file << qAlpha(tfc) << " ";
00181
00182 }
00183
00184 tf_r_file.close();
00185 tf_g_file.close();
00186 tf_b_file.close();
00187 tf_a_file.close();
00188
00189
00190 glBindTexture(GL_TEXTURE_1D,m_vr->m_tfid_s1);
00191
00192
00193 glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00194 glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00195
00196 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
00197 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
00198
00199
00200 glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_tf_texture);
00201
00202
00203
00204 glEnable(GL_TEXTURE_1D);
00205
00206
00207
00208 deleteTexture(m_vr->m_tfid_s2);
00209
00210
00211 glGenTextures(1, &(m_vr->m_tfid_s2));
00212
00213
00214 memset(m_tf_texture,0,256*4*sizeof(unsigned char));
00215
00216 tf_r_file.open ("tf_red_s2.txt");
00217 tf_g_file.open ("tf_green_s2.txt");
00218 tf_b_file.open ("tf_blue_s2.txt");
00219 tf_a_file.open ("tf_alpha_s2.txt");
00220
00221
00222
00223
00224 for (ix = 0; ix < 256; ix++)
00225 {
00226
00227 QRgb tfc = m_tfimg.pixel(ix,2);
00228
00229 m_tf_texture[ix*4 + 0] = (GLubyte) qRed(tfc);
00230 m_tf_texture[ix*4 + 1] = (GLubyte) qGreen(tfc);
00231 m_tf_texture[ix*4 + 2] = (GLubyte) qBlue(tfc);
00232 m_tf_texture[ix*4 + 3] = (GLubyte) qAlpha(tfc);
00233
00234
00235 tf_r_file << qRed(tfc) << " ";
00236 tf_g_file << qGreen(tfc) << " ";
00237 tf_b_file << qBlue(tfc) << " ";
00238 tf_a_file << qAlpha(tfc) << " ";
00239
00240 }
00241
00242 tf_r_file.close();
00243 tf_g_file.close();
00244 tf_b_file.close();
00245 tf_a_file.close();
00246
00247
00248 glBindTexture(GL_TEXTURE_1D,m_vr->m_tfid_s2);
00249
00250
00251 glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00252 glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00253
00254 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
00255 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
00256
00257
00258 glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_tf_texture);
00259
00260
00261
00262 glEnable(GL_TEXTURE_1D);
00263
00264
00265
00266 deleteTexture(m_vr->m_tfid_s3);
00267
00268
00269 glGenTextures(1, &(m_vr->m_tfid_s3));
00270
00271
00272 memset(m_tf_texture,0,256*4*sizeof(unsigned char));
00273
00274 tf_r_file.open ("tf_red_s3.txt");
00275 tf_g_file.open ("tf_green_s3.txt");
00276 tf_b_file.open ("tf_blue_s3.txt");
00277 tf_a_file.open ("tf_alpha_s3.txt");
00278
00279
00280
00281
00282 for (ix = 0; ix < 256; ix++)
00283 {
00284
00285 QRgb tfc = m_tfimg.pixel(ix,3);
00286
00287 m_tf_texture[ix*4 + 0] = (GLubyte) qRed(tfc);
00288 m_tf_texture[ix*4 + 1] = (GLubyte) qGreen(tfc);
00289 m_tf_texture[ix*4 + 2] = (GLubyte) qBlue(tfc);
00290 m_tf_texture[ix*4 + 3] = (GLubyte) qAlpha(tfc);
00291
00292
00293 tf_r_file << qRed(tfc) << " ";
00294 tf_g_file << qGreen(tfc) << " ";
00295 tf_b_file << qBlue(tfc) << " ";
00296 tf_a_file << qAlpha(tfc) << " ";
00297
00298 }
00299
00300 tf_r_file.close();
00301 tf_g_file.close();
00302 tf_b_file.close();
00303 tf_a_file.close();
00304
00305
00306 glBindTexture(GL_TEXTURE_1D,m_vr->m_tfid_s3);
00307
00308
00309 glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00310 glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00311
00312 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
00313 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
00314
00315
00316 glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_tf_texture);
00317
00318
00319 glEnable(GL_TEXTURE_2D);
00320 glEnable(GL_BLEND);
00321 glDisable(GL_DEPTH_TEST);
00322
00323 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
00324
00325
00326 m_vr->setVolume(m_vb);
00327 glEnable(GL_TEXTURE_2D);
00328 m_vr->renderSlice(m_current_slice);
00329
00330
00331 glFlush();
00332 glDisable(GL_TEXTURE_2D);
00333
00334 }
00335
00336 void sliceWidget2::resizeGL(int width, int height)
00337 {
00338 int side = qMin(width, height);
00339
00340
00341 glViewport (0, 0, (GLsizei) width, (GLsizei) height);
00342 glMatrixMode (GL_PROJECTION);
00343 glLoadIdentity ();
00344 glFrustum (-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
00345 glMatrixMode (GL_MODELVIEW);
00346 }
00347
00348 void sliceWidget2::mousePressEvent(QMouseEvent *event)
00349 {
00350 }
00351
00352 void sliceWidget2::mouseMoveEvent(QMouseEvent *event)
00353 {
00354 }
00355
00356 void sliceWidget2::vr_load_data3(VolumeBuffer *vb1)
00357 {
00358
00359 makeCurrent();
00360
00361 std::cout << "Data loading...";
00362
00363
00364 glEnable(GL_TEXTURE_2D);
00365 vb1->create2DTextures(GL_UNSIGNED_BYTE);
00366
00367 delete m_vb;
00368 m_vb = vb1;
00369
00370
00371
00372
00373 std::cout << "loaded\n";
00374 updateGL();
00375 }
00376
00377 void sliceWidget2::setSlice(int slice)
00378 {
00379 makeCurrent();
00380 m_current_slice = slice;
00381 updateGL();
00382 }
00383
00384 void sliceWidget2::setTFtexture(QImage &tfimg)
00385 {
00386 makeCurrent();
00387 int ix = 0;
00388
00389 for (ix = 0; ix < m_tfimg.width(); ix++)
00390 m_tfimg.setPixel(ix, 0, tfimg.pixel(ix,0));
00391
00392 updateGL();
00393 }
00394
00395 void sliceWidget2::setTFtexture1(QImage &tfimg)
00396 {
00397 makeCurrent();
00398 int ix = 0;
00399
00400 for (ix = 0; ix < m_tfimg.width(); ix++)
00401 m_tfimg.setPixel(ix, 1, tfimg.pixel(ix,0));
00402
00403 updateGL();
00404 }
00405
00406 void sliceWidget2::setTFtexture2(QImage &tfimg)
00407 {
00408 makeCurrent();
00409 int ix = 0;
00410
00411 for (ix = 0; ix < m_tfimg.width(); ix++)
00412 m_tfimg.setPixel(ix, 2, tfimg.pixel(ix,0));
00413
00414 updateGL();
00415 }
00416
00417 void sliceWidget2::setTFtexture3(QImage &tfimg)
00418 {
00419 makeCurrent();
00420 int ix = 0;
00421
00422 for (ix = 0; ix < m_tfimg.width(); ix++)
00423 m_tfimg.setPixel(ix, 3, tfimg.pixel(ix,0));
00424
00425 updateGL();
00426 }