我目前正在研究学校项目的光线跟踪器,对于本课程的这一步,我们需要使用灯光系统和Blinn-Phong
算法实现对象着色。
如果正确完成,下面是最终图像...
此时我还没有得到反射和阴影,只是试图让阴影首先工作并继续得到这样的结果图像......
非常明显地看到,应该具有最高镜面量的区域变绿,并且我在茶壶周围的某些地方会得到奇怪的黑色文物。我已经阅读和测试了几个小时,无法弄清楚原因!我90%确定我正确实施了算法。
这是我的代码与此问题相关...
void shade(RGB& shading, std::vector<Light> lights, Triangle t, Ray& r, Vector3 loc) {
float intensity = 1 / std::sqrt(lights.size());
for (std::size_t i = 0; i < lights.size(); ++i) {
Vector3 n = t.get_normal();
n.normalize();
Vector3 v = r.dir * -1;
v.normalize();
Vector3 l = lights[i].position - loc;
l.normalize();
Vector3 h = v + l;
h.normalize();
float diffuse = MAX(0, n.dot(l));
float spec = std::pow(MAX(0, n.dot(h)), t.fill.shine);
shading.r += (t.fill.kd * diffuse * t.fill.rgb.r + t.fill.ks * spec) * intensity;
shading.g += (t.fill.kd * diffuse * t.fill.rgb.g + t.fill.ks * spec) * intensity;
shading.b += (t.fill.kd * diffuse * t.fill.rgb.b + t.fill.ks * spec) * intensity;
}
}
// Main function
int main(int argc, char* argv[]) {
// Set input and output file names
std::string in_file = (argc > 1) ? argv[1] : "teapot-3.nff";
std::string out_file = (argc > 2) ? argv[2] : "output.ppm";
// Parse the NFF file and get the Viewpoint and Background data
NFFParser parser(in_file);
parser.read_file();
Viewpoint view = parser.getViewpoint();
Background background = parser.getBackground();
// Camera creation
Camera camera(view);
// Allocate array of pixels and create camera instance
Pixel* pixels = new Pixel[camera.x_res * camera.y_res];
// Collect object data to iterate over
std::vector<Polygon> polygons = parser.getPolygons();
std::vector<Patch> patches = parser.getPatches();
std::vector<Light> lights = parser.getLights();
std::vector<Triangle> triangles;
// Convert all polygons and patches to trianlges and
// build a vector of triangles to iterator over
for (std::size_t i = 0; i < polygons.size(); ++i) {
std::vector<Triangle> v = polygons[i].fan_to_triangles();
triangles.insert(triangles.end(), v.begin(), v.end());
}
for (std::size_t i = 0; i < patches.size(); ++i) {
std::vector<Triangle> v = patches[i].fan_to_triangles();
triangles.insert(triangles.end(), v.begin(), v.end());
}
std::cout << "Testing for intersections among objects..." << std::endl
<< "...this may take a moment..." << std::endl
<< "==========================================" << std::endl;
// Iterator over all pixels in the array
for (int y = 0; y < camera.y_res; ++y) {
for (int x = 0; x < camera.x_res; ++x) {
float t = INF;
Triangle closest;
// Map pixel to image plane coordinates
Vector3 image_location = camera.map_to_image(x, y);
Ray r(camera.e, image_location - camera.e);
// Iteration over Polygons
for (std::vector<Triangle>::iterator it = triangles.begin(); it != triangles.end(); ++it) {
if (it->intersects(&r, t) && t < r.t_min) {
closest = *it;
r.t_min = t;
}
}
if (r.t_min == INF) {
set_pixel(pixels, y, x, camera.y_res, background.rgb);
continue;
}
RGB shading;
shade(shading, lights, closest, r, image_location);
set_pixel(pixels, y, x, camera.y_res, shading);
}
}
// Write the array of pixels to the output PPM file
PPMWriter writer(out_file);
writer.write_pixels(pixels, camera.x_res, camera.y_res);
// Deallocate pixels array to avoid memory leaks
delete [] pixels;
return 0;
}
非常感谢任何指导或帮助!
修改 的
绿色着色是固定的,但RGB成分上限,但黑色瑕疵仍是一个问题。
答案 0 :(得分:2)
对于绿色位,在颜色计算中,您会溢出适合像素红色分量的内容。如果你看绿色附近的铜色(0xFE9E4E)与绿色区域(0x019D4F)中的颜色的RGB,你可以看到这个。
您需要在计算中加入一些溢出检查。