gpuray_glsl
diff sdr/rt.glsl @ 0:f234630e38ff
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 09 Nov 2014 13:03:36 +0200 |
parents | |
children | 2ed3da7dc0bc |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/sdr/rt.glsl Sun Nov 09 13:03:36 2014 +0200 1.3 @@ -0,0 +1,523 @@ 1.4 +/* vi:set filetype=glsl ts=4 sw=4: */ 1.5 +#version 120 1.6 +#extension GL_ARB_gpu_shader5 : enable 1.7 + 1.8 +#define M_PI 3.1415926 1.9 + 1.10 +#define INIT_EVERYTHING 1.11 +#define USE_XFORM 1.12 +#define OBJ_LINE_WIDTH 16.0 1.13 + 1.14 +struct Ray { 1.15 + vec3 origin, dir; 1.16 +}; 1.17 + 1.18 +struct Material { 1.19 + vec3 diffuse, specular; 1.20 + float shininess; 1.21 + vec4 megatex_rect; 1.22 + float reflectivity; 1.23 +}; 1.24 + 1.25 +struct HitPoint { 1.26 + float dist; 1.27 + vec3 pos, normal; 1.28 + vec2 texcoord; 1.29 + struct Material mat; 1.30 +}; 1.31 + 1.32 +struct Sphere { 1.33 + float index; 1.34 + vec3 pos; 1.35 + float radius; 1.36 + struct Material mat; 1.37 +}; 1.38 + 1.39 +struct Plane { 1.40 + float index; 1.41 + vec3 normal; 1.42 + float dist; 1.43 + struct Material mat; 1.44 +}; 1.45 + 1.46 +struct Box { 1.47 + float index; 1.48 + vec3 min, max; 1.49 + struct Material mat; 1.50 +}; 1.51 + 1.52 +struct Light { 1.53 + vec3 pos, color; 1.54 +}; 1.55 + 1.56 +vec3 shade(in Ray ray, in HitPoint hit); 1.57 +bool find_intersection(in Ray ray, out HitPoint hit); 1.58 +bool sphere_intersect(in Sphere sph, in Ray ray, out HitPoint pt); 1.59 +bool plane_intersect(in Plane plane, in Ray ray, out HitPoint pt); 1.60 +bool box_intersect(in Box box, in Ray ray, out HitPoint pt); 1.61 +vec3 transform(in vec3 v, in mat4 inv_xform); 1.62 +Ray transform(in Ray ray, in mat4 xform, in mat4 inv_xform); 1.63 +Ray get_primary_ray(); 1.64 + 1.65 +Sphere read_sphere(in float idx); 1.66 +Plane read_plane(in float idx); 1.67 +Box read_box(in float idx); 1.68 +Material read_material(in sampler2D tex, in float ty); 1.69 +void read_xform(in float idx, out mat4 xform, out mat4 inv_xform); 1.70 + 1.71 +uniform sampler2D tex_raydir; 1.72 +uniform sampler2D tex_spheres, tex_planes, tex_boxes; 1.73 +uniform sampler2D tex_megatex; 1.74 +uniform sampler2D tex_xforms; 1.75 +uniform samplerCube tex_env; 1.76 +uniform vec2 fog; 1.77 + 1.78 +uniform Light lights[8]; 1.79 +uniform int num_lights; 1.80 + 1.81 +int num_spheres, num_planes, num_boxes; 1.82 +float sph_tex_sz, plane_tex_sz, box_tex_sz, xform_tex_sz; 1.83 + 1.84 +#ifdef INIT_EVERYTHING 1.85 +Material default_material; 1.86 +#endif 1.87 + 1.88 +void main() 1.89 +{ 1.90 +#ifdef INIT_EVERYTHING 1.91 + default_material.diffuse = default_material.specular = vec3(0.0, 0.0, 0.0); 1.92 + default_material.shininess = 1.0; 1.93 + default_material.reflectivity = 0.0; 1.94 + default_material.megatex_rect = vec4(0.0, 0.0, 0.0, 0.0); 1.95 +#endif 1.96 + 1.97 + Ray ray = get_primary_ray(); 1.98 + 1.99 + /* read the various descriptors specifying dimensions and counts for 1.100 + * all the relevant data textures 1.101 + */ 1.102 + vec4 desc = texture2D(tex_spheres, vec2(0.0, 0.0)); 1.103 + num_spheres = int(desc.x); 1.104 + sph_tex_sz = desc.y; 1.105 + 1.106 + desc = texture2D(tex_planes, vec2(0.0, 0.0)); 1.107 + num_planes = int(desc.x); 1.108 + plane_tex_sz = desc.y; 1.109 + 1.110 + desc = texture2D(tex_boxes, vec2(0.0, 0.0)); 1.111 + num_boxes = int(desc.x); 1.112 + box_tex_sz = desc.y; 1.113 + 1.114 + xform_tex_sz = texture2D(tex_xforms, vec2(0.0, 0.0)).x; 1.115 + 1.116 + 1.117 + HitPoint hit; 1.118 +#ifdef INIT_EVERYTHING 1.119 + hit.dist = 0.0; 1.120 + hit.pos = hit.normal = vec3(0.0, 0.0, 0.0); 1.121 +#endif 1.122 + 1.123 + vec3 color = vec3(0.0, 0.0, 0.0); 1.124 + float energy = 1.0; 1.125 + 1.126 + int iter = 0; 1.127 + while(energy > 0.01 && iter++ < 4) { 1.128 + vec3 envcol = textureCube(tex_env, ray.dir).xyz; 1.129 + 1.130 + if(find_intersection(ray, hit)) { 1.131 + float fog_t = clamp((hit.dist - fog.x) / (fog.y - fog.x), 0.0, 1.0); 1.132 + color += mix(shade(ray, hit), envcol, fog_t) * energy; 1.133 + energy *= hit.mat.reflectivity * (1.0 - fog_t); 1.134 + ray.origin = hit.pos; 1.135 + ray.dir = reflect(ray.dir, hit.normal); 1.136 + } else { 1.137 + color += envcol * energy; 1.138 + energy = 0.0; 1.139 + iter = 100; 1.140 + } 1.141 + } 1.142 + 1.143 + gl_FragColor.xyz = color; 1.144 + gl_FragColor.w = 1.0; 1.145 +} 1.146 + 1.147 +vec3 shade(in Ray ray, in HitPoint hit) 1.148 +{ 1.149 + vec3 normal = faceforward(hit.normal, ray.dir, hit.normal); 1.150 + 1.151 + vec3 vdir = normalize(ray.dir); 1.152 + vec3 vref = reflect(vdir, normal); 1.153 + 1.154 + /* if there's no texture rect.zw will be (0, 0, 0, 0) so this will map onto 1.155 + * the top-left 1x1 null texture which is all white (having no effect) 1.156 + */ 1.157 + vec2 tc = mod(hit.texcoord, vec2(1.0, 1.0)) * hit.mat.megatex_rect.zw + hit.mat.megatex_rect.xy; 1.158 + 1.159 + vec3 diffuse_color = hit.mat.diffuse * texture2D(tex_megatex, tc).xyz; 1.160 + 1.161 + vec3 color = vec3(0.0, 0.0, 0.0); 1.162 + for(int i=0; i<num_lights; i++) { 1.163 + Ray shadow_ray; 1.164 + shadow_ray.origin = hit.pos; 1.165 + shadow_ray.dir = lights[i].pos - hit.pos; 1.166 + 1.167 + HitPoint shadow_hit; 1.168 + if(!find_intersection(shadow_ray, shadow_hit) || shadow_hit.dist > 1.0) { 1.169 + vec3 ldir = normalize(shadow_ray.dir); 1.170 + 1.171 + float diffuse = max(dot(ldir, normal), 0.0); 1.172 + float specular = pow(max(dot(ldir, vref), 0.0), hit.mat.shininess); 1.173 + 1.174 + color += (diffuse_color * diffuse + hit.mat.specular * specular) * lights[i].color; 1.175 + } 1.176 + } 1.177 + 1.178 + return color; 1.179 +} 1.180 + 1.181 +bool find_intersection(in Ray ray, out HitPoint hit) 1.182 +{ 1.183 + hit.dist = 100000.0; 1.184 +#ifdef INIT_EVERYTHING 1.185 + hit.pos = hit.normal = vec3(0.0, 0.0, 0.0); 1.186 + hit.mat = default_material; 1.187 + hit.texcoord = vec2(0.0, 0.0); 1.188 +#endif 1.189 + bool found = false; 1.190 + 1.191 + for(int i=0; i<num_spheres; i++) { 1.192 + Sphere sph = read_sphere(i); 1.193 + 1.194 + HitPoint tmphit; 1.195 + if(sphere_intersect(sph, ray, tmphit) && tmphit.dist < hit.dist) { 1.196 + hit = tmphit; 1.197 + found = true; 1.198 + } 1.199 + } 1.200 + 1.201 + for(int i=0; i<num_planes; i++) { 1.202 + Plane plane = read_plane(i); 1.203 + 1.204 + HitPoint tmphit; 1.205 + if(plane_intersect(plane, ray, tmphit) && tmphit.dist < hit.dist) { 1.206 + hit = tmphit; 1.207 + found = true; 1.208 + } 1.209 + } 1.210 + 1.211 + for(int i=0; i<num_boxes; i++) { 1.212 + Box box = read_box(i); 1.213 + 1.214 + HitPoint tmphit; 1.215 + if(box_intersect(box, ray, tmphit) && tmphit.dist < hit.dist) { 1.216 + hit = tmphit; 1.217 + found = true; 1.218 + } 1.219 + } 1.220 + 1.221 + return found; 1.222 +} 1.223 + 1.224 +#define EPSILON 1e-4 1.225 +#define SQ(x) ((x) * (x)) 1.226 + 1.227 +bool sphere_intersect(in Sphere sph, in Ray inray, out HitPoint pt) 1.228 +{ 1.229 +#ifdef USE_XFORM 1.230 + mat4 xform, inv_xform; 1.231 + read_xform(sph.index, xform, inv_xform); 1.232 + 1.233 + Ray ray = transform(inray, inv_xform, xform); 1.234 +#else 1.235 + Ray ray = inray; 1.236 +#endif 1.237 + 1.238 +#ifdef INIT_EVERYTHING 1.239 + pt.dist = 0.0; 1.240 + pt.pos = pt.normal = vec3(0.0, 0.0, 0.0); 1.241 + pt.mat = default_material; 1.242 + pt.texcoord = vec2(0.0, 0.0); 1.243 +#endif 1.244 + 1.245 + float a = dot(ray.dir, ray.dir); 1.246 + float b = dot(ray.dir, ray.origin - sph.pos) * 2.0; 1.247 + float c = dot(ray.origin, ray.origin) + dot(sph.pos, sph.pos) - 1.248 + 2.0 * dot(ray.origin, sph.pos) - sph.radius * sph.radius; 1.249 + 1.250 + float discr = b * b - 4.0 * a * c; 1.251 + if(discr < EPSILON) 1.252 + return false; 1.253 + 1.254 + float sqrt_discr = sqrt(discr); 1.255 + float t0 = (-b + sqrt_discr) / (2.0 * a); 1.256 + float t1 = (-b - sqrt_discr) / (2.0 * a); 1.257 + 1.258 + if(t0 < EPSILON) 1.259 + t0 = t1; 1.260 + if(t1 < EPSILON) 1.261 + t1 = t0; 1.262 + 1.263 + float t = min(t0, t1); 1.264 + if(t < EPSILON) 1.265 + return false; 1.266 + 1.267 + // fill the HitPoint structure 1.268 + pt.dist = t; 1.269 + pt.pos = ray.origin + ray.dir * t; 1.270 + pt.normal = (pt.pos - sph.pos) / sph.radius; 1.271 + pt.mat = sph.mat; 1.272 + 1.273 + pt.texcoord.x = 0.5 * atan(pt.normal.z, pt.normal.x) / M_PI + 0.5; 1.274 + pt.texcoord.y = acos(pt.normal.y) / M_PI; 1.275 + 1.276 +#ifdef USE_XFORM 1.277 + pt.pos = (xform * vec4(pt.pos, 1.0)).xyz; 1.278 + pt.normal = normalize(transform(pt.normal, xform)); 1.279 +#endif 1.280 + return true; 1.281 +} 1.282 + 1.283 +bool plane_intersect(in Plane plane, in Ray inray, out HitPoint pt) 1.284 +{ 1.285 +#ifdef USE_XFORM 1.286 + mat4 xform, inv_xform; 1.287 + read_xform(plane.index, xform, inv_xform); 1.288 + 1.289 + Ray ray = transform(inray, inv_xform, xform); 1.290 +#else 1.291 + Ray ray = inray; 1.292 +#endif 1.293 + 1.294 +#ifdef INIT_EVERYTHING 1.295 + pt.dist = 0.0; 1.296 + pt.pos = pt.normal = vec3(0.0, 0.0, 0.0); 1.297 + pt.mat = default_material; 1.298 + pt.texcoord = vec2(0.0, 0.0); 1.299 +#endif 1.300 + 1.301 + float ndotdir = dot(plane.normal, ray.dir); 1.302 + if(abs(ndotdir) < EPSILON) { 1.303 + return false; 1.304 + } 1.305 + 1.306 + vec3 planept = plane.normal * plane.dist; 1.307 + vec3 pptdir = planept - ray.origin; 1.308 + 1.309 + float t = dot(plane.normal, pptdir) / ndotdir; 1.310 + if(t < EPSILON) { 1.311 + return false; 1.312 + } 1.313 + 1.314 + pt.dist = t; 1.315 + pt.pos = ray.origin + ray.dir * t; 1.316 + pt.normal = plane.normal; 1.317 + pt.mat = plane.mat; 1.318 + pt.texcoord.x = pt.pos.x; 1.319 + pt.texcoord.y = pt.pos.z; 1.320 + 1.321 +#ifdef USE_XFORM 1.322 + pt.pos = (xform * vec4(pt.pos, 1.0)).xyz; 1.323 + pt.normal = normalize(transform(pt.normal, xform)); 1.324 +#endif 1.325 + return true; 1.326 +} 1.327 + 1.328 +bool box_intersect(in Box box, in Ray inray, out HitPoint pt) 1.329 +{ 1.330 +#ifdef USE_XFORM 1.331 + mat4 xform, inv_xform; 1.332 + read_xform(box.index, xform, inv_xform); 1.333 + 1.334 + Ray ray = transform(inray, inv_xform, xform); 1.335 +#else 1.336 + Ray ray = inray; 1.337 +#endif 1.338 + 1.339 +#ifdef INIT_EVERYTHING 1.340 + pt.dist = 0.0; 1.341 + pt.pos = pt.normal = vec3(0.0, 0.0, 0.0); 1.342 + pt.mat = default_material; 1.343 + pt.texcoord = vec2(0.0, 0.0); 1.344 +#endif 1.345 + 1.346 + vec3 param[2]; 1.347 + param[0] = box.min; 1.348 + param[1] = box.max; 1.349 + 1.350 + vec3 inv_dir = 1.0 / ray.dir; 1.351 + int sgn[3]; 1.352 + sgn[0] = inv_dir.x < 0.0 ? 1 : 0; 1.353 + sgn[1] = inv_dir.y < 0.0 ? 1 : 0; 1.354 + sgn[2] = inv_dir.z < 0.0 ? 1 : 0; 1.355 + 1.356 + float tmin = (param[sgn[0]].x - ray.origin.x) * inv_dir.x; 1.357 + float tmax = (param[1 - sgn[0]].x - ray.origin.x) * inv_dir.x; 1.358 + float tymin = (param[sgn[1]].y - ray.origin.y) * inv_dir.y; 1.359 + float tymax = (param[1 - sgn[1]].y - ray.origin.y) * inv_dir.y; 1.360 + 1.361 + pt.normal = vec3(ray.origin.x > 0.0 ? 1.0 : -1.0, 0.0, 0.0); 1.362 + 1.363 + if(tmin > tymax || tymin > tmax) { 1.364 + return false; 1.365 + } 1.366 + if(tymin > tmin) { 1.367 + pt.normal = vec3(0.0, ray.origin.y > 0.0 ? 1.0 : -1.0, 0.0); 1.368 + tmin = tymin; 1.369 + } 1.370 + if(tymax < tmax) { 1.371 + tmax = tymax; 1.372 + } 1.373 + 1.374 + float tzmin = (param[sgn[2]].z - ray.origin.z) * inv_dir.z; 1.375 + float tzmax = (param[1 - sgn[2]].z - ray.origin.z) * inv_dir.z; 1.376 + 1.377 + if(tmin > tzmax || tzmin > tmax) { 1.378 + return false; 1.379 + } 1.380 + if(tzmin > tmin) { 1.381 + pt.normal = vec3(0.0, 0.0, ray.origin.z > 0.0 ? 1.0 : -1.0); 1.382 + tmin = tzmin; 1.383 + } 1.384 + if(tzmax < tmax) { 1.385 + tmax = tzmax; 1.386 + } 1.387 + 1.388 + float t = tmin < EPSILON ? tmax : tmin; 1.389 + if(t >= 1e-4) { 1.390 + pt.dist = t; 1.391 + pt.pos = ray.origin + ray.dir * t; 1.392 + pt.mat = box.mat; 1.393 + 1.394 + float min_dist = 10000.0; 1.395 + 1.396 + vec3 offs = box.min + (box.max - box.min) / 2.0; 1.397 + vec3 local_pt = pt.pos - offs; 1.398 + 1.399 + vec3 dist = abs((box.max - offs) - abs(local_pt)); 1.400 + if(dist.x < min_dist) { 1.401 + min_dist = dist.x; 1.402 + pt.normal = sign(local_pt.x) * vec3(1.0, 0.0, 0.0); 1.403 + pt.texcoord = pt.pos.zy; 1.404 + } 1.405 + if(dist.y < min_dist) { 1.406 + min_dist = dist.y; 1.407 + pt.normal = sign(local_pt.y) * vec3(0.0, 1.0, 0.0); 1.408 + pt.texcoord = pt.pos.xz; 1.409 + } 1.410 + if(dist.z < min_dist) { 1.411 + pt.normal = sign(local_pt.y) * vec3(0.0, 0.0, 1.0); 1.412 + pt.texcoord = pt.pos.xy; 1.413 + } 1.414 + 1.415 + 1.416 +#ifdef USE_XFORM 1.417 + pt.pos = (xform * vec4(pt.pos, 1.0)).xyz; 1.418 + pt.normal = normalize(transform(pt.normal, xform)); 1.419 +#endif 1.420 + return true; 1.421 + } 1.422 + return false; 1.423 +} 1.424 + 1.425 +vec3 transform(in vec3 v, in mat4 xform) 1.426 +{ 1.427 + return mat3(xform) * v; 1.428 +} 1.429 + 1.430 +Ray transform(in Ray ray, in mat4 xform, in mat4 inv_xform) 1.431 +{ 1.432 + Ray res; 1.433 + res.origin = (xform * vec4(ray.origin, 1.0)).xyz; 1.434 + res.dir = transform(ray.dir, xform); 1.435 + return res; 1.436 +} 1.437 + 1.438 +Ray get_primary_ray() 1.439 +{ 1.440 + Ray ray; 1.441 + ray.origin = (gl_ModelViewMatrix * vec4(0.0, 0.0, 0.0, 1.0)).xyz; 1.442 + vec3 dir = texture2D(tex_raydir, gl_TexCoord[0].st).xyz; 1.443 + ray.dir = normalize(gl_NormalMatrix * dir); 1.444 + return ray; 1.445 +} 1.446 + 1.447 +#define ITEM(x) ((float(x) + 0.5) / OBJ_LINE_WIDTH) 1.448 + 1.449 +Sphere read_sphere(in float idx) 1.450 +{ 1.451 + Sphere sph; 1.452 + // +1 because the first scanline is the descriptor 1.453 + float ty = (idx + 1.0) / sph_tex_sz; 1.454 + 1.455 + sph.index = texture2D(tex_spheres, vec2(ITEM(0), ty)).x; 1.456 + 1.457 + vec4 texel = texture2D(tex_spheres, vec2(ITEM(1), ty)); 1.458 + sph.pos = texel.xyz; 1.459 + sph.radius = texel.w; 1.460 + 1.461 + sph.mat = read_material(tex_spheres, ty); 1.462 + return sph; 1.463 +} 1.464 + 1.465 +Plane read_plane(in float idx) 1.466 +{ 1.467 + Plane plane; 1.468 + // +1 (see above) 1.469 + float ty = (idx + 1.0) / plane_tex_sz; 1.470 + 1.471 + plane.index = texture2D(tex_planes, vec2(ITEM(0), ty)).x; 1.472 + 1.473 + vec4 texel = texture2D(tex_planes, vec2(ITEM(1), ty)); 1.474 + plane.normal = texel.xyz; 1.475 + plane.dist = texel.w; 1.476 + 1.477 + plane.mat = read_material(tex_planes, ty); 1.478 + return plane; 1.479 +} 1.480 + 1.481 +Box read_box(in float idx) 1.482 +{ 1.483 + Box box; 1.484 + float ty = (idx + 1.0) / box_tex_sz; 1.485 + 1.486 + box.index = texture2D(tex_boxes, vec2(ITEM(0), ty)).x; 1.487 + 1.488 + box.min = texture2D(tex_boxes, vec2(ITEM(1), ty)).xyz; 1.489 + box.max = texture2D(tex_boxes, vec2(ITEM(2), ty)).xyz; 1.490 + 1.491 + box.mat = read_material(tex_boxes, ty); 1.492 + return box; 1.493 +} 1.494 + 1.495 +void read_xform(in float idx, out mat4 xform, out mat4 inv_xform) 1.496 +{ 1.497 + float ty = (idx + 1.0) / xform_tex_sz; 1.498 + 1.499 + for(int i=0; i<4; i++) { 1.500 + xform[i] = texture2D(tex_xforms, vec2(ITEM(i), ty)); 1.501 + } 1.502 + inv_xform = inverse(xform); 1.503 + /*for(int i=0; i<4; i++) { 1.504 + inv_xform[i] = texture2D(tex_xforms, vec2(ITEM(float(i) + 4.0), ty)); 1.505 + }*/ 1.506 +} 1.507 + 1.508 +#define MAT_START 4 1.509 +Material read_material(in sampler2D tex, in float ty) 1.510 +{ 1.511 + Material mat; 1.512 + 1.513 + vec4 texel = texture2D(tex, vec2(ITEM(MAT_START), ty)); 1.514 + mat.diffuse = texel.xyz; 1.515 + 1.516 + texel = texture2D(tex, vec2(ITEM(MAT_START + 1), ty)); 1.517 + mat.specular = texel.xyz; 1.518 + mat.shininess = texel.w; 1.519 + 1.520 + texel = texture2D(tex, vec2(ITEM(MAT_START + 2), ty)); 1.521 + mat.reflectivity = texel.x; 1.522 + 1.523 + mat.megatex_rect = texture2D(tex, vec2(ITEM(MAT_START + 3), ty)); 1.524 + 1.525 + return mat; 1.526 +}