光线跟踪

来自CGTWiki
跳转至:导航搜索

Peter Shirley Ray Tracing

git地址

第一周

文件:Peter Shirley-Ray Tracing in One Weekend (2016).pdf

文件:光线跟踪入门(中文版).pdf

由于显示图像在ppm, 在这里我们使用 opencv 对图像进行显示。

  1. cv::Mat初识和它的六种创建方法
  2. Mat类对象元素的获取与赋值


矩阵与光线的转换

光线跟踪器坐标系

Ray CamereForRayTracing::getRayInWorldCoords(const double &u, const double &v)
{
	origin = _viewMatrix.getTrans();

	double l, r, t, b, n, f;
	_projectionMatrix.getFrustum(l, r, b, t, n, f);

	//相机坐标系下
	Vec3d lt(l, t, n);
	Vec3d rt(r, t, n);
	Vec3d lb(l, b, n);
	
	Vec3d hozLocal = rt - lt;
	Vec3d verLocal = lt - lb;

	Vec4d lb4 = Vec4d(lb,1.0);
	Vec4d hozLocal4 = Vec4d(hozLocal, 1.0);
	Vec4d verLocal4 = Vec4d(verLocal, 1.0);

	//世界坐标系下
	Vec4d lbw = lb4 * _viewMatrix;
	Vec4d hozWorld = hozLocal4 * _viewMatrix;
	Vec4d verWorld = verLocal4 * _viewMatrix;
	
	lower_left_corner.set(lbw.x(), lbw.y(), lbw.z());
	horizontal.set(hozWorld.x(), hozWorld.y(), hozWorld.z());
	vertical.set(verWorld.x(), verWorld.y(), verWorld.z());

	Ray ray;
	ray.origin = origin;
	Vec3d direction = lower_left_corner + horizontal*u +  vertical*v - ray.origin ;
	direction.normalize();
	ray.direction = direction;

	return ray;
}

输出区间[0,1)为double型的随机数

C++ 如何生成大随机数?

  1. drand48()[1]
#ifndef DRAND48_H  
#define DRAND48_H  
  
#include <stdlib.h>  
  
#define m 0x100000000LL  
#define c 0xB16  
#define a 0x5DEECE66DLL  
  
static unsigned long long seed = 1;  
  
double drand48(void)  
{  
    seed = (a * seed + c) & 0xFFFFFFFFFFFFLL;  
    unsigned int x = seed >> 16;  
    return  ((double)x / (double)m);  
      
}  
  
void srand48(unsigned int i)  
{  
    seed  = (((long long int)i) << 16) | rand();  
}  
  
#endif  

第二周

文件:Peter Shirley-Ray Tracing The Next Week (2016).pdf

余生

文件:Peter Shirley-Ray Tracing The Rest Of Your Life (2016).pdf