矢量下标超出范围错误-如何修复此SPECIFIC错误
Vector Subscript out of range error - How can i fix this SPECIFIC error?
概述
我一直在开发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"风格编程是一门垂死的艺术——犯简单的错误太容易了,后果很难诊断。
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 矢量下标超出范围错误-如何修复此SPECIFIC错误
- 为什么一个文件中没有错误,另一个文件中没有错误 - "Non Static Member Reference mush be relative to specific object"