Skip to content

Commit

Permalink
Started adding depth and shadow map textures preview debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
NEDJIMAbelgacem authored and wonder-sk committed Nov 6, 2020
1 parent e9ac731 commit 1e9e4d7
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 17 deletions.
9 changes: 9 additions & 0 deletions python/3d/auto_generated/qgs3dmapsettings.sip.in
Expand Up @@ -454,6 +454,15 @@ Sets the eye dome lighting distance value (contributes to the contrast of the im
Returns the eye dome lighting distance value (contributes to the contrast of the image)
%End

void setDebugShadowMapSettings( bool enabled, int corner, double size );
%Docstring
Sets the debugging settings of the shadowmap

.. versionadded:: 3.18
\see
%End


QList<QgsPointLightSettings> pointLights() const;
%Docstring
Returns list of point lights defined in the scene
Expand Down
5 changes: 5 additions & 0 deletions src/3d/qgs3dmapsettings.cpp
Expand Up @@ -704,3 +704,8 @@ void Qgs3DMapSettings::setShadowSettings( const QgsShadowSettings &shadowSetting
mShadowSettings = shadowSettings;
emit shadowSettingsChanged();
}

void Qgs3DMapSettings::setDebugShadowMapSettings( bool enabled, int corner, double size )
{

}
17 changes: 17 additions & 0 deletions src/3d/qgs3dmapsettings.h
Expand Up @@ -389,6 +389,15 @@ class _3D_EXPORT Qgs3DMapSettings : public QObject, public QgsTemporalRangeObjec
//! Returns the eye dome lighting distance value (contributes to the contrast of the image)
int eyeDomeLightingDistance() const { return mEyeDomeLightingDistance; }

/**
* Sets the debugging settings of the shadowmap
* \since QGIS 3.18
* \see
*/
void setDebugShadowMapSettings( bool enabled, int corner, double size );

// bool debugShadowMapEnabled() { return }

/**
* Returns list of point lights defined in the scene
* \since QGIS 3.6
Expand Down Expand Up @@ -644,6 +653,14 @@ class _3D_EXPORT Qgs3DMapSettings : public QObject, public QgsTemporalRangeObjec
bool mEyeDomeLightingEnabled = false;
double mEyeDomeLightingStrength = 1000.0;
int mEyeDomeLightingDistance = 1;

bool mDebugShadowMapEnabled = false;
int mDebugShadowMapCorner = 0;
double mDebugShadowMapSize = 0.1;

bool mDebugDepthMapEnabled = false;
int mDebugDepthMapCorner = 0;
double mDebugDepthMapSize = 0.1;
};


Expand Down
3 changes: 3 additions & 0 deletions src/3d/qgsshadowrenderingframegraph.cpp
Expand Up @@ -193,13 +193,16 @@ QgsShadowRenderingFrameGraph::QgsShadowRenderingFrameGraph( QWindow *window, Qt3
// textures preview pass
Qt3DRender::QFrameGraphNode *previewPass = constructTexturesPreviewPass();
previewPass->setParent( mMainViewPort );

this->addTexturePreviewOverlay( mForwardDepthTexture, QPointF( 0.8f, 0.8f ), QSizeF( 0.2f, 0.2f ) );
}

void QgsShadowRenderingFrameGraph::addTexturePreviewOverlay( Qt3DRender::QTexture2D *texture, const QPointF &centerNDC, const QSizeF &size, QVector<Qt3DRender::QParameter *> additionalShaderParameters )
{
QgsPreviewQuad *previewQuad = new QgsPreviewQuad( texture, centerNDC, size, additionalShaderParameters );
previewQuad->addComponent( mPreviewLayer );
previewQuad->setParent( mRootEntity );
mPreviewQuads.push_back( previewQuad );
}

QVector3D WorldPosFromDepth( QMatrix4x4 projMatrixInv, QMatrix4x4 viewMatrixInv, float texCoordX, float texCoordY, float depth )
Expand Down
22 changes: 15 additions & 7 deletions src/3d/shaders/postprocess.frag
Expand Up @@ -73,20 +73,27 @@ float CalcShadowFactor(vec4 LightSpacePos)
{
for(int y = -k; y <= k; ++y)
{
float pcfDepth = texture(shadowTexture, UVCoords + vec2(x, y) * texelSize).r;
float pcfDepth = texture2D(shadowTexture, UVCoords + vec2(x, y) * texelSize).r;
shadow += z - shadowBias >= pcfDepth ? 0.5 : 1.0;
}
}

return shadow / (2 * k + 1) / (2 * k + 1);
}

float linearizeDepth(float depth)
{
float ndc = depth * 2.0 - 1.0;
return (2.0 * nearPlane * farPlane) / (farPlane + nearPlane - ndc * (farPlane - nearPlane));
}

float edlFactor(vec2 coords)
{
vec2 texelSize = 2.0 / textureSize(shadowTexture, 0);
vec2 texelSize = 2.0 / textureSize(depthTexture, 0);
vec2 neighbours[4] = vec2[4](vec2(-1.0f, 0.0f), vec2(1.0f, 0.0f), vec2(0.0f, -1.0f), vec2(0.0f, 1.0f) );
float factor = 0.0f;
float centerDepth = texture(depthTexture, coords).r;
float centerDepth = texture2D(depthTexture, coords).r;
return linearizeDepth(centerDepth) / farPlane;
for (int i = 0; i < 4; i++)
{
vec2 neighbourCoords = coords + edlDistance * texelSize * neighbours[i];
Expand All @@ -103,10 +110,10 @@ float edlFactor(vec2 coords)

void main()
{
vec3 worldPosition = WorldPosFromDepth(texture(depthTexture, texCoord).r);
vec3 worldPosition = WorldPosFromDepth(texture2D(depthTexture, texCoord).r);
vec4 positionInLightSpace = projectionMatrix * viewMatrix * vec4(worldPosition, 1.0f);
positionInLightSpace /= positionInLightSpace.w;
vec3 color = texture(colorTexture, texCoord).rgb;
vec3 color = texture2D(colorTexture, texCoord).rgb;
// if shadow rendering is disabled or the pixel is outside the shadow rendering distance don't render shadows
if (renderShadows == 0 || worldPosition.x > shadowMaxX || worldPosition.x < shadowMinX || worldPosition.z > shadowMaxZ || worldPosition.z < shadowMinZ) {
fragColor = vec4(color.rgb, 1.0f);
Expand All @@ -116,7 +123,8 @@ void main()
}
if (edlEnabled != 0)
{
float shade = exp(-edlFactor(texCoord) * edlStrength);
fragColor = vec4(fragColor.rgb * shade, fragColor.a);
// float shade = exp(-edlFactor(texCoord) * edlStrength);
float f = edlFactor(texCoord);
fragColor = vec4(f, f, f, 1.0f);//vec4(fragColor.rgb * shade, fragColor.a);
}
}
2 changes: 1 addition & 1 deletion src/3d/shaders/preview.frag
Expand Up @@ -14,7 +14,7 @@ void main()
// When trying to display a depth texture make sure to linearize the depth value
// if you are using a perspective projection
if (isDepth)
fragColor = vec4(vec3(texture(previewTexture, texCoord).r), 1.0f);
fragColor = vec4(vec3(texture2D(previewTexture, texCoord).r), 1.0f);
else
fragColor = vec4(texture(previewTexture, texCoord).rgb, 1.0f);
}
10 changes: 10 additions & 0 deletions src/app/3d/qgs3dmapconfigwidget.cpp
Expand Up @@ -379,6 +379,16 @@ void Qgs3DMapConfigWidget::updateMaxZoomLevel()
labelZoomLevels->setText( QStringLiteral( "0 - %1" ).arg( zoomLevel ) );
}


void Qgs3DMapConfigWidget::debugShadowMapChanged( bool enabled, int corner, double size )
{

}
void Qgs3DMapConfigWidget::debugDepthMapChanged( bool enabled, int corner, double size )
{

}

void Qgs3DMapConfigWidget::validate()
{
mMessageBar->clearWidgets();
Expand Down
9 changes: 9 additions & 0 deletions src/app/3d/qgs3dmapconfigwidget.h
Expand Up @@ -48,6 +48,9 @@ class Qgs3DMapConfigWidget : public QWidget, private Ui::Map3DConfigWidget
void onTerrainLayerChanged();
void updateMaxZoomLevel();

void debugShadowMapChanged( bool enabled, int corner, double size );
void debugDepthMapChanged( bool enabled, int corner, double size );

void validate();

private:
Expand All @@ -57,6 +60,12 @@ class Qgs3DMapConfigWidget : public QWidget, private Ui::Map3DConfigWidget
QgsMesh3dSymbolWidget *mMeshSymbolWidget = nullptr;
QgsSkyboxRenderingSettingsWidget *mSkyboxSettingsWidget = nullptr;
QgsShadowRenderingSettingsWidget *mShadowSetiingsWidget = nullptr;
bool mDebugShadowMapEnabled = false;
int mDebugShadowMapCorner = 0;
double mDebugShadowMapSize = 0.1;
bool mDebugDepthMapEnabled = false;
int mDebugDepthMapCorner = 0;
double mDebugDepthMapSize = 0.1;
};

#endif // QGS3DMAPCONFIGWIDGET_H
158 changes: 149 additions & 9 deletions src/ui/3d/map3dconfigwidget.ui
Expand Up @@ -146,6 +146,11 @@
<normaloff>:/images/themes/default/propertyicons/system.svg</normaloff>:/images/themes/default/propertyicons/system.svg</iconset>
</property>
</item>
<item>
<property name="text">
<string>Debug</string>
</property>
</item>
</widget>
</item>
</layout>
Expand Down Expand Up @@ -179,7 +184,7 @@
<item>
<widget class="QStackedWidget" name="m3DOptionsStackedWidget">
<property name="currentIndex">
<number>4</number>
<number>5</number>
</property>
<widget class="QWidget" name="mPageTerrain">
<layout class="QVBoxLayout" name="verticalLayout_61">
Expand All @@ -205,8 +210,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>239</width>
<height>251</height>
<width>699</width>
<height>604</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayoutTerrain">
Expand Down Expand Up @@ -393,8 +398,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>98</width>
<height>43</height>
<width>699</width>
<height>604</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayoutLight">
Expand Down Expand Up @@ -477,8 +482,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>133</width>
<height>43</height>
<width>699</width>
<height>604</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayoutShadow">
Expand Down Expand Up @@ -567,8 +572,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>153</width>
<height>126</height>
<width>699</width>
<height>604</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayoutCameraSkybox">
Expand Down Expand Up @@ -874,6 +879,141 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="mPageDebug">
<layout class="QGridLayout" name="gridLayout_4">
<item row="0" column="0">
<widget class="QScrollArea" name="scrollAreaDebug">
<property name="widgetResizable">
<bool>true</bool>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>688</width>
<height>584</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_7">
<item row="0" column="0">
<widget class="QGroupBox" name="DebugSroupBox">
<property name="title">
<string>Debug</string>
</property>
<layout class="QGridLayout" name="gridLayout_8">
<item row="0" column="0">
<widget class="QGroupBox" name="mShadowMapDebugGroupBox">
<property name="title">
<string>Debug shadow map</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>false</bool>
</property>
<layout class="QGridLayout" name="gridLayout_10">
<item row="0" column="1">
<widget class="QComboBox" name="mDebugShadowMapCornerComboBox"/>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_8">
<property name="text">
<string>Corner:</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_10">
<property name="text">
<string>Size</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QDoubleSpinBox" name="mDebugShadowMapSizeSpinBox">
<property name="maximum">
<double>1.000000000000000</double>
</property>
<property name="singleStep">
<double>0.100000000000000</double>
</property>
<property name="value">
<double>0.100000000000000</double>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="1" column="0">
<widget class="QGroupBox" name="mDepthMapDebugGroupBox">
<property name="title">
<string>Debug depth map</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>false</bool>
</property>
<layout class="QGridLayout" name="gridLayout_9">
<item row="0" column="0">
<widget class="QLabel" name="label_9">
<property name="text">
<string>Corner</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="mDebugDepthMapCornerComboBox"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_11">
<property name="text">
<string>Size</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QDoubleSpinBox" name="mDebugDepthMapSizeSpinBox">
<property name="maximum">
<double>1.000000000000000</double>
</property>
<property name="singleStep">
<double>0.100000000000000</double>
</property>
<property name="value">
<double>0.100000000000000</double>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="2" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
Expand Down

0 comments on commit 1e9e4d7

Please sign in to comment.