Visualisierung 2
Comparison of Hue Preserving Rendering to Alpha Composing
GLRenderWidget.cpp
Go to the documentation of this file.
1 
8 #include "GLRenderWidget.h"
9 #include "FileReader.h"
10 #include <iostream>
11 
12 #ifdef _DEBUG
13 #define ENABLE_GL_DBG_OUTPUT
14 #endif
15 
16 GLRenderWidget::GLRenderWidget(QWidget *parent) : QOpenGLWidget(parent), vbo(QOpenGLBuffer::VertexBuffer), indices(QOpenGLBuffer::IndexBuffer), dirty(false), hasData(false), hasShader(false) {
17  QSurfaceFormat fmt;
18  fmt.setRenderableType(QSurfaceFormat::OpenGL);
19  fmt.setDepthBufferSize(24);
20  fmt.setVersion(4, 3);
21  fmt.setProfile(QSurfaceFormat::CoreProfile);
22 #ifdef ENABLE_GL_DBG_OUTPUT
23  fmt.setOption(QSurfaceFormat::DebugContext);
24 #endif
25  setFormat(fmt);
26  QSurfaceFormat::setDefaultFormat(fmt);
27 }
28 
30 {
31 }
32 
34  auto ctxfmt = context()->format();
35  auto prof = ctxfmt.profile();
36  auto ctxprofile = prof == QSurfaceFormat::CompatibilityProfile ? "Comp" : prof == QSurfaceFormat::CoreProfile ? "Core" : "No Profile";
37  auto ctxvers = ctxfmt.version();
38  std::cout << " Version: " << ctxvers.first << "." << ctxvers.second << " | Profile " << ctxprofile << std::endl;
39 
40  initializeOpenGLFunctions();
41 #ifdef ENABLE_GL_DBG_OUTPUT
42  logger = new QOpenGLDebugLogger(this);
43  logger->initialize();
44 #endif
45 
46  glEnable(GL_BLEND);
47  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
48  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
49  glViewport(0, 0, (GLint)this->width(), (GLint)this->height());
50 }
51 
52 
54  glViewport(0, 0, (GLint)width, (GLint)height);
55 }
56 
58 
59  if (dirty && hasShader) {
60  updateVBOs();
61  }
62  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
63 
64  if (hasShader && hasData && hasRegionSeperatorValueAndColors && volume != nullptr && volume->isCreated()) {
65  vao.bind();
66  shader->bind();
67  volume->bind();
68  shader->setUniformValue("regionSeperator",regionSeperator);
69  shader->setUniformValue("regionColor1",regionColor1);
70  shader->setUniformValue("regionColor2",regionColor2);
71  shader->setUniformValue("depth",volume->depth());
72  shader->setUniformValue("height", volume->height());
73  shader->setUniformValue("width", volume->width());
74  shader->setUniformValue("orientation", orientation);
75  glDrawElements(GL_TRIANGLES, (GLuint)numVerts, GL_UNSIGNED_INT, 0);
76  shader->release();
77  vao.release();
78 #ifdef ENABLE_GL_DBG_OUTPUT
79  auto msgs = logger->loggedMessages();
80  for (const auto& msg : msgs) {
81  std::cout << "Rendering: " << msg.message().toStdString();
82  }
83 #endif
84  }
85 }
86 
88  objectData = data;
89  dirty = true;
90 }
91 
93  orientation = ori;
94 }
95 
96 void GLRenderWidget::setShader(const std::string &vert, const std::string &frag) {
97  makeCurrent();
98  shader = new QOpenGLShaderProgram;
99 
100  size_t error = 0;
101  auto vertShaderCode = readFile(vert, error);
102  if (error) {
103  // log error?
104  }
105  auto fragShaderCode = readFile(frag, error);
106  if (error) {
107  // log error
108  }
109 
110  auto vstatus = shader->addShaderFromSourceCode(QOpenGLShader::Vertex, vertShaderCode.c_str());
111  if (!vstatus) {
112  std::cout << "unable to add vert shader" << std::endl;
113  }
114  auto fstatus = shader->addShaderFromSourceCode(QOpenGLShader::Fragment, fragShaderCode.c_str());
115  if (!fstatus){
116  std::cout << "unable to add frag shader" << std::endl;
117  }
118  if (vstatus && fstatus) {
119  if (!shader->link()) {
120  std::cout << "unable to link shaders" << std::endl;
121  } else {
122  hasShader = true;
123  }
124  }
125  shader->bindAttributeLocation("pos", 0);
126 
127 #ifdef ENABLE_GL_DBG_OUTPUT
128  auto msgs = logger->loggedMessages();
129  for (const auto& msg : msgs) {
130  std::cout << "Shader creation: " << msg.message().toStdString();
131  }
132 #endif
133 }
134 
135 void GLRenderWidget::updateVBOs() {
136  if (!vao.isCreated()) {
137  vao.create();
138  }
139  vao.bind();
140 
141  vbo.destroy();
142  indices.destroy();
143 
144  vbo.create();
145  vbo.bind();
146  vbo.allocate(objectData.vertices.data(), sizeof(glm::vec3) * objectData.vertices.size());
147 
148  glEnableVertexAttribArray(0);
149  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
150 
151 
152 #ifdef ENABLE_GL_DBG_OUTPUT
153  auto msgs = logger->loggedMessages();
154  for (const auto& msg : msgs) {
155  std::cout << "VBO creation: " << msg.message().toStdString();
156  }
157 #endif
158 
159  indices.create();
160  indices.bind();
161  indices.allocate(objectData.indices.data(), sizeof(unsigned int) * objectData.indices.size());
162 
163  numVerts = objectData.indices.size();
164  dirty = false;
165  hasData = true;
166 
167  vao.release();
168 }
169 
170 void GLRenderWidget::setVolumeTexture(QOpenGLTexture *volume3Dtex) {
171  volume = volume3Dtex;
172 }
173 
174 void GLRenderWidget::setRegion(float sep,QColor c1, QColor c2){
175  regionSeperator=sep;
176  regionColor1 =c1;
177  regionColor2 =c2;
178  hasRegionSeperatorValueAndColors=true;
179 }
180 
void setRenderObject(RenderData &data)
setRenderObject sets vertex positions and indices of the rendered quad.
void resizeGL(int width, int height)
resizeGL handles resize events
QOpenGLVertexArrayObject vao
uniform int height
Definition: alpha.frag:20
QOpenGLShaderProgram * shader
void setOrientation(int ori)
setOrientation sets the orientation parameter for this rendering window.
void setRegion(float sep, QColor c1, QColor c2)
setRegion sets the transfer function parameters for this rendering window.
QOpenGLBuffer indices
void setShader(const std::string &vert, const std::string &frag)
setShader sets the shader to be used for rendering.
QOpenGLDebugLogger * logger
std::string readFile(const std::string &filePath, size_t &error)
readFile reads the content of a file and returns it as string.
Definition: FileReader.cpp:14
GLRenderWidget(QWidget *parent)
File reader functionality.
QOpenGLBuffer vbo
uniform int width
Definition: alpha.frag:19
std::vector< unsigned int > indices
array with indices for the given vertex array
Definition: Renderable.h:25
RenderData objectData
std::vector< glm::vec3 > vertices
array with vertices (vec3)
Definition: Renderable.h:21
void initializeGL()
initializeGL performs openGL setup.
Helper struct for passing renderable data in the application.
Definition: Renderable.h:17
void setVolumeTexture(QOpenGLTexture *volume3Dtex)
setVolumeTexture sets the volume that should be rendered in this window.
void paintGL()
paintGL passes unfiroms to shader program and renders volume into GUI window.