GLLineShader2D::GLLineShader2D(QObject *parent): QOpenGLShaderProgram(parent), vshader(), fshader(), attr_coords(), attr_colour(), uniform_mvp() { initializeOpenGLFunctions(); vshader = new QOpenGLShader(QOpenGLShader::Vertex, this); vshader->compileSourceCode ("#version 110\n" "\n" "attribute vec3 coord2d;\n" "attribute vec3 colour;\n" "varying vec4 f_colour;\n" "uniform mat4 mvp;\n" "uniform float zoom;\n" "\n" "void log10(in float v1, out float v2) { v2 = log2(v1) * 0.30103; }\n" "\n" "void main(void) {\n" " gl_Position = mvp * vec4(coord2d[0], coord2d[1], -2.0, 1.0);\n" " // Logistic function offset by LOD and correction factor to set the transition points\n" " float logzoom;\n" " log10(zoom, logzoom);\n" " f_colour = vec4(colour, 1.0 / (1.0 + pow(10.0,((-logzoom-1.0+coord2d[2])*30.0))));\n" "}\n"); if (!vshader->isCompiled()) { std::cerr << "Failed to compile vertex shader\n" << vshader->log().toStdString() << std::endl; } fshader = new QOpenGLShader(QOpenGLShader::Fragment, this); fshader->compileSourceCode ("#version 110\n" "\n" "varying vec4 f_colour;\n" "\n" "void main(void) {\n" " gl_FragColor = f_colour;\n" "}\n"); if (!fshader->isCompiled()) { std::cerr << "Failed to compile fragment shader\n" << fshader->log().toStdString() << std::endl; } addShader(vshader); addShader(fshader); link(); if (!isLinked()) { std::cerr << "Failed to link shader program\n" << log().toStdString() << std::endl; } attr_coords = attributeLocation("coord2d"); if (attr_coords == -1) std::cerr << "Failed to bind coordinate location" << std::endl; attr_colour = attributeLocation("colour"); if (attr_coords == -1) std::cerr << "Failed to bind colour location" << std::endl; uniform_mvp = uniformLocation("mvp"); if (uniform_mvp == -1) std::cerr << "Failed to bind transform" << std::endl; uniform_zoom = uniformLocation("zoom"); if (uniform_zoom == -1) std::cerr << "Failed to bind zoom factor" << std::endl; }
GLImageShader2D::GLImageShader2D(QObject *parent): QOpenGLShaderProgram(parent), vshader(), fshader(), attr_coords(), attr_texcoords(), uniform_mvp(), uniform_texture(), uniform_lut(), uniform_min(), uniform_max() { initializeOpenGLFunctions(); vshader = new QOpenGLShader(QOpenGLShader::Vertex, this); std::string vsource("#version 110\n" "\n" "attribute vec2 coord2d;\n" "attribute vec2 texcoord;\n" "varying vec2 f_texcoord;\n" "uniform mat4 mvp;\n" "\n" "void main(void) {\n" " gl_Position = mvp * vec4(coord2d, 0.0, 1.0);\n" " f_texcoord = texcoord;\n" "}\n"); vshader->compileSourceCode(vsource.c_str()); if (!vshader->isCompiled()) { std::cerr << "Failed to compile vertex shader\n" << vshader->log().toStdString() << std::endl; } fshader = new QOpenGLShader(QOpenGLShader::Fragment, this); std::string fsource("#version 110\n" "#extension GL_EXT_texture_array : enable\n" "\n" "varying vec2 f_texcoord;\n" "uniform sampler2D tex;\n" "uniform sampler1DArray lut;\n" "uniform vec3 texmin;\n" "uniform vec3 texmax;\n" "uniform vec3 correction;\n" "\n" "void main(void) {\n" " vec2 flipped_texcoord = vec2(f_texcoord.x, 1.0 - f_texcoord.y);\n" " vec4 texval = texture2D(tex, flipped_texcoord);\n" "\n" " gl_FragColor = texture1DArray(lut, vec2(((((texval[0] * correction[0]) - texmin[0]) / (texmax[0] - texmin[0]))), 0.0));\n" "}\n"); fshader->compileSourceCode(fsource.c_str()); if (!fshader->isCompiled()) { std::cerr << "Failed to compile fragment shader\n" << fshader->log().toStdString() << std::endl; } addShader(vshader); addShader(fshader); link(); if (!isLinked()) { std::cerr << "Failed to link shader program\n" << log().toStdString() << std::endl; } attr_coords = attributeLocation("coord2d"); if (attr_coords == -1) std::cerr << "Failed to bind coordinates" << std::endl; attr_texcoords = attributeLocation("texcoord"); if (attr_texcoords == -1) std::cerr << "Failed to bind texture coordinates" << std::endl; uniform_mvp = uniformLocation("mvp"); if (uniform_mvp == -1) std::cerr << "Failed to bind transform" << std::endl; uniform_texture = uniformLocation("tex"); if (uniform_texture == -1) std::cerr << "Failed to bind texture uniform " << std::endl; uniform_lut = uniformLocation("lut"); if (uniform_lut == -1) std::cerr << "Failed to bind lut uniform " << std::endl; uniform_min = uniformLocation("texmin"); if (uniform_min == -1) std::cerr << "Failed to bind min uniform " << std::endl; uniform_max = uniformLocation("texmax"); if (uniform_max == -1) std::cerr << "Failed to bind max uniform " << std::endl; uniform_corr = uniformLocation("correction"); if (uniform_corr == -1) std::cerr << "Failed to bind correction uniform " << std::endl; }
// StoreShaderProgram // ====================================================================== StoreShaderProgram::StoreShaderProgram (const char* vert, const char* frag) : ShaderProgram (vert, frag) { m_vValueLocation = attributeLocation("vValue"); }