Skip to content

Commit 39a9ba2

Browse files
author
Carmelo Fernandez Aguera
committedMay 8, 2021
Lambert shading.
1 parent bb3d922 commit 39a9ba2

File tree

5 files changed

+39
-9
lines changed

5 files changed

+39
-9
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,4 @@ sports
5656
/samples/gltfViewer/bin/shaders/ui.frag.spv
5757
*.spv
5858
/tools/gltfOptimizer/bin/
59+
/.vscode

‎samples/gltfViewer/shaders/gbuffer.frag

+14-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,21 @@
1919
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2020
#version 450
2121

22-
layout(location = 0) in vec3 vVtxColor;
22+
layout(location = 0) in vec3 vPxlNormal;
2323
layout(location = 0) out vec4 outColor;
2424

25+
layout(push_constant) uniform Constants
26+
{
27+
mat4 proj;
28+
mat4 view;
29+
vec3 lightDir;
30+
vec3 ambiendColor;
31+
vec3 lightColor;
32+
} frameInfo;
33+
2534
void main() {
26-
outColor = vec4(vVtxColor, 1.0);
35+
vec3 normal = normalize(vPxlNormal);
36+
float ndl = max(0, dot(frameInfo.lightDir, normal));
37+
vec3 pxlColor = frameInfo.ambiendColor + ndl * frameInfo.lightColor;
38+
outColor = vec4(pxlColor, 1.0);
2739
}

‎samples/gltfViewer/shaders/gbuffer.vert

+7-4
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,15 @@ layout(push_constant) uniform Constants
2828
{
2929
mat4 proj;
3030
mat4 view;
31-
} camera;
31+
vec3 lightDir;
32+
vec3 ambiendColor;
33+
vec3 lightColor;
34+
} frameInfo;
3235

33-
layout(location = 0) out vec3 vVtxColor;
36+
layout(location = 0) out vec3 vPxlNormal;
3437

3538
void main() {
3639
mat4 world = worldMtx[gl_InstanceIndex];
37-
gl_Position = camera.proj * (camera.view * (world * vec4(position, 1.0)));
38-
vVtxColor = normal * 0.5 + 0.5;
40+
gl_Position = frameInfo.proj * (frameInfo.view * (world * vec4(position, 1.0)));
41+
vPxlNormal = (world * vec4(normal, 0)).xyz;
3942
}

‎samples/gltfViewer/src/player.cpp

+14-3
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ namespace rev {
100100

101101
// UI pipeline layout
102102
vk::PushConstantRange camerasPushRange(
103-
vk::ShaderStageFlagBits::eVertex,
103+
vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment,
104104
0, sizeof(CameraPushConstants));
105105

106106
vk::PipelineLayoutCreateInfo layoutInfo({},
@@ -135,6 +135,10 @@ namespace rev {
135135
createCamera();
136136
m_sceneRoot->init();
137137

138+
m_cameraPushC.ambiendColor = Vec4f(0.2f, 0.2f, 0.4f);
139+
m_cameraPushC.lightColor = Vec4f(1.f, 1.f, 1.f);
140+
m_cameraPushC.lightDir = normalize(Vec4f(1.f, 1.f, 1.f, 0.f));
141+
138142
/*
139143
// Default scene light
140144
{
@@ -391,7 +395,7 @@ namespace rev {
391395
}
392396

393397
// Clear
394-
auto clearColor = vk::ClearColorValue(std::array<float,4>{ m_bgColor, m_bgColor, m_bgColor, 1.f });
398+
auto clearColor = vk::ClearColorValue(std::array<float,4>{ m_cameraPushC.ambiendColor.x(), m_cameraPushC.ambiendColor.y(), m_cameraPushC.ambiendColor.z(), 1.f });
395399

396400
auto swapchainImage = renderContext().swapchainAquireNextImage(m_imageAvailableSemaphore, cmd);
397401

@@ -444,7 +448,12 @@ namespace rev {
444448
float aspect = viewport.width / viewport.height;
445449
m_cameraPushC.proj = mFlybyCam->projection(aspect);// .transpose();
446450
m_cameraPushC.view = mFlybyCam->view();// .transpose();
447-
cmd.pushConstants<CameraPushConstants>(m_gbufferPipelineLayout, vk::ShaderStageFlagBits::eVertex, 0, m_cameraPushC);
451+
452+
cmd.pushConstants<CameraPushConstants>(
453+
m_gbufferPipelineLayout,
454+
vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment,
455+
0,
456+
m_cameraPushC);
448457

449458
// Draw all instances in a single batch
450459
m_rasterData.bindBuffers(cmd);
@@ -553,6 +562,8 @@ namespace rev {
553562
{
554563
ImGui::Text("%s", "Sample text");
555564
ImGui::SliderFloat("Background", &m_bgColor, 0.f, 1.f);
565+
ImGui::ColorPicker3("Ambient Color", reinterpret_cast<float*>(&m_cameraPushC.ambiendColor));
566+
ImGui::ColorPicker3("Light Color", reinterpret_cast<float*>(&m_cameraPushC.lightColor));
556567
}
557568
ImGui::End();
558569

‎samples/gltfViewer/src/player.h

+3
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ namespace rev {
9797
{
9898
math::Mat44f proj;
9999
math::Mat44f view;
100+
math::Vec4f lightDir;
101+
math::Vec4f ambiendColor;
102+
math::Vec4f lightColor;
100103
} m_cameraPushC;
101104

102105
private:

0 commit comments

Comments
 (0)