0,引入
这个场景中包含两匹马,不知道怎么起名,所以就叫“Two Horses”啦。还有就是用到各种反射模型、各种光照模型、各种纹理、PLY格式的几何模型。
贴图如下:
1,测试代码
接下来,贴出World::build()的代码,完整代码参考后面的下载链接。
#include "World.h"
#include "Ambient.h"
#include "Pinhole.h"
#include "Directional.h"
#include "PointLight.h"
#include "RayCast.h"
#include "Whitted.h"
#include "Matte.h"
#include "Plane.h"
#include "Phong.h"
#include "MultiJittered.h"
#include "AmbientOccluder.h"
#include "Emissive.h"
#include "AreaLight.h"
#include "Rectangle.h"
#include "AreaLighting.h"
#include "Instance.h"
#include "Disk.h"
#include "Grid.h"
#include "CubicNoise.h"
#include "Image.h"
#include "FBmTextureRamp.h"
#include "InstanceTexture.h"
#include "SV_Matte.h"
#include "SV_Phong.h"
#include "Wood.h"
#include "Checker3D.h"
#include "Dielectric.h"
#include "Annulus.h"
#include "TorusPartConvex.h"
#include "SphericalMap.h"
#include "Image.h"
#include "ImageTexture.h"
#include "OpenCylinder.h"
#include "Reflective.h"
#include "SolidCylinder.h"
#include "SolidCylinderChecker.h"
#include "SV_PlaneChecker.h"
#include "SV_Reflective.h"
#include "SV_GlossyReflector.h"
#include <iostream>
#include <fstream>
using namespace std;
void
World::build(void){
int num_samples = 256;
int a = 1;
vp.set_hres(600/a); // 600*500
vp.set_vres(500/a);
vp.set_samples(num_samples);
vp.set_max_depth(4);
// tracer_ptr = new RayCast(this);
// tracer_ptr = new Whitted(this);
tracer_ptr = new AreaLighting(this);
Pinhole* pinhole_ptr = new Pinhole;
pinhole_ptr->set_eye(20, 10, 80);
pinhole_ptr->set_lookat(-4, -2, -2);
pinhole_ptr->set_view_distance(3600/a); // 3600
pinhole_ptr->compute_uvw();
set_camera(pinhole_ptr);
Ambient* ambient_ptr = new Ambient;
ambient_ptr->scale_radiance(5.0);
set_ambient_light(ambient_ptr);
MultiJittered* sampler_ptr = new MultiJittered(num_samples);
AmbientOccluder* occluder_ptr = new AmbientOccluder;
occluder_ptr->scale_radiance(1.0);
occluder_ptr->set_min_amount(0.0);
occluder_ptr->set_sampler(sampler_ptr);
// set_ambient_light(occluder_ptr);
// emissive sphere
Emissive* emissive_ptr = new Emissive;
emissive_ptr->scale_radiance(8.0);
emissive_ptr->set_ce(white);
Disk* disk_ptr = new Disk(Point3D(2.0, 10.0, 10.0), 1.5, Normal(0, -1, 0));
disk_ptr->set_material(emissive_ptr);
disk_ptr->set_sampler(new MultiJittered(num_samples));
add_object(disk_ptr);
AreaLight* area_light_ptr = new AreaLight;
area_light_ptr->set_object(disk_ptr);
area_light_ptr->set_cast_shadow(true);
add_light(area_light_ptr);
PointLight* point_light_ptr1 = new PointLight;
point_light_ptr1->set_location(30, 10, 30);
point_light_ptr1->scale_radiance(6.0);
point_light_ptr1->set_cast_shadow(false);
// add_light(point_light_ptr1);
plane left
Plane* plane_left_ptr = new Plane();
// wood texture
RGBColor light_color(0.5, 0.2, 0.065);
RGBColor dark_color(0.05);
Wood* wood_ptr = new Wood(light_color, dark_color);
wood_ptr->set_grainy(1.0);
wood_ptr->set_ringy(1.0);
InstanceTexture* transformed_wood_plane_ptr = new InstanceTexture(wood_ptr);
transformed_wood_plane_ptr->scale(0.4);
transformed_wood_plane_ptr->rotate_z(110);
SV_PlaneChecker* sv_plane_checker_left_ptr = new SV_PlaneChecker;
sv_plane_checker_left_ptr->set_size(2.0);
sv_plane_checker_left_ptr->set_line_width(0.4);
sv_plane_checker_left_ptr->set_color1(new ConstantColor(RGBColor(0.5)));
sv_plane_checker_left_ptr->set_color2(new ConstantColor(RGBColor(0.5)));
sv_plane_checker_left_ptr->set_line_color(transformed_wood_plane_ptr);
// textured material:
float exp_left = 1000.0;
SV_GlossyReflector* sv_glossy_left_ptr = new SV_GlossyReflector;
sv_glossy_left_ptr->set_samples(num_samples, exp_left);
sv_glossy_left_ptr->set_ka(0.1); // 0.4
sv_glossy_left_ptr->set_kd(0.25); // 0.8
sv_glossy_left_ptr->set_ks(0.1); // 0.5
sv_glossy_left_ptr->set_exp(exp_left); // for phong
sv_glossy_left_ptr->set_cd(sv_plane_checker_left_ptr);
sv_glossy_left_ptr->set_kr(0.75);
sv_glossy_left_ptr->set_exponent(exp_left); // for glossy.
sv_glossy_left_ptr->set_cr(new ConstantColor(white)); // green
Instance* instance_plane_left_ptr = new Instance(plane_left_ptr);
instance_plane_left_ptr->rotate_z(90);
instance_plane_left_ptr->translate(-4, 0, 0);
instance_plane_left_ptr->set_material(sv_glossy_left_ptr);
add_object(instance_plane_left_ptr);
rectangle back 1
Rectangle* rectangle_back_ptr1 = new Rectangle(Point3D(-4, -2, 0), Vector3D(20, 0, 0), Vector3D(0, 2, 0), Normal(0, 0, 1));
// noise:
CubicNoise* noise_back1_ptr = new CubicNoise;
noise_back1_ptr->set_num_octaves(6);
noise_back1_ptr->set_gain(0.5);
noise_back1_ptr->set_lacunarity(2.0);
// ramp image:
Image* image_back1_ptr = new Image;
image_back1_ptr->read_ppm_file(".\\TextureFiles\\ppm\\sandstone_ramp4.ppm");
// marble texture:
FBmTextureRamp* texture_back1_ptr = new FBmTextureRamp(image_back1_ptr);
texture_back1_ptr->set_noise(noise_back1_ptr);
texture_back1_ptr->set_perturbation(0.1);
InstanceTexture* it_back1_ptr = new InstanceTexture(texture_back1_ptr);
it_back1_ptr->scale(0.5);
it_back1_ptr->rotate_z(60);
it_back1_ptr->translate(1.0, 4.0, 0.0);
// textured material:
float exp_back1 = 100.0;
SV_GlossyReflector* sv_glossy_back1_ptr = new SV_GlossyReflector;
sv_glossy_back1_ptr->set_samples(num_samples, exp_back1);
sv_glossy_back1_ptr->set_ka(0.1); // 0.4
sv_glossy_back1_ptr->set_kd(0.25); // 0.8
sv_glossy_back1_ptr->set_ks(0.1); // 0.5
sv_glossy_back1_ptr->set_exp(exp_back1); // for phong
sv_glossy_back1_ptr->set_cd(it_back1_ptr);
sv_glossy_back1_ptr->set_kr(0.75);
sv_glossy_back1_ptr->set_exponent(exp_back1); // for glossy.
sv_glossy_back1_ptr->set_cr(new ConstantColor(white)); // green
Instance* instance_rectangle_back1_ptr = new Instance(rectangle_back_ptr1);
instance_rectangle_back1_ptr->translate(0, 0, -4);
instance_rectangle_back1_ptr->set_material(sv_glossy_back1_ptr);
add_object(instance_rectangle_back1_ptr);
rectangle back 2
Rectangle* rectangle_back_ptr2 = new Rectangle();
// image:
Image* image_back_ptr2 = new Image;
image_back_ptr2->read_ppm_file(".\\TextureFiles\\ppm\\CloudsLowRes.ppm");
// image based texture:
ImageTexture* texture_image_ptr = new ImageTexture;
texture_image_ptr->set_image(image_back_ptr2);
// material:
SV_Phong* sv_phong_back_ptr2 = new SV_Phong;
sv_phong_back_ptr2->set_ka(0.1);
sv_phong_back_ptr2->set_kd(0.5);
sv_phong_back_ptr2->set_cd(texture_image_ptr);
sv_phong_back_ptr2->set_ks(0.1);
sv_phong_back_ptr2->set_exp(20.0);
Instance* instance_rectangle_back2_ptr = new Instance(rectangle_back_ptr2);
instance_rectangle_back2_ptr->scale(10, 1, 4);
instance_rectangle_back2_ptr->rotate_x(90);
instance_rectangle_back2_ptr->translate(6, 4, -4);
instance_rectangle_back2_ptr->set_material(sv_phong_back_ptr2);
add_object(instance_rectangle_back2_ptr);
plane down
Plane* plane_down_ptr = new Plane();
// noise:
CubicNoise* noise_down_ptr = new CubicNoise;
noise_down_ptr->set_num_octaves(6);
noise_down_ptr->set_gain(0.5);
noise_down_ptr->set_lacunarity(2.0);
// ramp image:
Image* image_down_ptr = new Image;
image_down_ptr->read_ppm_file(".\\TextureFiles\\ppm\\BlueMarbleRamp.ppm");
// marble texture:
FBmTextureRamp* texture_down_ptr = new FBmTextureRamp(image_down_ptr);
texture_down_ptr->set_noise(noise_down_ptr);
texture_down_ptr->set_perturbation(4.0);
InstanceTexture* it_down_ptr = new InstanceTexture(texture_down_ptr);
// it_down_ptr->scale(0.5);
it_down_ptr->rotate_z(60);
it_down_ptr->translate(1.0, 4.0, 0.0);
SV_PlaneChecker* sv_plane_checker_down_ptr = new SV_PlaneChecker;
sv_plane_checker_down_ptr->set_size(4.0);
sv_plane_checker_down_ptr->set_line_width(0.05);
sv_plane_checker_down_ptr->set_color1(it_down_ptr);
sv_plane_checker_down_ptr->set_color2(it_down_ptr);
sv_plane_checker_down_ptr->set_line_color(new ConstantColor(RGBColor(0.2)));
// textured material:
float exp_down = 1000.0;
SV_GlossyReflector* sv_glossy_down_ptr = new SV_GlossyReflector;
sv_glossy_down_ptr->set_samples(num_samples, exp_down);
sv_glossy_down_ptr->set_ka(0.1); // 0.4
sv_glossy_down_ptr->set_kd(0.25); // 0.8
sv_glossy_down_ptr->set_ks(0.1); // 0.5
sv_glossy_down_ptr->set_exp(exp_down); // for phong
sv_glossy_down_ptr->set_cd(sv_plane_checker_down_ptr);
sv_glossy_down_ptr->set_kr(0.75);
sv_glossy_down_ptr->set_exponent(exp_down); // for glossy.
sv_glossy_down_ptr->set_cr(new ConstantColor(white)); // green
Instance* instance_plane_down_ptr = new Instance(plane_down_ptr);
instance_plane_down_ptr->translate(0, -2, 0);
instance_plane_down_ptr->set_material(sv_glossy_down_ptr);
add_object(instance_plane_down_ptr);
horse
char* file_horse_name = ".\\PLYFiles\\Horse97K.ply";
Grid* grid_horse_ptr = new Grid(new Mesh);
grid_horse_ptr->read_smooth_triangles(file_horse_name);
grid_horse_ptr->setup_cells();
// textured material:
float exp_horse1 = 1000000.0;
SV_GlossyReflector* sv_glossy_horse_ptr1 = new SV_GlossyReflector;
sv_glossy_horse_ptr1->set_samples(num_samples, exp_horse1);
sv_glossy_horse_ptr1->set_ka(0.1); // 0.4
sv_glossy_horse_ptr1->set_kd(0.4); // 0.8
sv_glossy_horse_ptr1->set_ks(0.1); // 0.5
sv_glossy_horse_ptr1->set_exp(exp_horse1); // for phong
sv_glossy_horse_ptr1->set_cd(new ConstantColor(RGBColor(1.0, 0.2, 0.2)));
sv_glossy_horse_ptr1->set_kr(0.75);
sv_glossy_horse_ptr1->set_exponent(exp_horse1); // for glossy.
sv_glossy_horse_ptr1->set_cr(new ConstantColor(white)); // green
Instance* instance_horse_ptr1 = new Instance(grid_horse_ptr);
instance_horse_ptr1->scale(9);
instance_horse_ptr1->rotate_y(-30);
instance_horse_ptr1->translate(-1, 0.5, 14);
instance_horse_ptr1->set_material(sv_glossy_horse_ptr1);
add_object(instance_horse_ptr1);
// textured material:
float exp_horse2 = 1000000.0;
SV_GlossyReflector* sv_glossy_horse_ptr2 = new SV_GlossyReflector;
sv_glossy_horse_ptr2->set_samples(num_samples, exp_horse2);
sv_glossy_horse_ptr2->set_ka(0.1); // 0.4
sv_glossy_horse_ptr2->set_kd(0.4); // 0.8
sv_glossy_horse_ptr2->set_ks(0.1); // 0.5
sv_glossy_horse_ptr2->set_exp(exp_horse2); // for phong
sv_glossy_horse_ptr2->set_cd(new ConstantColor(RGBColor(0.5, 1.0, 0.5)));
sv_glossy_horse_ptr2->set_kr(0.75);
sv_glossy_horse_ptr2->set_exponent(exp_horse2); // for glossy.
sv_glossy_horse_ptr2->set_cr(new ConstantColor(white)); // green
Instance* instance_horse_ptr2 = new Instance(grid_horse_ptr);
instance_horse_ptr2->scale(9);
instance_horse_ptr2->rotate_y(-30);
instance_horse_ptr2->translate(2, 0.5, 8);
instance_horse_ptr2->set_material(sv_glossy_horse_ptr2);
add_object(instance_horse_ptr2);
}
2,输出图形
3,其他说明
完整代码下载链接:
http://download.csdn.net/detail/libing_zeng/9814873
4,补充图形
前面的图形参数:
单像素点采样次数为256;
back rectangle 1的参数:
instance_rectangle_back2_ptr->scale(10, 1, 4);
instance_rectangle_back2_ptr->rotate_x(90);
instance_rectangle_back2_ptr->translate(6, 4, -4);
补充的图形参数:
单像素点采样次数为16;
back rectangle 1的参数:
instance_rectangle_back2_ptr->scale(4, 1, 4);
instance_rectangle_back2_ptr->rotate_x(90);
instance_rectangle_back2_ptr->translate(0, 4, -4);
输出图形:
(天空的云看起来更为真实;但是,由于采样次数只有16,对于面积光源来说太少了,所以出现“白色”噪声点)
后来想想还是再生成一个“单像素点采样次数为256”的图形。
单像素点采样次数为256;
back rectangle 1的参数:
instance_rectangle_back2_ptr->scale(4, 1, 4);
instance_rectangle_back2_ptr->rotate_x(90);
instance_rectangle_back2_ptr->translate(0, 4, -4);
输出图形:
(这个图形是好几天之后生成的,所以没有包含在之前的代码包中)