gpuray_glsl
diff sdr/rt.glsl @ 4:2ed3da7dc0bc
broken
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 10 Nov 2014 01:26:00 +0200 |
parents | f234630e38ff |
children |
line diff
1.1 --- a/sdr/rt.glsl Sun Nov 09 20:13:33 2014 +0200 1.2 +++ b/sdr/rt.glsl Mon Nov 10 01:26:00 2014 +0200 1.3 @@ -46,6 +46,13 @@ 1.4 struct Material mat; 1.5 }; 1.6 1.7 +struct Cone { 1.8 + float index; 1.9 + float angle; 1.10 + float start, end; 1.11 + struct Material mat; 1.12 +}; 1.13 + 1.14 struct Light { 1.15 vec3 pos, color; 1.16 }; 1.17 @@ -55,6 +62,7 @@ 1.18 bool sphere_intersect(in Sphere sph, in Ray ray, out HitPoint pt); 1.19 bool plane_intersect(in Plane plane, in Ray ray, out HitPoint pt); 1.20 bool box_intersect(in Box box, in Ray ray, out HitPoint pt); 1.21 +bool cone_intersect(in Cone cone, in Ray ray, out HitPoint pt); 1.22 vec3 transform(in vec3 v, in mat4 inv_xform); 1.23 Ray transform(in Ray ray, in mat4 xform, in mat4 inv_xform); 1.24 Ray get_primary_ray(); 1.25 @@ -62,11 +70,12 @@ 1.26 Sphere read_sphere(in float idx); 1.27 Plane read_plane(in float idx); 1.28 Box read_box(in float idx); 1.29 +Cone read_cone(in float idx); 1.30 Material read_material(in sampler2D tex, in float ty); 1.31 void read_xform(in float idx, out mat4 xform, out mat4 inv_xform); 1.32 1.33 uniform sampler2D tex_raydir; 1.34 -uniform sampler2D tex_spheres, tex_planes, tex_boxes; 1.35 +uniform sampler2D tex_spheres, tex_planes, tex_boxes, tex_cones; 1.36 uniform sampler2D tex_megatex; 1.37 uniform sampler2D tex_xforms; 1.38 uniform samplerCube tex_env; 1.39 @@ -75,8 +84,8 @@ 1.40 uniform Light lights[8]; 1.41 uniform int num_lights; 1.42 1.43 -int num_spheres, num_planes, num_boxes; 1.44 -float sph_tex_sz, plane_tex_sz, box_tex_sz, xform_tex_sz; 1.45 +int num_spheres, num_planes, num_boxes, num_cones; 1.46 +float sph_tex_sz, plane_tex_sz, box_tex_sz, cone_tex_sz, xform_tex_sz; 1.47 1.48 #ifdef INIT_EVERYTHING 1.49 Material default_material; 1.50 @@ -108,6 +117,10 @@ 1.51 num_boxes = int(desc.x); 1.52 box_tex_sz = desc.y; 1.53 1.54 + desc = texture2D(tex_cones, vec2(0.0, 0.0)); 1.55 + num_cones = int(desc.x); 1.56 + cone_tex_sz = desc.y; 1.57 + 1.58 xform_tex_sz = texture2D(tex_xforms, vec2(0.0, 0.0)).x; 1.59 1.60 1.61 @@ -215,6 +228,16 @@ 1.62 } 1.63 } 1.64 1.65 + for(int i=0; i<num_cones; i++) { 1.66 + Cone cone = read_cone(i); 1.67 + 1.68 + HitPoint tmphit; 1.69 + if(cone_intersect(cone, ray, tmphit) && tmphit.dist < hit.dist) { 1.70 + hit = tmphit; 1.71 + found = true; 1.72 + } 1.73 + } 1.74 + 1.75 return found; 1.76 } 1.77 1.78 @@ -419,6 +442,77 @@ 1.79 return false; 1.80 } 1.81 1.82 +bool cone_intersect(in Cone cone, in Ray inray, out HitPoint pt) 1.83 +{ 1.84 +#ifdef USE_XFORM 1.85 + mat4 xform, inv_xform; 1.86 + read_xform(cone.index, xform, inv_xform); 1.87 + 1.88 + Ray ray = transform(inray, inv_xform, xform); 1.89 +#else 1.90 + Ray ray = inray; 1.91 +#endif 1.92 + 1.93 +#ifdef INIT_EVERYTHING 1.94 + pt.dist = 0.0; 1.95 + pt.pos = pt.normal = vec3(0.0, 0.0, 0.0); 1.96 + pt.mat = default_material; 1.97 + pt.texcoord = vec2(0.0, 0.0); 1.98 +#endif 1.99 + 1.100 + float slope = 2.0 * cone.angle / M_PI; 1.101 + 1.102 + float a = SQ(ray.dir.x) - SQ(slope * ray.dir.y) + SQ(ray.dir.z); 1.103 + float b = 2.0 * ray.origin.x * ray.dir.x - 2.0 * SQ(slope) * ray.origin.y * ray.dir.y + 1.104 + 2.0 * ray.origin.z * ray.dir.z; 1.105 + float c = SQ(ray.origin.x) - SQ(slope * ray.origin.y) + SQ(ray.origin.z); 1.106 + 1.107 + float discr = b * b - 4.0 * a * c; 1.108 + if(discr < EPSILON) 1.109 + return false; 1.110 + 1.111 + float sqrt_discr = sqrt(discr); 1.112 + float t0 = (-b + sqrt_discr) / (2.0 * a); 1.113 + float t1 = (-b - sqrt_discr) / (2.0 * a); 1.114 + 1.115 + if(t0 < EPSILON) 1.116 + t0 = t1; 1.117 + if(t1 < EPSILON) 1.118 + t1 = t0; 1.119 + 1.120 + float t = min(t0, t1); 1.121 + if(t < EPSILON) 1.122 + return false; 1.123 + 1.124 + // fill the HitPoint structure 1.125 + pt.dist = t; 1.126 + pt.pos = ray.origin + ray.dir * t; 1.127 + 1.128 + if(pt.pos.y < cone.start || pt.pos.y >= cone.end) { 1.129 + return false; 1.130 + } 1.131 + 1.132 + float radius = pt.pos.y * slope; 1.133 + pt.normal = vec3(pt.pos.x, 0.0, pt.pos.z) / radius; 1.134 + 1.135 + float ny = sin(cone.angle); 1.136 + float xzlen = sqrt(1.0 - ny * ny); 1.137 + pt.normal.x *= xzlen; 1.138 + pt.normal.y = ny; 1.139 + pt.normal.z *= xzlen; 1.140 + 1.141 + pt.mat = cone.mat; 1.142 + 1.143 + pt.texcoord.x = 0.5 * atan(pt.normal.z, pt.normal.x) / M_PI + 0.5; 1.144 + pt.texcoord.y = (pt.pos.y - cone.start) / (cone.end - cone.start); 1.145 + 1.146 +#ifdef USE_XFORM 1.147 + pt.pos = (xform * vec4(pt.pos, 1.0)).xyz; 1.148 + pt.normal = normalize(transform(pt.normal, xform)); 1.149 +#endif 1.150 + return true; 1.151 +} 1.152 + 1.153 vec3 transform(in vec3 v, in mat4 xform) 1.154 { 1.155 return mat3(xform) * v; 1.156 @@ -489,6 +583,22 @@ 1.157 return box; 1.158 } 1.159 1.160 +Cone read_cone(in float idx) 1.161 +{ 1.162 + Cone cone; 1.163 + float ty = (idx + 1.0) / cone_tex_sz; 1.164 + 1.165 + cone.index = texture2D(tex_cones, vec2(ITEM(0), ty)).x; 1.166 + 1.167 + vec4 texel = texture2D(tex_cones, vec2(ITEM(1), ty)); 1.168 + cone.angle = texel.x; 1.169 + cone.start = texel.y; 1.170 + cone.end = texel.z; 1.171 + 1.172 + cone.mat = read_material(tex_cones, ty); 1.173 + return cone; 1.174 +} 1.175 + 1.176 void read_xform(in float idx, out mat4 xform, out mat4 inv_xform) 1.177 { 1.178 float ty = (idx + 1.0) / xform_tex_sz;