Commit ebeae5dc authored by Laszlo Agocs's avatar Laszlo Agocs

shadowmap example skeleton

Shaders and camera logic not yet there.

It is essential to have a test that exercises depth-only passes and
rendering into a depth texture that is then used in another pass. Some
simple shadow casting directional light is a good example for this.
parent fbeefcd7
......@@ -14,7 +14,8 @@ SUBDIRS += \
triquadcube \
offscreen \
floattexture \
mrt
mrt \
shadowmap
qtConfig(vulkan) {
SUBDIRS += \
......
qsb --glsl 120 --hlsl 50 --msl 12 -c shadowmap.vert -o shadowmap.vert.qsb
qsb --glsl 120 --hlsl 50 --msl 12 -c shadowmap.frag -o shadowmap.frag.qsb
qsb --glsl 120 --hlsl 50 --msl 12 -c light.vert -o light.vert.qsb
qsb --glsl 120 --hlsl 50 --msl 12 -c light.frag -o light.frag.qsb
#version 440
layout(location = 0) in vec3 vECVertNormal;
layout(location = 1) in vec3 vECVertPos;
layout(location = 0) out vec4 fragColor;
void main()
{
vec3 ECCameraPosition = vec3(0.0, 0.0, 4.0); // matches C++
vec3 ka = vec3(0.05, 0.05, 0.05);
vec3 kd = vec3(0.3, 0.7, 0.6);
vec3 ks = vec3(0.66, 0.66, 0.66);
vec3 ECLightPosition = vec3(0.0, 10.0, 1.0); // matches C++
vec3 attenuation = vec3(1.0, 0.0, 0.0);
vec3 color = vec3(1.0, 1.0, 1.0);
float intensity = 0.8;
float specularExp = 150.0;
vec3 unnormL = ECLightPosition - vECVertPos;
float dist = length(unnormL);
float att = 1.0 / (attenuation.x + attenuation.y * dist + attenuation.z * dist * dist);
vec3 N = normalize(vECVertNormal);
vec3 L = normalize(unnormL);
float NL = max(0.0, dot(N, L));
vec3 dColor = att * intensity * color * NL;
vec3 R = reflect(-L, N);
vec3 V = normalize(ECCameraPosition - vECVertPos);
float RV = max(0.0, dot(R, V));
vec3 sColor = att * intensity * color * pow(RV, specularExp);
fragColor = vec4(ka + kd * dColor + ks * sColor, 1.0);
}
#version 440
layout(location = 0) in vec4 position;
layout(location = 1) in vec3 normal;
layout(std140, binding = 0) uniform buf {
mat4 mvp;
mat4 mv;
mat4 m;
mat3 n;
} ubuf;
out gl_PerVertex { vec4 gl_Position; };
layout(location = 0) out vec3 vECVertNormal;
layout(location = 1) out vec3 vECVertPos;
void main()
{
vECVertNormal = normalize(ubuf.n * normal);
vECVertPos = vec3(ubuf.m * position);
gl_Position = ubuf.mvp * position;
}
This diff is collapsed.
#version 440
void main()
{
}
TEMPLATE = app
QT += rhi
SOURCES = \
shadowmap.cpp
RESOURCES = shadowmap.qrc
target.path = $$[QT_INSTALL_EXAMPLES]/rhi/shadowmap
INSTALLS += target
<!DOCTYPE RCC><RCC version="1.0">
<qresource>
<file>shadowmap.vert.qsb</file>
<file>shadowmap.frag.qsb</file>
<file>light.vert.qsb</file>
<file>light.frag.qsb</file>
</qresource>
</RCC>
#version 440
layout(location = 0) in vec4 position;
layout(std140, binding = 0) uniform buf {
mat4 mvp;
} ubuf;
out gl_PerVertex { vec4 gl_Position; };
void main()
{
gl_Position = ubuf.mvp * position;
}
#ifndef CUBENORM_H
#define CUBENORM_H
// clang-format off
static const float cube[] = {
-1.0f,-1.0f,-1.0f, -1, 0, 0, // -X side
-1.0f,-1.0f, 1.0f, -1, 0, 0,
-1.0f, 1.0f, 1.0f, -1, 0, 0,
-1.0f, 1.0f, 1.0f, -1, 0, 0,
-1.0f, 1.0f,-1.0f, -1, 0, 0,
-1.0f,-1.0f,-1.0f, -1, 0, 0,
-1.0f,-1.0f,-1.0f, 0, 0, -1, // -Z side
1.0f, 1.0f,-1.0f, 0, 0, -1,
1.0f,-1.0f,-1.0f, 0, 0, -1,
-1.0f,-1.0f,-1.0f, 0, 0, -1,
-1.0f, 1.0f,-1.0f, 0, 0, -1,
1.0f, 1.0f,-1.0f, 0, 0, -1,
-1.0f,-1.0f,-1.0f, 0, -1, 0, // -Y side
1.0f,-1.0f,-1.0f, 0, -1, 0,
1.0f,-1.0f, 1.0f, 0, -1, 0,
-1.0f,-1.0f,-1.0f, 0, -1, 0,
1.0f,-1.0f, 1.0f, 0, -1, 0,
-1.0f,-1.0f, 1.0f, 0, -1, 0,
-1.0f, 1.0f,-1.0f, 0, 1, 0, // +Y side
-1.0f, 1.0f, 1.0f, 0, 1, 0,
1.0f, 1.0f, 1.0f, 0, 1, 0,
-1.0f, 1.0f,-1.0f, 0, 1, 0,
1.0f, 1.0f, 1.0f, 0, 1, 0,
1.0f, 1.0f,-1.0f, 0, 1, 0,
1.0f, 1.0f,-1.0f, 1, 0, 0, // +X side
1.0f, 1.0f, 1.0f, 1, 0, 0,
1.0f,-1.0f, 1.0f, 1, 0, 0,
1.0f,-1.0f, 1.0f, 1, 0, 0,
1.0f,-1.0f,-1.0f, 1, 0, 0,
1.0f, 1.0f,-1.0f, 1, 0, 0,
-1.0f, 1.0f, 1.0f, 0, 0, 1, // +Z side
-1.0f,-1.0f, 1.0f, 0, 0, 1,
1.0f, 1.0f, 1.0f, 0, 0, 1,
-1.0f,-1.0f, 1.0f, 0, 0, 1,
1.0f,-1.0f, 1.0f, 0, 0, 1,
1.0f, 1.0f, 1.0f, 0, 0, 1
};
// clang-format on
#endif
......@@ -157,6 +157,7 @@ protected:
QRhiRenderPassDescriptor *m_rp = nullptr;
QMatrix4x4 m_proj;
QMatrix4x4 m_rawProj;
QElapsedTimer m_timer;
int m_frameCount;
......@@ -337,8 +338,9 @@ void Window::resizeSwapChain()
m_frameCount = 0;
m_timer.restart();
m_proj = m_r->clipSpaceCorrMatrix();
m_proj.perspective(45.0f, outputSize.width() / (float) outputSize.height(), 0.01f, 1000.0f);
m_rawProj.setToIdentity();
m_rawProj.perspective(45.0f, outputSize.width() / (float) outputSize.height(), 0.01f, 1000.0f);
m_proj = m_r->clipSpaceCorrMatrix() * m_rawProj;
m_proj.translate(0, 0, -4);
}
......
......@@ -2782,6 +2782,28 @@ QRhiGraphicsPipeline::QRhiGraphicsPipeline(QRhiImplementation *rhi)
Regardless of the return value, calling release() is always safe.
*/
/*!
\fn void QRhiGraphicsPipeline::setDepthTest(bool enable)
Enables or disables depth testing. Both depth test and the writing out of
depth data are disabled by default.
\sa setDepthWrite()
*/
/*!
\fn void QRhiGraphicsPipeline::setDepthWrite(bool enable)
Controls the writing out of depth data into the depth buffer. By default
this is disabled. Depth write is typically enabled together with the depth
test.
\note Enabling depth write without having depth testing enabled may not
lead to the desired result, and should be avoided.
\sa setDepthTest()
*/
/*!
\class QRhiSwapChain
\inmodule QtRhi
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment