Skip to content

Lab2: Chang Liu #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,137 @@
# lab06-debugging

# Members
- Chang Liu

# Link
- [https://www.shadertoy.com/view/msKcRR](https://www.shadertoy.com/view/msKcRR)

# Bugs Found
See comments in code
```GLSL
void raycast(vec2 uv, out vec3 dir,
out vec3 eye, out vec3 ref) {
eye = rotateY(vec3(0.0, 0.0, 15.0), sin(iTime * 0.1) * 3.14159 * 0.5);
ref = vec3(0.0, 0.0, 0.0);

float len = tan(3.14159 * 0.125) * distance(eye, ref);
vec3 H = normalize(cross(vec3(0.0, 1.0, 0.0), ref - eye));
vec3 V = normalize(cross(H, eye - ref));
V *= len;

// ============================== bug2 ===================================
// the aspect ratio wasn't correct
// iResolution.x / iResolution.x ---> iResolution.x / iResolution.y

H *= len * iResolution.x / iResolution.y;
vec3 p = ref + uv.x * H + uv.y * V;
dir = normalize(p - eye);
}

void march(vec3 origin, vec3 dir, out float t, out int hitObj) {
t = 0.001;

// ============================== bug5 ===================================
// march more times so rays won't be terminated early

for(int i = 0; i < 256; ++i) {
vec3 pos = origin + t * dir;
float m;
sceneMap3D(pos, m, hitObj);
if(m < 0.01) {
return;
}
t += m;
}
t = -1.0;
hitObj = -1;
}


vec3 computeMaterial(int hitObj, vec3 p, vec3 d, vec3 n) {
switch(hitObj) {
case 0:
// Center sphere
return vec3(1.0, 0.67, 0.67);
break;
case 1:
// Back sphere
return vec3(0.67, 1.0, 0.67);
break;
case 2:
// Front sphere
return vec3(0.67, 0.67, 1.0);
break;
case 3:
// Floor
float t = floor(mod((sin(p.x) + sin(p.z)) * 0.5, 2.0));
return mix(vec3(0.7, 0.4, 0.2), vec3(1.0), t);
break;
case -1:
// Background
break;
}
return vec3(255.0, 229.0, 119.0) / 255.0;
}

Intersection sdf3D(vec3 dir, vec3 eye) {
float t;
int hitObj;
march(eye, dir, t, hitObj);

if(t == -1.0) {
return Intersection(t, skyColor(dir),
vec3(eye + 1000.0 * dir),
-1);
}

vec3 isect = eye + t * dir;
vec3 nor = computeNormal(isect);

vec3 material = computeMaterial(hitObj, isect, dir, nor);

// ============================== bug4 ===================================
// it was 'dir = reflect(eye, nor)', which was wrong

dir = reflect(dir, nor);
march(isect + dir * 0.01, dir, t, hitObj);
vec3 specReflCol;
if(hitObj == -1) {
specReflCol = skyColor(dir);
}
else {
vec3 isect2 = isect + t * dir;
nor = computeNormal(isect2);
specReflCol = computeMaterial(hitObj, isect2, dir, nor);
}
float fresnel = 1.0 - max(0.0, dot(normalize(eye - isect), nor));
fresnel = 0.25 + 0.75 * fresnel;
vec3 sdfColor = mix(material, specReflCol * material, fresnel);

return Intersection(t, sdfColor, isect, hitObj);
}

void mainImage( out vec4 fragColor, in vec2 fragCoord ) {
// Normalized pixel coordinates (from 0 to 1)
vec2 uv = fragCoord/iResolution.xy;
// [-1, 1]

// ============================== bug4 ===================================
// it was 'vec uv = ...', which caused a compilation error

vec2 uv2 = 2.0 * uv - vec2(1.0);

vec3 dir, eye, ref;

// ============================== bug3 ===================================
// uv2 instead of uv should be passed here

raycast(uv2, dir, eye, ref);
fragColor = vec4(sdf3D(dir, eye).color, 1.);

}
```

# Setup

Create a [Shadertoy account](https://www.shadertoy.com/). Either fork this shadertoy, or create a new shadertoy and copy the code from the [Debugging Puzzle](https://www.shadertoy.com/view/flGfRc).
Expand Down