Skip to content

Commit

Permalink
qgsgoochmaterialsettings: Merge datadefined and constant cases
Browse files Browse the repository at this point in the history
This is similar to the previous QgsPhongMaterialSettings change:

This commit makes two changes to `QgsGoochMaterialSettings`:
1. It merges `dataDefinedMaterial()` and `constantColorMaterial` into
one new function: `buildMaterial`.

2. There is one fragment shader file called `gooch.frag`. This is
achieved by a `#define` logic. If `DATA_DEFINED` is defined, this is
the dataDefined case. Otherwise, this is the constant case.
  • Loading branch information
ptitjano committed Jul 16, 2024
1 parent d1fd312 commit 778e46e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 32 deletions.
57 changes: 30 additions & 27 deletions src/3d/materials/qgsgoochmaterialsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "qgsgoochmaterialsettings.h"
#include "qgscolorutils.h"
#include "qgs3dutils.h"

#include <Qt3DExtras/QGoochMaterial>
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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 );
Expand All @@ -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 );
Expand All @@ -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;
}
2 changes: 1 addition & 1 deletion src/3d/materials/qgsgoochmaterialsettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};


Expand Down
2 changes: 1 addition & 1 deletion src/3d/shaders.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<file>shaders/preview.frag</file>
<file>shaders/phongDataDefined.vert</file>
<file>shaders/phong.frag</file>
<file>shaders/goochDataDefined.frag</file>
<file>shaders/gooch.frag</file>
<file>shaders/goochDataDefined.vert</file>
<file>shaders/depth_render.frag</file>
<file>shaders/depth_render.vert</file>
Expand Down
22 changes: 19 additions & 3 deletions src/3d/shaders/goochDataDefined.frag → src/3d/shaders/gooch.frag
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) {
Expand All @@ -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;
Expand Down

0 comments on commit 778e46e

Please sign in to comment.