00001 #include "glew/glew.h"
00002 #include "GLWidget.h"
00003 #include "TimeCounter.h"
00004
00005 #include <QtDebug>
00006
00007 GLWidget::GLWidget(Ui_MainWindow uiMainWindow, QWidget *parent) : QGLWidget(parent)
00008 {
00009 QObject::connect(uiMainWindow.action2D, SIGNAL(triggered()), this, SLOT(view2D()));
00010 QObject::connect(uiMainWindow.action3D, SIGNAL(triggered()), this, SLOT(view3D()));
00011
00012 QObject::connect(uiMainWindow.actionGroupShader, SIGNAL(triggered(QAction *)), this, SLOT(shaderChanged(QAction *)));
00013
00014 gl3DView = new GL3DView();
00015 gl3DView->setData(this, &volumeDesc, &transferFunctionDesc, &stateDesc);
00016 gl2DView = new GL2DView();
00017 gl2DView->setData(this, &volumeDesc, &transferFunctionDesc, &stateDesc);
00018
00019 glCurrentView = gl3DView;
00020
00021 updateAsked = false;
00022 timerIdentifier = startTimer(50);
00023 }
00024
00025 GLWidget::~GLWidget()
00026 {
00027 killTimer(timerIdentifier);
00028 glCurrentView->uninitializeGL();
00029 delete gl2DView;
00030 delete gl3DView;
00031 }
00032
00033 void GLWidget::updateGL()
00034 {
00035 updateAsked = true;
00036 }
00037
00038 void GLWidget::timerEvent(QTimerEvent *event)
00039 {
00040 if(updateAsked)
00041 {
00042 updateAsked = false;
00043 QGLWidget::updateGL();
00044 }
00045 }
00046
00047 QSize GLWidget::minimumSizeHint() const
00048 {
00049 return QSize(128, 128);
00050 }
00051
00052 QSize GLWidget::sizeHint() const
00053 {
00054 return QSize(512, 512);
00055 }
00056
00057 void GLWidget::initializeGL()
00058 {
00059 GLenum glError;
00060
00061 glError = glGetError();
00062 if(glError != GL_NO_ERROR)
00063 {
00064 qDebug() << "initializeGL: an error was still registered: " << (char*)gluErrorString(glError);
00065 }
00066
00067 glError = glewInit();
00068 if(glError != GLEW_OK)
00069 {
00070
00071 qDebug() << "Error initializing GLEW: " << (char*)glewGetErrorString(glError);
00072
00073
00074 }
00075 else
00076 {
00077
00078 glCurrentView->initializeGL();
00079 }
00080 }
00081
00082 void GLWidget::paintGL()
00083 {
00084 static bool benchmark = true;
00085 static float fps = 0.0f;
00086 LONGLONG startCount;
00087 GLenum glError;
00088
00089 glError = glGetError();
00090 if(glError != GL_NO_ERROR)
00091 {
00092 qDebug() << "paintGL: an error was still registered: " << (char*)gluErrorString(glError);
00093 }
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 glCurrentView->paintGL();
00118
00119
00120
00121
00122
00123 }
00124
00125 void GLWidget::resizeGL(int width, int height)
00126 {
00127 lastWidth = width;
00128 lastHeight = height;
00129 glCurrentView->resizeGL(width, height);
00130 update();
00131 }
00132
00133 void GLWidget::mousePressEvent(QMouseEvent *event)
00134 {
00135 glCurrentView->mousePressEvent(event);
00136 }
00137
00138 void GLWidget::mouseMoveEvent(QMouseEvent *event)
00139 {
00140 glCurrentView->mouseMoveEvent(event);
00141 }
00142
00143 void GLWidget::wheelEvent(QWheelEvent *event)
00144 {
00145 glCurrentView->wheelEvent(event);
00146 }
00147
00148 void GLWidget::changeVolume(Volume *volume)
00149 {
00150 GLenum glError;
00151
00152 glError = glGetError();
00153 if(glError != GL_NO_ERROR)
00154 {
00155 qDebug() << "changeVolume: an error was still registered: " << (char*)gluErrorString(glError);
00156 }
00157
00158 if(volumeDesc.volumeTexture != 0) {
00159 glDeleteTextures(1, &(volumeDesc.volumeTexture));
00160 glError = glGetError();
00161 if(glError != GL_NO_ERROR)
00162 {
00163 qDebug() << "Error deleting texture: " << (char*)gluErrorString(glError);
00164 volumeDesc.volumeTexture = 0;
00165 }
00166 }
00167
00168 glGenTextures(1, &(volumeDesc.volumeTexture));
00169 glError = glGetError();
00170 if(glError != GL_NO_ERROR)
00171 {
00172 qDebug() << "Error generating texture: " << (char*)gluErrorString(glError);
00173 volumeDesc.volumeTexture = 0;
00174 }
00175 else if(volumeDesc.volumeTexture)
00176 {
00177
00178
00179 glBindTexture(GL_TEXTURE_3D, volumeDesc.volumeTexture);
00180 glError = glGetError();
00181 if(glError != GL_NO_ERROR)
00182 {
00183 qDebug() << "Could not bind texture: " << (char*)gluErrorString(glError);
00184 }
00185 else
00186 {
00187
00188 volumeDesc.set(volume->getWidth(), volume->getHeight(), volume->getDepth());
00189
00190 qDebug() << "- volume size: " << volumeDesc.realWidth << volumeDesc.realHeight << volumeDesc.realDepth << " | texture size: " << volumeDesc.width << volumeDesc.height << volumeDesc.depth;
00191
00192
00193 glTexImage3D(GL_TEXTURE_3D_EXT, 0, GL_LUMINANCE12, volumeDesc.width, volumeDesc.height, volumeDesc.depth, 0, GL_LUMINANCE, GL_UNSIGNED_SHORT, NULL);
00194 glTexSubImage3D(GL_TEXTURE_3D_EXT, 0, volumeDesc.xOffset, volumeDesc.yOffset, volumeDesc.zOffset, volumeDesc.realWidth, volumeDesc.realHeight, volumeDesc.realDepth, GL_LUMINANCE, GL_UNSIGNED_SHORT, (void*)volume->getData());
00195 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00196 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00197
00198 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
00199 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
00200 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
00201
00202
00203
00204 glError = glGetError();
00205 if(glError != GL_NO_ERROR)
00206 {
00207 qDebug() << "Could not upload texture: " << (char*)gluErrorString(glError);
00208 }
00209 }
00210 }
00211
00212 updateGL();
00213 }
00214
00215 void GLWidget::changeTransferFunction(unsigned int *transferFunction, int length)
00216 {
00217 transferFunctionDesc.set(transferFunction, length);
00218 updateGL();
00219 }
00220
00221 void GLWidget::switchView(GLView *newView)
00222 {
00223 glCurrentView->uninitializeGL();
00224 glCurrentView = newView;
00225 glCurrentView->initializeGL();
00226 glCurrentView->resizeGL(lastWidth, lastHeight);
00227 updateGL();
00228 }
00229
00230 void GLWidget::view2D()
00231 {
00232 if(glCurrentView != gl2DView) {
00233 qDebug() << "2D view";
00234 switchView(gl2DView);
00235 }
00236 }
00237
00238 void GLWidget::view3D()
00239 {
00240 if(glCurrentView != gl3DView) {
00241 qDebug() << "3D view";
00242 switchView(gl3DView);
00243 }
00244 }
00245
00246 void GLWidget::shaderChanged(QAction *qAction)
00247 {
00248
00249 stateDesc.setShaderIndex(qAction->property("ShaderIndex").toInt());
00250 updateGL();
00251 }