矢量下标超出范围错误-如何修复此SPECIFIC错误

Vector Subscript out of range error - How can i fix this SPECIFIC error?

本文关键字:错误 SPECIFIC 何修复 范围 下标      更新时间:2023-10-16

概述

我一直在开发OpenGL 3.3渲染引擎,好消息是,如果我为对象硬编码值,它们将完美渲染。坏消息是,当试图加载.obj文件时,我遇到了严重的问题。

我使用GLFW、GLM和GLEW并不是说这很重要,只是可能会有所帮助。

N。B.我的.obj加载程序是基于这里找到的加载程序。

问题

当我运行我的代码时,我只会得到错误:

调试断言失败
线路:1201
表达式:矢量下标超出范围

我该怎么解决这个问题?!

我已经调试了代码,我知道是哪行导致了错误,但在进一步挖掘时,我发现它试图索引下标为"0"的向量,所以我试图初始化容器的一些值,但错误仍然存在。

代码

这是所有的代码,请向下滚动查看详细信息。

OBJLoader.cpp

bool OBJLoader::loadGLDataFromOBJ(){
    bool processed = false;
    while (true){
        char currentLine[128];
        int res = fscanf(file, "%s", currentLine);
        if (res == EOF){
            break;
        }
        if (strcmp(currentLine, "v") == 0){
            Vector3f vertex;
            fscanf(file, "%f %f %fn", &vertex.x, &vertex.y, &vertex.z);
            raw_vPositions.push_back(vertex);
        }
        else if (strcmp(currentLine, "vt") == 0){
            Vector2f uv;
            fscanf(file, "%f %fn", &uv.x, &uv.y);
            raw_vTextureCoords.push_back(uv);
        }
        else if (strcmp(currentLine, "vn") == 0){
            Vector3f normal;
            fscanf(file, "%f %f %fn", &normal.x, &normal.y, &normal.z);
            raw_vNormals.push_back(normal);
        }
        else if (strcmp(currentLine, "f") == 0){
            unsigned int vertexData1[3], vertexData2[3], vertexData3[3];
            int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%dn",
                &vertexData1[0], &vertexData1[0], &vertexData1[0],
                &vertexData2[1], &vertexData2[1], &vertexData2[1],
                &vertexData3[2], &vertexData3[2], &vertexData3[2]);
            if (matches != 9){
                printf("File can't be read by our simple parser : ( Try exporting with other optionsn");
                return false;
            }
            if (!processed){
                gl_vTextureCoords.resize(raw_vPositions.size() * 2);
                gl_vNormals.resize(raw_vPositions.size() * 3);
                processed = true;
            }
            processVertex(vertexData1);
            processVertex(vertexData2);
            processVertex(vertexData3);
        }
    }
    gl_vPositions.resize(raw_vPositions.size() * 3);
    gl_vIndexBuffer.resize(indices.size());
    int vertexPointer = 0;
    for (int i = 0; i < raw_vPositions.size(); i++){
        gl_vPositions[vertexPointer++] = raw_vPositions[i].x;
        gl_vPositions[vertexPointer++] = raw_vPositions[i].y;
    }
    for (int j = 0; j < indices.size(); j++) gl_vIndexBuffer[j] = indices[j];
    return true;
}
void OBJLoader::processVertex(unsigned int vertexData[]){
    int currentVertexPointer = *(vertexData) - 1;
    indices.push_back(currentVertexPointer);
    Vector2f currentTexture = raw_vTextureCoords[*(vertexData+1) - 1];
    gl_vTextureCoords[currentVertexPointer * 2] = currentTexture.x;
    gl_vTextureCoords[currentVertexPointer * 2 + 1] = currentTexture.y;
    Vector3f currentNormal = raw_vNormals[*(vertexData + 2) - 1];
    gl_vNormals[currentVertexPointer * 3] = currentNormal.x;
    gl_vNormals[currentVertexPointer * 3 + 1] = currentNormal.y;
    gl_vNormals[currentVertexPointer * 3 + 2] = currentNormal.z;
}

bool OBJLoader::loadOBJFromFile(const char* filePath){
    file = fopen(filePath, "r");
    if (!file){
        printf("Impossible to open the file !n");
        return false;
    }
    else{
        printf("File opened: %s", filePath);
    }
    return true;
}

std::vector<float> OBJLoader::getPositions(){
    return this->gl_vPositions;
}
std::vector<float> OBJLoader::getTextureCoords(){
    return this->gl_vTextureCoords;
}
std::vector<float> OBJLoader::getNormals(){
    return this->gl_vNormals;
}
std::vector<unsigned short> OBJLoader::getIndexData(){
    return this->gl_vIndexBuffer;
}

OBJ加载器.h

#include "glm/glm.hpp"
#include <vector>
typedef glm::vec3 Vector3f;
typedef glm::vec2 Vector2f;
typedef std::vector<Vector3f> List_Vec3;
typedef std::vector<Vector2f> List_Vec2;
typedef std::vector<unsigned short> List_Int;
class OBJLoader{
private:
    //These will be populated and then outputted to the user.
    std::vector<unsigned short> gl_vIndexBuffer = std::vector<unsigned short>(10);
    std::vector<float> gl_vPositions = std::vector<float>(10);
    std::vector<float> gl_vTextureCoords = std::vector<float>(10);
    std::vector<float> gl_vNormals = std::vector<float>(10);
    //These come from the obj file.
    List_Int indices;
    List_Vec3 raw_vPositions;
    List_Vec2 raw_vTextureCoords;
    List_Vec3 raw_vNormals;
    FILE* file; //The file of the obj.
    //Private functions
    bool loadOBJFromFile(const char* filePath); //Populates the file.
    bool loadGLDataFromOBJ();
    void processVertex(unsigned int[]);
public:
    OBJLoader(){}; //The default constructor.
    bool loadGLDataFromFile(const char* filePath); //Returns true if data loaded.
    std::vector<float> getPositions();
    std::vector<float> getTextureCoords();
    std::vector<float> getNormals();
    std::vector<unsigned short> getIndexData();
};

详细信息

这项功能似乎让我感到悲伤:

void OBJLoader::processVertex(unsigned int vertexData[]){
    int currentVertexPointer = *(vertexData) - 1;
    indices.push_back(currentVertexPointer);
    Vector2f currentTexture = raw_vTextureCoords[*(vertexData+1) - 1];
    gl_vTextureCoords[currentVertexPointer * 2] = currentTexture.x;
    gl_vTextureCoords[currentVertexPointer * 2 + 1] = currentTexture.y;
    Vector3f currentNormal = raw_vNormals[*(vertexData + 2) - 1];
    gl_vNormals[currentVertexPointer * 3] = currentNormal.x;
    gl_vNormals[currentVertexPointer * 3 + 1] = currentNormal.y;
    gl_vNormals[currentVertexPointer * 3 + 2] = currentNormal.z;
}

更具体地说,这些线路:

gl_vTextureCoords[currentVertexPointer * 2] = currentTexture.x;
gl_vTextureCoords[currentVertexPointer * 2 + 1] = currentTexture.y;
gl_vNormals[currentVertexPointer * 3] = currentNormal.x;
gl_vNormals[currentVertexPointer * 3 + 1] = currentNormal.y;
gl_vNormals[currentVertexPointer * 3 + 2] = currentNormal.z;

出于某种原因,如果我一直踩在while循环上,它在第一次迭代时中断,并且在检查下标的值时,它等于0,因此它调用类似于:

gl_vTextureCoords[0] = currentTexture.x;
gl_vTextureCoords[1] = currentTexture.y;

这在理论和实践中都应该存在。在类i中,定义向量将用10个空值初始化,在运行中,当循环达到其"f"值时,它无论如何都会调整列表的大小,那么为什么零是一个如此硬的数字呢?

错误在这里

unsigned int vertexData1[3], vertexData2[3], vertexData3[3];
int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%dn",
   &vertexData1[0], &vertexData1[0], &vertexData1[0],
   &vertexData2[1], &vertexData2[1], &vertexData2[1],
   &vertexData3[2], &vertexData3[2], &vertexData3[2]);

我很确定你的意思是:

unsigned int vertexData1[3], vertexData2[3], vertexData3[3];
int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%dn",
   &vertexData1[0], &vertexData1[1], &vertexData1[2],
   &vertexData2[0], &vertexData2[1], &vertexData2[2],
   &vertexData3[0], &vertexData3[1], &vertexData3[2]);

或者:

unsigned int vertexData1[3], vertexData2[3], vertexData3[3];
int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%dn",
   &vertexData1[0], &vertexData2[0], &vertexData3[0],
   &vertexData1[1], &vertexData2[1], &vertexData3[1],
   &vertexData1[2], &vertexData3[2], &vertexData3[2]);

这是一个很好的例子,说明了为什么老式的"C"风格编程是一门垂死的艺术——犯简单的错误太容易了,后果很难诊断。