Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 10475a6

Browse files
committed
[add] physically-rendering
1 parent d710460 commit 10475a6

File tree

13 files changed

+1263
-2
lines changed

13 files changed

+1263
-2
lines changed

‎README.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88

99
# 下载单一项目代码
1010

11-
Chrome安装 ``GitZip for github``
11+
Chrome安装 ``GitZip for github``插件
1212

1313
# 目录
1414

1515
内容 | 源代码位置 | 效果 |
16-
-|-|-|
16+
:-:|:-:|:-:|
17+
Physically Based Rendering(PBR基于物理渲染) | [源代码位置](./physically-rendering) | <img src="./result/PBR-OpenGL.png" width=200 >[[演示视频]](https://www.bilibili.com/video/BV1TV411z7qe) |
1718
Ray Tracing(光线追踪) | [源代码位置](./tinyraytracerYD) | <div align=center><img src="./result/tinyraytracer.jpg" width=200 ></div> |
1819
Software Rendering(软渲染) | [源代码位置](./tinyrendererYD) | <div align=center><img src="./result/africanhead.png" width=200 ></div> |
1920
Fluid Simulation(物理动画流体模拟) | [源代码位置](./melt-animation) | <div align=center><img src="./result/IISPH.gif" width=200 ></div> |

‎physically-rendering/background.fs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#version 330 core
2+
out vec4 FragColor;
3+
in vec3 WorldPos;
4+
5+
uniform samplerCube environmentMap;
6+
7+
void main()
8+
{
9+
vec3 envColor = textureLod(environmentMap, WorldPos, 0.0).rgb;
10+
11+
// HDR tonemap and gamma correct
12+
envColor = envColor / (envColor + vec3(1.0));
13+
envColor = pow(envColor, vec3(1.0/2.2));
14+
15+
FragColor = vec4(envColor, 1.0);
16+
}

‎physically-rendering/background.vs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#version 330 core
2+
layout (location = 0) in vec3 aPos;
3+
4+
uniform mat4 projection;
5+
uniform mat4 view;
6+
7+
out vec3 WorldPos;
8+
9+
void main()
10+
{
11+
WorldPos = aPos;
12+
13+
mat4 rotView = mat4(mat3(view));
14+
vec4 clipPos = projection * rotView * vec4(WorldPos, 1.0);
15+
16+
gl_Position = clipPos.xyww;
17+
}

‎physically-rendering/brdf.fs‎

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#version 330 core
2+
out vec2 FragColor;
3+
in vec2 TexCoords;
4+
5+
const float PI = 3.14159265359;
6+
7+
// Importance sampling
8+
float RadicalInverse_VdC(uint bits)
9+
{
10+
bits = (bits << 16u) | (bits >> 16u);
11+
bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u);
12+
bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u);
13+
bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u);
14+
bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u);
15+
return float(bits) * 2.3283064365386963e-10; // / 0x100000000
16+
}
17+
vec2 Hammersley(uint i, uint N)
18+
{
19+
return vec2(float(i)/float(N), RadicalInverse_VdC(i));
20+
}
21+
22+
vec3 ImportanceSampleGGX(vec2 Xi, vec3 N, float roughness)
23+
{
24+
float a = roughness*roughness;
25+
26+
float phi = 2.0 * PI * Xi.x;
27+
float cosTheta = sqrt((1.0 - Xi.y) / (1.0 + (a*a - 1.0) * Xi.y));
28+
float sinTheta = sqrt(1.0 - cosTheta*cosTheta);
29+
30+
vec3 H;
31+
H.x = cos(phi) * sinTheta;
32+
H.y = sin(phi) * sinTheta;
33+
H.z = cosTheta;
34+
35+
vec3 up = abs(N.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(1.0, 0.0, 0.0);
36+
vec3 tangent = normalize(cross(up, N));
37+
vec3 bitangent = cross(N, tangent);
38+
39+
vec3 sampleVec = tangent * H.x + bitangent * H.y + N * H.z;
40+
return normalize(sampleVec);
41+
}
42+
43+
float GeometrySchlickGGX(float NdotV, float roughness)
44+
{
45+
float a = roughness;
46+
float k = (a * a) / 2.0;
47+
48+
float nom = NdotV;
49+
float denom = NdotV * (1.0 - k) + k;
50+
51+
return nom / denom;
52+
}
53+
54+
float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
55+
{
56+
float NdotV = max(dot(N, V), 0.0);
57+
float NdotL = max(dot(N, L), 0.0);
58+
float ggx2 = GeometrySchlickGGX(NdotV, roughness);
59+
float ggx1 = GeometrySchlickGGX(NdotL, roughness);
60+
61+
return ggx1 * ggx2;
62+
}
63+
64+
vec2 IntegrateBRDF(float NdotV, float roughness)
65+
{
66+
vec3 V;
67+
V.x = sqrt(1.0 - NdotV*NdotV);
68+
V.y = 0.0;
69+
V.z = NdotV;
70+
71+
float A = 0.0;
72+
float B = 0.0;
73+
74+
vec3 N = vec3(0.0, 0.0, 1.0);
75+
76+
const uint SAMPLE_COUNT = 1024u;
77+
for(uint i = 0u; i < SAMPLE_COUNT; ++i)
78+
{
79+
vec2 Xi = Hammersley(i, SAMPLE_COUNT);
80+
vec3 H = ImportanceSampleGGX(Xi, N, roughness);
81+
vec3 L = normalize(2.0 * dot(V, H) * H - V);
82+
83+
float NdotL = max(L.z, 0.0);
84+
float NdotH = max(H.z, 0.0);
85+
float VdotH = max(dot(V, H), 0.0);
86+
87+
if(NdotL > 0.0)
88+
{
89+
float G = GeometrySmith(N, V, L, roughness);
90+
float G_Vis = (G * VdotH) / (NdotH * NdotV);
91+
float Fc = pow(1.0 - VdotH, 5.0);
92+
93+
A += (1.0 - Fc) * G_Vis;
94+
B += Fc * G_Vis;
95+
}
96+
}
97+
A /= float(SAMPLE_COUNT);
98+
B /= float(SAMPLE_COUNT);
99+
return vec2(A, B);
100+
}
101+
102+
void main()
103+
{
104+
vec2 integratedBRDF = IntegrateBRDF(TexCoords.x, TexCoords.y);
105+
FragColor = integratedBRDF;
106+
}

‎physically-rendering/brdf.vs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#version 330 core
2+
layout (location = 0) in vec3 aPos;
3+
layout (location = 1) in vec2 aTexCoords;
4+
5+
out vec2 TexCoords;
6+
7+
void main()
8+
{
9+
TexCoords = aTexCoords;
10+
gl_Position = vec4(aPos, 1.0);
11+
}

‎physically-rendering/cubemap.vs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#version 330 core
2+
layout (location = 0) in vec3 aPos;
3+
4+
out vec3 WorldPos;
5+
6+
uniform mat4 projection;
7+
uniform mat4 view;
8+
9+
void main()
10+
{
11+
WorldPos = aPos;
12+
gl_Position = projection * view * vec4(WorldPos, 1.0);
13+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#version 330 core
2+
out vec4 FragColor;
3+
in vec3 WorldPos;
4+
5+
uniform sampler2D equirectangularMap;
6+
7+
const vec2 invAtan = vec2(0.1591, 0.3183);
8+
vec2 SampleSphericalMap(vec3 v)
9+
{
10+
vec2 uv = vec2(atan(v.z, v.x), asin(v.y));
11+
uv *= invAtan;
12+
uv += 0.5;
13+
return uv;
14+
}
15+
16+
void main()
17+
{
18+
vec2 uv = SampleSphericalMap(normalize(WorldPos));
19+
vec3 color = texture(equirectangularMap, uv).rgb;
20+
21+
FragColor = vec4(color, 1.0);
22+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#version 330 core
2+
out vec4 FragColor;
3+
in vec3 WorldPos;
4+
5+
uniform samplerCube environmentMap;
6+
7+
const float PI = 3.14159265359;
8+
9+
void main()
10+
{
11+
vec3 N = normalize(WorldPos);
12+
vec3 irradiance = vec3(0.0);
13+
14+
// calculate coordinate
15+
vec3 up = vec3(0.0, 1.0, 0.0);
16+
vec3 right = cross(up, N);
17+
up = cross(N, right);
18+
19+
float sampleDelta = 0.025;
20+
float nrSamples = 0.0;
21+
for(float phi = 0.0; phi < 2.0 * PI; phi += sampleDelta)
22+
{
23+
for(float theta = 0.0; theta < 0.5 * PI; theta += sampleDelta)
24+
{
25+
vec3 tangentSample = vec3(sin(theta) * cos(phi), sin(theta) * sin(phi), cos(theta));
26+
vec3 sampleVec = tangentSample.x * right + tangentSample.y * up + tangentSample.z * N;
27+
irradiance += texture(environmentMap, sampleVec).rgb * cos(theta) * sin(theta);
28+
nrSamples++;
29+
}
30+
}
31+
32+
irradiance = PI * irradiance * (1.0 / float(nrSamples));
33+
FragColor = vec4(irradiance, 1.0);
34+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /