Skip to content

Commit

Permalink
添加 Curved PN Triangles
Browse files Browse the repository at this point in the history
  • Loading branch information
luxiaodong committed Sep 6, 2022
1 parent 5c5a5db commit 4f0034a
Show file tree
Hide file tree
Showing 20 changed files with 748 additions and 1 deletion.
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 @@ -16,6 +16,7 @@
B0272DD728BDA078002D3602 /* geometryshader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B0272DD628BDA078002D3602 /* geometryshader.cpp */; };
B0272DDC28BDAC49002D3602 /* displacement.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B0272DDA28BDAC49002D3602 /* displacement.cpp */; };
B0272E3D28C33A24002D3602 /* terraintessellation.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B0272E3B28C33A24002D3602 /* terraintessellation.cpp */; };
B0272E4228C6E773002D3602 /* curvedpntriangles.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B0272E4028C6E773002D3602 /* curvedpntriangles.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 @@ -123,6 +124,8 @@
B0272DDB28BDAC49002D3602 /* displacement.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = displacement.h; sourceTree = "<group>"; };
B0272E3B28C33A24002D3602 /* terraintessellation.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = terraintessellation.cpp; sourceTree = "<group>"; };
B0272E3C28C33A24002D3602 /* terraintessellation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = terraintessellation.h; sourceTree = "<group>"; };
B0272E4028C6E773002D3602 /* curvedpntriangles.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = curvedpntriangles.cpp; sourceTree = "<group>"; };
B0272E4128C6E773002D3602 /* curvedpntriangles.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = curvedpntriangles.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 @@ -356,6 +359,15 @@
path = terraintessellation;
sourceTree = "<group>";
};
B0272E3F28C6E773002D3602 /* curvedpntriangles */ = {
isa = PBXGroup;
children = (
B0272E4028C6E773002D3602 /* curvedpntriangles.cpp */,
B0272E4128C6E773002D3602 /* curvedpntriangles.h */,
);
path = curvedpntriangles;
sourceTree = "<group>";
};
B066DE2028A5074600726A95 /* multithread */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -785,6 +797,7 @@
B0E13A182861729300D1D2B6 /* sample */ = {
isa = PBXGroup;
children = (
B0272E3F28C6E773002D3602 /* curvedpntriangles */,
B0272E3A28C33A24002D3602 /* terraintessellation */,
B0272DD928BDAC49002D3602 /* displacement */,
B0272DD428BDA078002D3602 /* geometryshader */,
Expand Down Expand Up @@ -957,6 +970,7 @@
B0B5D02C28753328003A175D /* gltfLoader.cpp in Sources */,
B0272E3D28C33A24002D3602 /* terraintessellation.cpp in Sources */,
B0B5D0862876AC3F003A175D /* checkheader.c in Sources */,
B0272E4228C6E773002D3602 /* curvedpntriangles.cpp in Sources */,
B0B5D110288A8CCA003A175D /* gltfloading.cpp in Sources */,
B0B5D0A328794562003A175D /* vertex.cpp in Sources */,
B0B5D07D2876AB40003A175D /* texture.c in Sources */,
Expand Down
166 changes: 166 additions & 0 deletions Vulkan/Vulkan_Sample/Vulkan/assets/models/deer.gltf

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions Vulkan/Vulkan_Sample/Vulkan/assets/shaders/tessellation/base.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#version 450

layout (set = 1, binding = 0) uniform sampler2D samplerColorMap;

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

layout (location = 0) out vec4 outFragColor;

void main()
{
vec3 N = normalize(inNormal);
vec3 L = normalize(vec3(-4.0, -4.0, 0.0));

vec4 color = texture(samplerColorMap, inUV);

outFragColor.rgb = vec3(clamp(max(dot(N,L), 0.0), 0.2, 1.0)) * color.rgb;
}
Binary file not shown.
15 changes: 15 additions & 0 deletions Vulkan/Vulkan_Sample/Vulkan/assets/shaders/tessellation/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);
outNormal = inNormal;
outUV = inUV;
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#version 450

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(void)
{
if (gl_InvocationID == 0)
{
gl_TessLevelInner[0] = 1.0;
gl_TessLevelOuter[0] = 1.0;
gl_TessLevelOuter[1] = 1.0;
gl_TessLevelOuter[2] = 1.0;
}

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,27 @@
#version 450

layout (triangles, fractional_odd_spacing, cw) in;

layout (binding = 1) uniform UBO
{
mat4 projection;
mat4 model;
float tessAlpha;
} ubo;

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

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

void main(void)
{
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);
gl_Position = ubo.projection * ubo.model * gl_Position;

outNormal = gl_TessCoord.x*inNormal[0] + gl_TessCoord.y*inNormal[1] + gl_TessCoord.z*inNormal[2];
outUV = gl_TessCoord.x*inUV[0] + gl_TessCoord.y*inUV[1] + gl_TessCoord.z*inUV[2];
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#version 450

// PN patch data
struct PnPatch
{
float b210;
float b120;
float b021;
float b012;
float b102;
float b201;
float b111;
float n110;
float n011;
float n101;
};

// tessellation levels
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 = 3) out vec2 outUV[3];
layout(location = 6) out PnPatch outPatch[3];

float wij(int i, int j)
{
return dot(gl_in[j].gl_Position.xyz - gl_in[i].gl_Position.xyz, inNormal[i]);
}

float vij(int i, int j)
{
vec3 Pj_minus_Pi = gl_in[j].gl_Position.xyz
- gl_in[i].gl_Position.xyz;
vec3 Ni_plus_Nj = inNormal[i]+inNormal[j];
return 2.0*dot(Pj_minus_Pi, Ni_plus_Nj)/dot(Pj_minus_Pi, Pj_minus_Pi);
}

void main()
{
// get data
gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position;
outNormal[gl_InvocationID] = inNormal[gl_InvocationID];
outUV[gl_InvocationID] = inUV[gl_InvocationID];

// set base
float P0 = gl_in[0].gl_Position[gl_InvocationID];
float P1 = gl_in[1].gl_Position[gl_InvocationID];
float P2 = gl_in[2].gl_Position[gl_InvocationID];
float N0 = inNormal[0][gl_InvocationID];
float N1 = inNormal[1][gl_InvocationID];
float N2 = inNormal[2][gl_InvocationID];

// compute control points
outPatch[gl_InvocationID].b210 = (2.0*P0 + P1 - wij(0,1)*N0)/3.0;
outPatch[gl_InvocationID].b120 = (2.0*P1 + P0 - wij(1,0)*N1)/3.0;
outPatch[gl_InvocationID].b021 = (2.0*P1 + P2 - wij(1,2)*N1)/3.0;
outPatch[gl_InvocationID].b012 = (2.0*P2 + P1 - wij(2,1)*N2)/3.0;
outPatch[gl_InvocationID].b102 = (2.0*P2 + P0 - wij(2,0)*N2)/3.0;
outPatch[gl_InvocationID].b201 = (2.0*P0 + P2 - wij(0,2)*N0)/3.0;
float E = ( outPatch[gl_InvocationID].b210
+ outPatch[gl_InvocationID].b120
+ outPatch[gl_InvocationID].b021
+ outPatch[gl_InvocationID].b012
+ outPatch[gl_InvocationID].b102
+ outPatch[gl_InvocationID].b201 ) / 6.0;
float V = (P0 + P1 + P2)/3.0;
outPatch[gl_InvocationID].b111 = E + (E - V)*0.5;
outPatch[gl_InvocationID].n110 = N0+N1-vij(0,1)*(P1-P0);
outPatch[gl_InvocationID].n011 = N1+N2-vij(1,2)*(P2-P1);
outPatch[gl_InvocationID].n101 = N2+N0-vij(2,0)*(P0-P2);

// set tess levels
gl_TessLevelOuter[gl_InvocationID] = ubo.tessLevel;
gl_TessLevelInner[0] = ubo.tessLevel;
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#version 450

// PN patch data
struct PnPatch
{
float b210;
float b120;
float b021;
float b012;
float b102;
float b201;
float b111;
float n110;
float n011;
float n101;
};

layout (binding = 1) uniform UBO
{
mat4 projection;
mat4 model;
float tessAlpha;
} ubo;

layout(triangles, fractional_odd_spacing, cw) in;

layout(location = 0) in vec3 iNormal[];
layout(location = 3) in vec2 iTexCoord[];
layout(location = 6) in PnPatch iPnPatch[];

layout(location = 0) out vec3 oNormal;
layout(location = 1) out vec2 oTexCoord;

#define uvw gl_TessCoord

void main()
{
vec3 uvwSquared = uvw * uvw;
vec3 uvwCubed = uvwSquared * uvw;

// extract control points
vec3 b210 = vec3(iPnPatch[0].b210, iPnPatch[1].b210, iPnPatch[2].b210);
vec3 b120 = vec3(iPnPatch[0].b120, iPnPatch[1].b120, iPnPatch[2].b120);
vec3 b021 = vec3(iPnPatch[0].b021, iPnPatch[1].b021, iPnPatch[2].b021);
vec3 b012 = vec3(iPnPatch[0].b012, iPnPatch[1].b012, iPnPatch[2].b012);
vec3 b102 = vec3(iPnPatch[0].b102, iPnPatch[1].b102, iPnPatch[2].b102);
vec3 b201 = vec3(iPnPatch[0].b201, iPnPatch[1].b201, iPnPatch[2].b201);
vec3 b111 = vec3(iPnPatch[0].b111, iPnPatch[1].b111, iPnPatch[2].b111);

// extract control normals
vec3 n110 = normalize(vec3(iPnPatch[0].n110, iPnPatch[1].n110, iPnPatch[2].n110));
vec3 n011 = normalize(vec3(iPnPatch[0].n011, iPnPatch[1].n011, iPnPatch[2].n011));
vec3 n101 = normalize(vec3(iPnPatch[0].n101, iPnPatch[1].n101, iPnPatch[2].n101));

// compute texcoords
oTexCoord = gl_TessCoord[2]*iTexCoord[0] + gl_TessCoord[0]*iTexCoord[1] + gl_TessCoord[1]*iTexCoord[2];

// normal
// Barycentric normal
vec3 barNormal = gl_TessCoord[2]*iNormal[0] + gl_TessCoord[0]*iNormal[1] + gl_TessCoord[1]*iNormal[2];
vec3 pnNormal = iNormal[0]*uvwSquared[2] + iNormal[1]*uvwSquared[0] + iNormal[2]*uvwSquared[1]
+ n110*uvw[2]*uvw[0] + n011*uvw[0]*uvw[1]+ n101*uvw[2]*uvw[1];
oNormal = ubo.tessAlpha*pnNormal + (1.0-ubo.tessAlpha) * barNormal;

// compute interpolated pos
vec3 barPos = gl_TessCoord[2]*gl_in[0].gl_Position.xyz
+ gl_TessCoord[0]*gl_in[1].gl_Position.xyz
+ gl_TessCoord[1]*gl_in[2].gl_Position.xyz;

// save some computations
uvwSquared *= 3.0;

// compute PN position
vec3 pnPos = gl_in[0].gl_Position.xyz*uvwCubed[2]
+ gl_in[1].gl_Position.xyz*uvwCubed[0]
+ gl_in[2].gl_Position.xyz*uvwCubed[1]
+ b210*uvwSquared[2]*uvw[0]
+ b120*uvwSquared[0]*uvw[2]
+ b201*uvwSquared[2]*uvw[1]
+ b021*uvwSquared[0]*uvw[1]
+ b102*uvwSquared[1]*uvw[2]
+ b012*uvwSquared[1]*uvw[0]
+ b111*6.0*uvw[0]*uvw[1]*uvw[2];

// final position and normal
vec3 finalPos = (1.0-ubo.tessAlpha)*barPos + ubo.tessAlpha*pnPos;
gl_Position = ubo.projection * ubo.model * vec4(finalPos,1.0);
}
Binary file not shown.
4 changes: 3 additions & 1 deletion Vulkan/Vulkan_Sample/Vulkan/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "sample/geometryshader/geometryshader.h"
#include "sample/displacement/displacement.h"
#include "sample/terraintessellation/terraintessellation.h"
#include "sample/curvedpntriangles/curvedpntriangles.h"

int main(int argc, const char * argv[])
{
Expand Down Expand Up @@ -88,8 +89,9 @@ int main(int argc, const char * argv[])
// ComputerShader app("computershader");
// GeometryShader app("geometryshader");
// Displacement app("displacement");
// TerrainTessellation app("terraintessellation");

TerrainTessellation app("terraintessellation");
CurvedPnTriangles app("curvedpntriangles");

try {
app.run();
Expand Down
Loading

0 comments on commit 4f0034a

Please sign in to comment.