From 0b017158241b9a358ba2fa44b3f0cd39012d7349 Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Tue, 12 Apr 2022 09:11:31 -0400 Subject: [PATCH] Use mdl normals to display some lighting --- renderer/shaders/mesh.frag | 12 +++++++++++- renderer/shaders/mesh.frag.spv | Bin 424 -> 992 bytes renderer/shaders/mesh.vert | 8 +++++++- renderer/shaders/mesh.vert.spv | Bin 1152 -> 1392 bytes renderer/src/renderer.cpp | 19 +++++++++++-------- 5 files changed, 29 insertions(+), 10 deletions(-) diff --git a/renderer/shaders/mesh.frag b/renderer/shaders/mesh.frag index 35a774d..d3146d1 100644 --- a/renderer/shaders/mesh.frag +++ b/renderer/shaders/mesh.frag @@ -1,7 +1,17 @@ #version 450 +layout(location = 0) in vec3 inNormal; +layout(location = 1) in vec3 inFragPos; + layout(location = 0) out vec4 outColor; void main() { - outColor = vec4(0.0, 1.0, 0.0, 1.0); + const vec3 lightPos = vec3(3); + + vec3 norm = normalize(inNormal); + vec3 lightDir = normalize(lightPos - inFragPos); + + float diff = max(dot(norm, lightDir), 0.0); + + outColor = vec4(0.0, 1.0, 0.0, 1.0) * diff; } diff --git a/renderer/shaders/mesh.frag.spv b/renderer/shaders/mesh.frag.spv index 47d2768453f81ddde416df6318329f014c5cd0f7..08be789dc604211732bd431227671d942df574d6 100644 GIT binary patch literal 992 zcmYk3TWb?h6ot3BHMU998tuhf)Aps%5`0h*L|f=(UV;()0Yk_n?Sbh`NhX4Cg6}^0 zi~Lo-2(EAJhMZxs_g-i1b6E=mH!iKc$=@4aAIh*}fV@)SUgE$%vqrqi7zPpR(#kX`4 zry-lfNtjK>Mff^F*Id$tY@9BVWkFqv<+G_D>WnO(-6~jJANL8_Co!YcxpgIJxV|Ya z!pu4D9I~s~==yU$SB}?~ViKlP*YKu%QJm*#UiL-O;1AED_O~-1YW94;s|hfJ{c;*s z=3B}e;(Efi^vsHyDfd)272yE&sv_Rh@9%5w0C%jK-i}~)c6Oo)-G%_?@ILNJAHGi% z(bDfePnEN{*KhGoI|9Q>5`A^gzq$jonX41NUe_J{4yev7hk2(cUc<*_noO%4v$#`@H<_7OR zkCnR*`}lAdc=y>>ji~6u^zi^ONDW9F YOiylPk{7W9nhP=sB>NZWJ_{fQ0FdnsiU0rr diff --git a/renderer/shaders/mesh.vert b/renderer/shaders/mesh.vert index f981623..dbad9f4 100644 --- a/renderer/shaders/mesh.vert +++ b/renderer/shaders/mesh.vert @@ -1,11 +1,17 @@ #version 450 layout(location = 0) in vec3 inPosition; +layout(location = 1) in vec3 inNormal; -layout( push_constant ) uniform PushConstant { +layout(location = 0) out vec3 outNormal; +layout(location = 1) out vec3 outFragPos; + +layout(push_constant) uniform PushConstant { mat4 mvp; }; void main() { gl_Position = mvp * vec4(inPosition, 1.0); + outNormal = inNormal; + outFragPos = inNormal; } diff --git a/renderer/shaders/mesh.vert.spv b/renderer/shaders/mesh.vert.spv index 16ad614cf3797041c32f34971a18c48d214713bd..b2a2cd0911800df103051033fdaa5f7eb1885429 100644 GIT binary patch delta 306 zcmZqR{J_P_%%sfDz`)4B#lXv;Ig!^?nTr7gSb#V;F*6Uu<^^I&AXWilRUlU17-`RF z#mc}67RfIy@yjpDP0RtxfW$y5GV|a)|7*L&CQDS;Pe(~gKjGJ`bfmVPFlmlWA z0P#UuL3~CiAEXV;-|Wg1&S shaderStages = {vertexShaderStageInfo, fragmentShaderStageInfo}; VkVertexInputBindingDescription binding = {}; - binding.stride = sizeof(float) * 3; + binding.stride = sizeof(Vertex); VkVertexInputAttributeDescription positionAttribute = {}; positionAttribute.format = VK_FORMAT_R32G32B32_SFLOAT; + positionAttribute.offset = offsetof(Vertex, position); + + VkVertexInputAttributeDescription normalAttribute = {}; + normalAttribute.format = VK_FORMAT_R32G32B32_SFLOAT; + normalAttribute.location = 1; + normalAttribute.offset = offsetof(Vertex, normal); VkPipelineVertexInputStateCreateInfo vertexInputState = {}; vertexInputState.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;