实现光线追踪需从摄像机向像素发射光线,计算与球体交点并着色。1. 定义Vec3和Ray类用于数学运算;2. 通过解二次方程实现光线与球体求交;3. 使用Lambert模型根据法线与光照方向夹角计算漫反射颜色;4. 在主循环中遍历像素生成光线,检测交点后着色并写入图像;5. 最终以PPM格式输出暖色调球体渲染结果。

实现一个简单的光线追踪渲染器是理解计算机图形学核心概念的绝佳方式。用C++从零开始写一个基础的光线追踪器,不需要复杂的库或框架,只需要基本的数学知识和对光线与物体交互的理解。
1. 光线追踪的基本原理
光线追踪的核心思想是从摄像机出发,向场景中的每个像素发射一条光线,然后计算这条光线是否与场景中的物体相交,如果相交,就根据光照模型计算该点的颜色。
主要步骤包括:
- 定义摄像机位置和图像平面
- 为每个像素生成一条方向光线
- 检测光线与物体的最近交点
- 计算交点处的颜色(考虑光源、材质、反射等)
- 将颜色写入图像
2. 基础数据结构设计
首先需要几个关键类:三维向量、光线、物体(如球体)、材质和颜色输出。
立即学习“C++免费学习笔记(深入)”;
// Vector3.h
struct Vec3 {
float x, y, z;
Vec3(float x = 0, float y = 0, float z = 0) : x(x), y(y), z(z) {}
Vec3 operator+(const Vec3& b) const { return Vec3(x + b.x, y + b.y, z + b.z); }
Vec3 operator-(const Vec3& b) const { return Vec3(x - b.x, y - b.y, z - b.z); }
Vec3 operator*(float t) const { return Vec3(x * t, y * t, z * t); }
float dot(const Vec3& b) const { return x * b.x + y * b.y + z * b.z; }
Vec3 cross(const Vec3& b) const { return Vec3(y * b.z - z * b.y, z * b.x - x * b.z, x * b.y - y * b.x); }
Vec3 normalize() const { float len = sqrtf(dot(*this)); return len > 0 ? (*this) * (1.0f / len) : *this; }
};
// Ray.h
struct Ray {
Vec3 origin, direction;
Ray(const Vec3& o, const Vec3& d) : origin(o), direction(d.normalize()) {}
Vec3 pointAt(float t) const { return origin + direction * t; }
};
3. 实现球体与光线求交
球体是最容易求交的几何体之一。给定球心和半径,利用几何公式解二次方程判断是否有交点。
bool intersectSphere(const Ray& ray, const Vec3& center, float radius, float& t) {
Vec3 oc = ray.origin - center;
float a = ray.direction.dot(ray.direction);
float b = 2.0f * oc.dot(ray.direction);
float c = oc.dot(oc) - radius * radius;
float discriminant = b * b - 4 * a * c;
if (discriminant
t = (-b - sqrtf(discriminant)) / (2.0f * a);
return t > 0.001f; // 避免自相交
}
4. 简单的着色与光照
使用 Lambert 漫反射模型进行着色。假设有一个方向光,颜色由法向与光照方向的夹角决定。
Vec3 shade(const Ray& ray, const Vec3& hitPoint, const Vec3& normal, const Vec3& lightDir) {
float diff = fmaxf(0.0f, normal.dot(lightDir.normalize() * -1));
return Vec3(1.0f, 0.8f, 0.6f) * diff; // 暖色调漫反射
}
5. 主渲染循环
设置图像分辨率,遍历每个像素生成光线,尝试与球体相交,并记录颜色。
int main() {
const int width = 800, height = 600;
unsigned char* image = new unsigned char[width * height * 3];
Vec3 camera(0, 0, -5);
Vec3 sphereCenter(0, 0, 0);
float sphereRadius = 1.0f;
Vec3 lightDir(-1, -1, -1);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
float u = (2.0f * (x + 0.5f) / width - 1.0f) * (float)width / height;
float v = (2.0f * (y + 0.5f) / height - 1.0f);
Ray ray(camera, Vec3(u, v, 0) - camera);
float t;
if (intersectSphere(ray, sphereCenter, sphereRadius, t)) {
Vec3 hit = ray.pointAt(t);
Vec3 normal = (hit - sphereCenter).normalize();
Vec3 color = shade(ray, hit, normal, lightDir);
int idx = (y * width + x) * 3;
image[idx + 0] = (unsigned char)(color.x * 255);
image[idx + 1] = (unsigned char)(color.y * 255);
image[idx + 2] = (unsigned char)(color.z * 255);
} else {
int idx = (y * width + x) * 3;
image[idx + 0] = image[idx + 1] = image[idx + 2] = 128; // 背景色
}
}
}
// 保存为PPM格式(简单图像格式)
FILE* f = fopen("render.ppm", "w");
fprintf(f, "P3\n%d %d\n255\n", width, height);
for (int i = 0; i < width * height * 3; i += 3) {
fprintf(f, "%d %d %d ", image[i], image[i+1], image[i+2]);
}
fclose(f);
delete[] image;
return 0;
}
基本上就这些。这个渲染器虽然只能画一个球,但已经包含了光线追踪的核心流程:光线生成、求交、着色、输出。后续可以扩展支持多个物体、镜面反射、阴影、纹理、BVH加速等。









