gpt4 book ai didi

c++ - 指向派生类对象的指针丢失 vfptr

转载 作者:行者123 更新时间:2023-11-30 01:31:48 25 4
gpt4 key购买 nike

首先,我正在尝试编写一个普通的简单 Ray Tracer。在我的 Ray Tracer 中,世界上有多种类型的几何体,它们都派生自一个名为“SceneObject”的基类。我在这里包含了它的标题。

/**
Interface for all objects that will appear in a scene
*/
class SceneObject
{
public:
mat4 M, M_inv;
Color c;

SceneObject();
~SceneObject();

/**
The transformation matrix to be applied to all points
of this object. Identity leaves the object in world frame.
*/
void setMatrix(mat4 M);
void setMatrix(MatrixStack mStack);
void getMatrix(mat4& M);

/**
The color of the object
*/
void setColor(Color c);
void getColor(Color& c);

/**
Alter one portion of the color, leaving
the rest as they were.
*/
void setDiffuse(vec3 rgb);
void setSpecular(vec3 rgb);
void setEmission(vec3 rgb);
void setAmbient(vec3 rgb);
void setShininess(double s);

/**
Fills 'inter' with information regarding an intersection between
this object and 'ray'. Ray should be in world frame.
*/
virtual void intersect(Intersection& inter, Ray ray) = 0;

/**
Returns a copy of this SceneObject
*/
virtual SceneObject* clone() = 0;

/**
Print information regarding this SceneObject for debugging
*/
virtual void print() = 0;
};

如您所见,我包含了几个要在别处实现的虚函数。在本例中,我只有两个派生类——Sphere 和 Triangle,它们都实现了缺少的成员函数。最后,我有一个 Parser 类,它充满了执行实际“光线追踪”部分的静态方法。这是相关部分的几个片段

void Parser::trace(Camera cam, Scene scene, string outputFile, int maxDepth) {
int width = cam.getNumXPixels();
int height = cam.getNumYPixels();
vector<vector<vec3>> colors;
colors.clear();
for (int i = 0; i< width; i++) {
vector<vec3> ys;
for (int j = 0; j<height; j++) {
Intersection intrsct;
Ray ray; cam.getRay(ray, i, j);
vec3 color;
printf("Obtaining color for Ray[%d,%d]\n", i,j);
getColor(color, scene, ray, maxDepth);
ys.push_back(color);
}
colors.push_back(ys);
}
printImage(colors, width, height, outputFile);
}

void Parser::getColor(vec3& color, Scene scene, Ray ray, int numBounces)
{
Intersection inter; scene.intersect(inter,ray);
if(inter.isIntersecting()){
Color c; inter.getColor(c);
c.getAmbient(color);
} else {
color = vec3(0,0,0);
}
}

现在,我放弃了真正的光线追踪部分,而是简单地返回第一个物体命中的颜色(如果有的话)。毫无疑问,您已经注意到,计算机知道光线与对象相交的唯一方法是通过 Scene.intersect(),我也将其包括在内。使用的成员变量是“vector objects” see end

好了,现在进入正题。我首先创建一个场景并用 Parser::trace() 方法之外的对象填充它。现在出于一些奇怪的原因,我为 i=j=0 施放了 Ray,一切都很好。然而,当第二条光线转换时,我场景中存储的所有对象都不再识别它们的 vfptr(也就是说,我仍然可以访问除虚拟方法之外的所有 SceneObject 方法)!我用调试器逐步检查代码,发现所有 vfptr 的信息都在 getColor() 结束和循环继续之间的某个地方丢失了。但是,如果我将 getColor() 的参数更改为使用 Scene& 而不是 Scene,则不会发生丢失。这是什么疯狂的巫术?

场景代码,按要求:

#include <vector>
#include <limits>
#include "Intersection.h"
#include "LightSource.h"
#include "SceneObject.h"

using namespace std;

/**
Contains a list of scene objects. A ray can be
intersected with a scene to find its color
*/
class Scene
{
public:
vector<SceneObject*> objects;
vector<LightSource*> lights;

Scene(void);
~Scene(void);

/**
Add an object to the scene
*/
void addObject(SceneObject& o);

/**
Add a light source to the scene
*/
void addLight(LightSource& l);

/**
Fill 'l' with all light sources in the scene
*/
void getLightSources(vector<LightSource*>& l);

/**
Fills 'i' with information regarding an
intersection with the closest object in the scene
IF there is an intersection. Check i.isIntersecting()
to see if an intersection was actually found.
*/
void intersect(Intersection& i, Ray r);

void print();
};

#include "Scene.h"

Scene::Scene(void)
{
}

Scene::~Scene(void)
{
for(int i=0;i<objects.size();i++){
delete objects[i];
}
for(int i=0;i<lights.size();i++){
delete lights[i];
}
}

void Scene::addObject(SceneObject& o)
{
objects.push_back(o.clone());
}

void Scene::addLight(LightSource& l)
{
lights.push_back(l.clone());
}

void Scene::getLightSources(vector<LightSource*>& l)
{
l = lights;
}

void Scene::intersect(Intersection& i, Ray r)
{
Intersection result;
result.setDistance(numeric_limits<double>::infinity());
result.setIsIntersecting(false);

double oldDist; result.getDistance(oldDist);

/* Cycle through all objects, making result
the closest one */
for(int ind=0; ind<objects.size(); ind++){
SceneObject* thisObj = objects[ind];
Intersection betterIntersect;
thisObj->intersect(betterIntersect, r);

double newDist; betterIntersect.getDistance(newDist);
if (newDist < oldDist){
result = betterIntersect;
oldDist = newDist;
}
}

i = result;
}

void Scene::print()
{
printf("%d Objects:\n", objects.size());
for(int i=0;i<objects.size();i++){
objects[i]->print();
}
}

最佳答案

问题是您在 Scene 的析构函数中删除了您的 SceneObjects 并且您使用默认的复制构造函数,它使用指针 vector 进行平面复制。这意味着,Scene 的每个拷贝都引用相同的 SceneObjects。如果这些 Scene 中的一个被破坏,它们都会丢失它们所引用的对象。如果您通过引用传递场景,这没有问题,因为在这种情况下,不会创建拷贝并随后销毁。

关于c++ - 指向派生类对象的指针丢失 vfptr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2707817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com