template<UnsignedInt dimensions> Flat<dimensions>::Flat(const Flags flags): transformationProjectionMatrixUniform(0), colorUniform(1), _flags(flags) { #ifdef MAGNUM_BUILD_STATIC /* Import resources on static build, if not already */ if(!Utility::Resource::hasGroup("MagnumShaders")) importShaderResources(); #endif Utility::Resource rs("MagnumShaders"); #ifndef MAGNUM_TARGET_GLES const Version version = Context::current().supportedVersion({Version::GL320, Version::GL310, Version::GL300, Version::GL210}); #else const Version version = Context::current().supportedVersion({Version::GLES300, Version::GLES200}); #endif Shader vert = Implementation::createCompatibilityShader(rs, version, Shader::Type::Vertex); Shader frag = Implementation::createCompatibilityShader(rs, version, Shader::Type::Fragment); vert.addSource(flags & Flag::Textured ? "#define TEXTURED\n" : "") .addSource(rs.get("generic.glsl")) .addSource(rs.get(vertexShaderName<dimensions>())); frag.addSource(flags & Flag::Textured ? "#define TEXTURED\n" : "") .addSource(rs.get("Flat.frag")); CORRADE_INTERNAL_ASSERT_OUTPUT(Shader::compile({vert, frag})); attachShaders({vert, frag}); #ifndef MAGNUM_TARGET_GLES if(!Context::current().isExtensionSupported<Extensions::GL::ARB::explicit_attrib_location>(version)) #else if(!Context::current().isVersionSupported(Version::GLES300)) #endif { bindAttributeLocation(Position::Location, "position"); if(flags & Flag::Textured) bindAttributeLocation(TextureCoordinates::Location, "textureCoordinates"); } CORRADE_INTERNAL_ASSERT_OUTPUT(link()); #ifndef MAGNUM_TARGET_GLES if(!Context::current().isExtensionSupported<Extensions::GL::ARB::explicit_uniform_location>(version)) #endif { transformationProjectionMatrixUniform = uniformLocation("transformationProjectionMatrix"); colorUniform = uniformLocation("color"); } #ifndef MAGNUM_TARGET_GLES if(!Context::current().isExtensionSupported<Extensions::GL::ARB::shading_language_420pack>(version)) #endif { if(flags & Flag::Textured) setUniform(uniformLocation("textureData"), TextureLayer); } /* Set defaults in OpenGL ES (for desktop they are set in shader code itself) */ #ifdef MAGNUM_TARGET_GLES /* Default to fully opaque white so we can see the texture */ if(flags & Flag::Textured) setColor(Color4(1.0f)); #endif }
template<UnsignedInt dimensions> Flat<dimensions>::Flat(const Flags flags): transformationProjectionMatrixUniform(0), colorUniform(1), _flags(flags) { Utility::Resource rs("MagnumShaders"); #ifndef MAGNUM_TARGET_GLES const Version version = Context::current()->supportedVersion({Version::GL320, Version::GL310, Version::GL300, Version::GL210}); #else const Version version = Context::current()->supportedVersion({Version::GLES300, Version::GLES200}); #endif Shader vert(version, Shader::Type::Vertex); vert.addSource(flags & Flag::Textured ? "#define TEXTURED\n" : "") .addSource(rs.get("compatibility.glsl")) .addSource(rs.get("generic.glsl")) .addSource(rs.get(vertexShaderName<dimensions>())); CORRADE_INTERNAL_ASSERT_OUTPUT(vert.compile()); attachShader(vert); Shader frag(version, Shader::Type::Fragment); frag.addSource(flags & Flag::Textured ? "#define TEXTURED\n" : "") .addSource(rs.get("compatibility.glsl")) .addSource(rs.get("Flat.frag")); CORRADE_INTERNAL_ASSERT_OUTPUT(frag.compile()); attachShader(frag); #ifndef MAGNUM_TARGET_GLES if(!Context::current()->isExtensionSupported<Extensions::GL::ARB::explicit_attrib_location>(version)) #else if(!Context::current()->isVersionSupported(Version::GLES300)) #endif { bindAttributeLocation(Position::Location, "position"); if(flags & Flag::Textured) bindAttributeLocation(TextureCoordinates::Location, "textureCoordinates"); } CORRADE_INTERNAL_ASSERT_OUTPUT(link()); #ifndef MAGNUM_TARGET_GLES if(!Context::current()->isExtensionSupported<Extensions::GL::ARB::explicit_uniform_location>(version)) #endif { transformationProjectionMatrixUniform = uniformLocation("transformationProjectionMatrix"); colorUniform = uniformLocation("color"); } #ifndef MAGNUM_TARGET_GLES if(!Context::current()->isExtensionSupported<Extensions::GL::ARB::shading_language_420pack>(version)) #endif { if(flags & Flag::Textured) setUniform(uniformLocation("textureData"), TextureLayer); } /* Set defaults in OpenGL ES (for desktop they are set in shader code itself) */ #ifdef MAGNUM_TARGET_GLES setColor(Color4(1.0f)); // Default to white, with full transperancy (so we can see the texture) #endif }
ColorCorrectionShader::ColorCorrectionShader() { Corrade::Utility::Resource rs("shader"); attachShader(Shader::fromData(Version::GL330, Shader::Type::Vertex, rs.get("ColorCorrectionShader.vert"))); attachShader(Shader::fromData(Version::GL330, Shader::Type::Fragment, rs.get("ColorCorrectionShader.frag"))); link(); transformationProjectionMatrixUniform = uniformLocation("transformationProjectionMatrix"); setUniform(uniformLocation("textureData"), TextureLayer); setUniform(uniformLocation("colorCorrectionTextureData"), ColorCorrectionTextureLayer); }
ReflectorShader::ReflectorShader() { Utility::Resource rs("data"); Shader vert(Version::GL330, Shader::Type::Vertex); vert.addSource(rs.get("ReflectorShader.vert")); CORRADE_INTERNAL_ASSERT_OUTPUT(vert.compile()); attachShader(vert); Shader frag(Version::GL330, Shader::Type::Fragment); frag.addSource(rs.get("ReflectorShader.frag")); CORRADE_INTERNAL_ASSERT_OUTPUT(frag.compile()); attachShader(frag); CORRADE_INTERNAL_ASSERT_OUTPUT(link()); transformationMatrixUniform = uniformLocation("transformationMatrix"); normalMatrixUniform = uniformLocation("normalMatrix"); projectionMatrixUniform = uniformLocation("projectionMatrix"); cameraMatrixUniform = uniformLocation("cameraMatrix"); reflectivityUniform = uniformLocation("reflectivity"); diffuseColorUniform = uniformLocation("diffuseColor"); setUniform(uniformLocation("textureData"), TextureLayer); setUniform(uniformLocation("tarnishTextureData"), TarnishTextureLayer); }
void ShaderProgram::setUniform(const std::string& name, const Texture& tex, GLuint unit) { //Try to find the cached info auto it = textures_.find(unit); if(it != textures_.end()) //If the unit was found { //Replace the texture and uniform location it->second = std::make_pair(uniformLocation(name), &tex); } else //If the unit was not found { //Save the texture unit and the texture we want to associate with it textures_.insert(std::make_pair(unit, std::make_pair(uniformLocation(name), &tex))); } }
void ShaderProgram::setUniform(const std::string& name, const glm::mat4& matrix) { if(!ext::separateShaderObjects()) //If we can't do uniforms without binding { //Get the currently-bound program and bind this program auto previous = getBound(); bind(); //Set the uniform glCheck(glUniformMatrix4fv(uniformLocation(name), 1, GL_FALSE, glm::value_ptr(matrix))); //Set the previous program back glCheck(glUseProgram(previous)); } else //If we can do uniforms without binding { //Set the uniform with this program glCheck(glProgramUniformMatrix4fv(program_, uniformLocation(name), 1, GL_FALSE, glm::value_ptr(matrix))); } }
void ShaderProgram::setUniform(const std::string& name, float first, float second, float third, float fourth) { if(!ext::separateShaderObjects()) //If we can't do uniforms without binding { //Get the currently-bound program and bind this program auto previous = getBound(); bind(); //Set the uniform glCheck(glUniform4f(uniformLocation(name), first, second, third, fourth)); //Set the previous program back glCheck(glUseProgram(previous)); } else //If we can do uniforms without binding { //Set the uniform with this program glCheck(glProgramUniform4f(program_, uniformLocation(name), first, second, third, fourth)); } }
template<UnsignedInt dimensions> Flat<dimensions>::Flat(): transformationProjectionMatrixUniform(0), colorUniform(1) { Utility::Resource rs("MagnumShaders"); #ifndef MAGNUM_TARGET_GLES Version v = Context::current()->supportedVersion({Version::GL320, Version::GL210}); #else Version v = Context::current()->supportedVersion({Version::GLES300, Version::GLES200}); #endif Shader frag(v, Shader::Type::Vertex); frag.addSource(rs.get("compatibility.glsl")) .addSource(rs.get(vertexShaderName<dimensions>())); CORRADE_INTERNAL_ASSERT_OUTPUT(frag.compile()); attachShader(frag); Shader vert(v, Shader::Type::Fragment); vert.addSource(rs.get("compatibility.glsl")) .addSource(rs.get("Flat.frag")); CORRADE_INTERNAL_ASSERT_OUTPUT(vert.compile()); attachShader(vert); #ifndef MAGNUM_TARGET_GLES if(!Context::current()->isExtensionSupported<Extensions::GL::ARB::explicit_attrib_location>() || Context::current()->version() == Version::GL210) #else if(!Context::current()->isVersionSupported(Version::GLES300)) #endif { bindAttributeLocation(Position::Location, "position"); } CORRADE_INTERNAL_ASSERT_OUTPUT(link()); #ifndef MAGNUM_TARGET_GLES if(!Context::current()->isExtensionSupported<Extensions::GL::ARB::explicit_uniform_location>()) #endif { transformationProjectionMatrixUniform = uniformLocation("transformationProjectionMatrix"); colorUniform = uniformLocation("color"); } }
TexturedTriangleShader::TexturedTriangleShader() { MAGNUM_ASSERT_VERSION_SUPPORTED(Version::GL330); const Utility::Resource rs{"textured-triangle-data"}; Shader vert{Version::GL330, Shader::Type::Vertex}; Shader frag{Version::GL330, Shader::Type::Fragment}; vert.addSource(rs.get("TexturedTriangleShader.vert")); frag.addSource(rs.get("TexturedTriangleShader.frag")); CORRADE_INTERNAL_ASSERT_OUTPUT(Shader::compile({vert, frag})); attachShaders({vert, frag}); CORRADE_INTERNAL_ASSERT_OUTPUT(link()); _colorUniform = uniformLocation("color"); setUniform(uniformLocation("textureData"), TextureLayer); }
TexturedTriangleShader::TexturedTriangleShader() { MAGNUM_ASSERT_VERSION_SUPPORTED(Version::GL330); Utility::Resource rs("data"); Shader vert(Version::GL330, Shader::Type::Vertex); vert.addSource(rs.get("TexturedTriangleShader.vert")); CORRADE_INTERNAL_ASSERT_OUTPUT(vert.compile()); attachShader(vert); Shader frag(Version::GL330, Shader::Type::Fragment); frag.addSource(rs.get("TexturedTriangleShader.frag")); CORRADE_INTERNAL_ASSERT_OUTPUT(frag.compile()); attachShader(frag); CORRADE_INTERNAL_ASSERT_OUTPUT(link()); colorUniform = uniformLocation("color"); setUniform(uniformLocation("textureData"), TextureLayer); }
ScalarDisplay::ScalarDisplay (const char* frag) : ShaderProgram (0, frag) { m_displayTypeLocation = uniformLocation ("u_displayType"); m_countTypeLocation = uniformLocation ("u_countType"); m_globalCountLocation = uniformLocation ("u_globalCount"); m_minValueLocation = uniformLocation("u_minValue"); m_maxValueLocation = uniformLocation("u_maxValue"); m_colorBarTexUnitLocation = uniformLocation("u_colorBarTexUnit"); m_scalarAverageTexUnitLocation = uniformLocation("u_scalarAverageTexUnit"); }
GLint SciIllLib::CGLShaderProgram::uniformLocationMap(std::string name){ #ifdef DEBUG if (m_map.count(name) > 0) return m_map[name]; else { GLint loc = uniformLocation(name.c_str()); m_map[name] = loc; return loc; } #else return m_map.value(name); #endif }
void DepthImageItem::paintShaded() { auto shaded_shader = viewport->getShadedShader(); glEnable(GL_DEPTH_TEST); loadSharedShaderVariables(shaded_shader); glActiveTexture(GL_TEXTURE0 + 1); glBindTexture(GL_TEXTURE_2D, shaded_tex); glUniform1i(shaded_shader->uniformLocation("shaded_tex"), 1); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); shaded_shader->release(); glDisable(GL_DEPTH_TEST); }
template<UnsignedInt dimensions> VertexColor<dimensions>::VertexColor(): transformationProjectionMatrixUniform(0) { Utility::Resource rs("MagnumShaders"); #ifndef MAGNUM_TARGET_GLES const Version version = Context::current()->supportedVersion({Version::GL310, Version::GL300, Version::GL210}); #else const Version version = Context::current()->supportedVersion({Version::GLES300, Version::GLES200}); #endif Shader vert(version, Shader::Type::Vertex); vert.addSource(rs.get("compatibility.glsl")) .addSource(rs.get(vertexShaderName<dimensions>())); CORRADE_INTERNAL_ASSERT_OUTPUT(vert.compile()); attachShader(vert); Shader frag(version, Shader::Type::Fragment); frag.addSource(rs.get("compatibility.glsl")) .addSource(rs.get("VertexColor.frag")); CORRADE_INTERNAL_ASSERT_OUTPUT(frag.compile()); attachShader(frag); #ifndef MAGNUM_TARGET_GLES if(!Context::current()->isExtensionSupported<Extensions::GL::ARB::explicit_attrib_location>(version)) #else if(!Context::current()->isVersionSupported(Version::GLES300)) #endif { bindAttributeLocation(Position::Location, "position"); bindAttributeLocation(Color::Location, "color"); } CORRADE_INTERNAL_ASSERT_OUTPUT(link()); #ifndef MAGNUM_TARGET_GLES if(!Context::current()->isExtensionSupported<Extensions::GL::ARB::explicit_uniform_location>(version)) #endif { transformationProjectionMatrixUniform = uniformLocation("transformationProjectionMatrix"); } /* Set defaults in OpenGL ES (for desktop they are set in shader code itself) */ #ifdef MAGNUM_TARGET_GLES setTransformationProjectionMatrix(typename DimensionTraits<dimensions, Float>::MatrixType()); #endif }
MotionBlurCamera::MotionBlurShader::MotionBlurShader() { Utility::Resource rs("shaders"); GL::Shader vert(GL::Version::GL330, GL::Shader::Type::Vertex); GL::Shader frag(GL::Version::GL330, GL::Shader::Type::Fragment); vert.addSource(rs.get("MotionBlurShader.vert")); frag.addSource(rs.get("MotionBlurShader.frag")); CORRADE_INTERNAL_ASSERT_OUTPUT(GL::Shader::compile({vert, frag})); attachShaders({vert, frag}); CORRADE_INTERNAL_ASSERT_OUTPUT(link()); for(Int i = 0; i != MotionBlurCamera::FrameCount; ++i) setUniform(uniformLocation(Utility::formatString("frame[{}]", i)), i); }
ShadowCasterShader::ShadowCasterShader() { MAGNUM_ASSERT_GL_VERSION_SUPPORTED(GL::Version::GL330); const Utility::Resource rs{"shadow-data"}; GL::Shader vert{GL::Version::GL330, GL::Shader::Type::Vertex}; GL::Shader frag{GL::Version::GL330, GL::Shader::Type::Fragment}; vert.addSource(rs.get("ShadowCaster.vert")); frag.addSource(rs.get("ShadowCaster.frag")); CORRADE_INTERNAL_ASSERT_OUTPUT(GL::Shader::compile({vert, frag})); attachShaders({vert, frag}); CORRADE_INTERNAL_ASSERT_OUTPUT(link()); _transformationMatrixUniform = uniformLocation("transformationMatrix"); }
Phong::Phong(const Flags flags): transformationMatrixUniform(0), projectionMatrixUniform(1), normalMatrixUniform(2), lightUniform(3), diffuseColorUniform(4), ambientColorUniform(5), specularColorUniform(6), lightColorUniform(7), shininessUniform(8), _flags(flags) { #ifdef MAGNUM_BUILD_STATIC /* Import resources on static build, if not already */ if(!Utility::Resource::hasGroup("MagnumShaders")) importShaderResources(); #endif Utility::Resource rs("MagnumShaders"); #ifndef MAGNUM_TARGET_GLES const Version version = Context::current().supportedVersion({Version::GL320, Version::GL310, Version::GL300, Version::GL210}); #else const Version version = Context::current().supportedVersion({Version::GLES300, Version::GLES200}); #endif Shader vert = Implementation::createCompatibilityShader(rs, version, Shader::Type::Vertex); Shader frag = Implementation::createCompatibilityShader(rs, version, Shader::Type::Fragment); vert.addSource(flags ? "#define TEXTURED\n" : "") .addSource(rs.get("generic.glsl")) .addSource(rs.get("Phong.vert")); frag.addSource(flags & Flag::AmbientTexture ? "#define AMBIENT_TEXTURE\n" : "") .addSource(flags & Flag::DiffuseTexture ? "#define DIFFUSE_TEXTURE\n" : "") .addSource(flags & Flag::SpecularTexture ? "#define SPECULAR_TEXTURE\n" : "") .addSource(rs.get("Phong.frag")); CORRADE_INTERNAL_ASSERT_OUTPUT(Shader::compile({vert, frag})); attachShaders({vert, frag}); #ifndef MAGNUM_TARGET_GLES if(!Context::current().isExtensionSupported<Extensions::GL::ARB::explicit_attrib_location>(version)) #else if(!Context::current().isVersionSupported(Version::GLES300)) #endif { bindAttributeLocation(Position::Location, "position"); bindAttributeLocation(Normal::Location, "normal"); if(flags) bindAttributeLocation(TextureCoordinates::Location, "textureCoordinates"); } CORRADE_INTERNAL_ASSERT_OUTPUT(link()); #ifndef MAGNUM_TARGET_GLES if(!Context::current().isExtensionSupported<Extensions::GL::ARB::explicit_uniform_location>(version)) #endif { transformationMatrixUniform = uniformLocation("transformationMatrix"); projectionMatrixUniform = uniformLocation("projectionMatrix"); normalMatrixUniform = uniformLocation("normalMatrix"); lightUniform = uniformLocation("light"); ambientColorUniform = uniformLocation("ambientColor"); diffuseColorUniform = uniformLocation("diffuseColor"); specularColorUniform = uniformLocation("specularColor"); lightColorUniform = uniformLocation("lightColor"); shininessUniform = uniformLocation("shininess"); } #ifndef MAGNUM_TARGET_GLES if(flags && !Context::current().isExtensionSupported<Extensions::GL::ARB::shading_language_420pack>(version)) #endif { if(flags & Flag::AmbientTexture) setUniform(uniformLocation("ambientTexture"), AmbientTextureLayer); if(flags & Flag::DiffuseTexture) setUniform(uniformLocation("diffuseTexture"), DiffuseTextureLayer); if(flags & Flag::SpecularTexture) setUniform(uniformLocation("specularTexture"), SpecularTextureLayer); } /* Set defaults in OpenGL ES (for desktop they are set in shader code itself) */ #ifdef MAGNUM_TARGET_GLES /* Default to fully opaque white so we can see the textures */ if(flags & Flag::AmbientTexture) setAmbientColor(Color4{1.0f}); else setAmbientColor(Color4{0.0f, 1.0f}); if(flags & Flag::DiffuseTexture) setDiffuseColor(Color4{1.0f}); setSpecularColor(Color4{1.0f}); setLightColor(Color4{.0f}); setShininess(80.0f); #endif }
MeshVisualizer::MeshVisualizer(const Flags flags): flags(flags), transformationProjectionMatrixUniform(0), viewportSizeUniform(1), colorUniform(2), wireframeColorUniform(3), wireframeWidthUniform(4), smoothnessUniform(5) { #ifndef MAGNUM_TARGET_GLES if(flags & Flag::Wireframe && !(flags & Flag::NoGeometryShader)) MAGNUM_ASSERT_EXTENSION_SUPPORTED(Extensions::GL::ARB::geometry_shader4); #elif defined(MAGNUM_TARGET_GLES2) if(flags & Flag::Wireframe) MAGNUM_ASSERT_EXTENSION_SUPPORTED(Extensions::GL::OES::standard_derivatives); #endif Utility::Resource rs("MagnumShaders"); #ifndef MAGNUM_TARGET_GLES const Version v = Context::current()->supportedVersion({Version::GL320, Version::GL310, Version::GL210}); #else const Version v = Context::current()->supportedVersion({Version::GLES300, Version::GLES200}); #endif Shader vert(v, Shader::Type::Vertex); vert.addSource(flags & Flag::Wireframe ? "#define WIREFRAME_RENDERING\n" : "") .addSource(flags & Flag::NoGeometryShader ? "#define NO_GEOMETRY_SHADER\n" : "") .addSource(rs.get("compatibility.glsl")) .addSource(rs.get("MeshVisualizer.vert")); CORRADE_INTERNAL_ASSERT_OUTPUT(vert.compile()); vert.compile(); attachShader(vert); #ifndef MAGNUM_TARGET_GLES if(flags & Flag::Wireframe && !(flags & Flag::NoGeometryShader)) { Shader geom(v, Shader::Type::Geometry); geom.addSource(rs.get("compatibility.glsl")) .addSource(rs.get("MeshVisualizer.geom")); CORRADE_INTERNAL_ASSERT_OUTPUT(geom.compile()); geom.compile(); attachShader(geom); } #endif Shader frag(v, Shader::Type::Fragment); frag.addSource(flags & Flag::Wireframe ? "#define WIREFRAME_RENDERING\n" : "") .addSource(flags & Flag::NoGeometryShader ? "#define NO_GEOMETRY_SHADER\n" : "") .addSource(rs.get("compatibility.glsl")) .addSource(rs.get("MeshVisualizer.frag")); CORRADE_INTERNAL_ASSERT_OUTPUT(frag.compile()); frag.compile(); attachShader(frag); #ifndef MAGNUM_TARGET_GLES if(!Context::current()->isExtensionSupported<Extensions::GL::ARB::explicit_attrib_location>() || Context::current()->version() == Version::GL210) #else if(!Context::current()->isVersionSupported(Version::GLES300)) #endif { bindAttributeLocation(Position::Location, "position"); #ifndef MAGNUM_TARGET_GLES if(!Context::current()->isVersionSupported(Version::GL310)) #endif { bindAttributeLocation(VertexIndex::Location, "vertexIndex"); } } CORRADE_INTERNAL_ASSERT_OUTPUT(link()); link(); #ifndef MAGNUM_TARGET_GLES if(!Context::current()->isExtensionSupported<Extensions::GL::ARB::explicit_uniform_location>()) #endif { transformationProjectionMatrixUniform = uniformLocation("transformationProjectionMatrix"); colorUniform = uniformLocation("color"); if(flags & Flag::Wireframe) { wireframeColorUniform = uniformLocation("wireframeColor"); wireframeWidthUniform = uniformLocation("wireframeWidth"); smoothnessUniform = uniformLocation("smoothness"); if(!(flags & Flag::NoGeometryShader)) viewportSizeUniform = uniformLocation("viewportSize"); } } /* Set defaults in OpenGL ES (for desktop they are set in shader code itself) */ #ifdef MAGNUM_TARGET_GLES setColor(Color3<>(1.0f)); if(flags & Flag::Wireframe) { setWireframeColor(Color3<>(0.0f)); setWireframeWidth(1.0f); setSmoothness(2.0f); } #endif }
MeshVisualizer::MeshVisualizer(const Flags flags): flags(flags), transformationProjectionMatrixUniform(0), viewportSizeUniform(1), colorUniform(2), wireframeColorUniform(3), wireframeWidthUniform(4), smoothnessUniform(5) { #ifndef MAGNUM_TARGET_GLES if(flags & Flag::Wireframe && !(flags & Flag::NoGeometryShader)) { MAGNUM_ASSERT_VERSION_SUPPORTED(Version::GL320); MAGNUM_ASSERT_EXTENSION_SUPPORTED(Extensions::GL::ARB::geometry_shader4); } #elif defined(MAGNUM_TARGET_GLES2) if(flags & Flag::Wireframe) MAGNUM_ASSERT_EXTENSION_SUPPORTED(Extensions::GL::OES::standard_derivatives); #endif Utility::Resource rs("MagnumShaders"); #ifndef MAGNUM_TARGET_GLES const Version version = Context::current()->supportedVersion({Version::GL320, Version::GL310, Version::GL300, Version::GL210}); CORRADE_INTERNAL_ASSERT_OUTPUT(flags & Flag::NoGeometryShader || version >= Version::GL320); #else const Version version = Context::current()->supportedVersion({Version::GLES300, Version::GLES200}); #endif Shader vert = Implementation::createCompatibilityShader(rs, version, Shader::Type::Vertex); Shader frag = Implementation::createCompatibilityShader(rs, version, Shader::Type::Fragment); vert.addSource(flags & Flag::Wireframe ? "#define WIREFRAME_RENDERING\n" : "") .addSource(flags & Flag::NoGeometryShader ? "#define NO_GEOMETRY_SHADER\n" : "") #ifdef MAGNUM_TARGET_WEBGL .addSource("#define SUBSCRIPTING_WORKAROUND\n") #elif defined(MAGNUM_TARGET_GLES2) .addSource(Context::current()->detectedDriver() & Context::DetectedDriver::ProbablyAngle ? "#define SUBSCRIPTING_WORKAROUND\n" : "") #endif .addSource(rs.get("generic.glsl")) .addSource(rs.get("MeshVisualizer.vert")); frag.addSource(flags & Flag::Wireframe ? "#define WIREFRAME_RENDERING\n" : "") .addSource(flags & Flag::NoGeometryShader ? "#define NO_GEOMETRY_SHADER\n" : "") .addSource(rs.get("MeshVisualizer.frag")); #ifndef MAGNUM_TARGET_GLES std::optional<Shader> geom; if(flags & Flag::Wireframe && !(flags & Flag::NoGeometryShader)) { geom = Implementation::createCompatibilityShader(rs, version, Shader::Type::Geometry); geom->addSource(rs.get("MeshVisualizer.geom")); } #endif #ifndef MAGNUM_TARGET_GLES if(geom) Shader::compile({vert, *geom, frag}); else #endif Shader::compile({vert, frag}); attachShaders({vert, frag}); #ifndef MAGNUM_TARGET_GLES if(geom) attachShader(*geom); #endif #ifndef MAGNUM_TARGET_GLES if(!Context::current()->isExtensionSupported<Extensions::GL::ARB::explicit_attrib_location>(version)) #else if(!Context::current()->isVersionSupported(Version::GLES300)) #endif { bindAttributeLocation(Position::Location, "position"); #ifndef MAGNUM_TARGET_GLES if(!Context::current()->isVersionSupported(Version::GL310)) #endif { bindAttributeLocation(VertexIndex::Location, "vertexIndex"); } } CORRADE_INTERNAL_ASSERT_OUTPUT(link()); #ifndef MAGNUM_TARGET_GLES if(!Context::current()->isExtensionSupported<Extensions::GL::ARB::explicit_uniform_location>(version)) #endif { transformationProjectionMatrixUniform = uniformLocation("transformationProjectionMatrix"); colorUniform = uniformLocation("color"); if(flags & Flag::Wireframe) { wireframeColorUniform = uniformLocation("wireframeColor"); wireframeWidthUniform = uniformLocation("wireframeWidth"); smoothnessUniform = uniformLocation("smoothness"); if(!(flags & Flag::NoGeometryShader)) viewportSizeUniform = uniformLocation("viewportSize"); } } /* Set defaults in OpenGL ES (for desktop they are set in shader code itself) */ #ifdef MAGNUM_TARGET_GLES setColor(Color3(1.0f)); if(flags & Flag::Wireframe) { setWireframeColor(Color3(0.0f)); setWireframeWidth(1.0f); setSmoothness(2.0f); } #endif }
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; }
void Shader::setUniform(std::string name, const glm::mat4& value) { glUniformMatrix4fv( uniformLocation(name), 1, GL_FALSE, glm::value_ptr(value) ); }
void Shader::setUniform(std::string name, GLfloat value) { glUniform1f(uniformLocation(name), value); }
void Shader::setUniform(std::string name, bool value) { glUniform1i(uniformLocation(name), value); }
void Z3DShaderProgram::loadFromSourceCode(const QStringList &vertSrcs, const QStringList &geomSrcs, const QStringList &fragSrcs, const QString &header) { removeAllShaders(); for (int i=0; i<vertSrcs.size(); ++i) { QString vertSrc = header + vertSrcs[i]; #ifdef _QT5_ if (!addShaderFromSourceCode(QOpenGLShader::Vertex, vertSrc)) { #else if (!addShaderFromSourceCode(QGLShader::Vertex, vertSrc)) { #endif LDEBUG() << vertSrc; throw Exception("Can not compile vertex shader. Error log: " + log().toStdString()); } } for (int i=0; i<geomSrcs.size(); ++i) { QString geomSrc = header + geomSrcs[i]; #ifdef _QT5_ if (!addShaderFromSourceCode(QOpenGLShader::Geometry, geomSrc)) { #else if (!addShaderFromSourceCode(QGLShader::Geometry, geomSrc)) { #endif LDEBUG() << geomSrc; throw Exception("Can not compile geometry shader. Error log: " + log().toStdString()); } } for (int i=0; i<fragSrcs.size(); ++i) { QString fragSrc = header + fragSrcs[i]; #ifdef _QT5_ if (!addShaderFromSourceCode(QOpenGLShader::Fragment, fragSrc)) { #else if (!addShaderFromSourceCode(QGLShader::Fragment, fragSrc)) { #endif LDEBUG() << fragSrc; throw Exception("Can not compile fragment shader. Error log: " + log().toStdString()); } } if (!link()) { throw Exception("Can not link shader. Error log: " + log().toStdString()); } } void Z3DShaderProgram::loadFromSourceCode(const QStringList &vertSrcs, const QStringList &fragSrcs, const QString &header) { loadFromSourceCode(vertSrcs, QStringList(), fragSrcs, header); } void Z3DShaderProgram::setHeaderAndRebuild(const QString &header) { loadFromSourceCode(m_vertSrcs, m_geomSrcs, m_fragSrcs, header); } GLint Z3DShaderProgram::getUniformLocation(const QString &name) { GLint l = uniformLocation(name); if (l == -1 && logUniformLocationError()) { LWARN() << "Failed to locate uniform:" << name; } return l; }
void Shader::setUniform(std::string name, const glm::vec4& value) { glUniform4fv(uniformLocation(name), 1, glm::value_ptr(value)); }
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; }
void ShaderProgram::setUniform( const std::string & name, const Matrix4x4 & mat ) { glUniformMatrix4fv(uniformLocation(name), 1, GL_TRUE, mat.data); GL_WARN_IF_ERROR(); }
// AddShaderProgram // ====================================================================== AddShaderProgram::AddShaderProgram (const char* frag) : ShaderProgram (0, frag) { m_previousTexUnitLocation = uniformLocation("previousTexUnit"); m_stepTexUnitLocation = uniformLocation("stepTexUnit"); }