HRESULT CTDDDLoader::ParseTDDD(IStream* pStream, ILMFNode* parentChildren) { IFFRESULT iffresult; IFFCHUNK ck; while ((iffresult = IFFDescendChunk(pStream, &ck)) == IFF_OK) { switch (ck.ckID) { case ID_OBJ: { ParseOBJ(pStream, parentChildren); } break; default: ATLTRACE("%4.4s\n", &ck.ckID); } if (iffresult < 0) break; iffresult = IFFAscendChunk(pStream, &ck); } return iffresult; }
//-***************************************************************************** void ParseOBJ( Reader &iReadInto, const std::string &iFileName ) { std::ifstream fStr( iFileName.c_str() ); if ( !fStr ) { ParseReader reader( iReadInto ); reader.start( iFileName ); std::stringstream sstr; sstr << "ERROR: OBJ stream \"" << iFileName << "\": " << std::endl << "Couldn't open file: " << iFileName << std::endl; reader.error( iFileName, sstr.str(), 0 ); return; } ParseOBJ( iReadInto, iFileName, fStr ); }
static struct Mesh * LoadOBJ(const char *filename) { Mesh *theMesh; theMesh = malloc(sizeof(Mesh)); theMesh->coordIndex = NULL; theMesh->vertices = NULL; // ProcessMesh may deal with these theMesh->triangleCountList = NULL; theMesh->vertexNormals = NULL; theMesh->vertexToTriangleTable = NULL; theMesh->textureCoords = NULL; theMesh->textureIndex = NULL; theMesh->normalsIndex = NULL; hasPositionIndices = true; hasTexCoordIndices = false; hasNormalIndices = false; vertCount=0; texCount=0; normalsCount=0; coordCount=0; fp = fopen(filename, "rw"); if (fp == NULL) { fprintf(stderr, "Unable to open file '%s'\n", filename); fflush(stderr); return NULL; } ParseOBJ(theMesh); fclose(fp); // Allocate arrays! if (vertCount > 0) theMesh->vertices = malloc(sizeof(GLfloat) * vertCount); if (texCount > 0) theMesh->textureCoords = malloc(sizeof(GLfloat) * texCount); if (normalsCount > 0) theMesh->vertexNormals = malloc(sizeof(GLfloat) * normalsCount); if (hasPositionIndices) theMesh->coordIndex = malloc(sizeof(int) * coordCount); if (hasNormalIndices) theMesh->normalsIndex = malloc(sizeof(int) * coordCount); if (hasTexCoordIndices) theMesh->textureIndex = malloc(sizeof(int) * coordCount); // Zero again vertCount=0; texCount=0; normalsCount=0; coordCount=0; fp = fopen(filename, "rw"); if (fp == NULL) return NULL; ParseOBJ(theMesh); fclose(fp); theMesh->vertexCount = vertCount/3; theMesh->coordCount = coordCount; // Counters for tex and normals, texCount and normalsCount theMesh->texCount = texCount/2; theMesh->normalsCount = normalsCount/3; // Should be the same as vertexCount! // This assumption could make handling of some models break! return theMesh; }