Skip to content

Commit 7a78531

Browse files
Note to self: Remember to revert the non-triangle related stuff
1 parent 44a073e commit 7a78531

File tree

2 files changed

+30
-37
lines changed

2 files changed

+30
-37
lines changed

examples_tests/42.FragmentShaderPathTracer/common.glsl

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// basic settings
22
#define MAX_DEPTH 2
3-
#define SAMPLES 16
3+
#define SAMPLES 1
44

55
// firefly and variance reduction techniques
66
//#define KILL_DIFFUSE_SPECULAR_PATHS
@@ -84,59 +84,48 @@ float Sphere_getSolidAngle(in Sphere sphere, in vec3 origin)
8484

8585
struct Triangle
8686
{
87-
/*
8887
vec3 vertex0;
8988
uint bsdfLightIDs;
9089
vec3 vertex1;
91-
uint padding;
90+
uint padding0;
9291
vec3 vertex2;
93-
uint padding;
94-
*/
95-
vec4 planeEq;
96-
vec4 boundaryPlanes[2];
97-
float area;
98-
uint bsdfLightIDs;
92+
uint padding1;
9993
};
10094

10195
Triangle Triangle_Triangle(in mat3 vertices, in uint bsdfID, in uint lightID)
10296
{
103-
const vec3 edges[2] = vec3[2](vertices[1]-vertices[0],vertices[2]-vertices[0]);
104-
//
10597
Triangle tri;
106-
tri.planeEq.xyz = cross(edges[0],edges[1]);
107-
tri.boundaryPlanes[0].xyz = cross(tri.planeEq.xyz,edges[0]);
108-
tri.boundaryPlanes[0].xyz/= dot(tri.boundaryPlanes[0].xyz,edges[1]);
109-
tri.boundaryPlanes[1].xyz = cross(edges[1],tri.planeEq.xyz);
110-
tri.boundaryPlanes[1].xyz/= dot(tri.boundaryPlanes[1].xyz,edges[0]);
111-
//
112-
tri.planeEq.w = dot(tri.planeEq.xyz, vertices[0]);
113-
tri.boundaryPlanes[0].w = -dot(tri.boundaryPlanes[0].xyz, vertices[0]);
114-
tri.boundaryPlanes[1].w = -dot(tri.boundaryPlanes[1].xyz, vertices[0]);
115-
//
116-
tri.area = length(tri.planeEq.xyz)*0.5;
98+
tri.vertex0 = vertices[0];
99+
tri.vertex1 = vertices[1];
100+
tri.vertex2 = vertices[2];
117101
//
118102
tri.bsdfLightIDs = bitfieldInsert(bsdfID, lightID, 16, 16);
119103
return tri;
120104
}
121105

122-
123106
// return intersection distance if found, FLT_NAN otherwise
124107
float Triangle_intersect(in Triangle tri, in vec3 origin, in vec3 direction)
125108
{
126-
const float NdotD = dot(direction,tri.planeEq.xyz);
127-
const float t = (tri.planeEq.w-dot(origin,tri.planeEq.xyz))/NdotD;
128-
// speculative intersection
129-
const vec3 intersectionPoint = origin+direction*t;
130-
const float distToEdge[2] = float[2](
131-
dot(intersectionPoint,tri.boundaryPlanes[0].xyz)+tri.boundaryPlanes[0].w,
132-
dot(intersectionPoint,tri.boundaryPlanes[1].xyz)+tri.boundaryPlanes[1].w
133-
);
134-
return t<0.f||distToEdge[0]<0.f||distToEdge[1]<0.f||(distToEdge[0]+distToEdge[1])>1.f ? irr_glsl_FLT_NAN:t;
109+
const vec3 edges[2] = vec3[2](tri.vertex1-tri.vertex0,tri.vertex2-tri.vertex0);
110+
111+
const vec3 h = cross(direction,edges[1]);
112+
const float a = dot(edges[0],h);
113+
114+
const vec3 relOrigin = origin-tri.vertex0;
115+
116+
const float u = dot(relOrigin,h)/a;
117+
118+
const vec3 q = cross(relOrigin,edges[0]);
119+
const float v = dot(direction,q)/a;
120+
121+
const float t = dot(edges[1],q)/a;
122+
123+
return t>0.f&&u>=0.f&&v>=0.f&&(u+v)<=1.f ? t:irr_glsl_FLT_NAN;
135124
}
136125

137126
vec3 Triangle_getNormalTimesArea(in Triangle tri)
138127
{
139-
return tri.planeEq.xyz;
128+
return cross(tri.vertex1-tri.vertex0,tri.vertex2-tri.vertex0)*0.5;
140129
}
141130

142131

@@ -183,7 +172,7 @@ float BSDFNode_getMISWeight(in BSDFNode bsdf)
183172
{
184173
const float alpha = BSDFNode_getRoughness(bsdf);
185174
const bool notDiffuse = BSDFNode_isNotDiffuse(bsdf);
186-
const float DIFFUSE_MIS_WEIGHT = 0.5;
175+
const float DIFFUSE_MIS_WEIGHT = 0.0;
187176
return notDiffuse ? mix(1.0,DIFFUSE_MIS_WEIGHT,alpha):DIFFUSE_MIS_WEIGHT; // TODO: test alpha*alpha
188177
}
189178

@@ -311,7 +300,7 @@ void missProgram()
311300
vec2 uv = SampleSphericalMap(rayStack[stackPtr]._immutable.direction);
312301
finalContribution *= textureLod(envMap, uv, 0.0).rgb;
313302
#else
314-
const vec3 kConstantEnvLightRadiance = vec3(0.15,0.21,0.3);
303+
const vec3 kConstantEnvLightRadiance = vec3(0.0);// 0.15, 0.21, 0.3);
315304
finalContribution *= kConstantEnvLightRadiance;
316305
#endif
317306
}

examples_tests/42.FragmentShaderPathTracer/litByTriangle.frag

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ Sphere spheres[SPHERE_COUNT] = {
1616
};
1717
#define TRIANGLE_COUNT 1
1818
Triangle triangles[TRIANGLE_COUNT] = {
19-
Triangle_Triangle(mat3(vec3(-1.8,0.35,0.3),vec3(-1.2,0.35,0.0),vec3(-1.5,0.8,-0.3)),INVALID_ID_16BIT,0u)
19+
//Triangle_Triangle(mat3(vec3(-1.8,0.35,0.3),vec3(-1.2,0.35,0.0),vec3(-1.5,0.8,-0.3)),INVALID_ID_16BIT,0u)
20+
Triangle_Triangle(mat3(vec3(-4,0.7,-4),vec3(0.0,0.7,0.0),vec3(-4.0,0.8,4.0)),INVALID_ID_16BIT,0u)
2021
};
2122

2223

2324
#define LIGHT_COUNT 1
2425
Light lights[LIGHT_COUNT] = {
26+
//{vec3(30.0,25.0,15.0)*0.01,0u}
2527
{vec3(30.0,25.0,15.0),0u}
2628
};
2729

@@ -71,6 +73,7 @@ vec3 irr_glsl_light_deferred_eval_and_prob(out float pdf, in float intersectionT
7173
return Light_getRadiance(light);
7274
}
7375

76+
7477
irr_glsl_LightSample irr_glsl_light_generate_and_remainder_and_pdf(out vec3 remainder, out float pdf, out float newRayMaxT, in vec3 origin, in irr_glsl_AnisotropicViewSurfaceInteraction interaction, in vec3 u, in int depth)
7578
{
7679
// normally we'd pick from set of lights, using `u.z`
@@ -79,7 +82,8 @@ irr_glsl_LightSample irr_glsl_light_generate_and_remainder_and_pdf(out vec3 rema
7982

8083
const Triangle tri = triangles[Light_getObjectID(light)];
8184

82-
vec3 point = vec3(-1.5,0.5,0.0);//-inverse(transpose(mat3(tri.planeEq.xyz,tri.boundaryPlanes[0].xyz,tri.boundaryPlanes[1].xyz)))*vec3(tri.planeEq.w,-tri.boundaryPlanes[0].w,-tri.boundaryPlanes[1].w);
85+
const vec3 edges[2] = vec3[2](tri.vertex1-tri.vertex0,tri.vertex2-tri.vertex0);
86+
vec3 point = tri.vertex0+edges[0]*(1.0-u.y)+edges[1]*u.y*sqrt(u.x);
8387
vec3 L = point-origin;
8488

8589
const float distanceSq = dot(L,L);

0 commit comments

Comments
 (0)