我最近一直在搞乱OpenGL和c ++(使用FreeGLUT和GLEW)并开始尝试从3DS Max 2014导入.obj文件。如果我手动输入顶点和索引但是,当试图获取时,一切都很好他们从一个文件我得到奇怪的数字。 当获得指数而不是1时,有1000而不是3而有3965,当有12时是1212(有一些和三个等等但有时它们是随机的)
以下是我用来导入的代码:http://pastebin.com/1ds6fgj9
或
std::vector<std::string> Object::split(std::string str, char splitter){
char charText[1000];
int number = 0;
int numberOfWords = 0;
char current[10][100];
std::vector<std::string> tokens;
strcpy(charText, str.c_str());
int size = sizeof(charText) / sizeof(*charText);
//std::cout << size << " size\n";
for (int i = 0; i < splitter; i++){
if (charText[i] == splitter){
tokens.push_back(current[numberOfWords]);
numberOfWords++;
number = 0;
}
else{
current[numberOfWords][number] = charText[i];
number++;
}
}
return tokens;
}
Object::Object(std::string fileName, Vector3f pos, Vector3f rot, float Size, Vector3f color){
Position = pos;
Rotation = rot;
size = Size;
colors = color;
std::string data;
std::ifstream file(fileName);
std::string line;
Vector3f _verts[1024];
int noVerts = 0;
unsigned int _inds[1024];
int noIndices = 0;
while (getline(file, line)) // same as: while (getline( myfile, line ).good())
{
char charText[1024];
strcpy(charText, line.c_str());
if (charText[0] == 'v' && charText[1] != 'n' && charText[1] != 't'){
std::vector<std::string> splitLine = split(line, ' ');
_verts[noVerts] = Vector3f(atoi(splitLine[1].c_str()), atoi(splitLine[2].c_str()), atoi(splitLine[3].c_str()));
//std::cout << "Vertices Number:" << noVerts << " x:" << _verts[noVerts].getX() << " y:" << _verts[noVerts].getY() << " z:" << _verts[noVerts].getZ() << "\n";
noVerts++;
}
if (charText[0] == 'f'){
std::vector<std::string> splitLine = split(line, ' ');
for (int i = 0; i < 3; i++){
std::vector<std::string> ssl = split(splitLine[i+1], '/');
//std::cout << atoi(splitLine[i].c_str()) << "\n";
std::cout << i << ": " << atoi(ssl[0].c_str()) << " " << atoi(ssl[1].c_str()) << " " << atoi(ssl[2].c_str()) << "\n";
_inds[noIndices] = atoi(ssl[0].c_str());
noIndices++;
_inds[noIndices] = atoi(ssl[1].c_str());
noIndices++;
_inds[noIndices] = atoi(ssl[2].c_str());
noIndices++;
//std::cout << "number of indices:" << noIndices<< "\n";
}
}
}
verts = _verts;
inds = _inds;
SizeV = sizeof(verts);
SizeI = sizeof(inds);
noPoints = SizeI / sizeof(*inds);
length = SizeV / sizeof(*verts);
Matrix4f posM, rotM, sizeM;
sizeM.InitScaleTransform(size, size, size);
rotM.InitRotateTransform(rot.getX(), rot.getY(), rot.getZ());
posM.InitTranslationTransform(pos.getX(), pos.getY(), pos.getZ());
M = posM * rotM * sizeM;
for (unsigned int i = 0; i < length; i++){
verts[i] = _verts[i];
}
}
我认为在阅读文件时我的代码是错误的,但如果不是只是要求查看我的其他代码。
[编辑]
它为所有obj文件执行此操作(未尝试使用其他程序导出,并且我尝试过的obj文件只有很少的面)。