您的位置:首页 > 其它

Example 2 : Multi Sampling And Filtering Methods

2016-06-07 16:47 274 查看
#include<vector>
#include<iostream>
#include"Vector3.h"
#include"rgb.h"
#include"Image.h"
#include"Shape.h"
#include"Triangle.h"
#include"Sphere.h"
#include <fstream>
#include "Sample.h"
using namespace std;

#define IMAGE_WIDTH 512
#define IMAGE_HEIGHT 384
#define NUM_SAMPLES 16

int main()
{

Image im(IMAGE_WIDTH, IMAGE_HEIGHT);

int sample_type, r, c, s;

Vector2* xySamples = new Vector2[NUM_SAMPLES];

float x, y, total, tmp;

ofstream singleFile("single.ppm");
ofstream box_16File("box_16.ppm");

Sample sampler;

for (sample_type = 0; sample_type < 4; sample_type++)
{

for (r = 0; r < IMAGE_HEIGHT; r++)
{
for (c = 0; c < IMAGE_WIDTH; c++)
{
switch (sample_type)
{
case 0:
for (s = 0; s < NUM_SAMPLES; s++)
{
xySamples[s].setX(0);
xySamples[s].setY(0);
}
break;
case 1:
// jitter 理解为: 为一个pixel, 产生NUM_SAMPLES个偏移值
sampler.jitter(xySamples, NUM_SAMPLES);
//sampler.multiJitter()

// filter 理解为: 控制一个pixel,NUM_SAMPLES个偏移值的范围,从[0,1] -> [-0.5, 0.5]
sampler.boxFilter(xySamples, NUM_SAMPLES);
//sampler.cubicFilter()
break;

}
// this is a 2D function defined for sample locations in screen space,so it is evaluated directly instead of tracing rays.
total = 0;
for (s = 0; s < NUM_SAMPLES; s++)
{
x = c + xySamples[s].x();
y = r + xySamples[s].y();
tmp = (x*x + y*y) / 100;
total += 0.5f * (1.0f + (float)sin(tmp));
}
// 平均一个Pixel的颜色
total /= NUM_SAMPLES;

im.set(r, c, rgb(total, total, total));

}
}

if (sample_type == 0){
im.writePPM(singleFile);
}
else{
im.writePPM(box_16File);
}
}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Realistic Ray Tracin