C++光线跟踪器-只有一个对象出现在场景中

C++ Raytracer - Only one object appearing in scene

本文关键字:在场 一个对象 光线跟踪 -只 C++      更新时间:2023-10-16

我正在使用光线跟踪器渲染Sphereflake,但在尝试在场景中显示多个对象时遇到了问题。在下面的场景中,我只是想测试一个场景中有两个球体,但由于某种原因,场景中只出现一个球体,通常是半径最大的球体。

另一件奇特的事情是,即使场景中有一个相机集,输出窗口似乎总是显示中心的主要对象((0,0),屏幕坐标在[-1,-1]->[1,1]之间),而不是与相机坐标空间相关。

我不确定这是否是父层次结构问题,也不确定我是如何渲染对象的,但如果能深入了解问题持续存在的原因,我将不胜感激。

main.cpp(创建场景渲染对象)

#include <stdio.h>
#include <iostream>
#include <vector>
#include <math.h>
#include <glm.hpp>
#include <gtc/matrix_transform.hpp> 
#include <Raytracer/Raytracer.h>
using namespace glm;
using namespace Raytracer;
using namespace Raytracer::Scenes;
using namespace Raytracer::Objects;
/**
* Places a few spheres in the scene and adds some lights.
*
* @param scene The scene
*/
Scene *BuildScene(int depth, float aspect)
{
const int materialCount = 6;
vec3 colors[materialCount] =
{
vec3(1.0f, 0.0f, 0.0f),
vec3(1.0f, 1.0f, 0.0f),
vec3(0.0f, 1.0f, 0.0f),
vec3(0.0f, 1.0f, 1.0f),
vec3(0.0f, 0.0f, 1.0f),
vec3(1.0f, 0.0f, 1.0f)
};
Material *materials[materialCount];
for (int i = 0; i < materialCount; i++)
{
materials[i] = new Material();
if (materials[i] == NULL)
return NULL;
vec3 ambient = colors[i] * 0.01f;
materials[i]->SetAmbient(ambient);
materials[i]->SetDiffuse(colors[i]);
materials[i]->SetShininess(25.0f);
}
if (depth <= 0)
return NULL;
// Create the scene.
Scene *scene = new Scene();
if (scene == NULL)
return NULL;
Sphere * s1 = new Sphere(0.33f, materials[5]);
s1->SetPosition(vec3(5.0f, 0.0f, -2.0f));
Sphere * s2 = new Sphere(0.33f, materials[1]);
s2->SetPosition(vec3((5.0f, 0.33f, -2.0f));
s1->AddChild(s2);
// Create a light.
Light *light = new PointLight(vec3(10.0f));
if (light == NULL)
{
delete scene;
return NULL;
}
light->SetPosition(vec3(-5.0f, 3.0f, 2.0f));
scene->AddChild(light);
// Create a camera.
Camera *camera = new Camera(vec3(-2.0f, 2.0f, 4.0f), vec3(0.0f, 0.0f, 0.0f), 
vec3(0.0f, 1.0f, 0.0f), Camera::DefaultFov, aspect);
scene->AddChild(s1);
if (camera == NULL)
{
delete scene;
return NULL;
}
scene->AddChild(camera);
scene->SetActiveCamera(camera);
return scene;
}
/**
* Renders the scene and saves the result to a BMP file.
*
* @param fileName The name of the file
* @param width The image width
* @param height The image height
*/
void Render(const char *fileName, int width, int height)
{
if (fileName == NULL || width <= 0 || height <= 0)
return;
SimpleRenderer renderer;
renderer.SetAccelerator(new SimpleAccelerator());
renderer.SetIntegrator(new PhongIntegrator());
puts("Generiere Szene...");
Scene *scene = BuildScene(3, (float)width / height);
if (scene == NULL)
return;
puts("Rendere Bild...");
Image *image = renderer.Render(*scene, width, height);
if (image != NULL)
{
puts("Speichere Ergebnis...");
image->SaveBMP(fileName, 2.2f);
delete image;
}
delete scene;
}
/**
* The main program
*/
int main()
{
Render("image.bmp", 512, 512);
return 0;
}

具有如上所述的s1.radius = 0.33f & s2.radius = 0.33f的两个球体的场景的示例

场景1

具有s1.radius = 0.33f & s2.radius = 1.0f的两个球体的场景的另一个示例

场景2

正如你所看到的,相机作为一个视角似乎是无效的,因为无论球体的位置如何,唯一的区别是它的照明,但它将始终位于显示窗口的中心

由于s2是作为s1的子项附着的,因此它被绘制在X轴上比s1更远5个单位处:

s2->SetPosition(vec3((5.0f, 0.33f, -2.0f));
...
s1->AddChild(s2);

由于你的相机是向下看正x轴:

Camera *camera = new Camera(vec3(-2.0f, 2.0f, 4.0f), 
vec3(0.0f, 0.0f, 0.0f), vec3(0.0f, 1.0f, 0.0f), Camera::DefaultFov, aspect);

s2只是被拉到s1的后面。

事实证明,这不是我的场景生成器的问题,而是子/父属性在另一个类SceneObject*中如何工作的问题。无论如何,我修复了AddChild函数,现在代码可以与相机透视图和场景中的多个项目一起使用

相关文章: