opengl obj文件打开失败是什么原因

zhaoboweibowen 2016-06-04 08:53:08
bool load_obj_file(const char *path, vector<float> &verts, vector<unsigned int> &faces, bool centerize = false, bool normalize = false)
{
FILE *file = fopen(path, "r");
if (file == NULL)
return false;

float minx, miny, minz, maxx, maxy, maxz;
minx = miny = minz = maxx = maxy = maxz = 0.0f;

while (true) {
char lineHeader[128];
int res = fscanf(file, "%s", lineHeader);
if (res == EOF)
break;

if (strcmp(lineHeader, "v") == 0) {
float vx, vy, vz;
fscanf(file, "%f %f %f\n", &vx, &vy, &vz);
verts.push_back(vx);
verts.push_back(vy);
verts.push_back(vz);

minx = min(vx, minx); maxx = max(vx, maxx);
miny = min(vy, miny); maxy = max(vy, maxy);
minz = min(vz, minz); maxz = max(vz, maxz);
}
else if (strcmp(lineHeader, "f") == 0) {
std::string vertex1, vertex2, vertex3;
unsigned int vertexIndex[3];
int matches = fscanf(file, "%d %d %d\n", &vertexIndex[0], &vertexIndex[1], &vertexIndex[2]);
if (matches != 3){
printf("File format error. Please check the file content.\n");
return false;
}
faces.push_back(vertexIndex[0] - 1);
faces.push_back(vertexIndex[1] - 1);
faces.push_back(vertexIndex[2] - 1);
}
else {
char stupidBuffer[1000];
fgets(stupidBuffer, 1000, file);
}

}


if (centerize) {

float cenx = 0.5f * (maxx + minx);
float ceny = 0.5f * (maxy + miny);
float cenz = 0.5f * (maxz + minz);


for (unsigned int i = 0; i < verts.size();) {
verts[i++] -= cenx;
verts[i++] -= ceny;
verts[i++] -= cenz;
}
}

if (normalize) {
float scale = 1.0f / max(0.00001f, max(maxx - minx, max(maxy - miny, maxz - minz)));
for (unsigned int i = 0; i < verts.size();) {
verts[i++] *= scale;
}
}

return true;
}




初始化的一部分if (!load_obj_file(path, _verts, _faces, true, true)) {
printf("Error occured when initilizing the scene.");
return;
}


编译错误:错误 1 error C2143: 语法错误 : 缺少“;”(在“常量”的前面) e:\miniengine\miniengine\miniengine\models\cube.obj 4 1 MiniEngine
错误 2 error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int e:\miniengine\miniengine\miniengine\models\cube.obj 4 1 MiniEngine


上一封 下一封« 返回
...全文
514 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhaoboweibowen 2016-06-07
  • 打赏
  • 举报
回复
我不确定代码有没有写错,那是哪里错了,环境问题(按说不会错),网上说也可能是磁盘格式问题,但是不知道怎么搞。 那怎么搞 1877756138qq号,私聊一下可否。
天鹅梦 2016-06-06
  • 打赏
  • 举报
回复
错误不在你贴的这些代码里

19,468

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧