Pointers to Derived Class Objects Losing vfptr
- by duckworthd
To begin, I am trying to write a run-of-the-mill, simple Ray Tracer. In my Ray Tracer, I have multiple types of geometries in the world, all derived from a base class called "SceneObject". I've included the header for it here.
/**
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;
};
As you can see, I've included a couple virtual functions to be implemented elsewhere. In this case, I have only two derived class -- Sphere and Triangle, both of which implement the missing member functions. Finally, I have a Parser class, which is full of static methods that do the actual "Ray Tracing" part. Here's a couple snippets for relevant portions
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);
}
}
Right now, I've forgone the true Ray Tracing part and instead simply return the color of the first object hit, if any. As you have no doubt noticed, the only way the computer knows that a ray has intersected an object is through Scene.intersect(), which I also include.
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;
}
Alright, now for the problem. I begin by creating a scene and filling it with objects outside of the Parser::trace() method. Now for some odd reason, I cast Ray for i=j=0 and everything works wonderfully. However, by the time the second ray is cast all of the objects stored in my Scene no longer recognize their vfptr's! I stepped through the code with a debugger and found that the information to all the vfptr's are lost somewhere between the end of getColor() and the continuation of the loop. However, if I change the arguments of getColor() to use a Scene& instead of a Scene, then no loss occurs. What crazy voodoo is this?