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

实现一个简单的光线追踪渲染器是理解计算机图形学核心概念的绝佳方式。用C++从零开始写一个基础的光线追踪器,不需要复杂的库或框架,只需要基本的数学知识和对光线与物体交互的理解。
光线追踪的核心思想是从摄像机出发,向场景中的每个像素发射一条光线,然后计算这条光线是否与场景中的物体相交,如果相交,就根据光照模型计算该点的颜色。
主要步骤包括:
首先需要几个关键类:三维向量、光线、物体(如球体)、材质和颜色输出。
立即学习“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; }
};
球体是最容易求交的几何体之一。给定球心和半径,利用几何公式解二次方程判断是否有交点。
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; // 避免自相交
}
使用 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; // 暖色调漫反射
}
设置图像分辨率,遍历每个像素生成光线,尝试与球体相交,并记录颜色。
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++) {<br>
for (int x = 0; x < width; x++) {<br>
float u = (2.0f * (x + 0.5f) / width - 1.0f) * (float)width / height;<br>
float v = (2.0f * (y + 0.5f) / height - 1.0f);<br>
Ray ray(camera, Vec3(u, v, 0) - camera);<br>
float t;<br>
if (intersectSphere(ray, sphereCenter, sphereRadius, t)) {<br>
Vec3 hit = ray.pointAt(t);<br>
Vec3 normal = (hit - sphereCenter).normalize();<br>
Vec3 color = shade(ray, hit, normal, lightDir);<br>
int idx = (y * width + x) * 3;<br>
image[idx + 0] = (unsigned char)(color.x * 255);<br>
image[idx + 1] = (unsigned char)(color.y * 255);<br>
image[idx + 2] = (unsigned char)(color.z * 255);<br>
} else {<br>
int idx = (y * width + x) * 3;<br>
image[idx + 0] = image[idx + 1] = image[idx + 2] = 128; // 背景色<br>
}<br>
}<br>
}<br>
// 保存为PPM格式(简单图像格式)<br>
FILE* f = fopen("render.ppm", "w");<br>
fprintf(f, "P3\n%d %d\n255\n", width, height);<br>
for (int i = 0; i < width * height * 3; i += 3) {<br>
fprintf(f, "%d %d %d ", image[i], image[i+1], image[i+2]);<br>
}<br>
fclose(f);<br>
delete[] image;<br>
return 0;<br>}
基本上就这些。这个渲染器虽然只能画一个球,但已经包含了光线追踪的核心流程:光线生成、求交、着色、输出。后续可以扩展支持多个物体、镜面反射、阴影、纹理、BVH加速等。
以上就是C++如何实现一个简单的渲染器_C++从零开始实现一个光线追踪渲染器的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号