C++如何实现一个简单的渲染器_C++从零开始实现一个光线追踪渲染器
技术百科
穿越時空
发布时间:2025-12-21
浏览: 次 实现光线追踪需从摄像机向像素发射光线,计算与球体交点并着色。1. 定义Vec3和Ray类用于数学运算;2. 通过解二次方程实现光线与球体求交;3. 使用Lambert模型根据法线与光照方向夹角计算漫反射颜色;4. 在主循环中遍历像素生成光线,检测交点后着色并写入图像;5. 最终以PPM格式输出暖色调球体渲染结果。
实现一个简单的光线追踪渲染器是理解计算机图形学核心概念的绝佳方式。用C++从零开始写一个基础的光线追踪器,不需要复杂的库或框架,只需要基本的数学知识和对光线与物体交互的理解。
1. 光线追踪的基本原理
光线追踪的核心思想是从摄像机出发,向场景中的每个像素发射一条光线,然后计算这条光线是否与场景中的物体相交,如果相交,就根据光照模型计算该点的颜色。
主要步骤包括:
- 定义摄像机位置和图像平面
- 为每个像素生成一条方向光线
- 检测光线与物体的最近交点
- 计算交点处的颜色(考虑光源、材质、反射等)
- 将颜色写入图像
2. 基础数据结构设计
首先需要几个关键类:三维向量、光线、物体(如球体)、材质和颜色输出。
// 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 discrimi
nant = 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加速等。
# ai
# 几个
# 多个
# 只需要
# 是从
# 不需要
# 这条
# 数据结构
# 计算机
# 循环
# c++
# if
# int
# this
# operator
# char
# len
# 遍历
# const
# bool
# Float
# 景中
# 渲染器
# 球心
相关栏目:
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
AI推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
SEO优化<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
技术百科<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
谷歌推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
百度推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
网络营销<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
案例网站<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
精选文章<?muma echo $count; ?>
】
相关推荐
- Win11无法拖拽文件到任务栏怎么办_Win11开
- Mac的“调度中心”与“空间”怎么用_Mac多桌面
- c++的STL算法库find怎么用 在容器中查找指
- Win10怎样安装Word样式库_Win10安装W
- Win11截图快捷键是什么_Win11自带截图工具
- Win11怎么设置默认浏览器Chrome_Wind
- Win11怎么设置右键刷新选项_Windows11
- php485函数执行慢怎么优化_php485性能提
- Win11开机Logo怎么换_Win11自定义启动
- 手机php怎么转mp4_手机端php文件转mp4a
- c++的mutex和lock_guard如何使用
- Win11怎么更改任务栏颜色_Windows11个
- 如何在Golang中使用replace替换模块_指
- Bpmn 2.0的XML文件怎么画流程图
- 如何在Golang中使用闭包_封装变量与函数作用域
- Win11任务栏天气怎么关闭 Win11隐藏天气小
- Win11 explorer.exe频繁崩溃_修复
- 如何在Golang中优化文件读写性能_使用缓冲和并
- Go 中的 := 运算符:类型推导机制与使用边界详
- windows如何修改文件默认打开方式_windo
- Win10如何更改网络连接_Windows10以太
- 如何在 Pandas 中按元素交集合并两列字符串
- 如何诊断并终止卡死的 multiprocessin
- Win11怎么设置麦克风权限_允许应用访问Win1
- 作用域操作符会影响性能吗_php静态调用性能分析【
- Windows音频驱动无声音原因解析_声卡驱动错误
- 如何使用Golang log设置日志输出格式_Go
- 如何提升Golang JSON序列化性能_Gola
- Win11怎么忘记WiFi网络_Win11删除已保
- Python异步网络编程_aiohttp说明【指导
- c# Task.Yield 的作用是什么 它和Ta
- php做exe支持多线程吗_并发处理实现方式【详解
- Win11开机速度慢怎么优化_Win11系统启动加
- c++如何判断文件是否存在_c++ filesys
- Win11怎么查看局域网电脑_Windows 11
- XAMPP 启动失败(Apache 突然停止)的终
- Python代码测试策略_质量保障解析【教程】
- PHP的Workerman对架构扩展有啥帮助_应用
- 如何从 Go 的 map[string]inter
- Windows10怎么备份注册表_Windows1
- win11 OneDrive怎么彻底关闭 Win1
- Python配置文件操作教程_JSONINIYAM
- win11如何清理传递优化文件 Win11为C盘瘦
- mac本地php环境如何开启curl_curl扩展
- 如何在Golang中编写异步函数测试_Golang
- Mac怎么设置鼠标滚动速度_Mac鼠标设置详细参数
- Win11怎么设置任务栏透明_Windows11使
- Win11怎么解压RAR文件 Win11自带解压功
- 为什么Go需要go mod文件_Go go mod
- Win11怎么设置任务栏图标大小_Windows1

QQ客服