rayzor
diff src/raytrace.cc @ 22:5380ff64e83f
minor changes from dos, and line endings cleanup
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 02 May 2014 14:32:58 +0300 |
parents | 252999cd1a3f |
children |
line diff
1.1 --- a/src/raytrace.cc Tue Apr 15 20:52:05 2014 +0300 1.2 +++ b/src/raytrace.cc Fri May 02 14:32:58 2014 +0300 1.3 @@ -1,78 +1,78 @@ 1.4 -#include <assert.h> 1.5 -#include <float.h> 1.6 -#include "raytrace.h" 1.7 -#include "rayzor.h" 1.8 -#include "scene.h" 1.9 -#include "logger.h" 1.10 - 1.11 -Vector3 ray_trace(const Ray &ray, int iter) 1.12 -{ 1.13 - RayHit hit; 1.14 - if(!scene->intersect(ray, &hit)) { 1.15 - return scene->get_background(ray); 1.16 - } 1.17 - 1.18 - return shade(hit, iter); 1.19 -} 1.20 - 1.21 -static inline float positive(float x) 1.22 -{ 1.23 - return x < 0.0f ? 0.0f : x; 1.24 -} 1.25 - 1.26 -Vector3 shade(const RayHit &hit, int iter) 1.27 -{ 1.28 - const Material &mtl = hit.obj->mtl; 1.29 - 1.30 - Vector3 pos = hit.ray.origin + hit.ray.dir * hit.dist; 1.31 - Vector3 norm = hit.obj->hit_normal(hit); 1.32 - norm = normalize(transform(normal_matrix(hit.obj->get_matrix()), norm)); 1.33 - Vector3 vdir = -normalize(hit.ray.dir); 1.34 - 1.35 - float ior = mtl.ior; 1.36 - 1.37 - if(dot(norm, hit.ray.dir) > 0.0) { 1.38 - norm = -norm; 1.39 - ior = 1.0 / mtl.ior; 1.40 - } 1.41 - 1.42 - Vector3 color = scene->get_ambient(); 1.43 - 1.44 - // for each light, calculate local illumination 1.45 - int num_lights = scene->get_light_count(); 1.46 - for(int i=0; i<num_lights; i++) { 1.47 - const Light *lt = scene->get_light(i); 1.48 - Vector3 ldir = lt->get_position() - pos; 1.49 - Vector3 lcol = lt->get_color(pos); 1.50 - 1.51 - RayHit hit; 1.52 - if(!scene->intersect(Ray(pos, ldir), &hit) || hit.dist > 1.0) { 1.53 - // if we can see the light, calculate and add its contribution 1.54 - ldir.normalize(); 1.55 - float ndotl = positive(dot(norm, ldir)); 1.56 - Vector3 diffuse = mtl.diffuse * lcol * ndotl; 1.57 - 1.58 - Vector3 hdir = normalize(ldir + vdir); 1.59 - float ndoth = positive(dot(norm, hdir)); 1.60 - Vector3 specular = mtl.specular * lcol * pow(ndoth, mtl.roughness * 128.0); 1.61 - 1.62 - color = color + lerp(specular, diffuse, mtl.roughness); 1.63 - } 1.64 - } 1.65 - 1.66 - if(mtl.reflect > 1e-4 && iter < 6) { 1.67 - Ray reflray(pos, reflect(vdir, norm)); 1.68 - 1.69 - Vector3 rcol = ray_trace(reflray, iter + 1) * mtl.specular * mtl.reflect; 1.70 - color = color + rcol; 1.71 - } 1.72 - 1.73 - if(mtl.refract > 1e-4 && iter < 6) { 1.74 - Ray refrray(pos, refract(vdir, norm, ior)); 1.75 - 1.76 - Vector3 rcol = ray_trace(refrray, iter + 1) * mtl.specular * mtl.refract; 1.77 - color = color + rcol; 1.78 - } 1.79 - 1.80 - return color; 1.81 -} 1.82 +#include <assert.h> 1.83 +#include <float.h> 1.84 +#include "raytrace.h" 1.85 +#include "rayzor.h" 1.86 +#include "scene.h" 1.87 +#include "logger.h" 1.88 + 1.89 +Vector3 ray_trace(const Ray &ray, int iter) 1.90 +{ 1.91 + RayHit hit; 1.92 + if(!scene->intersect(ray, &hit)) { 1.93 + return scene->get_background(ray); 1.94 + } 1.95 + 1.96 + return shade(hit, iter); 1.97 +} 1.98 + 1.99 +static inline float positive(float x) 1.100 +{ 1.101 + return x < 0.0f ? 0.0f : x; 1.102 +} 1.103 + 1.104 +Vector3 shade(const RayHit &hit, int iter) 1.105 +{ 1.106 + const Material &mtl = hit.obj->mtl; 1.107 + 1.108 + Vector3 pos = hit.ray.origin + hit.ray.dir * hit.dist; 1.109 + Vector3 norm = hit.obj->hit_normal(hit); 1.110 + norm = normalize(transform(normal_matrix(hit.obj->get_matrix()), norm)); 1.111 + Vector3 vdir = -normalize(hit.ray.dir); 1.112 + 1.113 + float ior = mtl.ior; 1.114 + 1.115 + if(dot(norm, hit.ray.dir) > 0.0) { 1.116 + norm = -norm; 1.117 + ior = 1.0 / mtl.ior; 1.118 + } 1.119 + 1.120 + Vector3 color = scene->get_ambient(); 1.121 + 1.122 + // for each light, calculate local illumination 1.123 + int num_lights = scene->get_light_count(); 1.124 + for(int i=0; i<num_lights; i++) { 1.125 + const Light *lt = scene->get_light(i); 1.126 + Vector3 ldir = lt->get_position() - pos; 1.127 + Vector3 lcol = lt->get_color(pos); 1.128 + 1.129 + RayHit hit; 1.130 + if(!scene->intersect(Ray(pos, ldir), &hit) || hit.dist > 1.0) { 1.131 + // if we can see the light, calculate and add its contribution 1.132 + ldir.normalize(); 1.133 + float ndotl = positive(dot(norm, ldir)); 1.134 + Vector3 diffuse = mtl.diffuse * lcol * ndotl; 1.135 + 1.136 + Vector3 hdir = normalize(ldir + vdir); 1.137 + float ndoth = positive(dot(norm, hdir)); 1.138 + Vector3 specular = mtl.specular * lcol * pow(ndoth, mtl.roughness * 128.0); 1.139 + 1.140 + color = color + lerp(specular, diffuse, mtl.roughness); 1.141 + } 1.142 + } 1.143 + 1.144 + if(mtl.reflect > 1e-4 && iter < 6) { 1.145 + Ray reflray(pos, reflect(vdir, norm)); 1.146 + 1.147 + Vector3 rcol = ray_trace(reflray, iter + 1) * mtl.specular * mtl.reflect; 1.148 + color = color + rcol; 1.149 + } 1.150 + 1.151 + if(mtl.refract > 1e-4 && iter < 6) { 1.152 + Ray refrray(pos, refract(vdir, norm, ior)); 1.153 + 1.154 + Vector3 rcol = ray_trace(refrray, iter + 1) * mtl.specular * mtl.refract; 1.155 + color = color + rcol; 1.156 + } 1.157 + 1.158 + return color; 1.159 +}