diff --git a/src/3d/materials/qgsgoochmaterialsettings.cpp b/src/3d/materials/qgsgoochmaterialsettings.cpp index 3be24b33ca73..8f3afc0f1866 100644 --- a/src/3d/materials/qgsgoochmaterialsettings.cpp +++ b/src/3d/materials/qgsgoochmaterialsettings.cpp @@ -15,6 +15,7 @@ #include "qgsgoochmaterialsettings.h" #include "qgscolorutils.h" +#include "qgs3dutils.h" #include #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) @@ -113,24 +114,7 @@ Qt3DRender::QMaterial *QgsGoochMaterialSettings::toMaterial( QgsMaterialSettings case QgsMaterialSettingsRenderingTechnique::TrianglesWithFixedTexture: case QgsMaterialSettingsRenderingTechnique::TrianglesFromModel: { - if ( dataDefinedProperties().hasActiveProperties() ) - return dataDefinedMaterial(); - Qt3DExtras::QGoochMaterial *material = new Qt3DExtras::QGoochMaterial; - material->setDiffuse( mDiffuse ); - material->setWarm( mWarm ); - material->setCool( mCool ); - - material->setSpecular( mSpecular ); - material->setShininess( mShininess ); - material->setAlpha( mAlpha ); - material->setBeta( mBeta ); - - if ( context.isSelected() ) - { - // update the material with selection colors - material->setDiffuse( context.selectionColor() ); - } - return material; + return buildMaterial( context ); } case QgsMaterialSettingsRenderingTechnique::Lines: @@ -234,11 +218,11 @@ void QgsGoochMaterialSettings::applyDataDefinedToGeometry( Qt3DQGeometry *geomet dataBuffer->setData( data ); } -Qt3DRender::QMaterial *QgsGoochMaterialSettings::dataDefinedMaterial() const +Qt3DRender::QMaterial *QgsGoochMaterialSettings::buildMaterial( const QgsMaterialContext &context ) const { Qt3DRender::QMaterial *material = new Qt3DRender::QMaterial; - Qt3DRender::QEffect *eff = new Qt3DRender::QEffect( material ); + Qt3DRender::QEffect *effect = new Qt3DRender::QEffect( material ); Qt3DRender::QTechnique *technique = new Qt3DRender::QTechnique; technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL ); @@ -253,11 +237,30 @@ Qt3DRender::QMaterial *QgsGoochMaterialSettings::dataDefinedMaterial() const Qt3DRender::QRenderPass *renderPass = new Qt3DRender::QRenderPass(); Qt3DRender::QShaderProgram *shaderProgram = new Qt3DRender::QShaderProgram(); - //Load shader programs - const QUrl urlVert( QStringLiteral( "qrc:/shaders/goochDataDefined.vert" ) ); - shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) ); - const QUrl urlFrag( QStringLiteral( "qrc:/shaders/goochDataDefined.frag" ) ); - shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Fragment, Qt3DRender::QShaderProgram::loadSource( urlFrag ) ); + const QByteArray fragmentShaderCode = Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral( "qrc:/shaders/gooch.frag" ) ) ); + + if ( dataDefinedProperties().hasActiveProperties() ) + { + //Load shader programs + const QUrl urlVert( QStringLiteral( "qrc:/shaders/goochDataDefined.vert" ) ); + shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) ); + + const QByteArray finalFragmentShaderCode = Qgs3DUtils::addDefinesToShaderCode( fragmentShaderCode, QStringList( {"DATA_DEFINED"} ) ); + shaderProgram->setFragmentShaderCode( finalFragmentShaderCode ); + } + else + { + //Load shader programs + const QUrl urlVert( QStringLiteral( "qrc:/shaders/default.vert" ) ); + shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) ); + shaderProgram->setFragmentShaderCode( fragmentShaderCode ); + + const QColor diffuseColor = context.isSelected() ? context.selectionColor() : mDiffuse; + effect->addParameter( new Qt3DRender::QParameter( QStringLiteral( "kd" ), diffuseColor ) ); + effect->addParameter( new Qt3DRender::QParameter( QStringLiteral( "ks" ), mSpecular ) ); + effect->addParameter( new Qt3DRender::QParameter( QStringLiteral( "kblue" ), mCool ) ); + effect->addParameter( new Qt3DRender::QParameter( QStringLiteral( "kyellow" ), mWarm ) ); + } renderPass->setShaderProgram( shaderProgram ); technique->addRenderPass( renderPass ); @@ -266,8 +269,8 @@ Qt3DRender::QMaterial *QgsGoochMaterialSettings::dataDefinedMaterial() const technique->addParameter( new Qt3DRender::QParameter( QStringLiteral( "alpha" ), mAlpha ) ); technique->addParameter( new Qt3DRender::QParameter( QStringLiteral( "beta" ), mBeta ) ); - eff->addTechnique( technique ); - material->setEffect( eff ); + effect->addTechnique( technique ); + material->setEffect( effect ); return material; } diff --git a/src/3d/materials/qgsgoochmaterialsettings.h b/src/3d/materials/qgsgoochmaterialsettings.h index e31847b79ca3..1537a87e172a 100644 --- a/src/3d/materials/qgsgoochmaterialsettings.h +++ b/src/3d/materials/qgsgoochmaterialsettings.h @@ -134,7 +134,7 @@ class _3D_EXPORT QgsGoochMaterialSettings : public QgsAbstractMaterialSettings float mBeta = 0.5f; //! Constructs a material from shader files - Qt3DRender::QMaterial *dataDefinedMaterial() const; + Qt3DRender::QMaterial *buildMaterial( const QgsMaterialContext &context ) const; }; diff --git a/src/3d/materials/qgsmetalroughmaterial.cpp b/src/3d/materials/qgsmetalroughmaterial.cpp index 500f5ccbc225..37e0110ef6be 100644 --- a/src/3d/materials/qgsmetalroughmaterial.cpp +++ b/src/3d/materials/qgsmetalroughmaterial.cpp @@ -14,6 +14,7 @@ ***************************************************************************/ #include "qgsmetalroughmaterial.h" +#include "qgs3dutils.h" #include #include #include @@ -198,25 +199,6 @@ void QgsMetalRoughMaterial::setTextureScale( float textureScale ) mTextureScaleParameter->setValue( textureScale ); } -QByteArray addDefinesToShaderCode( const QByteArray &shaderCode, const QStringList &defines ) -{ - // There is one caveat to take care of - GLSL source code needs to start with #version as - // a first directive, otherwise we get the old GLSL 100 version. So we can't just prepend the - // shader source code, but insert our defines at the right place. - - QStringList defineLines; - for ( const QString &define : defines ) - defineLines += "#define " + define + "\n"; - - QString definesText = defineLines.join( QString() ); - - QByteArray newShaderCode = shaderCode; - int versionIndex = shaderCode.indexOf( "#version " ); - int insertionIndex = versionIndex == -1 ? 0 : shaderCode.indexOf( '\n', versionIndex + 1 ) + 1; - newShaderCode.insert( insertionIndex, definesText.toLatin1() ); - return newShaderCode; -} - void QgsMetalRoughMaterial::init() { QObject::connect( mBaseColorParameter, &Qt3DRender::QParameter::valueChanged, @@ -282,7 +264,7 @@ void QgsMetalRoughMaterial::updateFragmentShader() if ( mFlatShading ) defines += "FLAT_SHADING"; - QByteArray finalShaderCode = addDefinesToShaderCode( fragmentShaderCode, defines ); + QByteArray finalShaderCode = Qgs3DUtils::addDefinesToShaderCode( fragmentShaderCode, defines ); mMetalRoughGL3Shader->setFragmentShaderCode( finalShaderCode ); } diff --git a/src/3d/materials/qgsphongmaterialsettings.cpp b/src/3d/materials/qgsphongmaterialsettings.cpp index a9a2b585da87..9ee94f927be1 100644 --- a/src/3d/materials/qgsphongmaterialsettings.cpp +++ b/src/3d/materials/qgsphongmaterialsettings.cpp @@ -15,6 +15,7 @@ #include "qgsphongmaterialsettings.h" #include "qgscolorutils.h" +#include "qgs3dutils.h" #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) #include @@ -113,10 +114,7 @@ Qt3DRender::QMaterial *QgsPhongMaterialSettings::toMaterial( QgsMaterialSettings case QgsMaterialSettingsRenderingTechnique::TrianglesWithFixedTexture: case QgsMaterialSettingsRenderingTechnique::TrianglesFromModel: { - if ( dataDefinedProperties().hasActiveProperties() ) - return dataDefinedMaterial(); - else - return constantColorMaterial( context ); + return buildMaterial( context ); } case QgsMaterialSettingsRenderingTechnique::Lines: @@ -255,11 +253,11 @@ void QgsPhongMaterialSettings::applyDataDefinedToGeometry( Qt3DQGeometry *geomet dataBuffer->setData( data ); } -Qt3DRender::QMaterial *QgsPhongMaterialSettings::constantColorMaterial( const QgsMaterialContext &context ) const +Qt3DRender::QMaterial *QgsPhongMaterialSettings::buildMaterial( const QgsMaterialContext &context ) const { Qt3DRender::QMaterial *material = new Qt3DRender::QMaterial; - Qt3DRender::QEffect *eff = new Qt3DRender::QEffect( material ); + Qt3DRender::QEffect *effect = new Qt3DRender::QEffect( material ); Qt3DRender::QTechnique *technique = new Qt3DRender::QTechnique; technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL ); @@ -274,72 +272,48 @@ Qt3DRender::QMaterial *QgsPhongMaterialSettings::constantColorMaterial( const Qg Qt3DRender::QRenderPass *renderPass = new Qt3DRender::QRenderPass(); Qt3DRender::QShaderProgram *shaderProgram = new Qt3DRender::QShaderProgram(); - //Load shader programs - const QUrl urlVert( QStringLiteral( "qrc:/shaders/phongConstant.vert" ) ); - shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) ); - const QUrl urlFrag( QStringLiteral( "qrc:/shaders/phongConstant.frag" ) ); - shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Fragment, Qt3DRender::QShaderProgram::loadSource( urlFrag ) ); - renderPass->setShaderProgram( shaderProgram ); technique->addRenderPass( renderPass ); - const QColor ambient = context.isSelected() ? context.selectionColor().darker() : mAmbient; - const QColor diffuse = context.isSelected() ? context.selectionColor() : mDiffuse; - - eff->addParameter( new Qt3DRender::QParameter( QStringLiteral( "shininess" ), static_cast< float >( mShininess ) ) ); - eff->addParameter( new Qt3DRender::QParameter( QStringLiteral( "opacity" ), static_cast< float >( mOpacity ) ) ); - eff->addParameter( new Qt3DRender::QParameter( QStringLiteral( "ambientColor" ), - QColor::fromRgbF( ambient.redF() * mAmbientCoefficient, - ambient.greenF() * mAmbientCoefficient, - ambient.blueF() * mAmbientCoefficient ) ) ); - eff->addParameter( new Qt3DRender::QParameter( QStringLiteral( "diffuseColor" ), - QColor::fromRgbF( diffuse.redF() * mDiffuseCoefficient, - diffuse.greenF() * mDiffuseCoefficient, - diffuse.blueF() * mDiffuseCoefficient ) ) ); - eff->addParameter( new Qt3DRender::QParameter( QStringLiteral( "specularColor" ), - QColor::fromRgbF( mSpecular.redF() * mSpecularCoefficient, - mSpecular.greenF() * mSpecularCoefficient, - mSpecular.blueF() * mSpecularCoefficient ) ) ); - - eff->addTechnique( technique ); - material->setEffect( eff ); - - return material; -} - -Qt3DRender::QMaterial *QgsPhongMaterialSettings::dataDefinedMaterial() const -{ - Qt3DRender::QMaterial *material = new Qt3DRender::QMaterial; - - Qt3DRender::QEffect *eff = new Qt3DRender::QEffect( material ); + const QByteArray fragmentShaderCode = Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral( "qrc:/shaders/phong.frag" ) ) ); - Qt3DRender::QTechnique *technique = new Qt3DRender::QTechnique; - technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL ); - technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile ); - technique->graphicsApiFilter()->setMajorVersion( 3 ); - technique->graphicsApiFilter()->setMinorVersion( 3 ); - Qt3DRender::QFilterKey *filterKey = new Qt3DRender::QFilterKey(); - filterKey->setName( QStringLiteral( "renderingStyle" ) ); - filterKey->setValue( QStringLiteral( "forward" ) ); - technique->addFilterKey( filterKey ); - - Qt3DRender::QRenderPass *renderPass = new Qt3DRender::QRenderPass(); - Qt3DRender::QShaderProgram *shaderProgram = new Qt3DRender::QShaderProgram(); - - //Load shader programs - const QUrl urlVert( QStringLiteral( "qrc:/shaders/phongDataDefined.vert" ) ); - shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) ); - const QUrl urlFrag( QStringLiteral( "qrc:/shaders/phongDataDefined.frag" ) ); - shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Fragment, Qt3DRender::QShaderProgram::loadSource( urlFrag ) ); - - renderPass->setShaderProgram( shaderProgram ); - technique->addRenderPass( renderPass ); + if ( dataDefinedProperties().hasActiveProperties() ) + { + // Load shader programs + const QUrl urlVert( QStringLiteral( "qrc:/shaders/phongDataDefined.vert" ) ); + shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) ); + const QByteArray finalFragmentShaderCode = Qgs3DUtils::addDefinesToShaderCode( fragmentShaderCode, QStringList( {"DATA_DEFINED"} ) ); + shaderProgram->setFragmentShaderCode( finalFragmentShaderCode ); + } + else + { + // Load shader programs + const QUrl urlVert( QStringLiteral( "qrc:/shaders/default.vert" ) ); + shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) ); + shaderProgram->setFragmentShaderCode( fragmentShaderCode ); + + const QColor ambient = context.isSelected() ? context.selectionColor().darker() : mAmbient; + const QColor diffuse = context.isSelected() ? context.selectionColor() : mDiffuse; + + effect->addParameter( new Qt3DRender::QParameter( QStringLiteral( "ambientColor" ), + QColor::fromRgbF( ambient.redF() * mAmbientCoefficient, + ambient.greenF() * mAmbientCoefficient, + ambient.blueF() * mAmbientCoefficient ) ) ); + effect->addParameter( new Qt3DRender::QParameter( QStringLiteral( "diffuseColor" ), + QColor::fromRgbF( diffuse.redF() * mDiffuseCoefficient, + diffuse.greenF() * mDiffuseCoefficient, + diffuse.blueF() * mDiffuseCoefficient ) ) ); + effect->addParameter( new Qt3DRender::QParameter( QStringLiteral( "specularColor" ), + QColor::fromRgbF( mSpecular.redF() * mSpecularCoefficient, + mSpecular.greenF() * mSpecularCoefficient, + mSpecular.blueF() * mSpecularCoefficient ) ) ); + } - eff->addParameter( new Qt3DRender::QParameter( QStringLiteral( "shininess" ), static_cast< float >( mShininess ) ) ); - eff->addParameter( new Qt3DRender::QParameter( QStringLiteral( "opacity" ), static_cast< float >( mOpacity ) ) ); + effect->addParameter( new Qt3DRender::QParameter( QStringLiteral( "shininess" ), static_cast< float >( mShininess ) ) ); + effect->addParameter( new Qt3DRender::QParameter( QStringLiteral( "opacity" ), static_cast< float >( mOpacity ) ) ); - eff->addTechnique( technique ); - material->setEffect( eff ); + effect->addTechnique( technique ); + material->setEffect( effect ); return material; } diff --git a/src/3d/materials/qgsphongmaterialsettings.h b/src/3d/materials/qgsphongmaterialsettings.h index b1557f6391dc..a14954838e1f 100644 --- a/src/3d/materials/qgsphongmaterialsettings.h +++ b/src/3d/materials/qgsphongmaterialsettings.h @@ -195,8 +195,7 @@ class _3D_EXPORT QgsPhongMaterialSettings : public QgsAbstractMaterialSettings double mOpacity = 1.0; //! Constructs a material from shader files - Qt3DRender::QMaterial *constantColorMaterial( const QgsMaterialContext &context ) const; - Qt3DRender::QMaterial *dataDefinedMaterial() const; + Qt3DRender::QMaterial *buildMaterial( const QgsMaterialContext &context ) const; }; diff --git a/src/3d/materials/qgsphongtexturedmaterialsettings.cpp b/src/3d/materials/qgsphongtexturedmaterialsettings.cpp index acbbd2212976..df48d6c45481 100644 --- a/src/3d/materials/qgsphongtexturedmaterialsettings.cpp +++ b/src/3d/materials/qgsphongtexturedmaterialsettings.cpp @@ -141,7 +141,7 @@ Qt3DRender::QMaterial *QgsPhongTexturedMaterialSettings::toMaterial( QgsMaterial Qt3DRender::QShaderProgram *shaderProgram = new Qt3DRender::QShaderProgram(); //Load shader programs - const QUrl urlVert( QStringLiteral( "qrc:/shaders/diffuseSpecular.vert" ) ); + const QUrl urlVert( QStringLiteral( "qrc:/shaders/default.vert" ) ); shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) ); const QUrl urlFrag( QStringLiteral( "qrc:/shaders/diffuseSpecular.frag" ) ); shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Fragment, Qt3DRender::QShaderProgram::loadSource( urlFrag ) ); diff --git a/src/3d/qgs3dutils.cpp b/src/3d/qgs3dutils.cpp index 8c685d7bb0a1..74709da626c2 100644 --- a/src/3d/qgs3dutils.cpp +++ b/src/3d/qgs3dutils.cpp @@ -902,3 +902,23 @@ Qt3DRender::QCullFace::CullingMode Qgs3DUtils::qt3DcullingMode( Qgs3DTypes::Cull } return Qt3DRender::QCullFace::NoCulling; } + + +QByteArray Qgs3DUtils::addDefinesToShaderCode( const QByteArray &shaderCode, const QStringList &defines ) +{ + // There is one caveat to take care of - GLSL source code needs to start with #version as + // a first directive, otherwise we get the old GLSL 100 version. So we can't just prepend the + // shader source code, but insert our defines at the right place. + + QStringList defineLines; + for ( const QString &define : defines ) + defineLines += "#define " + define + "\n"; + + QString definesText = defineLines.join( QString() ); + + QByteArray newShaderCode = shaderCode; + int versionIndex = shaderCode.indexOf( "#version " ); + int insertionIndex = versionIndex == -1 ? 0 : shaderCode.indexOf( '\n', versionIndex + 1 ) + 1; + newShaderCode.insert( insertionIndex, definesText.toLatin1() ); + return newShaderCode; +} diff --git a/src/3d/qgs3dutils.h b/src/3d/qgs3dutils.h index 7afb99dea215..433c0843b9c3 100644 --- a/src/3d/qgs3dutils.h +++ b/src/3d/qgs3dutils.h @@ -304,6 +304,16 @@ class _3D_EXPORT Qgs3DUtils * \since QGIS 3.34 */ static Qt3DRender::QCullFace::CullingMode qt3DcullingMode( Qgs3DTypes::CullingMode mode ); + + /** + * Inserts some define macros into a shader source code. + * + * \param shaderCode shader code + * \param defines list of defines to add + * + * \since QGIS 3.40 + */ + static QByteArray addDefinesToShaderCode( const QByteArray &shaderCode, const QStringList &defines ); }; #endif // QGS3DUTILS_H diff --git a/src/3d/shaders.qrc b/src/3d/shaders.qrc index 351525728254..7a33d600f801 100644 --- a/src/3d/shaders.qrc +++ b/src/3d/shaders.qrc @@ -1,7 +1,6 @@ shaders/diffuseSpecular.frag - shaders/diffuseSpecular.vert shaders/instanced.vert shaders/light.inc.frag shaders/lines.vert @@ -24,8 +23,8 @@ shaders/preview.vert shaders/preview.frag shaders/phongDataDefined.vert - shaders/phongDataDefined.frag - shaders/goochDataDefined.frag + shaders/phong.frag + shaders/gooch.frag shaders/goochDataDefined.vert shaders/depth_render.frag shaders/depth_render.vert @@ -33,8 +32,6 @@ shaders/ssao_factor_render.vert shaders/ssao_factor_blur.frag shaders/ssao_factor_blur.vert - shaders/phongConstant.frag - shaders/phongConstant.vert shaders/metalrough.frag shaders/default.vert diff --git a/src/3d/shaders/diffuseSpecular.vert b/src/3d/shaders/diffuseSpecular.vert deleted file mode 100644 index d2bc58abf0d4..000000000000 --- a/src/3d/shaders/diffuseSpecular.vert +++ /dev/null @@ -1,30 +0,0 @@ -// copied from qt3d/src/extras/shaders/gl3/default.vert - -#version 330 - -in vec3 vertexPosition; -in vec3 vertexNormal; -in vec2 vertexTexCoord; - -out vec3 worldPosition; -out vec3 worldNormal; -out vec2 texCoord; - -uniform mat4 modelMatrix; -uniform mat3 modelNormalMatrix; -uniform mat4 modelViewProjection; - -uniform float texCoordScale; - -void main(void) -{ - // Pass through scaled texture coordinates - texCoord = vertexTexCoord * texCoordScale; - - // Transform position and normal to world space - worldPosition = vec3(modelMatrix * vec4(vertexPosition, 1.0)); - worldNormal = normalize(modelNormalMatrix * vertexNormal); - - // Calculate vertex position in clip coordinates - gl_Position = modelViewProjection * vec4(vertexPosition, 1.0); -} diff --git a/src/3d/shaders/goochDataDefined.frag b/src/3d/shaders/gooch.frag similarity index 71% rename from src/3d/shaders/goochDataDefined.frag rename to src/3d/shaders/gooch.frag index f196b9fdad2b..c06ee0ef040c 100644 --- a/src/3d/shaders/goochDataDefined.frag +++ b/src/3d/shaders/gooch.frag @@ -1,11 +1,18 @@ #version 150 core +#ifdef DATA_DEFINED in DataColor { vec3 diffuse; vec3 warm; vec3 cool; vec3 specular; } vs_in; +#else +uniform vec3 kd; // Diffuse reflectivity +uniform vec3 ks; // Specular reflectivity +uniform vec3 kblue; // Cool color +uniform vec3 kyellow; // Warm color +#endif uniform float alpha; // Fraction of diffuse added to kblue @@ -27,8 +34,13 @@ vec3 goochModel( const in vec3 pos, const in vec3 n ) // http://www.cs.northwestern.edu/~ago820/SIG98/abstract.html // Calculate kcool and kwarm from equation (3) - vec3 kcool = clamp(vs_in.cool + alpha * vs_in.diffuse, 0.0, 1.0); - vec3 kwarm = clamp(vs_in.warm + beta * vs_in.diffuse, 0.0, 1.0); + #ifdef DATA_DEFINED + vec3 kcool = clamp(vs_in.cool + alpha * vs_in.diffuse, 0.0, 1.0); + vec3 kwarm = clamp(vs_in.warm + beta * vs_in.diffuse, 0.0, 1.0); + #else + vec3 kcool = clamp(kblue + alpha * kd, 0.0, 1.0); + vec3 kwarm = clamp(kyellow + beta * kd, 0.0, 1.0); + #endif vec3 result = vec3(0.0); for (int i = 0; i < lightCount; ++i) { @@ -54,7 +66,11 @@ vec3 goochModel( const in vec3 pos, const in vec3 n ) specular = pow( max( dot( r, v ), 0.0 ), shininess ); // Sum the blended tone and specular highlight - result += intensity + vs_in.specular * specular; + #ifdef DATA_DEFINED + result += intensity + vs_in.specular * specular; + #else + result += intensity + ks * specular; + #endif } return result; diff --git a/src/3d/shaders/phong.frag b/src/3d/shaders/phong.frag new file mode 100644 index 000000000000..3de363c70b1b --- /dev/null +++ b/src/3d/shaders/phong.frag @@ -0,0 +1,49 @@ +#version 330 + +uniform vec3 eyePosition; +uniform float shininess; + +in vec3 worldPosition; +in vec3 worldNormal; + +#ifdef DATA_DEFINED +in DataColor { + vec4 ambient; + vec4 diffuse; + vec4 specular; +} vs_in; +#else +uniform float opacity; +uniform vec3 ambientColor; +uniform vec3 diffuseColor; +uniform vec3 specularColor; +#endif + +out vec4 fragColor; + +#pragma include phong.inc.frag + +void main(void) +{ + vec3 worldView = normalize(eyePosition - worldPosition); + + #ifdef DATA_DEFINED + fragColor = phongFunction( + vs_in.ambient, + vs_in.diffuse, + vs_in.specular, + shininess, + worldPosition, + worldView, + worldNormal); + #else + fragColor = phongFunction( + vec4(ambientColor, opacity), + vec4(diffuseColor, opacity), + vec4(specularColor, opacity), + shininess, + worldPosition, + worldView, + worldNormal); + #endif +} diff --git a/src/3d/shaders/phongConstant.frag b/src/3d/shaders/phongConstant.frag deleted file mode 100644 index 49f4ee3aa431..000000000000 --- a/src/3d/shaders/phongConstant.frag +++ /dev/null @@ -1,29 +0,0 @@ -#version 330 - -uniform vec3 eyePosition; - -in vec3 worldPosition; -in vec3 worldNormal; - -uniform float shininess; -uniform float opacity; -uniform vec3 ambientColor; -uniform vec3 diffuseColor; -uniform vec3 specularColor; - -out vec4 fragColor; - -#pragma include phong.inc.frag - -void main(void) -{ - vec3 worldView = normalize(eyePosition - worldPosition); - fragColor = phongFunction( - vec4(ambientColor, opacity), - vec4(diffuseColor, opacity), - vec4(specularColor, opacity), - shininess, - worldPosition, - worldView, - worldNormal); -} diff --git a/src/3d/shaders/phongConstant.vert b/src/3d/shaders/phongConstant.vert deleted file mode 100644 index 83c45d103a74..000000000000 --- a/src/3d/shaders/phongConstant.vert +++ /dev/null @@ -1,21 +0,0 @@ -#version 330 - -in vec3 vertexPosition; -in vec3 vertexNormal; - -out vec3 worldPosition; -out vec3 worldNormal; - -uniform mat4 modelMatrix; -uniform mat3 modelNormalMatrix; -uniform mat4 modelViewProjection; - -void main() -{ - // Transform position, normal, and tangent to world space - worldPosition = vec3(modelMatrix * vec4(vertexPosition, 1.0)); - worldNormal = normalize(modelNormalMatrix * vertexNormal); - - // Calculate vertex position in clip coordinates - gl_Position = modelViewProjection * vec4(vertexPosition, 1.0); -} diff --git a/src/3d/shaders/phongDataDefined.frag b/src/3d/shaders/phongDataDefined.frag deleted file mode 100644 index 7e2409f3ab0a..000000000000 --- a/src/3d/shaders/phongDataDefined.frag +++ /dev/null @@ -1,30 +0,0 @@ -#version 330 - -uniform vec3 eyePosition; -uniform float shininess; - -in vec3 worldPosition; -in vec3 worldNormal; - -in DataColor { - vec4 ambient; - vec4 diffuse; - vec4 specular; -} vs_in; - -out vec4 fragColor; - -#pragma include phong.inc.frag - -void main(void) -{ - vec3 worldView = normalize(eyePosition - worldPosition); - fragColor = phongFunction( - vs_in.ambient, - vs_in.diffuse, - vs_in.specular, - shininess, - worldPosition, - worldView, - worldNormal); -} diff --git a/src/3d/symbols/qgspoint3dsymbol_p.cpp b/src/3d/symbols/qgspoint3dsymbol_p.cpp index 291a79118e24..8bec1d439723 100644 --- a/src/3d/symbols/qgspoint3dsymbol_p.cpp +++ b/src/3d/symbols/qgspoint3dsymbol_p.cpp @@ -229,7 +229,7 @@ Qt3DRender::QMaterial *QgsInstancedPoint3DSymbolHandler::material( const QgsPoin Qt3DRender::QShaderProgram *shaderProgram = new Qt3DRender::QShaderProgram; shaderProgram->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral( "qrc:/shaders/instanced.vert" ) ) ) ); - shaderProgram->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral( "qrc:/shaders/phongConstant.frag" ) ) ) ); + shaderProgram->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral( "qrc:/shaders/phong.frag" ) ) ) ); Qt3DRender::QRenderPass *renderPass = new Qt3DRender::QRenderPass; renderPass->setShaderProgram( shaderProgram );