0

好的。我有一个类 Sphere。我正在尝试使用给定数量的细分初始化 UVSphere 的坐标。该类有一个 2D 向量 verts,如下所示:

std::vector<std::vector<Angel::vec3>> verts;

其中 vec3 本质上是三个浮点数的结构

我有两个功能

void setupSphere();
void setupRaw();

谁的实现看起来像这样:

void Sphere::setupSphere()
{
float pi = std::atan(1.0f) * 4.0f;
float subdivAngle = 2 * pi / numSubdivide;

std::vector<Angel::vec3> yVectors;
yVectors.reserve(numSubdivide);
for (int i = 0; i < numSubdivide; i ++)
{
    float curAngle = subdivAngle * i;
    yVectors.push_back(Angel::vec3(std::cos(curAngle), 0.0, -std::sin(curAngle)));
}

int zNumSubdivide = numSubdivide + 4;
float zAngle = 2 * pi / zNumSubdivide;

for (int i = 1; i < zNumSubdivide / 2; i ++)
{
    float curAngle = pi / 2 - zAngle * i;
    float yCoord = std::sin(curAngle);
    float xzScale = std::cos(curAngle);
    std::vector<Angel::vec3> curVector;

    for (int j = 0; j < numSubdivide; j ++)
    {
        Angel::vec3 newPoint = yVectors[j] * xzScale;
        newPoint.y = yCoord;
        curVector.push_back(newPoint);
    }
    verts.push_back(curVector);
}

std::cout << "Size = " << verts.size() << std::endl;
setupRaw();
//setupTexture();
}

void Sphere::setupRaw()
{
std::cout << "TESTING" << std::endl;
std::cout << "Size = " << verts.size();
}

当我在 setupSphere() 末尾查看 verts 的内容时,它看起来很棒,但是在 setupRaw() 中,当我尝试打印 verts 的大小时它崩溃了。这里发生了什么?

4

0 回答 0