Skip to content
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

Submission: Kangning Li #4

Open
wants to merge 39 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
d9aaa7e
added IQ's code which I am annotating for reference. Added some skele…
likangning93 Oct 17, 2015
4385340
producing a crappy sphere.
likangning93 Oct 18, 2015
a1a505f
circle slightly better
likangning93 Oct 18, 2015
9c7d4d3
added notes
likangning93 Oct 18, 2015
7748733
added color spec. need to test union and plane
likangning93 Oct 18, 2015
8651772
union working
likangning93 Oct 18, 2015
0035707
something is wrong with union, depth is exactly reversed.
likangning93 Oct 18, 2015
24b1c21
problem was just forgetting to break on first thing struck. need to d…
likangning93 Oct 18, 2015
df6eea0
fixed plane
likangning93 Oct 18, 2015
d44c4b3
cube seems to be working
likangning93 Oct 18, 2015
174944f
normals seem to be working
likangning93 Oct 18, 2015
d455d4d
added castRaySphere. could maybe use a little more testing
likangning93 Oct 18, 2015
21b96f5
added ambient and sky colors for pretty
likangning93 Oct 18, 2015
30cc3b5
something stinks with sphere tracing
Oct 19, 2015
565056e
updated notes
likangning93 Oct 19, 2015
38626fc
basic sins height map seems to be working, but is there something up …
likangning93 Oct 19, 2015
012961e
fixed lambert
likangning93 Oct 19, 2015
617e0af
added helper and skeleton notes for menger sponge
likangning93 Oct 19, 2015
4caf023
something was wrong with the helper and causing a lot of grief
likangning93 Oct 19, 2015
e7534ca
a first stage alone is not a fractal
likangning93 Oct 19, 2015
9f0a902
slow fractal is slow BUT SLOW FRACTAL IS STILL A FRACTAL
likangning93 Oct 19, 2015
6f60d2d
was missing a chunk of fractal. fixed
likangning93 Oct 19, 2015
7287796
set up a scene that seems to show a lot of stuff... preparing for sha…
likangning93 Oct 19, 2015
c173592
added another capping check so we can have fake sky
likangning93 Oct 19, 2015
8f7ac58
soft shadows now exist. not as pretty as they could be, but they exist.
likangning93 Oct 19, 2015
7418616
ambient occlusion is now a thing. not very pretty though. still need …
likangning93 Oct 19, 2015
4fdb08c
starting to use defines for stuff like epsilon and max depth
likangning93 Oct 19, 2015
b5ebc80
added a buttload of new defines and configurability. also added depth…
likangning93 Oct 19, 2015
beede46
added step debugging
likangning93 Oct 19, 2015
fa1a06c
added pictures
likangning93 Oct 19, 2015
12f3183
added acknowledgement and charts
likangning93 Oct 19, 2015
e04cbf5
more readme.
likangning93 Oct 19, 2015
7c3b71d
added more explanation for features
likangning93 Oct 19, 2015
00024ba
played with header sizes?
likangning93 Oct 19, 2015
415af99
added a missing photo
likangning93 Oct 19, 2015
2db1f89
added up to menger sponge analyis for branch divergence
likangning93 Oct 19, 2015
f86d262
submission ready?
likangning93 Oct 19, 2015
209ee9c
added some more diagrams
likangning93 Oct 19, 2015
ca6d417
fixed readme something. also iq took my shader offline b/c it was cra…
Oct 19, 2015
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
350 changes: 350 additions & 0 deletions IQ_example_annotated.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,350 @@
// Created by inigo quilez - iq/2013
// License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

// A list of usefull distance function to simple primitives, and an example on how to
// do some interesting boolean operations, repetition and displacement.
//
// More info here: http://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm

float sdPlane( vec3 p )
{
return p.y;
}

float sdSphere( vec3 p, float s )
{
return length(p)-s;
}

float sdBox( vec3 p, vec3 b )
{
vec3 d = abs(p) - b;
return min(max(d.x,max(d.y,d.z)),0.0) + length(max(d,0.0));
}

float sdEllipsoid( in vec3 p, in vec3 r )
{
return (length( p/r ) - 1.0) * min(min(r.x,r.y),r.z);
}

float udRoundBox( vec3 p, vec3 b, float r )
{
return length(max(abs(p)-b,0.0))-r;
}

float sdTorus( vec3 p, vec2 t )
{
return length( vec2(length(p.xz)-t.x,p.y) )-t.y;
}

float sdHexPrism( vec3 p, vec2 h )
{
vec3 q = abs(p);
#if 0
return max(q.z-h.y,max((q.x*0.866025+q.y*0.5),q.y)-h.x);
#else
float d1 = q.z-h.y;
float d2 = max((q.x*0.866025+q.y*0.5),q.y)-h.x;
return length(max(vec2(d1,d2),0.0)) + min(max(d1,d2), 0.);
#endif
}

float sdCapsule( vec3 p, vec3 a, vec3 b, float r )
{
vec3 pa = p-a, ba = b-a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
return length( pa - ba*h ) - r;
}

float sdTriPrism( vec3 p, vec2 h )
{
vec3 q = abs(p);
#if 0
return max(q.z-h.y,max(q.x*0.866025+p.y*0.5,-p.y)-h.x*0.5);
#else
float d1 = q.z-h.y;
float d2 = max(q.x*0.866025+p.y*0.5,-p.y)-h.x*0.5;
return length(max(vec2(d1,d2),0.0)) + min(max(d1,d2), 0.);
#endif
}

float sdCylinder( vec3 p, vec2 h )
{
vec2 d = abs(vec2(length(p.xz),p.y)) - h;
return min(max(d.x,d.y),0.0) + length(max(d,0.0));
}

float sdCone( in vec3 p, in vec3 c )
{
vec2 q = vec2( length(p.xz), p.y );
float d1 = -q.y-c.z;
float d2 = max( dot(q,c.xy), q.y);
return length(max(vec2(d1,d2),0.0)) + min(max(d1,d2), 0.);
}

float sdConeSection( in vec3 p, in float h, in float r1, in float r2 )
{
float d1 = -p.y - h;
float q = p.y - h;
float si = 0.5*(r1-r2)/h;
float d2 = max( sqrt( dot(p.xz,p.xz)*(1.0-si*si)) + q*si - r2, q );
return length(max(vec2(d1,d2),0.0)) + min(max(d1,d2), 0.);
}


float length2( vec2 p )
{
return sqrt( p.x*p.x + p.y*p.y );
}

float length6( vec2 p )
{
p = p*p*p; p = p*p;
return pow( p.x + p.y, 1.0/6.0 );
}

float length8( vec2 p )
{
p = p*p; p = p*p; p = p*p;
return pow( p.x + p.y, 1.0/8.0 );
}

float sdTorus82( vec3 p, vec2 t )
{
vec2 q = vec2(length2(p.xz)-t.x,p.y);
return length8(q)-t.y;
}

float sdTorus88( vec3 p, vec2 t )
{
vec2 q = vec2(length8(p.xz)-t.x,p.y);
return length8(q)-t.y;
}

float sdCylinder6( vec3 p, vec2 h )
{
return max( length6(p.xz)-h.x, abs(p.y)-h.y );
}

//----------------------------------------------------------------------

float opS( float d1, float d2 )
{
return max(-d2,d1);
}

vec2 opU( vec2 d1, vec2 d2 )
{
return (d1.x<d2.x) ? d1 : d2;
}

vec3 opRep( vec3 p, vec3 c )
{
return mod(p,c)-0.5*c;
}

vec3 opTwist( vec3 p )
{
float c = cos(10.0*p.y+10.0);
float s = sin(10.0*p.y+10.0);
mat2 m = mat2(c,-s,s,c);
return vec3(m*p.xz,p.y);
}

//----------------------------------------------------------------------

vec2 map( in vec3 pos ) // output is a t and a material seed
{
vec2 res = opU( vec2( sdPlane( pos), 1.0 ),
vec2( sdSphere( pos-vec3( 0.0,0.25, 0.0), 0.25 ), 46.9 ) );
res = opU( res, vec2( sdBox( pos-vec3( 1.0,0.25, 0.0), vec3(0.25) ), 3.0 ) );
res = opU( res, vec2( udRoundBox( pos-vec3( 1.0,0.25, 1.0), vec3(0.15), 0.1 ), 41.0 ) );
res = opU( res, vec2( sdTorus( pos-vec3( 0.0,0.25, 1.0), vec2(0.20,0.05) ), 25.0 ) );
res = opU( res, vec2( sdCapsule( pos,vec3(-1.3,0.10,-0.1), vec3(-0.8,0.50,0.2), 0.1 ), 31.9 ) );
res = opU( res, vec2( sdTriPrism( pos-vec3(-1.0,0.25,-1.0), vec2(0.25,0.05) ),43.5 ) );
res = opU( res, vec2( sdCylinder( pos-vec3( 1.0,0.30,-1.0), vec2(0.1,0.2) ), 8.0 ) );
res = opU( res, vec2( sdCone( pos-vec3( 0.0,0.50,-1.0), vec3(0.8,0.6,0.3) ), 55.0 ) );
res = opU( res, vec2( sdTorus82( pos-vec3( 0.0,0.25, 2.0), vec2(0.20,0.05) ),50.0 ) );
res = opU( res, vec2( sdTorus88( pos-vec3(-1.0,0.25, 2.0), vec2(0.20,0.05) ),43.0 ) );
res = opU( res, vec2( sdCylinder6( pos-vec3( 1.0,0.30, 2.0), vec2(0.1,0.2) ), 12.0 ) );
res = opU( res, vec2( sdHexPrism( pos-vec3(-1.0,0.20, 1.0), vec2(0.25,0.05) ),17.0 ) );

res = opU( res, vec2( opS(
udRoundBox( pos-vec3(-2.0,0.2, 1.0), vec3(0.15),0.05),
sdSphere( pos-vec3(-2.0,0.2, 1.0), 0.25)), 13.0 ) );
res = opU( res, vec2( opS(
sdTorus82( pos-vec3(-2.0,0.2, 0.0), vec2(0.20,0.1)),
sdCylinder( opRep( vec3(atan(pos.x+2.0,pos.z)/6.2831,
pos.y,
0.02+0.5*length(pos-vec3(-2.0,0.2, 0.0))),
vec3(0.05,1.0,0.05)), vec2(0.02,0.6))), 51.0 ) );
res = opU( res, vec2( 0.7*sdSphere( pos-vec3(-2.0,0.25,-1.0), 0.2 ) +
0.03*sin(50.0*pos.x)*sin(50.0*pos.y)*sin(50.0*pos.z),
65.0 ) );
res = opU( res, vec2( 0.5*sdTorus( opTwist(pos-vec3(-2.0,0.25, 2.0)),vec2(0.20,0.05)), 46.7 ) );

res = opU( res, vec2(sdConeSection( pos-vec3( 0.0,0.35,-2.0), 0.15, 0.2, 0.1 ), 13.67 ) );

res = opU( res, vec2(sdEllipsoid( pos-vec3( 1.0,0.35,-2.0), vec3(0.15, 0.2, 0.05) ), 43.17 ) );

return res;
}

vec2 castRay( in vec3 ro, in vec3 rd )
{
float tmin = 1.0;
float tmax = 20.0;

#if 0
float tp1 = (0.0-ro.y)/rd.y; if( tp1>0.0 ) tmax = min( tmax, tp1 );
float tp2 = (1.6-ro.y)/rd.y; if( tp2>0.0 ) { if( ro.y>1.6 ) tmin = max( tmin, tp2 );
else tmax = min( tmax, tp2 ); }
#endif

float precis = 0.002;
float t = tmin;
float m = -1.0;
for( int i=0; i<50; i++ )
{
vec2 res = map( ro+rd*t );
if( res.x<precis || t>tmax ) break;
t += res.x;
m = res.y;
}

if( t>tmax ) m=-1.0;
return vec2( t, m );
}

// returns a shadow value between 0.0 and 1.0. 1.0 is lit, 0.0 is unlit
//
float softshadow( in vec3 ro, in vec3 rd, in float mint, in float tmax )
{
float res = 1.0;
float t = mint;
for( int i=0; i<16; i++ )
{
float h = map( ro + rd*t ).x;
res = min( res, 8.0*h/t );
t += clamp( h, 0.02, 0.10 ); // IQ's version doesn't let you go very far, does it? why?
if( h<0.001 || t>tmax ) break;
}
return clamp( res, 0.0, 1.0 );

}

vec3 calcNormal( in vec3 pos )
{
vec3 eps = vec3( 0.001, 0.0, 0.0 );
vec3 nor = vec3(
map(pos+eps.xyy).x - map(pos-eps.xyy).x,
map(pos+eps.yxy).x - map(pos-eps.yxy).x,
map(pos+eps.yyx).x - map(pos-eps.yyx).x );
return normalize(nor);
}

float calcAO( in vec3 pos, in vec3 nor )
{
float occ = 0.0;
float sca = 1.0;
for( int i=0; i<5; i++ )
{
float hr = 0.01 + 0.12*float(i)/4.0; // compute a sample distance
vec3 aopos = nor * hr + pos; // compute the sample position
float dd = map( aopos ).x; // get the distance function at the sample position
occ += -(dd-hr)*sca; // occlusion += distance function - sample distance * falloff term
sca *= 0.95;
}
return clamp( 1.0 - 3.0*occ, 0.0, 1.0 );
}




vec3 render( in vec3 ro, in vec3 rd )
{
vec3 col = vec3(0.8, 0.9, 1.0);
vec2 res = castRay(ro,rd);
float t = res.x;
float m = res.y;
if( m>-0.5 )
{
vec3 pos = ro + t*rd;
vec3 nor = calcNormal( pos );
vec3 ref = reflect( rd, nor );

// material
col = 0.45 + 0.3*sin( vec3(0.05,0.08,0.10)*(m-1.0) );

if( m<1.5 )
{

float f = mod( floor(5.0*pos.z) + floor(5.0*pos.x), 2.0);
col = 0.4 + 0.1*f*vec3(1.0);
}

// lighitng
float occ = calcAO( pos, nor );
vec3 lig = normalize( vec3(-0.6, 0.7, -0.5) );
float amb = clamp( 0.5+0.5*nor.y, 0.0, 1.0 );
float dif = clamp( dot( nor, lig ), 0.0, 1.0 );
float bac = clamp( dot( nor, normalize(vec3(-lig.x,0.0,-lig.z))), 0.0, 1.0 )*clamp( 1.0-pos.y,0.0,1.0);
float dom = smoothstep( -0.1, 0.1, ref.y );
float fre = pow( clamp(1.0+dot(nor,rd),0.0,1.0), 2.0 );
float spe = pow(clamp( dot( ref, lig ), 0.0, 1.0 ),16.0);

dif *= softshadow( pos, lig, 0.02, 2.5 );
dom *= softshadow( pos, ref, 0.02, 2.5 );

vec3 brdf = vec3(0.0);
brdf += 1.20*dif*vec3(1.00,0.90,0.60);
brdf += 1.20*spe*vec3(1.00,0.90,0.60)*dif;
brdf += 0.30*amb*vec3(0.50,0.70,1.00)*occ;
brdf += 0.40*dom*vec3(0.50,0.70,1.00)*occ;
brdf += 0.30*bac*vec3(0.25,0.25,0.25)*occ;
brdf += 0.40*fre*vec3(1.00,1.00,1.00)*occ;
brdf += 0.02;
col = col*brdf;

col = mix( col, vec3(0.8,0.9,1.0), 1.0-exp( -0.0005*t*t ) );

}

return vec3( clamp(col,0.0,1.0) );
}

mat3 setCamera( in vec3 ro, in vec3 ta, float cr )
{
vec3 cw = normalize(ta-ro);
vec3 cp = vec3(sin(cr), cos(cr),0.0);
vec3 cu = normalize( cross(cw,cp) );
vec3 cv = normalize( cross(cu,cw) );
return mat3( cu, cv, cw );
}

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 q = fragCoord.xy/iResolution.xy;
vec2 p = -1.0+2.0*q;
p.x *= iResolution.x/iResolution.y;
vec2 mo = iMouse.xy/iResolution.xy;

float time = 15.0 + iGlobalTime;

// camera
vec3 ro = vec3( -0.5+3.5*cos(0.1*time + 6.0*mo.x), 0.0, 0.5 + 3.5*sin(0.1*time + 6.0*mo.x) ); // camera position
vec3 ta = vec3( 0, -1, 0 ); // camera aim

// camera-to-world transformation
mat3 ca = setCamera( ro, ta, 0.0 );

// ray direction
vec3 rd = ca * normalize( vec3(p.xy,2.0) );

// render
vec3 col = render( ro, rd );

col = pow( col, vec3(0.4545) );

fragColor=vec4( col, 1.0 );
}
Loading