In my ray tracer I am constructing a Bounding Volume Hierarchy. I have used a couple of days trying to fix this error but I seem to be doing something fundamentally wrong.
This first method is the construction being called is the one below using a typedef std::vector Objects array.
void BVH::build(Objects* objs)
{
// construct the bounding volume hierarchy
int size = objs->size();
// Calculate the bounding box for this node
BBox bb = (*objs)[0]->boundingBox();
for ( int p = 1; p < size; p++ )
bb.expandToInclude( (*objs)[p]->boundingBox());
Vector3 pivot = (bb.max + bb.min) * 0.5f;
tree.bbox = bb;
int split = qsplit(objs, size, pivot.x, 0);
tree.left = subdivision(objs, split, 1);
tree.right = subdivision(&objs[split], size - split, 1);
}
This method is used by the one below to construct the leaf nodes in my binary tree.
Node* BVH::makeLeaf(Objects* objs, int num)
{
Node* node = new Node;
if ( num == 1 ) { node->objs = &objs[0]; }
else if ( num == 2 ) { node->objs = ((&objs)[0],(&objs)[1]); }
node->isLeaf = true;
return node;
}
This is a recursive method using the array of Objects to split and construct nodes and leaf nodes.
Node* BVH::subdivision(Objects* objs, int size, int axis)
{
if ( size == 1 ) { return makeLeaf(objs, 1); }
if ( size == 2 ) { return makeLeaf(objs, 2); }
Node* node = new Node;
node->isLeaf = false;
BBox bb = (*objs)[0]->boundingBox();
for ( int p = 1; p < size; p++ )
bb.expandToInclude((*objs)[p]->boundingBox());
node->bbox = bb;
Vector3 pivot = (bb.max + bb.min) * 0.5f;
int split = qsplit(objs, size, pivot[axis], axis);
node->left = subdivision(objs, split, (axis + 1) % 3);
node->right = subdivision(&objs[split], size - split, (axis + 1) % 3);
return node;
}
Running this code will give me an segmentation-fault inside subdivision.
Here's a post of the null pointer from gdb:
debug: Loading "teapot.obj"...
debug: Loaded "teapot.obj" with 576 triangles
Program received signal SIGSEGV, Segmentation fault.
0x000000000040bb9e in BVH::subdivision (this=0x61f0c0, objs=0x61f100, size=5, axis=1) at BVH.cpp:44
44 BBox bb = (*objs)[0]->boundingBox();
(gdb) bt
0 0x000000000040bb9e in BVH::subdivision (this=0x61f0c0, objs=0x61f100, size=5, axis=1) at BVH.cpp:44
1 0x000000000040bdef in BVH::subdivision (this=0x61f0c0, objs=0x61f0a0, size=9, axis=0) at BVH.cpp:56
2 0x000000000040bd62 in BVH::subdivision (this=0x61f0c0, objs=0x61f0a0, size=18, axis=2) at BVH.cpp:53
3 0x000000000040bd62 in BVH::subdivision (this=0x61f0c0, objs=0x61f0a0, size=36, axis=1) at BVH.cpp:53
4 0x000000000040bd62 in BVH::subdivision (this=0x61f0c0, objs=0x61f0a0, size=72, axis=0) at BVH.cpp:53
5 0x000000000040bd62 in BVH::subdivision (this=0x61f0c0, objs=0x61f0a0, size=144, axis=2) at BVH.cpp:53
6 0x000000000040bd62 in BVH::subdivision (this=0x61f0c0, objs=0x61f0a0, size=288, axis=1) at BVH.cpp:53
7 0x000000000040b9ca in BVH::build (this=0x61f0c0, objs=0x61f0a0) at BVH.cpp:21
8 0x00000000004121ac in Scene::preCalc (this=0x61f0a0) at Scene.cpp:42
9 0x0000000000402fde in makeTeapotScene () at assignment1.cpp:113
10 0x000000000040f973 in main (argc=1, argv=0x7fffffffe798) at main.cpp:65
(gdb) p (*objs)[0]
$1 = (Object *&) @0x0: <error reading variable>
(&objs)[0]
Is the same as
*(&objs)
which is the same as
objs
In the same way,
(&objs)[1]
is the same as
objs + 1
which is most likely not the address of a valid object.
The line
node->objs = ((&objs)[0],(&objs)[1]);
since it uses the comma operator, throws away the value of (&objs)[0]
and sets node->objs
to objs + 1
, which is invalid.
It's unclear what you expect that assignment to accomplish, so I have no idea what you should replace it with.