diff options
| author | Jean-Philippe Bruyère <jp_bruyere@hotmail.com> | 2019-04-01 14:24:57 +0200 |
|---|---|---|
| committer | Jean-Philippe Bruyère <jp_bruyere@hotmail.com> | 2019-04-01 14:24:57 +0200 |
| commit | 9af331260bf34cd2f7578e863367dec69b32fa68 (patch) | |
| tree | 47b91a2e1a71bcc8e86847faeacb35bff53bad19 /samples/Model | |
| parent | e4ff67fb5f3a492809dcac8b09b8b4b0ba288044 (diff) | |
model
Diffstat (limited to 'samples/Model')
| -rw-r--r-- | samples/Model/main.cs | 136 | ||||
| -rw-r--r-- | samples/Model/shaders/model.frag | 60 | ||||
| -rw-r--r-- | samples/Model/shaders/model.vert | 19 |
3 files changed, 161 insertions, 54 deletions
diff --git a/samples/Model/main.cs b/samples/Model/main.cs index 7db9546..8bb2691 100644 --- a/samples/Model/main.cs +++ b/samples/Model/main.cs @@ -18,15 +18,21 @@ namespace ModelSample { public Matrix4x4 projection; public Matrix4x4 view; public Matrix4x4 model; + public Vector4 lightPos; + } + + struct PushConstants { + public Matrix4x4 matrix; } Matrices matrices; + PushConstants pushCst; HostBuffer uboMats; - VkDescriptorSetLayoutBinding matricesBinding; - VkDescriptorSetLayoutBinding textureBinding; - DescriptorSetLayout dsLayout; + DescriptorSetLayout descLayoutMatrix; + DescriptorSetLayout descLayoutTextures; + DescriptorPool descriptorPool; DescriptorSet descriptorSet; @@ -39,27 +45,40 @@ namespace ModelSample { VkFormat depthFormat; Image depthTexture; - - float rotSpeed = 0.01f; + float rotSpeed = 0.01f, zoomSpeed = 0.01f; double lastMouseX, lastMouseY; float rotX = -1.5f, rotY = 2.7f, rotZ = 0f; + float zoom = 1.0f; Model helmet; Program () : base () { - descriptorPool = new DescriptorPool (dev, 2, + descriptorPool = new DescriptorPool (dev, 27, new VkDescriptorPoolSize (VkDescriptorType.UniformBuffer), - new VkDescriptorPoolSize (VkDescriptorType.CombinedImageSampler) + new VkDescriptorPoolSize (VkDescriptorType.CombinedImageSampler,3*26) + //new VkDescriptorPoolSize (VkDescriptorType.CombinedImageSampler), + //new VkDescriptorPoolSize (VkDescriptorType.CombinedImageSampler) + ); + + descLayoutMatrix = new DescriptorSetLayout (dev, + new VkDescriptorSetLayoutBinding (0, VkShaderStageFlags.Vertex, VkDescriptorType.UniformBuffer)); + + descLayoutTextures = new DescriptorSetLayout (dev, + new VkDescriptorSetLayoutBinding (0, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler), + new VkDescriptorSetLayoutBinding (1, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler), + new VkDescriptorSetLayoutBinding (2, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler) ); - matricesBinding = new VkDescriptorSetLayoutBinding (0, VkShaderStageFlags.Vertex, VkDescriptorType.UniformBuffer); - textureBinding = new VkDescriptorSetLayoutBinding (1, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler); + descriptorSet = descriptorPool.Allocate (descLayoutMatrix); - dsLayout = new DescriptorSetLayout (dev, matricesBinding, textureBinding); - descriptorSet = descriptorPool.Allocate (dsLayout); + VkPushConstantRange pushConstantRange = new VkPushConstantRange { + stageFlags = VkShaderStageFlags.Vertex, + size = (uint)Marshal.SizeOf<PushConstants>(), + offset = 0 + }; - pipelineLayout = new PipelineLayout (dev, dsLayout); + pipelineLayout = new PipelineLayout (dev, pushConstantRange, descLayoutMatrix, descLayoutTextures).Activate (); loadAssets (); @@ -84,47 +103,15 @@ namespace ModelSample { pipeline.Activate (); - helmet.textures[0].Descriptor.imageLayout = VkImageLayout.ShaderReadOnlyOptimal; using (DescriptorSetWrites uboUpdate = new DescriptorSetWrites (dev)) { - uboUpdate.AddWriteInfo (descriptorSet, matricesBinding, uboMats.Descriptor); - uboUpdate.AddWriteInfo (descriptorSet, textureBinding, helmet.textures[0].Descriptor); + uboUpdate.AddWriteInfo (descriptorSet, descLayoutMatrix.Bindings[0], uboMats.Descriptor); uboUpdate.Update (); } - uboMats.Map ();//permanent map - updateMatrices (); - } + matrices.lightPos = new Vector4 (0.0f, 0.0f, -2.0f, 1.0f); - void loadAssets () { - helmet = new Model (dev, presentQueue, cmdPool, "data/DamagedHelmet.gltf"); - } - public override void Update () { + uboMats.Map ();//permanent map updateMatrices (); - updateRequested = false; - } - - void updateMatrices () { - matrices.projection = Matrix4x4.CreatePerspectiveFieldOfView (Utils.DegreesToRadians (60f), (float)swapChain.Width / (float)swapChain.Height, 0.1f, 256.0f); - matrices.view = Matrix4x4.CreateTranslation (0, 0, -2.5f); - matrices.model = - Matrix4x4.CreateFromAxisAngle (Vector3.UnitX, rotX) * - Matrix4x4.CreateFromAxisAngle (Vector3.UnitY, rotY) * - Matrix4x4.CreateFromAxisAngle (Vector3.UnitZ, rotZ); - - uboMats.Update (matrices, (uint)Marshal.SizeOf<Matrices> ()); - } - - protected override void onMouseMove (double xPos, double yPos) { - double diffX = lastMouseX - xPos; - double diffY = lastMouseY - yPos; - if (MouseButton[0]) { - rotY -= rotSpeed * (float)diffX; - rotX -= rotSpeed * (float)diffY; - } - lastMouseX = xPos; - lastMouseY = yPos; - - updateRequested = true; } protected override void Prepare () { @@ -157,7 +144,8 @@ namespace ModelSample { cmds[i].BindPipeline (pipeline); - helmet.DrawAll (cmds[i]); + helmet.Bind (cmds[i]); + helmet.DrawAll (cmds[i], pipelineLayout); renderPass.End (cmds[i]); @@ -165,10 +153,58 @@ namespace ModelSample { } } + void loadAssets () { + //helmet = new Model (dev, presentQueue, cmdPool, "/mnt/devel/vulkan/glTF-Sample-Models-master/2.0/Sponza/glTF/Sponza.gltf"); + helmet = new Model (dev, presentQueue, cmdPool, "data/DamagedHelmet.gltf"); + foreach (Model.Material mat in helmet.materials) { + mat.descriptorSet = descriptorPool.Allocate (descLayoutTextures); + using (DescriptorSetWrites uboUpdate = new DescriptorSetWrites (dev)) { + uboUpdate.AddWriteInfo (mat.descriptorSet, descLayoutTextures.Bindings[0], helmet.textures[(int)mat.baseColorTexture].Descriptor); + uboUpdate.AddWriteInfo (mat.descriptorSet, descLayoutTextures.Bindings[1], helmet.textures[(int)mat.normalTexture].Descriptor); + uboUpdate.AddWriteInfo (mat.descriptorSet, descLayoutTextures.Bindings[2], helmet.textures[(int)mat.occlusionTexture].Descriptor); + uboUpdate.Update (); + } + } + + helmet.PipelineLayout = pipelineLayout; + } + void updateMatrices () { + matrices.projection = Matrix4x4.CreatePerspectiveFieldOfView (Utils.DegreesToRadians (60f), (float)swapChain.Width / (float)swapChain.Height, 0.01f, 1024.0f); + //matrices.view = Matrix4x4.CreateLookAt (new Vector3 (0, 0, -1), new Vector3 (0, 0, 0), Vector3.UnitY);//Matrix4x4.CreateTranslation (0, 0, -2.5f); + matrices.view = Matrix4x4.CreateTranslation (0, 0, -2.5f * zoom); + matrices.model = + Matrix4x4.CreateFromAxisAngle (Vector3.UnitX, rotX) * + Matrix4x4.CreateFromAxisAngle (Vector3.UnitY, rotY) * + Matrix4x4.CreateFromAxisAngle (Vector3.UnitZ, rotZ); + + uboMats.Update (matrices, (uint)Marshal.SizeOf<Matrices> ()); + } + + public override void Update () { + updateMatrices (); + updateRequested = false; + } + protected override void onMouseMove (double xPos, double yPos) { + double diffX = lastMouseX - xPos; + double diffY = lastMouseY - yPos; + if (MouseButton[0]) { + rotY -= rotSpeed * (float)diffX; + rotX -= rotSpeed * (float)diffY; + } else if (MouseButton[1]) { + zoom += zoomSpeed * (float)diffY; + } + lastMouseX = xPos; + lastMouseY = yPos; + + updateRequested = true; + } + + protected override void Dispose (bool disposing) { pipeline.Destroy (); - pipelineLayout.Destroy (); - dsLayout.Destroy (); + pipelineLayout.Dispose (); + descLayoutMatrix.Dispose (); + descLayoutTextures.Dispose (); for (int i = 0; i < swapChain.ImageCount; i++) frameBuffers[i].Destroy (); descriptorPool.Destroy (); diff --git a/samples/Model/shaders/model.frag b/samples/Model/shaders/model.frag index 73dfd79..e241ebe 100644 --- a/samples/Model/shaders/model.frag +++ b/samples/Model/shaders/model.frag @@ -3,13 +3,69 @@ #extension GL_ARB_separate_shader_objects : enable #extension GL_ARB_shading_language_420pack : enable -layout (binding = 1) uniform sampler2D samplerColor; +layout (set = 1, binding = 0) uniform sampler2D samplerColor; +layout (set = 1, binding = 1) uniform sampler2D samplerNormal; +layout (set = 1, binding = 2) uniform sampler2D samplerOcclusion; layout (location = 0) in vec2 inUV; +layout (location = 1) in vec3 inN; +layout (location = 2) in vec3 inV;//ViewDir layout (location = 0) out vec4 outFragColor; +vec3 light = vec3(1.0,.0,1.0); + +// http://www.thetenthplanet.de/archives/1180 +mat3 cotangent_frame(vec3 N, vec3 p, vec2 uv) +{ + // get edge vectors of the pixel triangle + vec3 dp1 = dFdx( p ); + vec3 dp2 = dFdy( p ); + vec2 duv1 = dFdx( uv ); + vec2 duv2 = dFdy( uv ); + + // solve the linear system + vec3 dp2perp = cross( dp2, N ); + vec3 dp1perp = cross( N, dp1 ); + vec3 T = dp2perp * duv1.x + dp1perp * duv2.x; + vec3 B = dp2perp * duv1.y + dp1perp * duv2.y; + + // construct a scale-invariant frame + float invmax = inversesqrt( max( dot(T,T), dot(B,B) ) ); + return mat3( T * invmax, B * invmax, N ); +} + +vec3 perturb_normal( vec3 N, vec3 V, vec2 texcoord ) +{ + // assume N, the interpolated vertex normal and + // V, the view vector (vertex to eye) + vec3 map = texture(samplerNormal, texcoord).xyz; + map = map * 255./127. - 128./127.; + mat3 TBN = cotangent_frame(N, -V, texcoord); + return normalize(TBN * map); +} + + + void main() { - outFragColor = texture(samplerColor, inUV); + vec4 diff = texture(samplerColor, inUV); + + vec3 n = normalize(inN); + vec3 l = normalize(light); + vec3 pn = perturb_normal(n, inV, inUV); + + float lambert = max(0.0, dot(pn, l)); + + diff.rgb *= lambert * texture(samplerOcclusion, inUV).rgb; + vec3 spec = vec3(0); + vec3 amb = vec3(0.1); + if (lambert >= 0.0) { + vec3 rd = reflect(-l, pn); + float s = dot(rd, normalize(inV)); + spec = vec3(0.9,0.9,0.9)* //light.specular.xyz * material.specular.xyz * + pow(max(0.0, s), 10.0); //0.5 = mat shininess + } + + outFragColor = vec4(diff.rgb + amb + spec , diff.a); // }
\ No newline at end of file diff --git a/samples/Model/shaders/model.vert b/samples/Model/shaders/model.vert index b1329ef..37b83d7 100644 --- a/samples/Model/shaders/model.vert +++ b/samples/Model/shaders/model.vert @@ -15,15 +15,30 @@ layout (binding = 0) uniform UBO } ubo; layout (location = 0) out vec2 outUV; +layout (location = 1) out vec3 outN; +layout (location = 2) out vec3 outV;//ViewDir out gl_PerVertex { vec4 gl_Position; }; +layout(push_constant) uniform PushConsts { + mat4 model; +} pc; void main() { - outUV = inUV; - gl_Position = ubo.projectionMatrix * ubo.viewMatrix * ubo.modelMatrix * vec4(inPos.xyz, 1.0); + outUV = inUV; + + mat4 mod = ubo.modelMatrix * pc.model; + vec4 pos = mod * vec4(inPos.xyz, 1.0); + + //outN = normalize(transpose(inverse(mat3(mod))) * inNormal); + outN = normalize(mat3(mod)* inNormal); + + mat4 viewInv = inverse(ubo.viewMatrix); + outV = -(ubo.viewMatrix * pos).xyz;//normalize(vec3(viewInv * vec4(0.0, 0.0, 0.0, 1.0) - pos)); + + gl_Position = ubo.projectionMatrix * ubo.viewMatrix * pos; } |