Skip to content

Commit

Permalink
添加 displacement ,效果不对
Browse files Browse the repository at this point in the history
  • Loading branch information
luxiaodong committed Aug 30, 2022
1 parent 608ffd5 commit c0b3ed9
Show file tree
Hide file tree
Showing 15 changed files with 553 additions and 2 deletions.
14 changes: 14 additions & 0 deletions Vulkan/Vulkan_Sample/Vulkan.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
B0272D8128B71F82002D3602 /* ssao.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B0272D7A28B70A45002D3602 /* ssao.cpp */; };
B0272D8628B899F9002D3602 /* computershader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B0272D8428B899F9002D3602 /* computershader.cpp */; };
B0272DD728BDA078002D3602 /* geometryshader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B0272DD628BDA078002D3602 /* geometryshader.cpp */; };
B0272DDC28BDAC49002D3602 /* displacement.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B0272DDA28BDAC49002D3602 /* displacement.cpp */; };
B066A145286213A600763515 /* assets in CopyFiles */ = {isa = PBXBuildFile; fileRef = B066A1422862125900763515 /* assets */; };
B066DE1E28A4FF5D00726A95 /* thread.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B066DE1D28A4FF5D00726A95 /* thread.cpp */; };
B066DE2328A5074600726A95 /* multithread.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B066DE2128A5074600726A95 /* multithread.cpp */; };
Expand Down Expand Up @@ -117,6 +118,8 @@
B0272D8528B899F9002D3602 /* computershader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = computershader.h; sourceTree = "<group>"; };
B0272DD528BDA078002D3602 /* geometryshader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = geometryshader.h; sourceTree = "<group>"; };
B0272DD628BDA078002D3602 /* geometryshader.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = geometryshader.cpp; sourceTree = "<group>"; };
B0272DDA28BDAC49002D3602 /* displacement.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = displacement.cpp; sourceTree = "<group>"; };
B0272DDB28BDAC49002D3602 /* displacement.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = displacement.h; sourceTree = "<group>"; };
B066A1422862125900763515 /* assets */ = {isa = PBXFileReference; lastKnownFileType = folder; path = assets; sourceTree = "<group>"; };
B066DE1C28A4FF5D00726A95 /* thread.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = thread.h; sourceTree = "<group>"; };
B066DE1D28A4FF5D00726A95 /* thread.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = thread.cpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -332,6 +335,15 @@
path = geometryshader;
sourceTree = "<group>";
};
B0272DD928BDAC49002D3602 /* displacement */ = {
isa = PBXGroup;
children = (
B0272DDA28BDAC49002D3602 /* displacement.cpp */,
B0272DDB28BDAC49002D3602 /* displacement.h */,
);
path = displacement;
sourceTree = "<group>";
};
B066DE2028A5074600726A95 /* multithread */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -761,6 +773,7 @@
B0E13A182861729300D1D2B6 /* sample */ = {
isa = PBXGroup;
children = (
B0272DD928BDAC49002D3602 /* displacement */,
B0272DD428BDA078002D3602 /* geometryshader */,
B0272D8328B899F9002D3602 /* computershader */,
B0272D7828B70A45002D3602 /* ssao */,
Expand Down Expand Up @@ -887,6 +900,7 @@
B0A47F1F282E4D6200209D51 /* main.cpp in Sources */,
B0B5D1222890D870003A175D /* gltfscenerendering.cpp in Sources */,
B0A010832897DD0D006A339B /* shadowmappingcascade.cpp in Sources */,
B0272DDC28BDAC49002D3602 /* displacement.cpp in Sources */,
B0272D7128B4C609002D3602 /* deferredmutisampling.cpp in Sources */,
B0B5D0202875293B003A175D /* camera.cpp in Sources */,
B066DE2728A5FDD800726A95 /* frustum.cpp in Sources */,
Expand Down
140 changes: 140 additions & 0 deletions Vulkan/Vulkan_Sample/Vulkan/assets/models/displacement_plane.gltf
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
{
"asset" : {
"generator" : "Khronos glTF Blender I/O v1.2.75",
"version" : "2.0"
},
"scene" : 0,
"scenes" : [
{
"name" : "Scene",
"nodes" : [
0
]
}
],
"nodes" : [
{
"mesh" : 0,
"name" : "plane",
"rotation" : [
0.7071068286895752,
0,
0,
0.7071067094802856
]
}
],
"materials" : [
{
"doubleSided" : true,
"emissiveFactor" : [
0,
0,
0
],
"name" : "Default OBJ",
"pbrMetallicRoughness" : {
"baseColorFactor" : [
0.800000011920929,
0.800000011920929,
0.800000011920929,
1
],
"metallicFactor" : 0,
"roughnessFactor" : 0.5
}
}
],
"meshes" : [
{
"name" : "plane",
"primitives" : [
{
"attributes" : {
"POSITION" : 0,
"NORMAL" : 1,
"TANGENT" : 2,
"TEXCOORD_0" : 3
},
"indices" : 4,
"material" : 0
}
]
}
],
"accessors" : [
{
"bufferView" : 0,
"componentType" : 5126,
"count" : 25,
"max" : [
2.5,
2.5,
-2.046362990897662e-14
],
"min" : [
-2.5,
-2.5,
-2.046362990897662e-14
],
"type" : "VEC3"
},
{
"bufferView" : 1,
"componentType" : 5126,
"count" : 25,
"type" : "VEC3"
},
{
"bufferView" : 2,
"componentType" : 5126,
"count" : 25,
"type" : "VEC4"
},
{
"bufferView" : 3,
"componentType" : 5126,
"count" : 25,
"type" : "VEC2"
},
{
"bufferView" : 4,
"componentType" : 5123,
"count" : 96,
"type" : "SCALAR"
}
],
"bufferViews" : [
{
"buffer" : 0,
"byteLength" : 300,
"byteOffset" : 0
},
{
"buffer" : 0,
"byteLength" : 300,
"byteOffset" : 300
},
{
"buffer" : 0,
"byteLength" : 400,
"byteOffset" : 600
},
{
"buffer" : 0,
"byteLength" : 200,
"byteOffset" : 1000
},
{
"buffer" : 0,
"byteLength" : 192,
"byteOffset" : 1200
}
],
"buffers" : [
{
"byteLength" : 1392,
"uri" : "data:application/octet-stream;base64,AAAgwAAAoD/rUbioAAAgwAAAIEDrUbioAACgvwAAoD/rUbioAACgvwAAIEDrUbioAAAAAAAAoD/rUbioAAAAAAAAIEDrUbioAACgPwAAoD/rUbioAACgPwAAIEDrUbioAAAgQAAAoD/rUbioAAAgQAAAIEDrUbioAAAgwPLne53rUbioAACgv/Lne53rUbioAAAAAPLne53rUbioAACgP/Lne53rUbioAAAgQPLne53rUbioAAAgwAAAoL/rUbioAACgvwAAoL/rUbioAAAAAAAAoL/rUbioAACgPwAAoL/rUbioAAAgQAAAoL/rUbioAAAgwAAAIMDrUbioAACgvwAAIMDrUbioAAAAAAAAIMDrUbioAACgPwAAIMDrUbioAAAgQAAAIMDrUbioAAAAAAAAAAAAAIC/AAAAAAAAAAAAAIC/AAAAAAAAAAAAAIC/AAAAAAAAAAAAAIC/AAAAAAAAAAAAAIC/AAAAAAAAAAAAAIC/AAAAAAAAAAAAAIC/AAAAAAAAAAAAAIC/AAAAAAAAAAAAAIC/AAAAAAAAAAAAAIC/AAAAAAAAAAAAAIC/AAAAAAAAAAAAAIC/AAAAAAAAAAAAAIC/AAAAAAAAAAAAAIC/AAAAAAAAAAAAAIC/AAAAAAAAAAAAAIC/AAAAAAAAAAAAAIC/AAAAAAAAAAAAAIC/AAAAAAAAAAAAAIC/AAAAAAAAAAAAAIC/AAAAAAAAAAAAAIC/AAAAAAAAAAAAAIC/AAAAAAAAAAAAAIC/AAAAAAAAAAAAAIC/AAAAAAAAAAAAAIC/AACAv48GALQAAACAAACAPwAAgL8AAAAAAAAAgAAAgD8AAIC/NQigtAAAAIAAAIA/AACAv0RgVbQAAACAAACAPwAAgL82CKC0AAAAgAAAgD8AAIC/AAAAAAAAAIAAAIA/AACAv9MJwDQAAACAAACAPwAAgL86YFU0AAAAgAAAgD8AAIC/MQggNQAAAIAAAIA/AACAvwAAAAAAAACAAACAPwAAgL8sXAW1AAAAgAAAgD8AAIC/2glAtAAAAIAAAIA/AACAv0hg1bMAAACAAACAPwAAgL9qsyo0AAAAgAAAgD8AAIC/+1wVNQAAAIAAAIA/AACAv+ph9bQAAACAAACAPwAAgL9jB5C0AAAAgAAAgD8AAIC/AAAAAAAAAIAAAIA/AACAv2uzqrMAAACAAACAPwAAgL+0WLepAAAAgAAAgD8AAIC/ZbMqtAAAAIAAAIA/AACAv9cJQLQAAACAAACAPwAAgL8AAAAAAAAAgAAAgD8AAIC/a7OqswAAAIAAAIA/AACAv2qzKrQAAACAAACAPyX2/z+42H++Jfb/P1jsf78S+58/uNh/vhD7nz9Y7H+/9v//PpDYf77w//8+WOx/v7TYf76Q2H++wNh/vljsf79S7H+/uNh/vlTsf79Y7H+/Jfb/P/r//z4S+58/AwAAP/b//z4DAAA/sNh/vgMAAD9S7H+/+v//Pin2/z8S+58/EvufPxb7nz/5//8+FvufP6jYf74W+58/Tux/vxf7nz8p9v8/Kvb/PxP7nz8r9v8/////Piv2/z+o2H++K/b/P07sf78s9v8/AAABAAIAAwACAAEAAgADAAQABQAEAAMABAAFAAYABwAGAAUABgAHAAgACQAIAAcACgAAAAsAAgALAAAACwACAAwABAAMAAIADAAEAA0ABgANAAQADQAGAA4ACAAOAAYADwAKABAACwAQAAoAEAALABEADAARAAsAEQAMABIADQASAAwAEgANABMADgATAA0AFAAPABUAEAAVAA8AFQAQABYAEQAWABAAFgARABcAEgAXABEAFwASABgAEwAYABIA"
}
]
}
26 changes: 26 additions & 0 deletions Vulkan/Vulkan_Sample/Vulkan/assets/shaders/displacement/base.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#version 450

layout (binding = 2) uniform sampler2D colorMap;

layout (location = 0) in vec3 inNormal;
layout (location = 1) in vec2 inUV;
layout (location = 2) in vec3 inEyePos;
layout (location = 3) in vec3 inLightVec;

layout (location = 0) out vec4 outFragColor;

void main()
{
vec3 N = normalize(inNormal);
vec3 L = normalize(vec3(1.0));

outFragColor.rgb = texture(colorMap, inUV).rgb;

vec3 Eye = normalize(-inEyePos);
vec3 Reflected = normalize(reflect(-inLightVec, inNormal));

vec4 IAmbient = vec4(0.0, 0.0, 0.0, 1.0);
vec4 IDiffuse = vec4(1.0) * max(dot(inNormal, inLightVec), 0.0);

outFragColor = vec4((IAmbient + IDiffuse) * vec4(texture(colorMap, inUV).rgb, 1.0));
}
Binary file not shown.
15 changes: 15 additions & 0 deletions Vulkan/Vulkan_Sample/Vulkan/assets/shaders/displacement/base.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#version 450

layout (location = 0) in vec3 inPos;
layout (location = 1) in vec3 inNormal;
layout (location = 2) in vec2 inUV;

layout (location = 0) out vec3 outNormal;
layout (location = 1) out vec2 outUV;

void main(void)
{
gl_Position = vec4(inPos.xyz, 1.0);
outUV = inUV;
outNormal = inNormal;
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#version 450

layout (binding = 0) uniform UBO
{
float tessLevel;
} ubo;

layout (vertices = 3) out;

layout (location = 0) in vec3 inNormal[];
layout (location = 1) in vec2 inUV[];

layout (location = 0) out vec3 outNormal[3];
layout (location = 1) out vec2 outUV[3];

void main()
{
if (gl_InvocationID == 0)
{
gl_TessLevelInner[0] = ubo.tessLevel;
gl_TessLevelOuter[0] = ubo.tessLevel;
gl_TessLevelOuter[1] = ubo.tessLevel;
gl_TessLevelOuter[2] = ubo.tessLevel;
}

gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position;
outNormal[gl_InvocationID] = inNormal[gl_InvocationID];
outUV[gl_InvocationID] = inUV[gl_InvocationID];
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#version 450

layout (binding = 1) uniform UBO
{
mat4 projection;
mat4 modelview;
vec4 lightPos;
float tessAlpha;
float tessStrength;
} ubo;

layout (binding = 2) uniform sampler2D displacementMap;

layout(triangles, equal_spacing, cw) in;

layout (location = 0) in vec3 inNormal[];
layout (location = 1) in vec2 inUV[];

layout (location = 0) out vec3 outNormal;
layout (location = 1) out vec2 outUV;
layout (location = 2) out vec3 outEyesPos;
layout (location = 3) out vec3 outLightVec;

void main()
{
gl_Position = (gl_TessCoord.x * gl_in[0].gl_Position) + (gl_TessCoord.y * gl_in[1].gl_Position) + (gl_TessCoord.z * gl_in[2].gl_Position);
outUV = gl_TessCoord.x * inUV[0] + gl_TessCoord.y * inUV[1] + gl_TessCoord.z * inUV[2];
outNormal = gl_TessCoord.x * inNormal[0] + gl_TessCoord.y * inNormal[1] + gl_TessCoord.z * inNormal[2];

gl_Position.xyz += normalize(outNormal) * (max(textureLod(displacementMap, outUV.st, 0.0).a, 0.0) * ubo.tessStrength);

outEyesPos = (gl_Position).xyz;
outLightVec = normalize(ubo.lightPos.xyz - outEyesPos);

gl_Position = ubo.projection * ubo.modelview * gl_Position;
}
Binary file not shown.
Binary file not shown.
5 changes: 4 additions & 1 deletion Vulkan/Vulkan_Sample/Vulkan/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "sample/ssao/ssao.h"
#include "sample/computershader/computershader.h"
#include "sample/geometryshader/geometryshader.h"
#include "sample/displacement/displacement.h"

int main(int argc, const char * argv[])
{
Expand Down Expand Up @@ -84,7 +85,9 @@ int main(int argc, const char * argv[])
// DeferredShadows app("deferredshadows");
// DeferredSsao app("ssao");
// ComputerShader app("computershader");
GeometryShader app("geometryshader");
// GeometryShader app("geometryshader");

Displacement app("displacement");

try {
app.run();
Expand Down
Loading

0 comments on commit c0b3ed9

Please sign in to comment.