c++ - 使用 Blinn-Phong 计算光线追踪器上像素的漫反射 r、g、b 值
问题描述
我正在尝试使用 Blinn-Phong 公式计算像素的 RGB 值。为此,我使用此功能:
Material getPixelColor(Ray ray, double min, int index, std::vector<Object*> Objects, std::vector<Object*> lightSources) {
Vector intersectionPoint = ray.getOrigin() + ray.getDirection() * min;
Vector n = Objects.at(index)->getNormalAt(intersectionPoint);
Vector reflectiondirection = ray.getDirection() - n * Vector::dot(ray.getDirection(), n) * 2;
Ray reflectionRay(intersectionPoint, reflectiondirection);
// check if ray intersects any other object;
double minimum = INFINITY;
int count = 0, indx = -1;
for (auto const& obj : Objects) {
double distance = obj->Intersect(reflectionRay);
if (minimum > distance) {
minimum = distance;
indx = count;
}
count++;
}
Material result(0,0,0);
if (recurseDepth >= 5 || indx == -1) {
recurseDepth = 0;
// Check if object is lit for each light source
for (auto const& light : lightSources) {
// Blinn-Phong
Vector lightDirection = (light->getPosition() - intersectionPoint).normalize();
double nl = Vector::dot(n, lightDirection);
nl = nl > 0 ? nl : 0.0;
result = result + (Objects.at(index)->getMaterial() * light->getMaterial() * nl);
}
}
else{
recurseDepth++;
result = result + getPixelColor(reflectionRay, minimum, indx, Objects, lightSources);
}
return result;
}
我得到的结果是这样的:
我一直试图找到几个小时的解决方案,但不能。我使用了错误的公式吗?
解决方案
经过大量研究,我删除了从其他对象获取颜色的部分:
Material getPixelColor(Ray ray, double min, int index, std::vector<Object*> Objects, std::vector<Object*> lightSources) {
Vector intersectionPoint = ray.getOrigin() + ray.getDirection() * min;
Vector n = Objects.at(index)->getNormalAt(intersectionPoint);
Material result(0,0,0);
// Check if object is lit for each light source
for (auto const& light : lightSources) {
//create a ray to the light and check if there is an object between the two
Vector lightDirection = (light->getPosition() - intersectionPoint).normalize();
Ray lightRay(intersectionPoint, lightDirection);
bool hit = false;
for (auto const& obj : Objects) {
double distance = obj->Intersect(lightRay);
if (INFINITY > distance && distance > 0.0001) {
hit = true;
break;
}
}
if (!hit) {
// Blinn-Phong
double nl = Vector::dot(n, lightDirection);
// clamp nl between 0 and 1
if (nl > 1.0) {
nl = 1.0;
}
else if (nl < 0.0) {
nl = 0.0;
}
result = result + (Objects.at(index)->getMaterial() * nl);
}
}
return result;
}
所以我得到了想要的结果:
推荐阅读
- r - 如何在R中的向量中找到唯一的数字对?
- reactjs - 在 React 中处理来自 api 的数据的最佳实践
- python - 无法清除机器人框架中的文本字段
- python - Web Scraping:如何获取课程
- php - PHP PDO Fetch 作为多维数组,第一列作为键,第二和第三作为键值对
- php - phpMyAdmin 中的 grab_globals.lib.php 中的 PHP 文件包含漏洞
- rundeck - 为什么上传文件时出错
- c# - Kendo Grid MVC - 服务器导出 Excel 日期时间字段(自定义格式)
- hp-uft - 在 Windows 应用程序中的 shopdropdown 菜单项时在 UFT 中出现“未指定的错误”
- reactjs - React 应用程序部署问题