Back to work on my raytracer. I didn't get it done by the deadline, but I'm still interested in making it work. I'm having trouble with smart pointers deleting themselves before I'm done using them.
std::shared_ptr<Image> render(int width, int height) const //the render function, part of the Scene class
{
std::shared_ptr<Vector4> dir(new Vector4), origin(new Vector4);
std::shared_ptr<SceneObject> hitobj;
std::shared_ptr<Intersection> i;
//irrelevant code here
if (trace_ray(origin,dir,hitobj))
i = hitobj->intersect(*origin,*dir);
//more code
}
bool trace_ray(std::shared_ptr<Vector4> origin, std::shared_ptr<Vector4> dir, std::shared_ptr<SceneObject> hitobj) const
{
bool hit = false;
//irrelevant stuff
hit = true;
hitobj = _objects[x]; // _objects is a vector of std::shared_ptr<SceneObject>
return hit;
}
As soon as the function returns, the shared pointer hitobj resets, even though I declared it in the render function. This causes the intersect to always fail, resulting in a blank scene.
There's a similar function that sets dir and origin. These work properly only when initialized as shown in the render function. Doing so inside the similar function leads to the same issue.
SceneObject can't be fixed in the same way. I tried setting up a dummy SceneObject, but the class doesn't support assignment. Even redirecting the pointer in the function, it just reverted to the dummy object after the function call.