2010-04-25 61 views
1

首先,我想写一个普通的,简单的Ray Tracer。在我的Ray Tracer中,我有多种类型的几何图形,都是从一个名为“SceneObject”的基类中派生出来的。我在这里包含了标题。指向衍生类对象的指针丢失vfptr

/** 
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; 
}; 

正如你所看到的,我已经包含了几个虚拟函数在其他地方实现。在这种情况下,我只有两个派生类 - 球体和三角形,它们都实现了缺少的成员函数。最后,我有一个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(),我也包含它。所使用的成员变量是“矢量对象”参见结束

好的,现在是问题所在。我首先创建一个场景,然后用Parser :: trace()方法以外的对象填充它。现在出于某种奇怪的原因,我把雷放在i = j = 0的位置,一切都很好。但是,在第二次射线投射时,存储在我的场景中的所有对象不再识别它们的vfptr(也就是说,我仍然可以访问除虚拟场景以外的所有SceneObject方法)!我用调试器遍历代码,发现所有vfptr的信息都在getColor()结束和循环的延续之间丢失。但是,如果我更改getColor()的参数以使用场景&而不是场景,则不会发生任何损失。这是什么疯狂的巫术?

代码场景,如要求:

#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(); 
    } 
} 
+1

对不起 - 我完全无法理解。然而,我会观察到你的基类需要一个虚拟析构函数。 – 2010-04-25 10:06:44

+1

这听起来很像你在'Scene'的复制构造函数和/或析构函数中有不正确的代码,但是没有看到'Scene'的定义,这很难确定。 – 2010-04-25 10:10:46

+0

可能不相关,但请确保您的方法是“const”正确的(http://www.parashift.com/c++-faq-lite/const-correctness.html)。 – kennytm 2010-04-25 10:12:56

回答

5

的问题是,你在Scene析构函数删除SceneObjects,并使用默认的拷贝构造函数,它不会简单复制与指针的载体。这意味着,Scene的每个副本都引用相同的SceneObjects。如果其中一个Scene s被选中,他们都会丢失他们提及的对象。如果你通过引用传递场景,这是没有问题的,因为在那种情况下没有复制并且随后被销毁。

+0

完美无瑕。谢谢! – duckworthd 2010-04-25 21:05:48

相关问题