# HG changeset patch # User Radek Brich # Date 1206538161 -3600 # Node ID 320d5d4668646f86b26d4cca4b63a8f0ce065c24 # Parent 6493fb65f0b1194c806f89847fe7cf1aa518fe7e move Sampler classes to sampler.cc fix demos, make whole project compilable again diff -r 6493fb65f0b1 -r 320d5d466864 ccdemos/spheres_shadow.cc --- a/ccdemos/spheres_shadow.cc Wed Mar 26 00:52:27 2008 +0100 +++ b/ccdemos/spheres_shadow.cc Wed Mar 26 14:29:21 2008 +0100 @@ -112,7 +112,9 @@ pyrit_verbosity = 2; Float *fdata = (Float *) malloc(w*h*3*sizeof(Float)); rt.setOversample(2); - rt.render(w, h, fdata); + DefaultSampler sampler(fdata, w, h); + rt.setSampler(&sampler); + rt.render(); struct image *img; new_image(&img, w, h, 3); diff -r 6493fb65f0b1 -r 320d5d466864 ccdemos/textures.cc --- a/ccdemos/textures.cc Wed Mar 26 00:52:27 2008 +0100 +++ b/ccdemos/textures.cc Wed Mar 26 14:29:21 2008 +0100 @@ -279,7 +279,9 @@ rt.setOversample(2); rt.setSubsample(1); rt.ambientocclusion(300, 5.0, 0.5); - rt.render(w, h, fdata); + DefaultSampler sampler(fdata, w, h); + rt.setSampler(&sampler); + rt.render(); struct image *img; new_image(&img, w, h, 3); diff -r 6493fb65f0b1 -r 320d5d466864 include/raytracer.h --- a/include/raytracer.h Wed Mar 26 00:52:27 2008 +0100 +++ b/include/raytracer.h Wed Mar 26 14:29:21 2008 +0100 @@ -3,7 +3,7 @@ * * This file is part of Pyrit Ray Tracer. * - * Copyright 2006, 2007 Radek Brich + * Copyright 2006, 2007, 2008 Radek Brich * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff -r 6493fb65f0b1 -r 320d5d466864 include/sampler.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/sampler.h Wed Mar 26 14:29:21 2008 +0100 @@ -0,0 +1,92 @@ +/* + * sampler.h: screen sample generation and image reconstruction + * + * This file is part of Pyrit Ray Tracer. + * + * Copyright 2008 Radek Brich + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef SAMPLER_H +#define SAMPLER_H + +#include "common.h" +#include "vector.h" + +using namespace std; + +/** + * sample + */ +class Sample +{ +public: + Float x,y; +}; + +/** + * A abstract sampler. + * It generates screen samples in coordinates between [-1..1] for height + * and [-w/h..w/h] for width. It works in phases: initSampleSet returns + * number of samples for each phase, then samples can be generated using + * nextSample method. The resulting colour of each sample should be returned + * via saveSample method. The sampler should save the results to given buffer + * and decide if other phase is needed. When the picture is complete, + * initSampleSet returns zero and picture can be read from buffer. + */ +class Sampler +{ +public: + Float *buffer; + int w,h; + + Sampler(Float *abuffer, int &aw, int &ah): buffer(abuffer), w(aw), h(ah) {}; + virtual ~Sampler() {}; + void resetBuffer(Float *abuffer, int &aw, int &ah) { buffer = abuffer; w = aw; h = ah; }; + virtual void init() = 0; + virtual int initSampleSet() = 0; + virtual Sample *nextSample(Sample *prev) = 0; + virtual void saveSample(Sample *samp, Colour &col) = 0; +}; + +/** + * default sample + */ +class DefaultSample: public Sample +{ + friend class DefaultSampler; + int sx,sy; +}; + +/** + * Default sampler. + */ +class DefaultSampler: public Sampler +{ + int phase; +public: + DefaultSampler(Float *abuffer, int &aw, int &ah): Sampler(abuffer, aw, ah), phase(-1) {}; + void init() { phase = 0; }; + int initSampleSet(); + Sample *nextSample(Sample *prev); + void saveSample(Sample *samp, Colour &col); +}; + +#endif diff -r 6493fb65f0b1 -r 320d5d466864 include/scene.h --- a/include/scene.h Wed Mar 26 00:52:27 2008 +0100 +++ b/include/scene.h Wed Mar 26 14:29:21 2008 +0100 @@ -3,7 +3,7 @@ * * This file is part of Pyrit Ray Tracer. * - * Copyright 2006, 2007 Radek Brich + * Copyright 2006, 2007, 2008 Radek Brich * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -30,6 +30,7 @@ #include #include +#include "sampler.h" #include "noise.h" #include "vector.h" #include "quaternion.h" @@ -56,62 +57,6 @@ }; /** - * sample - */ -class Sample -{ -public: - Float x,y; -}; - -/** - * A abstract sampler. - * It generates screen samples in coordinates between [-1..1] for height - * and [-w/h..w/h] for width. It works in phases: initSampleSet returns - * number of samples for each phase, then samples can be generated using - * nextSample method. The resulting colour of each sample should be returned - * via saveSample method. The sampler should save the results to given buffer - * and decide if other phase is needed. When the picture is complete, - * initSampleSet returns zero and picture can be read from buffer. - */ -class Sampler -{ -public: - Float *buffer; - int w,h; - - Sampler(Float *abuffer, int &aw, int &ah): buffer(abuffer), w(aw), h(ah) {}; - void resetBuffer(Float *abuffer, int &aw, int &ah) { buffer = abuffer; w = aw; h = ah; }; - virtual void init() = 0; - virtual int initSampleSet() = 0; - virtual Sample *nextSample(Sample *prev) = 0; - virtual void saveSample(Sample *samp, Colour &col) = 0; -}; - -/** - * default sample - */ -class DefaultSample: public Sample -{ - friend class DefaultSampler; - int sx,sy; -}; - -/** - * Default sampler. - */ -class DefaultSampler: public Sampler -{ - int phase; -public: - DefaultSampler(Float *abuffer, int &aw, int &ah): Sampler(abuffer, aw, ah), phase(-1) {}; - void init() { phase = 0; }; - int initSampleSet(); - Sample *nextSample(Sample *prev); - void saveSample(Sample *samp, Colour &col); -}; - -/** * a camera */ class Camera @@ -170,6 +115,7 @@ class Texture { public: + virtual ~Texture() {}; virtual Colour evaluate(Vector3 point) = 0; }; diff -r 6493fb65f0b1 -r 320d5d466864 src/Makefile --- a/src/Makefile Wed Mar 26 00:52:27 2008 +0100 +++ b/src/Makefile Wed Mar 26 14:29:21 2008 +0100 @@ -6,7 +6,7 @@ vpath %.cc $(ROOT)/src vpath %.h $(ROOT)/include -LIBOBJS=raytracer.o scene.o noise.o container.o kdtree.o octree.o +LIBOBJS=raytracer.o scene.o sampler.o noise.o container.o kdtree.o octree.o CCFLAGS+=-I$(ROOT)/include ### Targets ### @@ -42,7 +42,8 @@ ### Dependencies ### matrix.o: matrix.cc matrix.h vector.h common.h noise.o: noise.cc noise.h common.h -scene.o: scene.cc scene.h vector.h noise.h common.h +scene.o: scene.cc scene.h sampler.h vector.h noise.h common.h +sampler.o: sampler.cc sampler.h vector.h common.h container.o: container.cc container.h scene.h common.h kdtree.o: kdtree.cc kdtree.h scene.h common.h octree.o: octree.cc octree.h scene.h common.h diff -r 6493fb65f0b1 -r 320d5d466864 src/raytracer.cc --- a/src/raytracer.cc Wed Mar 26 00:52:27 2008 +0100 +++ b/src/raytracer.cc Wed Mar 26 14:29:21 2008 +0100 @@ -3,7 +3,7 @@ * * This file is part of Pyrit Ray Tracer. * - * Copyright 2006, 2007 Radek Brich + * Copyright 2006, 2007, 2008 Radek Brich * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff -r 6493fb65f0b1 -r 320d5d466864 src/raytracermodule.cc --- a/src/raytracermodule.cc Wed Mar 26 00:52:27 2008 +0100 +++ b/src/raytracermodule.cc Wed Mar 26 14:29:21 2008 +0100 @@ -622,7 +622,9 @@ printf("[pyrit] Running ray tracer\n"); ((RaytracerObject *)self)->raytracer->getTop()->optimize(); data = (Float *) malloc(w*h*3*sizeof(Float)); - ((RaytracerObject *)self)->raytracer->render(w, h, data); + DefaultSampler sampler(data, w, h); + ((RaytracerObject *)self)->raytracer->setSampler(&sampler); + ((RaytracerObject *)self)->raytracer->render(); if (!data) { Py_INCREF(Py_None); return Py_None; diff -r 6493fb65f0b1 -r 320d5d466864 src/sampler.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sampler.cc Wed Mar 26 14:29:21 2008 +0100 @@ -0,0 +1,85 @@ +/* + * scene.cc: screen sample generation and image reconstruction + * + * This file is part of Pyrit Ray Tracer. + * + * Copyright 2008 Radek Brich + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include + +#include "common.h" +#include "scene.h" + +int DefaultSampler::initSampleSet() +{ + if ( phase == 0 ) + { + phase++; + return w*h; + } + else + { + phase = -1; + return 0; + } +} + +Sample* DefaultSampler::nextSample(Sample *prev) +{ + DefaultSample *s = new DefaultSample; + if (prev) + { + DefaultSample *sp = static_cast(prev); + s->sx = sp->sx + 1; + s->sy = sp->sy; + if (s->sx >= w) + { + s->sx = 0; + s->sy++; + } + if (s->sy >= h) + { + delete s; + return NULL; + } + s->x = (Float)s->sx/h - (Float)w/h/2.0; + s->y = (Float)s->sy/h - 0.5; + } + else + { + s->x = -(Float)w/h/2.0; + s->y = -0.5; + s->sx = 0; + s->sy = 0; + } + return s; +} + +void DefaultSampler::saveSample(Sample *samp, Colour &col) +{ + DefaultSample *sp = static_cast(samp); + Float *buf = buffer + 3*(sp->sy*w + sp->sx); + *(buf++) = col.r; + *(buf++) = col.g; + *(buf++) = col.b; +} + diff -r 6493fb65f0b1 -r 320d5d466864 src/scene.cc --- a/src/scene.cc Wed Mar 26 00:52:27 2008 +0100 +++ b/src/scene.cc Wed Mar 26 14:29:21 2008 +0100 @@ -3,7 +3,7 @@ * * This file is part of Pyrit Ray Tracer. * - * Copyright 2006, 2007 Radek Brich + * Copyright 2006, 2007, 2008 Radek Brich * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -29,56 +29,6 @@ #include "common.h" #include "scene.h" -int DefaultSampler::initSampleSet() -{ - if ( phase == 0 ) - { - phase++; - return w*h; - } - else return 0; -} - -Sample* DefaultSampler::nextSample(Sample *prev) -{ - DefaultSample *s = new DefaultSample; - if (prev) - { - DefaultSample *sp = static_cast(prev); - s->sx = sp->sx + 1; - s->sy = sp->sy; - if (s->sx >= w) - { - s->sx = 0; - s->sy++; - } - if (s->sy >= h) - { - delete s; - return NULL; - } - s->x = (Float)s->sx/h - (Float)w/h/2.0; - s->y = (Float)s->sy/h - 0.5; - } - else - { - s->x = -(Float)w/h/2.0; - s->y = -0.5; - s->sx = 0; - s->sy = 0; - } - return s; -} - -void DefaultSampler::saveSample(Sample *samp, Colour &col) -{ - DefaultSample *sp = static_cast(samp); - Float *buf = buffer + 3*(sp->sy*w + sp->sx); - *(buf++) = col.r; - *(buf++) = col.g; - *(buf++) = col.b; -} - void Camera::rotate(const Quaternion &q) { /*