我写了自己的代码来解析.obj模型文件 - 本质上只是ASCII文本。根据我的测试,该文件被正确解析并存储在类中。我可以读取加载函数中的值(来自数据成员)。访问数组中的元素
当我尝试读回主渲染循环中的值时,会发生此问题。有上开始“INT V”行访问冲突错误:
for(int i = 0; i<data.numFaces; i++){
for(int j = 0; j<3; j++){ //Assuming triangles for now.
int v = data.faceList[i].vertex[j]; // Access violation here.
double vX = data.vertexList[v].x;
double vY = data.vertexList[v].y;
double vZ = data.vertexList[v].z;
glVertex3d(vX, vY, vZ);
}
}
我不知道是什么原因出现这种情况,我已经检查一切,我会这么想的。我在C++方面经验不是很丰富。我的大部分编程经验都是用Java,Python和PHP编写的,尽管我以前曾用C++编写过中型项目。
我敢肯定,这个问题是基本相关的内存分配或指针用于动态数组。
这里是代码的OBJ加载类的相关部分:
ObjData ObjLoader::LoadObj(std::string filename){
//... Initalization ...
// 1st pass: find number of elements so array sizes can be defined.
while(!file.eof()){
//...
}
//...close file...
_data.faceList = new ObjFace[_data.numFaces];
_data.vertexList = new ObjVert[_data.numVertices];
_data.uvList = new ObjUV[_data.numUVcoords];
_data.normalList = new ObjNormal[_data.numNormals];
//TODO: Make size dynamic according to each face. Just use the first 3 points for now.
for (int i = 0; i < _data.numFaces; i++){
_data.faceList[i].vertex = new int[3];
_data.faceList[i].normal = new int[3];
_data.faceList[i].uv = new int[3];
}
//... file stuff ...
// 2nd pass: read values into arrays.
while(!file.eof()){
//...
if(type=="v"){
_data.vertexList[currentVertex].x = atof(param1.c_str());
_data.vertexList[currentVertex].y = atof(param2.c_str());
_data.vertexList[currentVertex].z = atof(param3.c_str());
currentVertex++;
}else if(type=="vt"){
_data.uvList[currentUV].u = atof(param1.c_str());
_data.uvList[currentUV].v = atof(param2.c_str());
currentUV++;
}else if(type=="vn"){
_data.normalList[currentNormal].x = atof(param1.c_str());
_data.normalList[currentNormal].y = atof(param2.c_str());
_data.normalList[currentNormal].z = atof(param3.c_str());
currentNormal++;
}else if(type=="f"){
//...Within loop over each vertex in a single face ...
if(endPos != string::npos){
// Value before 1st "/" (Vertex index).
// ...find value in string...
_data.faceList[currentFace].vertex[i] = atoi(token.c_str()) -1; // File format begins indices from 1.
// Value between slashes (UV index).
// ...find value in string...
_data.faceList[currentFace].uv[i] = atoi(token.c_str()) -1;
// Value after 2nd "/" (Normal index).
// ...find value in string...
_data.faceList[currentFace].normal[i] = atoi(token.c_str()) -1;
}
//...End of loop over every vertex in a single face...
currentFace++;
}
}
return _data;
}
而且结构ObjFace,ObjVert,ObjUV和ObjNormal被定义为:
struct ObjVert{
float x, y, z;
};
struct ObjUV{
float u, v;
};
struct ObjNormal{
float x, y, z;
};
// Contains indexes.
struct ObjFace{
int* vertex;
int* uv;
int* normal;
};
感谢您的帮助。此外,在未来避免这些类型的错误的任何好的来源将不胜感激。
请问您可以添加更多的相关代码吗?例如,什么类型是'_data'?另外,在最上面的代码中,在后面的代码中'data'与'_data'是一样的,还是在某处我们没有看到一个赋值?在我看来,我们被要求对你的代码做太多的假设。 – KevenK 2010-06-15 20:33:46
哦,对,我试图删除尽可能多的无关代码。 顶部段是在我的主渲染循环,和之前只是,有到LoadObj的呼叫(这是低2个片段),其中所返回的值,如下所示被分配给数据: \t数据= LoaderClass.LoadObj( “cube.obj”); _data和data都是ObjData类型,它存储加载的.obj。 – usm 2010-06-15 20:44:26
虽然问题是(有点)不同,但您可能需要查看以前发布的关于读取obj文件的答案中的代码:http://stackoverflow.com/questions/2908854/opengl-problem-with-texture-in -mobi-obj/2909187#2909187 – 2010-06-15 20:44:31