我已经将我的另一个问题调试回MyMesh
构造函数。在这段代码中:
if (hollow) {
numTriangles = n*8;
triangles=new MyTriangle[numTriangles];
if (smooth) numSurfacePoints=n*8;
else numSurfacePoints=n*12;
surfacePoints=new SurfacePoint[numSurfacePoints];
}else {
numTriangles = n*4;
triangles=new MyTriangle[numTriangles];
if (smooth){
numSurfacePoints=n*4;
surfacePoints=new SurfacePoint[numSurfacePoints];
surfacePoints[n*4]=SurfacePoint(vector3(0,0,1),vector3(0,0,1));
surfacePoints[n*4+1]=SurfacePoint(vector3(0,0,-1),vector3(0,0,-1));
}else{
numSurfacePoints=n*6;
surfacePoints=new SurfacePoint[numSurfacePoints];
surfacePoints[n*6]=SurfacePoint(vector3(0,0,1),vector3(0,0,1));
surfacePoints[n*6+1]=SurfacePoint(vector3(0,0,-1),vector3(0,0,-1));
}
}
我正在为网格确定必要的 SurfacePoints 和 Triangles。布尔“空心”和“平滑”表示,如果我需要在圆锥体上打一个洞,或者法线是否相同,但我认为这无关紧要。
问题是:如果hollow==false,它会做错事,但不会崩溃,它甚至允许将值放入数组中,但是当我试图像这样计算它时:
for(int i=0;i<numSurfacePoints;i++){
std::cout<<"vertex "<<i<<"-> pos:"<<surfacePoints[i].pos.x<<" "<<
surfacePoints[i].pos.y<<" "<<surfacePoints[i].pos.z<<
" norm:"<<surfacePoints[i].norm.x<<" "<<surfacePoints[i].norm.y<<
" "<<surfacePoints[i].norm.z<<"\n";
}
它会在 i=0 时引发 bad_alloc 异常。
此外,曾经有一段时间,上面的代码段向 operator new 抛出了一个 bad_alloc,但这个问题自己解决了,但也许它是相关的。
有谁能够帮我?