src/sampler.cc
branchpyrit
changeset 47 320d5d466864
child 48 a4913301c626
equal deleted inserted replaced
46:6493fb65f0b1 47:320d5d466864
       
     1 /*
       
     2  * scene.cc: screen sample generation and image reconstruction
       
     3  *
       
     4  * This file is part of Pyrit Ray Tracer.
       
     5  *
       
     6  * Copyright 2008  Radek Brich
       
     7  *
       
     8  * Permission is hereby granted, free of charge, to any person obtaining a copy
       
     9  * of this software and associated documentation files (the "Software"), to deal
       
    10  * in the Software without restriction, including without limitation the rights
       
    11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
       
    12  * copies of the Software, and to permit persons to whom the Software is
       
    13  * furnished to do so, subject to the following conditions:
       
    14  *
       
    15  * The above copyright notice and this permission notice shall be included in
       
    16  * all copies or substantial portions of the Software.
       
    17  *
       
    18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
       
    19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
       
    20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
       
    21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
       
    22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
       
    23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
       
    24  * THE SOFTWARE.
       
    25  */
       
    26 
       
    27 #include <math.h>
       
    28 
       
    29 #include "common.h"
       
    30 #include "scene.h"
       
    31 
       
    32 int DefaultSampler::initSampleSet()
       
    33 {
       
    34 	if ( phase == 0 )
       
    35 	{
       
    36 		phase++;
       
    37 		return w*h;
       
    38 	}
       
    39 	else
       
    40 	{
       
    41 		phase = -1;
       
    42 		return 0;
       
    43 	}
       
    44 }
       
    45 
       
    46 Sample* DefaultSampler::nextSample(Sample *prev)
       
    47 {
       
    48 	DefaultSample *s = new DefaultSample;
       
    49 	if (prev)
       
    50 	{
       
    51 		DefaultSample *sp = static_cast<DefaultSample*>(prev);
       
    52 		s->sx = sp->sx + 1;
       
    53 		s->sy = sp->sy;
       
    54 		if (s->sx >= w)
       
    55 		{
       
    56 			s->sx = 0;
       
    57 			s->sy++;
       
    58 		}
       
    59 		if (s->sy >= h)
       
    60 		{
       
    61 			delete s;
       
    62 			return NULL;
       
    63 		}
       
    64 		s->x = (Float)s->sx/h - (Float)w/h/2.0;
       
    65 		s->y = (Float)s->sy/h - 0.5;
       
    66 	}
       
    67 	else
       
    68 	{
       
    69 		s->x = -(Float)w/h/2.0;
       
    70 		s->y = -0.5;
       
    71 		s->sx = 0;
       
    72 		s->sy = 0;
       
    73 	}
       
    74 	return s;
       
    75 }
       
    76 
       
    77 void DefaultSampler::saveSample(Sample *samp, Colour &col)
       
    78 {
       
    79 	DefaultSample *sp = static_cast<DefaultSample*>(samp);
       
    80 	Float *buf = buffer + 3*(sp->sy*w + sp->sx);
       
    81 	*(buf++) = col.r;
       
    82 	*(buf++) = col.g;
       
    83 	*(buf++) = col.b;
       
    84 }
       
    85