port from perforce

This commit is contained in:
2026-04-18 22:31:51 +02:00
commit 8d0ab5b7cc
8409 changed files with 3972376 additions and 0 deletions

View File

@@ -0,0 +1,213 @@
// FragmentProgram
//
// porting GLSL by kioku based on syoyo's AS3 Ambient Occlusion
// [http://lucille.atso-net.jp/blog/?p=638]
varying vec3 org,dir;
struct Ray
{
vec3 org;
vec3 dir;
};
struct Sphere
{
vec3 center;
float radius;
};
struct Plane
{
vec3 p;
vec3 n;
};
struct Intersection
{
float t;
vec3 p; // hit point
vec3 n; // normal
int hit;
};
void shpere_intersect(Sphere s, Ray ray, inout Intersection isect)
{
// rs = ray.org - sphere.center
vec3 rs = ray.org - s.center;
float B = dot(rs, ray.dir);
float C = dot(rs, rs) - (s.radius * s.radius);
float D = B * B - C;
if (D > 0.0)
{
float t = -B - sqrt(D);
if ( (t > 0.0) && (t < isect.t) )
{
isect.t = t;
isect.hit = 1;
// calculate normal.
vec3 p = vec3(ray.org.x + ray.dir.x * t,
ray.org.y + ray.dir.y * t,
ray.org.z + ray.dir.z * t);
vec3 n = p - s.center;
n = normalize(n);
isect.n = n;
isect.p = p;
}
}
}
void plane_intersect(Plane pl, Ray ray, inout Intersection isect)
{
// d = -(p . n)
// t = -(ray.org . n + d) / (ray.dir . n)
float d = -dot(pl.p, pl.n);
float v = dot(ray.dir, pl.n);
if (abs(v) < 1.0e-6)
return; // the plane is parallel to the ray.
float t = -(dot(ray.org, pl.n) + d) / v;
if ( (t > 0.0) && (t < isect.t) )
{
isect.hit = 1;
isect.t = t;
isect.n = pl.n;
vec3 p = vec3(ray.org.x + t * ray.dir.x,
ray.org.y + t * ray.dir.y,
ray.org.z + t * ray.dir.z);
isect.p = p;
}
}
Sphere sphere[3];
Plane plane;
void Intersect(Ray r, inout Intersection i)
{
for (int c = 0; c < 3; c++)
{
shpere_intersect(sphere[c], r, i);
}
plane_intersect(plane, r, i);
}
void orthoBasis(out vec3 basis[3], vec3 n)
{
basis[2] = vec3(n.x, n.y, n.z);
basis[1] = vec3(0.0, 0.0, 0.0);
if ((n.x < 0.6) && (n.x > -0.6))
basis[1].x = 1.0;
else if ((n.y < 0.6) && (n.y > -0.6))
basis[1].y = 1.0;
else if ((n.z < 0.6) && (n.z > -0.6))
basis[1].z = 1.0;
else
basis[1].x = 1.0;
basis[0] = cross(basis[1], basis[2]);
basis[0] = normalize(basis[0]);
basis[1] = cross(basis[2], basis[0]);
basis[1] = normalize(basis[1]);
}
int seed = 0;
float random()
{
seed = int(mod(float(seed)*1364.0+626.0, 509.0));
return float(seed)/509.0;
}
vec3 computeAO(inout Intersection isect)
{
int i, j;
int ntheta = 8;
int nphi = 8;
float eps = 0.0001;
// Slightly move ray org towards ray dir to avoid numerical probrem.
vec3 p = vec3(isect.p.x + eps * isect.n.x,
isect.p.y + eps * isect.n.y,
isect.p.z + eps * isect.n.z);
// Calculate orthogonal basis.
vec3 basis[3];
orthoBasis(basis, isect.n);
float occlusion = 0.0;
for (j = 0; j < ntheta; j++)
{
for (i = 0; i < nphi; i++)
{
// Pick a random ray direction with importance sampling.
// p = cos(theta) / 3.141592
float r = random();
float phi = 2.0 * 3.141592 * random();
vec3 ref;
ref.x = cos(phi) * sqrt(1.0 - r);
ref.y = sin(phi) * sqrt(1.0 - r);
ref.z = sqrt(r);
// local -> global
vec3 rray;
rray.x = ref.x * basis[0].x + ref.y * basis[1].x + ref.z * basis[2].x;
rray.y = ref.x * basis[0].y + ref.y * basis[1].y + ref.z * basis[2].y;
rray.z = ref.x * basis[0].z + ref.y * basis[1].z + ref.z * basis[2].z;
vec3 raydir = vec3(rray.x, rray.y, rray.z);
Ray ray;
ray.org = p;
ray.dir = raydir;
Intersection occIsect;
occIsect.hit = 0;
occIsect.t = 1.0e+30;
occIsect.n = occIsect.p = vec3(0, 0, 0);
Intersect(ray, occIsect);
if (occIsect.hit != 0)
occlusion += 1.0;
}
}
// [0.0, 1.0]
occlusion = (float(ntheta * nphi) - occlusion) / float(ntheta * nphi);
return vec3(occlusion, occlusion, occlusion);
}
void main()
{
sphere[0].center = vec3(-2.0, 0.0, -3.5);
sphere[0].radius = 0.5;
sphere[1].center = vec3(-0.5, 0.0, -3.0);
sphere[1].radius = 0.5;
sphere[2].center = vec3(1.0, 0.0, -2.2);
sphere[2].radius = 0.5;
plane.p = vec3(0,-0.5, 0);
plane.n = vec3(0, 1.0, 0);
Intersection i;
i.hit = 0;
i.t = 1.0e+30;
i.n = i.p = vec3(0, 0, 0);
Ray r;
r.org = org;
r.dir = normalize(dir);
seed = int(mod(dir.x * dir.y * 4525434.0, 65536.0));
vec4 col = vec4(0,0,0,0);
Intersect(r, i);
if (i.hit != 0)
{
col.rgb = computeAO(i);
}
gl_FragColor = col;
}

View File

@@ -0,0 +1,9 @@
// VertexProgram
// This program is for 16:10 aspect ratio
varying vec3 org,dir;
void main()
{
gl_Position=gl_Vertex;
org=vec3(0,0,0);
dir=normalize(-vec3(-gl_Vertex.x*1.6,-gl_Vertex.y,1));
}

View File

@@ -0,0 +1,99 @@
// FragmentProgram
// based on iq/rgba 's seminar
// "Rendering Worlds with Two Triangles with raytracing on the GPU in 4096 bytes"
// at NVSCENE 08
// I have watched this great seminar, I have coded the below test program. ;)
// [http://www.rgba.org/iq/]
varying vec3 org,dir;
float flr(vec3 p, float f)
{
return abs(f - p.y);
}
float sph(vec3 p, vec4 spr)
{
return length(spr.xyz-p) - spr.w;
}
float cly(vec3 p, vec4 cld)
{
return length(vec2(cld.x + 0.5 * sin(p.y + p.z * 2.0), cld.z) - p.xz) - cld.w;
}
float scene(vec3 p)
{
float d = flr(p, -5.0);
d = min(d, flr(p, 5.0));
d = min(d, sph(p, vec4( 0,-2, 15, 1.5)));
d = min(d, sph(p, vec4(-8, 0, 20, 2.0)));
d = min(d, sph(p, vec4(-5, 4, 15, 0.5)));
d = min(d, sph(p, vec4(-1, 3, 15, 2.0)));
d = min(d, sph(p, vec4( 2,-3, 15, 0.5)));
d = min(d, cly(p, vec4(10, 0, 20, 1.0)));
d = min(d, cly(p, vec4( 4, 0, 15, 1.0)));
d = min(d, cly(p, vec4( 0, 0, 20, 1.0)));
d = min(d, cly(p, vec4(-2, 0, 25, 1.0)));
d = min(d, cly(p, vec4(-6, 0, 30, 1.0)));
d = min(d, cly(p, vec4(-12,0, 35, 1.0)));
return d;
}
vec3 getN(vec3 p)
{
float eps = 0.01;
return normalize(vec3(
scene(p+vec3(eps,0,0))-scene(p-vec3(eps,0,0)),
scene(p+vec3(0,eps,0))-scene(p-vec3(0,eps,0)),
scene(p+vec3(0,0,eps))-scene(p-vec3(0,0,eps))
));
}
float AO(vec3 p,vec3 n)
{
float dlt = 0.5;
float oc = 0.0, d = 1.0;
for(int i = 0; i < 6; i++)
{
oc += (float(i) * dlt - scene(p + n * float(i) * dlt)) / d;
d *= 2.0;
}
return 1.0 - oc;
}
void main()
{
float g,d = 0.0;
vec3 p = org;
for(int i = 0; i < 64; i++)
{
d = scene(p);
p = p + d * dir;
}
if(d > 1.0)
{
gl_FragColor = vec4(0,0,0,1);
return;
}
vec3 n = getN(p);
float a = AO(p,n);
vec3 s = vec3(0,0,0);
vec3 lp[3],lc[3];
lp[0] = vec3(-4,0,4);
lp[1] = vec3(2,3,8);
lp[2] = vec3(4,-2,24);
lc[0] = vec3(1.0,0.5,0.4);
lc[1] = vec3(0.4,0.5,1.0);
lc[2] = vec3(0.2,1.0,0.5);
for(int i = 0; i < 3; i++)
{
vec3 l,lv;
lv = lp[i] - p;
l = normalize(lv);
g = length(lv);
g = max(0.0,dot(l,n)) / g * float(10);
s += g * lc[i];
}
float fg = min(1.0,20.0 / length(p - org));
gl_FragColor = vec4(s * a,1) * fg * fg;
}

View File

@@ -0,0 +1,9 @@
// VertexProgram
varying vec3 org,dir;
void main()
{
gl_Position=gl_Vertex;
org=vec3(0,0,0);
dir=normalize(vec3(gl_Vertex.x*1.6,gl_Vertex.y,2));
}

View File

@@ -0,0 +1,10 @@
// FragmentProgram
varying vec4 p;
void main()
{
float g = p.y * 0.5 + 0.5;
gl_FragColor = vec4(g,g,g,0);
return;
}

View File

@@ -0,0 +1,10 @@
// VertexProgram
varying vec4 p;
void main()
{
gl_Position = gl_Vertex;
p = gl_Vertex;
}

View File

@@ -0,0 +1,191 @@
// FragmentProgram
const int raytraceDepth = 8;
varying vec3 org,dir;
struct Ray
{
vec3 org;
vec3 dir;
};
struct Sphere
{
vec3 c;
float r;
vec3 col;
};
struct Plane
{
vec3 p;
vec3 n;
vec3 col;
};
struct Intersection
{
float t;
vec3 p; // hit point
vec3 n; // normal
int hit;
vec3 col;
};
void shpere_intersect(Sphere s, Ray ray, inout Intersection isect)
{
// rs = ray.org - sphere.c
vec3 rs = ray.org - s.c;
float B = dot(rs, ray.dir);
float C = dot(rs, rs) - (s.r * s.r);
float D = B * B - C;
if (D > 0.0)
{
float t = -B - sqrt(D);
if ( (t > 0.0) && (t < isect.t) )
{
isect.t = t;
isect.hit = 1;
// calculate normal.
vec3 p = vec3(ray.org.x + ray.dir.x * t,
ray.org.y + ray.dir.y * t,
ray.org.z + ray.dir.z * t);
vec3 n = p - s.c;
n = normalize(n);
isect.n = n;
isect.p = p;
isect.col = s.col;
}
}
}
void plane_intersect(Plane pl, Ray ray, inout Intersection isect)
{
// d = -(p . n)
// t = -(ray.org . n + d) / (ray.dir . n)
float d = -dot(pl.p, pl.n);
float v = dot(ray.dir, pl.n);
if (abs(v) < 1.0e-6)
return; // the plane is parallel to the ray.
float t = -(dot(ray.org, pl.n) + d) / v;
if ( (t > 0.0) && (t < isect.t) )
{
isect.hit = 1;
isect.t = t;
isect.n = pl.n;
vec3 p = vec3(ray.org.x + t * ray.dir.x,
ray.org.y + t * ray.dir.y,
ray.org.z + t * ray.dir.z);
isect.p = p;
float offset = 0.2;
vec3 dp = p + offset;
if ((mod(dp.x, 1.0) > 0.5 && mod(dp.z, 1.0) > 0.5)
|| (mod(dp.x, 1.0) < 0.5 && mod(dp.z, 1.0) < 0.5))
isect.col = pl.col;
else
isect.col = pl.col * 0.5;
}
}
Sphere sphere[3];
Plane plane;
void Intersect(Ray r, inout Intersection i)
{
for (int c = 0; c < 3; c++)
{
shpere_intersect(sphere[c], r, i);
}
plane_intersect(plane, r, i);
}
int seed = 0;
float random()
{
seed = int(mod(float(seed)*1364.0+626.0, 509.0));
return float(seed)/509.0;
}
vec3 computeLightShadow(in Intersection isect)
{
int i, j;
int ntheta = 16;
int nphi = 16;
float eps = 0.0001;
// Slightly move ray org towards ray dir to avoid numerical probrem.
vec3 p = vec3(isect.p.x + eps * isect.n.x,
isect.p.y + eps * isect.n.y,
isect.p.z + eps * isect.n.z);
vec3 lightPoint = vec3(5,5,5);
Ray ray;
ray.org = p;
ray.dir = normalize(lightPoint - p);
Intersection lisect;
lisect.hit = 0;
lisect.t = 1.0e+30;
lisect.n = lisect.p = lisect.col = vec3(0, 0, 0);
Intersect(ray, lisect);
if (lisect.hit != 0)
return vec3(0.0,0.0,0.0);
else
{
float shade = max(0.0, dot(isect.n, ray.dir));
shade = pow(shade,3.0) + shade * 0.5;
return vec3(shade,shade,shade);
}
}
void main()
{
sphere[0].c = vec3(-2.0, 0.0, -3.5);
sphere[0].r = 0.5;
sphere[0].col = vec3(1,0.3,0.3);
sphere[1].c = vec3(-0.5, 0.0, -3.0);
sphere[1].r = 0.5;
sphere[1].col = vec3(0.3,1,0.3);
sphere[2].c = vec3(1.0, 0.0, -2.2);
sphere[2].r = 0.5;
sphere[2].col = vec3(0.3,0.3,1);
plane.p = vec3(0,-0.5, 0);
plane.n = vec3(0, 1.0, 0);
plane.col = vec3(1,1, 1);
seed = int(mod(dir.x * dir.y * 4525434.0, 65536.0));
Ray r;
r.org = org;
r.dir = normalize(dir);
vec4 col = vec4(0,0,0,1);
float eps = 0.0001;
vec3 bcol = vec3(1,1,1);
for (int j = 0; j < raytraceDepth; j++)
{
Intersection i;
i.hit = 0;
i.t = 1.0e+30;
i.n = i.p = i.col = vec3(0, 0, 0);
Intersect(r, i);
if (i.hit != 0)
{
col.rgb += bcol * i.col * computeLightShadow(i);
bcol *= i.col;
}
else
{
break;
}
r.org = vec3(i.p.x + eps * i.n.x,
i.p.y + eps * i.n.y,
i.p.z + eps * i.n.z);
r.dir = reflect(r.dir, vec3(i.n.x, i.n.y, i.n.z));
}
gl_FragColor = col;
}

View File

@@ -0,0 +1,10 @@
// VertexProgram
// This program is 16:10 ratio
varying vec3 org,dir;
void main()
{
gl_Position=gl_Vertex;
org=vec3(0,0,0);
dir=normalize(-vec3(-gl_Vertex.x*1.6,-gl_Vertex.y,1));
}