From 63b6f5c937a2f0dcf93ef9fd1a586985c32631ce Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 22 Nov 2014 11:18:34 +0200 Subject: [PATCH] Add a raytracer example program --- .gitignore | 1 + Build | 7 ++++ examples/raytrace.cpp | 92 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 examples/raytrace.cpp diff --git a/.gitignore b/.gitignore index 8354f32..c773a80 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ temp /libmspmath.a /libmspmath.so /mspmath.pc +/raytrace /tests/test diff --git a/Build b/Build index 5e5297d..20875c4 100644 --- a/Build +++ b/Build @@ -12,4 +12,11 @@ package "mspmath" map "source" "include/msp"; }; }; + + program "raytrace" + { + source "examples/raytrace.cpp"; + require "mspcore"; + use "mspmath"; + }; }; diff --git a/examples/raytrace.cpp b/examples/raytrace.cpp new file mode 100644 index 0000000..2af8653 --- /dev/null +++ b/examples/raytrace.cpp @@ -0,0 +1,92 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace Msp; + +class RayTracer: public RegisteredApplication +{ +private: + Geometry::Shape *shape; + unsigned width; + unsigned height; + string filename; + UInt8 *pixels; + +public: + RayTracer(int, char **); + virtual ~RayTracer(); + + virtual int main(); +private: + void load_shape(); +}; + + +RayTracer::RayTracer(int argc, char **argv): + shape(0), + width(500), + height(500), + pixels(new UInt8[width*height]) +{ + GetOpt getopt; + getopt.add_option('w', "width", width, GetOpt::REQUIRED_ARG); + getopt.add_option('h', "height", height, GetOpt::REQUIRED_ARG); + getopt.add_argument("filename", filename, GetOpt::REQUIRED_ARG); + getopt(argc, argv); +} + +RayTracer::~RayTracer() +{ + delete shape; + delete[] pixels; +} + +int RayTracer::main() +{ + load_shape(); + + for(unsigned y=0; y ray(LinAl::Vector(0, 0, 5), LinAl::Vector(xf, yf, -2)); + Geometry::SurfacePoint points[4]; + unsigned count = shape->get_intersections(ray, points, 4); + UInt8 *pixel = pixels+y*width+x; + if(count) + *pixel = 255*(0.2+max(dot(points[0].normal, LinAl::Vector(0, 0, 1)), 0.0)*0.8); + else + *pixel = 0; + } + } + + IO::Buffered cout_buf(IO::cout); + IO::print(cout_buf, "P2\n"); + IO::print(cout_buf, "%d %d\n", width, height); + IO::print(cout_buf, "255\n"); + + for(unsigned y=0; y loader; + loader.load(parser); + shape = loader.get_shape().clone(); +} -- 2.45.2