简体   繁体   English

运行时检查失败 #2 - 变量“normalIndex”周围的堆栈已损坏

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

I'm working on my own personal game engine and I came across this problem.我正在开发自己的个人游戏引擎,但遇到了这个问题。 Trying to load an OBJ file to use with DirectX but the LoadObjFile keeps giving me the error尝试加载要与 DirectX 一起使用的 OBJ 文件,但 LoadObjFile 一直给我错误

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

What can I do?我能做什么? sometimes the variable name changes to 'uvIndex'有时变量名称会更改为“uvIndex”

Here's my code:这是我的代码:

    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;
    }

It's worth noting that I'm using a file that has something like 300000 vertices.值得注意的是,我正在使用一个包含 300000 个顶点的文件。 The obj file is here http://tf3dm.com/3d-model/millenium-falcon-82947.html obj 文件在这里http://tf3dm.com/3d-model/millenium-falcon-82947.html

This is how the function is being called (based on @O'Neil's comment这就是函数的调用方式(基于@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 was kind enough to point me in the right direction. @O'Neil 很友好地为我指明了正确的方向。 I was making the assumption that every face had 3 vertices, so I declared these我假设每个面都有 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;
            }

What I didn't consider is that sometimes I have 4 vertices, which was overflowing those variables.我没有考虑的是有时我有 4 个顶点,这些顶点溢出了这些变量。

This is the final code:这是最终的代码:

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;
}

and this is the function call这是函数调用

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.

相关问题 运行时检查失败#2-变量“ z”周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable ''z" was corrupted 运行时检查失败2:变量&#39;expression&#39;周围的堆栈已损坏 - Run-Time Check Failure #2: Stack around the variable 'expression' was corrupted 运行时检查失败#2-变量&#39;ap&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'ap' was corrupted 错误:运行时检查失败 #2 - 变量周围的堆栈已损坏 - ERROR: run-time check failure #2 - stack around the variable was corrupted 运行时检查失败#2-变量&#39;projectAverage&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'projectAverage' was corrupted 运行时检查失败#2-围绕变量&#39;&#39;的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable '' was corrupted 运行时检查失败#2-变量&#39;seqA&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'seqA' was corrupted 运行时检查失败#2-变量&#39;k&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'k' was corrupted 运行时检查失败 #2 - 变量“板”周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'board' was corrupted 运行时检查失败 #2 - 变量“应用程序”周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'application' was corrupted
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM