<RayTracing In Weekend>����ʼ�

����RayTracing In A Weekendһ���ʵ��ϸ�ڡ�

Chapter1��Output an Image

����һ��ppm�����������ص���XnView��

��һ����������Ĵ��벢�������������õ�PPM��ʽ���ļ��Ļ�����Ҫ�ڴ��������д��txt�ļ��Ĵ��룺

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "pch.h"
#include <iostream>
#include <fstream>

using namespace std;
int main()
{
int nx = 200;
int ny = 100;

ofstream outfile(".\\result\\FirstPicture.txt", ios_base::out);
outfile << "P3\n" << nx << " " << ny << "\n255\n";
for (int j = ny - 1; j >= 0; j--)
{
for (int i = 0; i < nx; i++)
{
float r = float(i) / float(nx);
float g = float(j) / float(ny);
float b = 0.2;
int ir = int(255.99*r);
int ig = int(255.99*g);
int ib = int(255.99*b);
outfile << ir << " " << ig << " " << ib << "\n";
std::cout << ir << " " << ig << " " << ib << "\n";
}
}

return 0;
}

��VmView�п��Կ�����Ӧ��ͼƬ��

Chapter2��The vec3 class

������һ�����vec3���������

����������ļӼ��˳�������������ļӼ��˳��������IJ������ȡ�

1
2
3
4
5
6
7
8
9
10
11
inline float dot(const vec3 &v1, const vec3 &v2)
{
return v1.e[0] * v2.e[0] + v1.e[1] * v2.e[1] + v1.e[2] * v2.e[2];
}

inline vec3 cross(const vec3 &v1, const vec3 &v2)
{
return vec3((v1.e[1] * v2.e[2] - v1.e[2] * v2.e[1]),
(-(v1.e[0] * v2.e[2] - v1.e[2] * v2.e[0])),
(v1.e[0] * v2.e[1] - v1.e[1] * v2.e[0]));
}

Chapter3��Rays,a simple camera, and background

�����������࣬�������������������ֱ𷵻�ԭ�㡢�����Լ����߱��������߱�����һ��Vec3��ʾ��

Chapter4:

�����еĴ����м�����һ������������ཻ�ĺ���������������Դ�2D��������Բ���ཻ��⺯������ȥ���⡣

����Ὣ����뵽������ͷ�ļ��С�

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
bool sphere :: hit(const ray& r, float t_min, float t_max, hit_record& rec)const {
vec3 oc = r.origin() - center;

float a = dot(r.direction(), r.direction());
float b = dot(oc, r.direction());
float c = dot(oc, oc) - radius * radius;

float discriminant = b * b - a * c;
if (discriminant > 0)
{
float temp = (-b - sqrt(b*b - a * c)) / a;
if (temp < t_max && temp > t_min)
{
rec.t = temp;
rec.p = r.point_at_parameter(rec.t);
rec.normal = (rec.p - center) / radius;
return true;
}
temp = (-b + sqrt(b*b - a * c)) / a;
if (temp<t_max && temp>t_min)
{
rec.t = temp;
rec.p = r.point_at_parameter(rec.t);
rec.normal = (rec.p - center) / radius;
return true;
}
}
return false;
}

Chapter5��Surface normals and multiple objects.

�����˶Է��߷�������㣬������Ӧ�����߼�⺯�����뵽�˶�Ӧ��ͷ�ļ��С�

�����ĵ�����ij���������Ϊ�õ�ķ��߷���

���ⶨ����һ���࣬�������㲢�洢�����������������һ����о�������ԭ�������һ���㡣

Chapter6��Antialasing

��Ŀ��Ϊ����ݡ�

�������������ÿ�������ص�Ϊ���ģ��������Ϊ1�ķ�Χ�ڲ���n�Σ���n��������ֵ����ƽ����Ϊ�õ����յ�����ֵ��

������ͼ���������Ƶķ�ʽ����Ϊ��ֵ�˲���

1
2
3
4
5
6
7
8
9
10
11
vec3 col(0, 0, 0);
for (int s = 0; s < ns; s++)
{
float random = rand() % (100) / (float)(100);
float u = float(i + random) / float(nx);
float v = float(j + random) / float(ny);
ray r = cam.get_ray(u, v);
vec3 p = r.point_at_parameter(2.0);
col += color(r, world);
}
col /= float(ns);

Chapter7��Diffuse Materials

��Ŀ��Ϊ��������ʡ�

Pick a random point s from the unit radius sphere that is tangent to the hitpoint, and send a ray from the hitpoint p to the random point s. That sphere has center (p+N):

�������е����еĵ�λ�뾶����ѡ��һ�������s������һ�����ߴ����е����������s������Ϊ��p+N��

����������ǽ����䷽�����һ������ķ�����Ϊ������ķ���

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
vec3 random_in_unit_sphere()
{
vec3 p;
do {
float random0 = rand() % (100) / (float)(100);
float random1 = rand() % (100) / (float)(100);
float random2 = rand() % (100) / (float)(100);
p = 2.0*vec3(random0, random1, random2) - vec3(1, 1, 1);
} while (p.squared_length() >= 1.0);

return p;
}

vec3 color(const ray& r, hitable *world)
{
hit_record rec;
if (world->hit(r, 0.001, (numeric_limits<float>::max)(), rec))
{
vec3 target = rec.p + rec.normal + random_in_unit_sphere();
return 0.5*color(ray(rec.p,target - rec.p), world);
}
else
{
vec3 unit_direction = unit_vector(r.direction());
float t = 0.5*(unit_direction.y() + 1.0);
return (1.0 - t)*vec3(1.0, 1.0, 1.0) + t * vec3(0.5, 0.7, 1.0);
}
}

Chapter8��Metal

�ڰ��¼����˼򵥵IJ��ʡ�

���Ƕ�����һ���򵥵��ʲ������������������һ���ʣ�����ġ����֡����������������ԡ����۹۲��ߵ��ӽ���Σ��ʲ�����Թ۲��ߵı������ȶ�����ͬ�ġ�

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#ifndef LANBERTIANH
#define LANBERTIANH

#include "material.h"
class lambertian :public material
{
public:
lambertian(const vec3& a):albedo(a){}
virtual bool scatter(const ray& r_in, const hit_record& rec, vec3& attenuation, ray& scattered)const;
vec3 albedo;
};

vec3 random_in_unit_sphere() {
vec3 p;
do {
float random0 = rand() % (100) / (float)(100);
float random1 = rand() % (100) / (float)(100);
float random2 = rand() % (100) / (float)(100);
p = 2.0*vec3(random0, random1, random2) - vec3(1, 1, 1);
} while (p.squared_length() >= 1.0);
return p;
}

bool lambertian::scatter(const ray& r_in, const hit_record& rec, vec3& attenuation, ray& scattered) const {
vec3 target = rec.p + rec.normal + random_in_unit_sphere();
scattered = ray(rec.p, target - rec.p);
attenuation = albedo;
return true;
}

#endif // !LANBERTIANH

ע��ԭ��������������û���ҵ���Ӧ�����ã���������ݴ�������rand()���档

����ԭ�黹������һ�����淴���࣬�����ķ��亯���ο�����������

1
2
3
4
vec3 reflect(const vec3& v, const vec3& n)
{
return v - 2 * dot(v, n)*n;
}

�ڱ��µ��������������һ��ģ���IJ��������ģ���IJ���ʹ�÷�����ߵķ�������һ������ƫ�ơ�

Chapter9��Dielectrics

��һ�µı���Ϊ����ʣ�ʵ������˵�������ݰ���������ˮ����͸���Ŀ���������ߵIJ��ʣ�

1
2
3
4
5
6
7
8
9
10
11
12
bool refract(const vec3& v, const vec3& n, float ni_over_nt, vec3& refracted)
{
vec3 uv = unit_vector(v);
float dt = dot(uv, n);
float discriminant = 1.0 - ni_over_nt * ni_over_nt*(1 - dt * dt);
if (discriminant > 0)
{
refracted = ni_over_nt * (uv - n * dt) - n * sqrt(discriminant);
return true;
}
else return false;
}

ע�⣬���������жϰ���һ��ȫ����������

��������Ĺ�ʽ��ο���˹��������)

Chapter10��Positionable camera

�ڵ�ʮ�£����Ƕ�����һ������͸�ӹ��ܵ�������ڹ۲쳡����

��������λ�ã��۲췽��fov�ȡ�

Chapter11��Defocus Blur

��Ŀ�ķ���Ϊɢ��ģ�����������ͷ�ļ��м�����һ�����Ȧ�뽹�ർ�³���ģ���Ĺ��ܡ�

Chapter12��

��ʮ��������һ���ܽ��������������˽�����Ҫ������һЩϸ�ڣ�

You now have a cool ray tracer! What next?

  1. Lights. You can do this explicitly, by sending shadow rays to lights. Or it can be done implicitly by making some objects emit light,
  2. biasing scattered rays toward them, and then downweighting those rays to cancel out the bias. Both work. I am in the minority in favoring the latter approach.
  3. Triangles. Most cool models are in triangle form. The model I/O is the worst and almost everybody tries to get somebody else��s code to do this.
  4. Surface textures. This lets you paste images on like wall paper. Pretty easy and a good thing to do.
  5. Solid textures. Ken Perlin has his code online. Andrew Kensler has some very cool info at his blog.
  6. Volumes and media. Cool stuff and will challenge your software architecture. I favor making volumes have the hitable interface and probabilistically have intersections based on density. Your rendering code doesn��t even have to know it has volumes with that method.
  7. Parallelism. Run N copies of your code on N cores with different random seeds. Average the N runs. This averaging can also be done hierarchically where N/2 pairs can be averaged to get N/4 images, and pairs of those can be averaged. That method of parallelism should extend well into the thousands of cores with very little coding.

Have fun, and please send me your cool images!

��������һ���ܿ��Ray Tracer�ˣ���ô�������أ�

  1. �ƣ�������ͨ����ƹⷢ����Ӱ��������ȷ��ִ�д˲���������������ͨ��ʹһЩ���巢������ʽ����ɡ�
  2. ��ɢ�����ƫ�����ǣ�Ȼ�������Щ�����Ե���ƫ����߶���Ч������Ϊ�������ϲ����һ���������ˡ�
  3. �����Σ�����ģ���������εġ�ģ��I/O�������ģ�����ÿ���˶���ͼ���Ʊ��˵Ĵ�����������һ�㣨I/O����
  4. �����������������Խ�������ǽֽһ�����������ϡ��dz��򵥣�Ҳ��һ��ֵ��ȥ���ĺ��¡�
  5. ��̵������� Ken Perlin�����ṩ���Ĵ��롣 Andrew Kensler�����IJ�������һЩ�dz������Ϣ��
  6. ����ý�塣�ܿ�Ķ���������ս���������ܹ������޳�ʹ�����пɵ����Ľ��棬���Ҹ����Ͼ��л����ܶȵĽ���㡣���ij��ִ�����������֪�������и÷����ľ����������Ҳ��Ǿ��������ɡ���
  7. ���С�ʹ�ò�ͬ�����������N������������N�����븱����ƽ��N�����С�����ƽ��Ҳ���Էֲ�ε���ɣ�����N / 2�Կ��Ա�ƽ���Ի��N / 4��ͼ�񣬲��ҿ��Զ���Щͼ��ĶԽ���ƽ�������ֲ��з���Ӧ�ÿ��Ժܺõ���չ����ǧ���ں��С�

���������Ȥ���뽫��ܿ��ͼ���͸��Ұɡ�