在我的光线追踪器中,我正在构建一个包围体层次结构。我已经用了几天试图修复这个错误,但我似乎在做一些根本错误的事情。
第一种方法是被调用的构造,下面是使用 typedef std::vector Objects 数组的构造。
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);
}
下面的方法使用此方法来构造我的二叉树中的叶节点。
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;
}
这是一种递归方法,使用对象数组来拆分和构造节点和叶节点。
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;
}
运行此代码会给我细分内的分段错误。
这是来自 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>