13 #define ENABLE_GL_DBG_OUTPUT 16 GLRenderWidget::GLRenderWidget(QWidget *parent) : QOpenGLWidget(parent), vbo(QOpenGLBuffer::VertexBuffer), indices(QOpenGLBuffer::IndexBuffer), dirty(false), hasData(false), hasShader(false) {
18 fmt.setRenderableType(QSurfaceFormat::OpenGL);
19 fmt.setDepthBufferSize(24);
21 fmt.setProfile(QSurfaceFormat::CoreProfile);
22 #ifdef ENABLE_GL_DBG_OUTPUT 23 fmt.setOption(QSurfaceFormat::DebugContext);
26 QSurfaceFormat::setDefaultFormat(fmt);
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;
40 initializeOpenGLFunctions();
41 #ifdef ENABLE_GL_DBG_OUTPUT 42 logger =
new QOpenGLDebugLogger(
this);
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());
59 if (dirty && hasShader) {
62 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
64 if (hasShader && hasData && hasRegionSeperatorValueAndColors && volume !=
nullptr && volume->isCreated()) {
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);
78 #ifdef ENABLE_GL_DBG_OUTPUT 79 auto msgs =
logger->loggedMessages();
80 for (
const auto& msg : msgs) {
81 std::cout <<
"Rendering: " << msg.message().toStdString();
98 shader =
new QOpenGLShaderProgram;
101 auto vertShaderCode =
readFile(vert, error);
105 auto fragShaderCode =
readFile(frag, error);
110 auto vstatus =
shader->addShaderFromSourceCode(QOpenGLShader::Vertex, vertShaderCode.c_str());
112 std::cout <<
"unable to add vert shader" << std::endl;
114 auto fstatus =
shader->addShaderFromSourceCode(QOpenGLShader::Fragment, fragShaderCode.c_str());
116 std::cout <<
"unable to add frag shader" << std::endl;
118 if (vstatus && fstatus) {
120 std::cout <<
"unable to link shaders" << std::endl;
125 shader->bindAttributeLocation(
"pos", 0);
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();
135 void GLRenderWidget::updateVBOs() {
136 if (!
vao.isCreated()) {
148 glEnableVertexAttribArray(0);
149 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
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();
171 volume = volume3Dtex;
178 hasRegionSeperatorValueAndColors=
true;
std::string readFile(const std::string &filePath, size_t &error)
readFile reads the content of a file and returns it as string.
File reader functionality.
std::vector< unsigned int > indices
array with indices for the given vertex array
std::vector< glm::vec3 > vertices
array with vertices (vec3)
Helper struct for passing renderable data in the application.