提要
本文先介绍光线追踪的理论,然后着重一步一步来搭建渲染场景,从最基本的向量类开始.采用的语言是c++,利用面向对象的思想,一些基础的线性代数和空间几何的知识也会用到,编程的框架用的是GLFW,渲染用到的是OpenGL。
原理
光线追踪,简单地说,就是从摄影机的位置,通过影像平面上的像素位置(比较正确的说法是取样(sampling)位置),发射一束光线到场景,求光线和几何图形间最近的交点,再求该交点的著色。如果该交点的材质是反射性的,可以在该交点向反射方向继续追踪。光线追踪除了容易支持一些全局光照效果外,亦不局限于三角形作为几何图形的单位。任何几何图形,能与一束光线计算交点(intersection point),就能支持。
示意图如下:
光线追踪有一些很棒的特性,比如:能够生成高度真实感的图形,特别是对于表面光滑的对象,缺点是所需的计算量大的惊人.
原理其实非常的简单,但具体实现起来的时候会有很多细节的地方.
代码实现
向量类
可以表示空上的点(x,y,z).
gvector3.h
<span style="font-size:14px;">#ifndef GVECTOR3_H
#define GVECTOR3_H
#include <iostream>
#include <cmath>
#define MIN(x,y) (x)>(y)?(y):(x);
#define MAX(x,y) (x)>(y)?(x):(y);
using namespace std;
class GVector3
{
public:
float x;
float y;
float z;
// 缺省构造函数
GVector3();
~GVector3();
// 用户构造函数
GVector3(float posX, float posY, float posZ);
//输出向量信息
void getInfo();
//矢量加法
GVector3 operator+(GVector3 v);
//矢量减法
GVector3 operator-(GVector3 v);
//数乘
GVector3 operator*(float n);
//数除
GVector3 operator/(float n);
//向量点积
float dotMul(GVector3 v2);
//向量叉乘
GVector3 crossMul(GVector3 v2);
//绝对值化
GVector3 abs();
//获取分量中的最大值
float max();
//获取分量的最小值
float min();
//获取矢量长度
float getLength();
//向量单位化
GVector3 normalize();
//求两点之间的距离
float getDist(GVector3 v);
//返回零向量
static inline GVector3 zero(){ return GVector3(0,0,0); }
//打印向量的分量值
void show();
};
#endif // GVECTOR3_H
</span>
gvector3.cpp
#include "gvector3.h"
GVector3::GVector3()
{
}
GVector3::~GVector3()
{
}
GVector3::GVector3(float posX, float posY, float posZ)
{
x=posX;
y=posY;
z=posZ;
}
GVector3 GVector3::operator+(GVector3 v)
{
return GVector3(x+v.x,v.y+y,v.z+z);
}
GVector3 GVector3::operator-(GVector3 v)
{
return GVector3(x-v.x,y-v.y,z-v.z);
}
GVector3 GVector3::operator*(float n)
{
return GVector3(x*n,y*n,z*n);
}
GVector3 GVector3::operator/(float n)
{
return GVector3(x/n,y/n,z/n);
}
void GVector3::getInfo()
{
cout<<"x:"<<x<<" y:"<<y<<" z:"<<z<<endl;
}
GVector3 GVector3::abs()
{
if(x<0) x*=-1;
if(y<0) y*=-1;
if(z<0) z*=-1;
return GVector3(x,y,z);
}
float GVector3::dotMul(GVector3 v2)
{
return (x*v2.x+y*v2.y+z*v2.z);
}
GVector3 GVector3::crossMul(GVector3 v2)
{
GVector3 vNormal;
// 计算垂直矢量
vNormal.x = ((y * v2.z) - (z * v2.y));
vNormal.y = ((z * v2.x) - (x * v2.z));
vNormal.z = ((x * v2.y) - (y * v2.x));
return vNormal;
}
float GVector3::getLength()
{
return (float)sqrt(x*x+y*y+z*z);
}
GVector3 GVector3::normalize()
{
float length=getLength();
x=x/length;
y=y/length;
z=z/length;
return GVector3(x,y,z);
}
void GVector3::show()
{
cout<<"x:"<<x<<" y:"<<y<<" z"<<z<<endl;
}
float GVector3::max()
{
float tmp=MAX(y,z);
return MAX(x,tmp);
}
float GVector3::min()
{
float tmp=MIN(y,z);
return MIN(x,tmp);
}
float GVector3::getDist(GVector3 v)
{
float tmp=(x-v.x)*(x-v.x)+(y-v.y)*(y-v.y)+(z-v.z)*(z-v.z);
return sqrt(tmp);
}
着重解释一下向量的点乘和叉乘。
点乘,也叫向量的内积、数量积。顾名思义,求下来的结果是一个数。 在物理学中,已知力与位移求功,实际上就是求向量F与向量s的内积,即要用点乘。
叉乘,也叫向量的外积、向量积。顾名思义,求下来的结果是一个向量,记这个向量为c。 向量c的