00001 #include "VolumeBuffer.h"
00002 #include <QtGui>
00003 #include <QtOpenGL>
00004
00005 #include <math.h>
00006
00007 #include "sliceWidget1.h"
00008 #include <GL/gl.h>
00009 #include <GL/glu.h>
00010 #include <GL/glut.h>
00011
00012 CGcontext s1cgContext;
00013
00014 void s1cgErrorCallback()
00015 {
00016 CGerror lastError = cgGetError();
00017 if(lastError)
00018 {
00019 printf("%s\n", cgGetErrorString(lastError));
00020 printf("%s\n", cgGetLastListing(s1cgContext));
00021 exit(1);
00022 }
00023 }
00024
00025 sliceWidget1::sliceWidget1(QWidget *parent)
00026 : QGLWidget(parent)
00027 {
00028 setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
00029
00030 m_vb = new VolumeBuffer(GL_UNSIGNED_BYTE, 256, 256, 256, 0);
00031 s1cgContext = cgCreateContext();
00032 cgSetErrorCallback(s1cgErrorCallback);
00033
00034 m_draw_selection = 0;
00035
00036 selection_quad.first = QPointF(0.0f,0.0f);
00037 selection_quad.second = QPointF(0.0f,0.0f);
00038
00039 m_current_slice = 0;
00040
00041 }
00042
00043 sliceWidget1::~sliceWidget1()
00044 {
00045 makeCurrent();
00046 }
00047
00048 QSize sliceWidget1::minimumSizeHint() const
00049 {
00050 return QSize(50, 50);
00051 }
00052
00053 QSize sliceWidget1::sizeHint() const
00054 {
00055 return QSize(256, 256);
00056 }
00057
00058 void sliceWidget1::initializeGL()
00059 {
00060 glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
00061 glewInit();
00062 qglClearColor(trolltechPurple.dark());
00063 glShadeModel (GL_FLAT);
00064
00065 glEnable(GL_BLEND);
00066 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00067
00068 m_vr = new VolumeRender(s1cgContext,m_vb);
00069 }
00070
00071 void sliceWidget1::paintGL()
00072 {
00073 makeCurrent();
00074 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00075
00076 glLoadIdentity();
00077 glTranslated(0.0, 0.0, 0.0);
00078 glColor4f(1.0f,1.0f,1.0f,1.0f);
00079
00080 glEnable(GL_TEXTURE_2D);
00081 glEnable(GL_BLEND);
00082 glDisable(GL_DEPTH_TEST);
00083
00084 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094 glEnable(GL_TEXTURE_2D);
00095 m_vb->drawZSlice(m_current_slice);
00096
00097 if (m_draw_selection)
00098 {
00099
00100 glDisable(GL_TEXTURE_2D);
00101 glEnable(GL_DEPTH_TEST);
00102 glDepthFunc(GL_ALWAYS);
00103 glColor4f(1.0f,1.0f,0.0f,0.2f);
00104 glBegin(GL_QUADS);
00105
00106
00107
00108
00109
00110
00111 glVertex3f (m_bl_x, m_bl_y, 0.0f);
00112 glVertex3f (m_br_x, m_br_y, 0.0f);
00113 glVertex3f (m_tr_x, m_tr_y, 0.0f);
00114 glVertex3f (m_tl_x, m_tl_y, 0.0f);
00115 glEnd();
00116 }
00117
00118 glFlush();
00119 glDisable(GL_TEXTURE_2D);
00120 }
00121
00122 void sliceWidget1::resizeGL(int width, int height)
00123 {
00124 int side = qMin(width, height);
00125
00126
00127 glViewport (0, 0, (GLsizei) width, (GLsizei) height);
00128 glMatrixMode (GL_PROJECTION);
00129 glLoadIdentity ();
00130 glFrustum (-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
00131 glMatrixMode (GL_MODELVIEW);
00132 }
00133
00134 void sliceWidget1::mousePressEvent(QMouseEvent *event)
00135 {
00136 if(event->button() == Qt::RightButton)
00137 m_draw_selection = 0;
00138
00139 if(event->button() == Qt::LeftButton)
00140 m_draw_selection = 1;
00141
00142 if(m_draw_selection)
00143 {
00144 int ww = this->width() - 1;
00145 int wh = this->height() - 1;
00146
00147 int x_mouse = event->x();
00148 if(x_mouse <= 0)
00149 x_mouse = 0;
00150 else if(x_mouse >= ww)
00151 x_mouse = ww;
00152
00153 int y_mouse = event->y();
00154 if(y_mouse <= 0)
00155 y_mouse = 0;
00156 else if(y_mouse >= wh)
00157 y_mouse = wh;
00158
00159 int x_real = x_mouse;
00160 int y_real = wh - y_mouse;
00161
00162 selection_quad.first = QPointF((float)x_real, (float)y_real);
00163
00164 selection_quad.second = selection_quad.first;
00165
00166 std::cout << "start x:" << selection_quad.first.x() << ", y:" << selection_quad.first.y() << "\n";
00167
00168 int bl_x = qMin(selection_quad.first.x(), selection_quad.second.x());
00169 int bl_y = qMax(selection_quad.first.y(), selection_quad.second.y());
00170
00171 int br_x = qMax(selection_quad.first.x(), selection_quad.second.x());
00172 int br_y = qMax(selection_quad.first.y(), selection_quad.second.y());
00173
00174 int tr_x = qMax(selection_quad.first.x(), selection_quad.second.x());
00175 int tr_y = qMin(selection_quad.first.y(), selection_quad.second.y());
00176
00177 int tl_x = qMin(selection_quad.first.x(), selection_quad.second.x());
00178 int tl_y = qMin(selection_quad.first.y(), selection_quad.second.y());
00179
00180 m_bl_x = -1.0 + (2*(float)bl_x/(float)ww);
00181 m_bl_y = -1.0 + (2*(float)bl_y/(float)wh);
00182 m_br_x = -1.0 + (2*(float)br_x/(float)ww);
00183 m_br_y = -1.0 + (2*(float)br_y/(float)wh);
00184 m_tr_x = -1.0 + (2*(float)tr_x/(float)ww);
00185 m_tr_y = -1.0 + (2*(float)tr_y/(float)wh);
00186 m_tl_x = -1.0 + (2*(float)tl_x/(float)ww);
00187 m_tl_y = -1.0 + (2*(float)tl_y/(float)wh);
00188
00189
00190
00191 updateGL();
00192 }
00193 }
00194
00195 void sliceWidget1::mouseReleaseEvent(QMouseEvent *event)
00196 {
00197 m_draw_selection = 1;
00198
00199 int ww = this->width() - 1;
00200 int wh = this->height() - 1;
00201
00202 int x_mouse = event->x();
00203 if(x_mouse <= 0)
00204 x_mouse = 0;
00205 else if(x_mouse >= ww)
00206 x_mouse = ww;
00207
00208 int y_mouse = event->y();
00209 if(y_mouse <= 0)
00210 y_mouse = 0;
00211 else if(y_mouse >= wh)
00212 y_mouse = wh;
00213
00214 int x_real = x_mouse;
00215 int y_real = wh - y_mouse;
00216
00217 selection_quad.second = QPointF((float)x_real, (float)y_real);
00218
00219
00220
00221 std::cout << "stop x:" << selection_quad.second.x() << ", y:" << selection_quad.second.y() << "\n";
00222
00223 int bl_x = qMin(selection_quad.first.x(), selection_quad.second.x());
00224 int bl_y = qMax(selection_quad.first.y(), selection_quad.second.y());
00225
00226 int br_x = qMax(selection_quad.first.x(), selection_quad.second.x());
00227 int br_y = qMax(selection_quad.first.y(), selection_quad.second.y());
00228
00229 int tr_x = qMax(selection_quad.first.x(), selection_quad.second.x());
00230 int tr_y = qMin(selection_quad.first.y(), selection_quad.second.y());
00231
00232 int tl_x = qMin(selection_quad.first.x(), selection_quad.second.x());
00233 int tl_y = qMin(selection_quad.first.y(), selection_quad.second.y());
00234
00235 m_bl_x = -1.0 + (2*(float)bl_x/(float)ww);
00236 m_bl_y = -1.0 + (2*(float)bl_y/(float)wh);
00237 m_br_x = -1.0 + (2*(float)br_x/(float)ww);
00238 m_br_y = -1.0 + (2*(float)br_y/(float)wh);
00239 m_tr_x = -1.0 + (2*(float)tr_x/(float)ww);
00240 m_tr_y = -1.0 + (2*(float)tr_y/(float)wh);
00241 m_tl_x = -1.0 + (2*(float)tl_x/(float)ww);
00242 m_tl_y = -1.0 + (2*(float)tl_y/(float)wh);
00243
00244
00245
00246 emit selectionChanged(selection_quad);
00247 updateGL();
00248 }
00249
00250 void sliceWidget1::mouseMoveEvent(QMouseEvent *event)
00251 {
00252
00253 if(m_draw_selection)
00254 {
00255 int ww = this->width() - 1;
00256 int wh = this->height() - 1;
00257
00258 int x_mouse = event->x();
00259 if(x_mouse <= 0)
00260 x_mouse = 0;
00261 else if(x_mouse >= ww)
00262 x_mouse = ww;
00263
00264 int y_mouse = event->y();
00265 if(y_mouse <= 0)
00266 y_mouse = 0;
00267 else if(y_mouse >= wh)
00268 y_mouse = wh;
00269
00270 int x_real = x_mouse;
00271 int y_real = wh - y_mouse;
00272
00273 selection_quad.second = QPointF((float)x_real, (float)y_real);
00274
00275
00276
00277 int bl_x = qMin(selection_quad.first.x(), selection_quad.second.x());
00278 int bl_y = qMax(selection_quad.first.y(), selection_quad.second.y());
00279
00280 int br_x = qMax(selection_quad.first.x(), selection_quad.second.x());
00281 int br_y = qMax(selection_quad.first.y(), selection_quad.second.y());
00282
00283 int tr_x = qMax(selection_quad.first.x(), selection_quad.second.x());
00284 int tr_y = qMin(selection_quad.first.y(), selection_quad.second.y());
00285
00286 int tl_x = qMin(selection_quad.first.x(), selection_quad.second.x());
00287 int tl_y = qMin(selection_quad.first.y(), selection_quad.second.y());
00288
00289
00290
00291 m_bl_x = -1.0 + (2*(float)bl_x/(float)ww);
00292 m_bl_y = -1.0 + (2*(float)bl_y/(float)wh);
00293 m_br_x = -1.0 + (2*(float)br_x/(float)ww);
00294 m_br_y = -1.0 + (2*(float)br_y/(float)wh);
00295 m_tr_x = -1.0 + (2*(float)tr_x/(float)ww);
00296 m_tr_y = -1.0 + (2*(float)tr_y/(float)wh);
00297 m_tl_x = -1.0 + (2*(float)tl_x/(float)ww);
00298 m_tl_y = -1.0 + (2*(float)tl_y/(float)wh);
00299
00300
00301 updateGL();
00302 }
00303 }
00304
00305 void sliceWidget1::wheelEvent(QWheelEvent *event)
00306 {
00307
00308 int max_slices = m_vb->getDepth() - 1;
00309
00310 int mul = 1;
00311
00312 int numDegrees = event->delta() / 8;
00313 int numSteps = numDegrees / 15;
00314
00315
00316
00317
00318 m_current_slice = m_current_slice + mul*numSteps;
00319
00320 if(m_current_slice <= 0)
00321 m_current_slice = 0;
00322 else if(m_current_slice >= max_slices)
00323 m_current_slice = max_slices;
00324
00325 emit sliceChanged(m_current_slice);
00326 updateGL();
00327 }
00328
00329 void sliceWidget1::vr_load_data2(VolumeBuffer *vb1)
00330 {
00331
00332 makeCurrent();
00333
00334 std::cout << "Data loading...";
00335
00336
00337 glEnable(GL_TEXTURE_2D);
00338 vb1->create2DTexturesZ(GL_UNSIGNED_BYTE);
00339
00340 delete m_vb;
00341 m_vb = vb1;
00342
00343
00344
00345
00346 std::cout << "loaded\n";
00347 updateGL();
00348 }
00349