1 #include <msp/core/application.h>
2 #include <msp/core/getopt.h>
3 #include <msp/core/inttypes.h>
4 #include <msp/geometry/loader.h>
5 #include <msp/io/buffered.h>
6 #include <msp/io/print.h>
11 class RayTracer: public RegisteredApplication<RayTracer>
14 Geometry::Shape<double, 3> *shape;
21 RayTracer(int, char **);
30 RayTracer::RayTracer(int argc, char **argv):
34 pixels(new UInt8[width*height])
37 getopt.add_option('w', "width", width, GetOpt::REQUIRED_ARG);
38 getopt.add_option('h', "height", height, GetOpt::REQUIRED_ARG);
39 getopt.add_argument("filename", filename, GetOpt::REQUIRED_ARG);
43 RayTracer::~RayTracer()
53 vector<Geometry::SurfacePoint<double, 3> > points(shape->get_max_ray_intersections());
55 for(unsigned y=0; y<height; ++y)
57 double yf = 1.0-y*2.0/height;
58 for(unsigned x=0; x<width; ++x)
60 double xf = x*2.0/width-1.0;
61 Geometry::Ray<double, 3> ray(LinAl::Vector<double, 3>(0, 0, 5), LinAl::Vector<double, 3>(xf, yf, -2));
62 unsigned count = shape->get_intersections(ray, &points[0], points.size());
63 UInt8 *pixel = pixels+y*width+x;
65 *pixel = 255*(0.2+max(dot(points[0].normal, LinAl::Vector<double, 3>(0, 0, 1)), 0.0)*0.8);
71 IO::Buffered cout_buf(IO::cout);
72 IO::print(cout_buf, "P2\n");
73 IO::print(cout_buf, "%d %d\n", width, height);
74 IO::print(cout_buf, "255\n");
76 for(unsigned y=0; y<height; ++y)
78 for(unsigned x=0; x<width; ++x)
79 IO::print(cout_buf, "%d ", pixels[y*width+x]);
80 IO::print(cout_buf, "\n");
86 void RayTracer::load_shape()
88 IO::BufferedFile in(filename);
89 DataFile::Parser parser(in, filename);
90 Geometry::Loader<double, 3> loader;
92 shape = loader.get_shape().clone();