簡體   English   中英

運行時檢查失敗 #2 - 變量“normalIndex”周圍的堆棧已損壞

[英]Run-Time Check Failure #2 - Stack around the variable 'normalIndex' was corrupted

我正在開發自己的個人游戲引擎,但遇到了這個問題。 嘗試加載要與 DirectX 一起使用的 OBJ 文件,但 LoadObjFile 一直給我錯誤

Run-Time Check Failure #2 - Stack around the variable 'normalIndex' was corrupted.

我能做什么? 有時變量名稱會更改為“uvIndex”

這是我的代碼:

    bool Renderer::LoadObjFile(
        const char* path, 
        std::vector < D3DXVECTOR3 > *vertices,
        std::vector < D3DXVECTOR2 > *textureVertices,
        std::vector < D3DXVECTOR3 > *normals,
        std::vector< unsigned int > *vertexIndices,
        std::vector< unsigned int > *uvIndices,
        std::vector< unsigned int > *normalIndices)
    {
        std::ifstream infile(path);  // construct object and open file
        if (!infile) { 
            std::cerr << "Error opening file!" << std::endl; 
            return false;
        }

        std::string line;

        while (std::getline(infile, line))
        {
            if (line.substr(0, 2) == "v ")
            {
                line = line.substr(2);                  // Eliminate line header
                std::string buf;                        // Have a buffer string
                std::stringstream ss(line);             // Insert the string into a stream
                std::vector<std::string> substrings;    // Create vector to hold our words
                while (ss >> buf)
                    substrings.push_back(buf);

                D3DVECTOR vertex = { (float)atof(substrings[0].c_str()), (float)atof(substrings[1].c_str()), (float)atof(substrings[2].c_str()) };
                vertices->push_back(vertex);
            }
            else if (line.substr(0, 3) == "vn ")
            {
                line = line.substr(3);                  // Eliminate line header
                std::string buf;                        // Have a buffer string
                std::stringstream ss(line);             // Insert the string into a stream
                std::vector<std::string> substrings;    // Create vector to hold our words
                while (ss >> buf)
                    substrings.push_back(buf);

                D3DVECTOR normal = { (float)atof(substrings[0].c_str()), (float)atof(substrings[1].c_str()), (float)atof(substrings[2].c_str()) };
                normals->push_back(normal);
            }
            else if (line.substr(0, 3) == "vt ")
            {
                line = line.substr(3);                  // Eliminate line header
                std::string buf;                        // Have a buffer string
                std::stringstream ss(line);             // Insert the string into a stream
                std::vector<std::string> substrings;    // Create vector to hold our words
                while (ss >> buf)
                    substrings.push_back(buf);

                D3DXVECTOR2 vertexTexture = { (float)std::stod(substrings[0].c_str()), (float)std::stod(substrings[1].c_str()) };
                textureVertices->push_back(vertexTexture);
            }
            else if (line.substr(0, 2) == "f ")
            {
                line = line.substr(2);                  // Eliminate line header
                std::string buf;                        // Have a buffer string
                std::stringstream ss(line);             // Insert the string into a stream
                std::vector<std::string> substrings;    // Create vector to hold our words
                while (ss >> buf)
                    substrings.push_back(buf);

                unsigned long vertexIndex[3], uvIndex[3], normalIndex[3];
                for (int k = 0; k < 3; k++)
                {
                    vertexIndex[k] = 0;
                    uvIndex[k] = 0;
                    normalIndex[k] = 0;
                }

                std::string delimiter = "/";
                unsigned int pos = 0;
                std::string token;
                for (unsigned int t = 0; t < substrings.size(); t++)
                {
                    if ((pos = substrings[t].find(delimiter)) != std::string::npos)
                    {
                        token.clear();
                        token = substrings[t].substr(0, pos);
                        vertexIndex[t] = atol(token.c_str()) - 1;
                        substrings[t].erase(0, pos + delimiter.length());
                    }

                    if ((pos = substrings[t].find(delimiter)) != std::string::npos)
                    {
                        token.clear();
                        token = substrings[t].substr(0, pos);
                        uvIndex[t] = atol(token.c_str()) - 1;
                        substrings[t].erase(0, pos + delimiter.length());
                    }

                    token.clear();
                    token = substrings[t];
                    normalIndex[t] = atol(token.c_str()) - 1;
                    substrings[t].clear();
                }

                vertexIndices->push_back(vertexIndex[0]);
                vertexIndices->push_back(vertexIndex[1]);
                vertexIndices->push_back(vertexIndex[2]);

                uvIndices->push_back(uvIndex[0]);
                uvIndices->push_back(uvIndex[1]);
                uvIndices->push_back(uvIndex[2]);

                normalIndices->push_back(normalIndex[0]);
                normalIndices->push_back(normalIndex[1]);
                normalIndices->push_back(normalIndex[2]);
            }
            else if (line.substr(0, 2) == "s ")
            {
                continue;
            }
            else 
            {
                std::cout << line << std::endl;
            }
        }

        return true;
    }

值得注意的是,我正在使用一個包含 300000 個頂點的文件。 obj 文件在這里http://tf3dm.com/3d-model/millenium-falcon-82947.html

這就是函數的調用方式(基於@O'Neil 的評論

std::vector< D3DXVECTOR3 > vs;
std::vector< D3DXVECTOR2 > vts;
std::vector< D3DXVECTOR3 > ns;
std::vector< unsigned int > vertexIndices, uvIndices, normalIndices;

bool result = LoadObjFile("millenium-falcon.obj", &vs, &vts, &ns, &vertexIndices, &uvIndices, &normalIndices);

@O'Neil 很友好地為我指明了正確的方向。 我假設每個面都有 3 個頂點,所以我聲明了這些

            unsigned long vertexIndex[3], uvIndex[3], normalIndex[3];
            for (int k = 0; k < 3; k++)
            {
                vertexIndex[k] = 0;
                uvIndex[k] = 0;
                normalIndex[k] = 0;
            }

我沒有考慮的是有時我有 4 個頂點,這些頂點溢出了這些變量。

這是最終的代碼:

bool Renderer::LoadObjFile(
    const char* path, 
    std::vector < D3DXVECTOR3 > &vertices,
    std::vector < D3DXVECTOR2 > &textureVertices,
    std::vector < D3DXVECTOR3 > &normals,
    std::vector< unsigned int > &vertexIndices,
    std::vector< unsigned int > &uvIndices,
    std::vector< unsigned int > &normalIndices)
{
    std::ifstream infile(path);  // construct object and open file
    if (!infile) { 
        std::cerr << "Error opening file!" << std::endl; 
        return false;
    }

    std::string line;
    std::string buf;                        // Have a buffer string
    std::string delimiter = "/";
    unsigned int pos = 0;
    std::string token;

    while (std::getline(infile, line))
    {
        if (line.substr(0, 2) == "v ")
        {
            line = line.substr(2);                  // Eliminate line header
            std::stringstream ss(line);             // Insert the string into a stream
            buf.clear();
            std::vector<std::string> substrings;    // Create vector to hold our words
            while (ss >> buf)
                substrings.push_back(buf);

            D3DVECTOR vertex = { (float)atof(substrings[0].c_str()), (float)atof(substrings[1].c_str()), (float)atof(substrings[2].c_str()) };
            vertices.push_back(vertex);
        }
        else if (line.substr(0, 3) == "vn ")
        {
            line = line.substr(3);                  // Eliminate line header
            std::stringstream ss(line);             // Insert the string into a stream
            buf.clear();
            std::vector<std::string> substrings;    // Create vector to hold our words
            while (ss >> buf)
                substrings.push_back(buf);

            D3DVECTOR normal = { (float)atof(substrings[0].c_str()), (float)atof(substrings[1].c_str()), (float)atof(substrings[2].c_str()) };
            normals.push_back(normal);
        }
        else if (line.substr(0, 3) == "vt ")
        {
            line = line.substr(3);                  // Eliminate line header
            std::stringstream ss(line);             // Insert the string into a stream
            buf.clear();
            std::vector<std::string> substrings;    // Create vector to hold our words
            while (ss >> buf)
                substrings.push_back(buf);

            D3DXVECTOR2 vertexTexture = { (float)std::stod(substrings[0].c_str()), (float)std::stod(substrings[1].c_str()) };
            textureVertices.push_back(vertexTexture);
        }
        else if (line.substr(0, 2) == "f ")
        {
            line = line.substr(2);                  // Eliminate line header
            std::stringstream ss(line);             // Insert the string into a stream
            buf.clear();
            std::vector<std::string> substrings;    // Create vector to hold our words
            while (ss >> buf)
                substrings.push_back(buf);

            std::vector<unsigned int> vertexIndex, uvIndex, normalIndex;
            for (unsigned int t = 0; t < substrings.size(); t++)
            {
                if ((pos = substrings[t].find(delimiter)) != std::string::npos)
                {
                    token.clear();
                    token = substrings[t].substr(0, pos);
                    vertexIndex.push_back(atol(token.c_str()) - 1);
                    substrings[t].erase(0, pos + delimiter.length());
                }

                if ((pos = substrings[t].find(delimiter)) != std::string::npos)
                {
                    token.clear();
                    token = substrings[t].substr(0, pos);
                    uvIndex.push_back(atol(token.c_str()) - 1);
                    substrings[t].erase(0, pos + delimiter.length());
                }

                token.clear();
                token = substrings[t];
                normalIndex.push_back(atol(token.c_str()) - 1);
                substrings[t].clear();
            }

            for (unsigned int m = 0; m < vertexIndex.size(); m++)
                vertexIndices.push_back(vertexIndex[m]);

            for (unsigned int m = 0; m < uvIndex.size(); m++)
                uvIndices.push_back(uvIndex[m]);

            for (unsigned int m = 0; m < normalIndex.size(); m++)
                normalIndices.push_back(normalIndex[m]);
        }
        else if (line.substr(0, 2) == "s ")
        {
            continue;
        }
        else 
        {
            std::cout << line << std::endl;
        }
    }

    return true;
}

這是函數調用

std::vector< D3DXVECTOR3 > vs;
std::vector< D3DXVECTOR2 > vts;
std::vector< D3DXVECTOR3 > ns;
std::vector< unsigned int > vertexIndices, uvIndices, normalIndices;

bool result = LoadObjFile("millenium-falcon.obj", vs, vts, ns, vertexIndices, uvIndices, normalIndices);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM