void Reader::readShape(){ shape = new Shape; readVertices(); readColor(); readFaces(); model->addShape(shape); }
//----------------------------------------------------------------------------------------------------------- bool RawMesh::read(const char* fileName) { if(fileName == nullptr) return false; std::ifstream stream(fileName, std::ios::binary); if(!stream) { std::cout << "error: could not open file" << std::endl; return false; } if(!readHeader(stream)) return false; if(!readVertices(stream)) return false; if(!readFaces(stream)) return false; if(!readMaterials(stream)) return false; if(!readBones(stream)) return false; if(stream.fail()) { std::cout << "error: broken file" << std::endl; return false; } return true; }
void XMLMesh::startElement(const xmlChar *name, const xmlChar **attrs) { switch (state) { case OUTSIDE: if (xmlStrcasecmp(name, (xmlChar *) "mesh") == 0) { readMesh(name, attrs); state = INSIDE_MESH; } break; case INSIDE_MESH: if (xmlStrcasecmp(name, (xmlChar *) "vertices") == 0) { readVertices(name, attrs); state = INSIDE_VERTICES; } else if (xmlStrcasecmp(name, (xmlChar *) "cells") == 0) { readCells(name, attrs); state = INSIDE_CELLS; } break; case INSIDE_VERTICES: if (xmlStrcasecmp(name, (xmlChar *) "vertex") == 0) readVertex(name, attrs); break; case INSIDE_CELLS: if (xmlStrcasecmp(name, (xmlChar *) "interval") == 0) { readInterval(name, attrs); } else if (xmlStrcasecmp(name, (xmlChar *) "triangle") == 0) { readTriangle(name, attrs); } else if (xmlStrcasecmp(name, (xmlChar *) "tetrahedron") == 0) { readTetrahedron(name, attrs); } break; default: break; } };
void readShape(ifstream* file, Model* m){ readVertices(file, m); skipLine(file); // Skip # verts skipLine(file); // Skip blank line skipLine(file); // Skip object name readFaces(file, m); skipLine(file); // Skip # faces }
void ObjLoader::readSubMesh(ifstream &in, Mesh3D *pMesh) { char strLine[256]; while(!in.eof()) { in>>type_; if(type_ == string("#")) { in.getline(strLine,256); continue; } //case: Vertex else if(type_ == string("v")) readVertices(in,strLine); //case: TexCoord //case: Face else if(type_ == string("f")) { readFaces(in, strLine); break; } //default else in.getline(strLine,256); }//end while //assign number of vertices pMesh->vertices_ = vertices_; pMesh->numVerts_=vertices_.size(); pMesh->numFaces_=faces_.size(); pMesh->numTexCoords_=texCoords_.size(); pMesh->texCoords_.reserve(texCoords_.size()); pMesh->faces_.reserve(faces_.size()); for(unsigned int i=0;i<vertices_.size();i++) { pMesh->vertices_[i] = vertices_[i]; } for(unsigned int i=0;i<texCoords_.size();i++) { pMesh->texCoords_[i] = texCoords_[i]; } for(unsigned int i=0;i<faces_.size();i++) { pMesh->faces_.push_back(TriFace(faces_[i].VertexIndex)); }//end for //reset the vectors faces_.clear(); texCoords_.clear(); vertices_.clear(); }
static int l_graphics_Mesh_setVertices(lua_State* state) { l_assertType(state, 1, l_graphics_isMesh); l_graphics_Mesh *mesh = l_graphics_toMesh(state, 1); bool hasColor; size_t count = readVertices(state, &hasColor, 2); graphics_Mesh_setVertices(&mesh->mesh, count, (graphics_Vertex const*)moduleData.buffer); return 0; }
PLYObject::PLYObject(FILE *in) { int i; nproperties = 0; hasnormal = hascolor = hastexture = false; nv = nf = ne = 0; vertices = NULL; #ifndef INTERLEAVED normals = NULL; colors = NULL; #endif texcoords = NULL; faces = NULL; // init bounding box for (i = 0; i < 3; i++) { min[i] = FLT_MAX; max[i] = -FLT_MAX; } // default order for (i = 0; i < 11; i++) order[i] = -1; if (!checkHeader(in)) { fprintf(stderr, "Error: could not read PLY file.\n"); return; } #ifdef INTERLEAVED vertices = (RenderPoint*)calloc(nv, sizeof(RenderPoint)); #else vertices = (Vector3f*)calloc(nv, sizeof(Vector3f)); normals = (Vector3f*)calloc(nv, sizeof(Vector3f)); if (hascolor) colors = (Color3u*)calloc(nv, sizeof(Color3u)); #endif if (hastexture){ texcoords = (Texture2f*)calloc(nf, sizeof(Texture2f)); } faces = (Index3i*)calloc(nf, sizeof(Index3i)); fnormals = (Vector3f*)calloc(nf, sizeof(Vector3f)); readVertices(in); readFaces(in); }
void G3D::readSoA(AbstractReader& reader, G3D::GeometrySoA& geometry) { if (reader.isOpen()) { geometry.info = readHeader(reader); if (geometry.info.numberVertices == 0) return; if (geometry.info.vertexType == SoA) readContent(reader, geometry); else if (geometry.info.vertexType == AoS) { geometry.info.vertexType = SoA; geometry.indices = readIndices(reader, geometry.info); std::vector<float> vertices = readVertices(reader, geometry.info); geometry.vertexAttributes = convertVertices(vertices, geometry.info); } } }
static int l_graphics_newMesh(lua_State* state) { bool useVertexColor; size_t count = readVertices(state, &useVertexColor, 1); graphics_Image const* texture = l_graphics_toTextureOrError(state, 2); graphics_MeshDrawMode mode = l_tools_toEnumOrError(state, 3, l_graphics_MeshDrawMode); l_graphics_Mesh* mesh = lua_newuserdata(state, sizeof(l_graphics_Mesh)); graphics_Mesh_new(&mesh->mesh, count, (graphics_Vertex*)moduleData.buffer, texture, mode, useVertexColor); lua_pushvalue(state, 2); mesh->textureRef = luaL_ref(state, LUA_REGISTRYINDEX); lua_rawgeti(state, LUA_REGISTRYINDEX, moduleData.meshMT); lua_setmetatable(state, -2); return 1; }
void _3dsLoader::processNextObjectChunk(Chunk ¤tChunk, Wrapper &wrapper, Mesh *mesh) const { while(!currentChunk.endOfFile()) { Chunk innerChunk(currentChunk); // purposefully ignore chunks not listed below switch(innerChunk.getID()) { case OBJECT_MESH: processNextObjectChunk(innerChunk, wrapper, mesh); break; case OBJECT_VERTICES: readVertices(innerChunk, mesh); break; case OBJECT_FACES: readVertexIndices(innerChunk, wrapper, mesh); break; case OBJECT_MATERIAL: readObjectMaterial(innerChunk, wrapper, mesh); break; case OBJECT_UV: readUVCoordinates(innerChunk, mesh); break; } } }
MeshData* LoadObj::readObjFile(std::string pObj) { bool first = true; fstream f(pObj.c_str()); string tmp; if(f) { while(f.eof() != true) { f >> tmp; if(tmp == "v") readVertices(f); if(tmp == "vt") readTexC(f); if(tmp == "vn") readNormal(f); if(tmp == "f") readFaces(f); if(tmp == "mtllib") readMtl(f); if(tmp == "g") readGroup(f); if(f.eof()) break; } } MeshData* data = new MeshData(); data->position = position; data->textureCoordinate = textureCoordinate; data->normal = normal; data->vertices = vertices; data->indices = indices; reset(); return data; }
void G3D::read(const std::string & file, GeometrySoA * const geometry) { std::fstream fs; fs.open(file.c_str(), std::fstream::in | std::fstream::binary); if (fs.is_open()) { readHeader(fs, geometry->info); if (geometry->info.vertexType == SoA) readContent(fs, *geometry); else if (geometry->info.vertexType == AoS) { geometry->info.vertexType = SoA; readIndices(fs, geometry->indices, geometry->info); float * vertices = NULL; readVertices(fs, vertices, geometry->info); convertVertices(vertices, geometry->vertexAttributes, geometry->info); cleanVertices(vertices); } fs.close(); } }
void AMD3Decoder::readMeshes() { DEBUG_OUT<<"AMD3Decoder::readMeshes()...\n"; if(!fd) return; //if(errorFlag) return; DEBUG_OUT<<"There are "<<nmeshes<<" meshes.\n"; if(!nmeshes) return; fseek(fd,meshesOffset,SEEK_SET); for(unsigned int i=0;i<nmeshes;i++) { lastMesh=i; DEBUG_OUT<<"mesh "<<i<<": \n"; // NOTE: Only read first mesh, since we lose track of the offset to the next mesh header if(!i) { /*if(!errorFlag)*/ readMeshHeader(); /*if(!errorFlag)*/ readVertices(); /*if(!errorFlag)*/ readTriangles(); /*if(!errorFlag)*/ readTextures(); /*if(!errorFlag)*/ readTextCoords(); } } }
void G3D::readContent(std::fstream & fs, GeometrySoA & geometry) { readIndices(fs, geometry.indices, geometry.info); readVertices(fs, geometry.vertexAttributes, geometry.info); }
void G3D::readContent(std::fstream & fs, GeometryAoS & geometry) { readIndices(fs, geometry.indices, geometry.info); readVertices(fs, geometry.vertices, geometry.info); }
static spAttachment *readAttachment(spSkeletonBinary *self, spSkin *skin, int slotIndex, const char *attachmentName) { spAttachment *attachment = NULL; float scale = self->scale; char *name = readString(self); if (name == NULL) name = (char *)attachmentName; switch ((spAttachmentType)readByte(self)) { case SP_ATTACHMENT_REGION: { spRegionAttachment *region; char *path = readString(self); if (path == NULL) path = name; attachment = spAttachmentLoader_createAttachment(self->attachmentLoader, skin, SP_ATTACHMENT_REGION, name, path); region = SUB_CAST(spRegionAttachment, attachment); if (path) { region->path = copyString(path); } region->rotation = readFloat(self); region->x = readFloat(self) * scale; region->y = readFloat(self) * scale; region->scaleX = readFloat(self); region->scaleY = readFloat(self); region->width = readFloat(self) * scale; region->height = readFloat(self) * scale; readColor(self, ®ion->r, ®ion->g, ®ion->b, ®ion->a); spRegionAttachment_updateOffset(region); spAttachmentLoader_configureAttachment(self->attachmentLoader, attachment); break; } case SP_ATTACHMENT_BOUNDING_BOX: { spBoundingBoxAttachment *boundingBox; int vertexCount = readVarint(self, true); attachment = spAttachmentLoader_createAttachment(self->attachmentLoader, skin, SP_ATTACHMENT_BOUNDING_BOX, name, name); boundingBox = SUB_CAST(spBoundingBoxAttachment, attachment); readVertices(self, SUPER(boundingBox), vertexCount); SUPER(boundingBox)->worldVerticesLength = vertexCount << 1; spAttachmentLoader_configureAttachment(self->attachmentLoader, attachment); break; } case SP_ATTACHMENT_MESH: { spMeshAttachment *mesh; int vertexCount; char *path = readString(self); if (path == NULL) path = name; attachment = spAttachmentLoader_createAttachment(self->attachmentLoader, skin, SP_ATTACHMENT_MESH, name, path); mesh = SUB_CAST(spMeshAttachment, attachment); if (path) { mesh->path = copyString(path); } readColor(self, &mesh->r, &mesh->g, &mesh->b, &mesh->a); vertexCount = readVarint(self, true); mesh->regionUVs = readFloats(self, 1, vertexCount << 1); mesh->trianglesCount = readVarint(self, true); mesh->triangles = readShorts(self, mesh->trianglesCount); readVertices(self, SUPER(mesh), vertexCount); SUPER(mesh)->worldVerticesLength = vertexCount << 1; mesh->hullLength = readVarint(self, true) << 1; spMeshAttachment_updateUVs(mesh); spAttachmentLoader_configureAttachment(self->attachmentLoader, attachment); break; } case SP_ATTACHMENT_LINKED_MESH: { spMeshAttachment *mesh; char *parent; char *skinName; char *path = readString(self); if (path == NULL) path = name; attachment = spAttachmentLoader_createAttachment(self->attachmentLoader, skin, SP_ATTACHMENT_LINKED_MESH, name, path); mesh = SUB_CAST(spMeshAttachment, attachment); if (path) { mesh->path = copyString(path); } readColor(self, &mesh->r, &mesh->g, &mesh->b, &mesh->a); skinName = readString(self); parent = readString(self); mesh->inheritDeform = readBoolean(self); addLinkedMesh(self, mesh, skinName, slotIndex, parent); break; } case SP_ATTACHMENT_PATH: { spPathAttachment *path; int vertexCount; attachment = spAttachmentLoader_createAttachment(self->attachmentLoader, skin, SP_ATTACHMENT_PATH, name, NULL); path = SUB_CAST(spPathAttachment, attachment); path->closed = readBoolean(self); path->constantSpeed = readBoolean(self); vertexCount = readVarint(self, true); readVertices(self, SUPER(path), vertexCount); SUPER(path)->worldVerticesLength = vertexCount << 1; path->lengthsLength = vertexCount / 3; path->lengths = MALLOC(float, path->lengthsLength); for (int i = 0; i < path->lengthsLength; i++) { path->lengths[i] = readFloat(self) * self->scale; } break; } } return attachment; }
/* MapPreviewCanvas::openMap * Opens a map from a mapdesc_t *******************************************************************/ bool MapPreviewCanvas::openMap(Archive::mapdesc_t map) { // All errors = invalid map Global::error = "Invalid map"; // Check if this map is a pk3 map bool map_archive = false; if (map.archive) { map_archive = true; // Attempt to open entry as wad archive temp_archive = new WadArchive(); if (!temp_archive->open(map.head)) { delete temp_archive; return false; } // Detect maps vector<Archive::mapdesc_t> maps = temp_archive->detectMaps(); // Set map if there are any in the archive if (maps.size() > 0) map = maps[0]; else return false; } // Parse UDMF map if (map.format == MAP_UDMF) { ArchiveEntry* udmfdata = NULL; for (ArchiveEntry* mapentry = map.head; mapentry != map.end; mapentry = mapentry->nextEntry()) { // Check entry type if (mapentry->getType() == EntryType::getType("udmf_textmap")) { udmfdata = mapentry; break; } } if (udmfdata == NULL) return false; // Start parsing Tokenizer tz; tz.openMem(udmfdata->getData(), udmfdata->getSize(), map.head->getName()); // Get first token string token = tz.getToken(); size_t vertcounter = 0, linecounter = 0, thingcounter = 0; while (!token.IsEmpty()) { if (!token.CmpNoCase("namespace")) { // skip till we reach the ';' do { token = tz.getToken(); } while (token.Cmp(";")); } else if (!token.CmpNoCase("vertex")) { // Get X and Y properties bool gotx = false; bool goty = false; double x = 0.; double y = 0.; do { token = tz.getToken(); if (!token.CmpNoCase("x") || !token.CmpNoCase("y")) { bool isx = !token.CmpNoCase("x"); token = tz.getToken(); if (token.Cmp("=")) { wxLogMessage("Bad syntax for vertex %i in UDMF map data", vertcounter); return false; } if (isx) x = tz.getDouble(), gotx = true; else y = tz.getDouble(), goty = true; // skip to end of declaration after each key do { token = tz.getToken(); } while (token.Cmp(";")); } } while (token.Cmp("}")); if (gotx && goty) addVertex(x, y); else { wxLogMessage("Wrong vertex %i in UDMF map data", vertcounter); return false; } vertcounter++; } else if (!token.CmpNoCase("linedef")) { bool special = false; bool twosided = false; bool gotv1 = false, gotv2 = false; size_t v1 = 0, v2 = 0; do { token = tz.getToken(); if (!token.CmpNoCase("v1") || !token.CmpNoCase("v2")) { bool isv1 = !token.CmpNoCase("v1"); token = tz.getToken(); if (token.Cmp("=")) { wxLogMessage("Bad syntax for linedef %i in UDMF map data", linecounter); return false; } if (isv1) v1 = tz.getInteger(), gotv1 = true; else v2 = tz.getInteger(), gotv2 = true; // skip to end of declaration after each key do { token = tz.getToken(); } while (token.Cmp(";")); } else if (!token.CmpNoCase("special")) { special = true; // skip to end of declaration after each key do { token = tz.getToken(); } while (token.Cmp(";")); } else if (!token.CmpNoCase("sideback")) { twosided = true; // skip to end of declaration after each key do { token = tz.getToken(); } while (token.Cmp(";")); } } while (token.Cmp("}")); if (gotv1 && gotv2) addLine(v1, v2, twosided, special); else { wxLogMessage("Wrong line %i in UDMF map data", linecounter); return false; } linecounter++; } else if (S_CMPNOCASE(token, "thing")) { // Get X and Y properties bool gotx = false; bool goty = false; double x = 0.; double y = 0.; do { token = tz.getToken(); if (!token.CmpNoCase("x") || !token.CmpNoCase("y")) { bool isx = !token.CmpNoCase("x"); token = tz.getToken(); if (token.Cmp("=")) { wxLogMessage("Bad syntax for thing %i in UDMF map data", vertcounter); return false; } if (isx) x = tz.getDouble(), gotx = true; else y = tz.getDouble(), goty = true; // skip to end of declaration after each key do { token = tz.getToken(); } while (token.Cmp(";")); } } while (token.Cmp("}")); if (gotx && goty) addThing(x, y); else { wxLogMessage("Wrong thing %i in UDMF map data", vertcounter); return false; } vertcounter++; } else { // Check for side or sector definition (increase counts) if (S_CMPNOCASE(token, "sidedef")) n_sides++; else if (S_CMPNOCASE(token, "sector")) n_sectors++; // map preview ignores sidedefs, sectors, comments, // unknown fields, etc. so skip to end of block do { token = tz.getToken(); } while (token.Cmp("}")); } // Iterate to next token token = tz.getToken(); } } // Non-UDMF map if (map.format != MAP_UDMF) { // Read vertices (required) if (!readVertices(map.head, map.end, map.format)) return false; // Read linedefs (required) if (!readLines(map.head, map.end, map.format)) return false; // Read things if (map.format != MAP_UDMF) readThings(map.head, map.end, map.format); // Read sides & sectors (count only) ArchiveEntry* sidedefs = NULL; ArchiveEntry* sectors = NULL; while (map.head) { // Check entry type if (map.head->getType() == EntryType::getType("map_sidedefs")) sidedefs = map.head; if (map.head->getType() == EntryType::getType("map_sectors")) sectors = map.head; // Exit loop if we've reached the end of the map entries if (map.head == map.end) break; else map.head = map.head->nextEntry(); } if (sidedefs && sectors) { // Doom64 map if (map.format != MAP_DOOM64) { n_sides = sidedefs->getSize() / 30; n_sectors = sectors->getSize() / 26; } // Doom/Hexen map else { n_sides = sidedefs->getSize() / 12; n_sectors = sectors->getSize() / 16; } } } // Clean up if (map_archive) { temp_archive->close(); delete temp_archive; temp_archive = NULL; } // Refresh map Refresh(); return true; }
CModelFile* CModelReader::read() { atUint32 magic = base::readUint32(); if (magic != 0xDEADBABE) { Athena::io::MemoryWriter tmp; decompressFile(tmp, base::data(), base::length()); if (tmp.length() > 0x10) base::setData(tmp.data(), tmp.length()); magic = base::readUint32(); } if (magic != 0xDEADBABE /*&& magic != 0x9381000A*/) THROW_INVALID_DATA_EXCEPTION("Not a valid CMDL magic, expected 0xDEADBABE got 0x%.8X\n", magic); atUint32 version; if (magic != 0x9381000A) version = base::readUint32(); else version = CModelFile::DKCR; if (!(version >= CModelFile::MetroidPrime1 && version <= CModelFile::DKCR)) THROW_INVALID_DATA_EXCEPTION("Only Metroid Prime 1 to 3 models are supported got v%i\n", version); try { m_result = new CModelFile; m_result->m_version = (CModelFile::Version)version; m_result->m_flags = base::readUint32(); m_result->m_boundingBox.min.x = base::readFloat(); m_result->m_boundingBox.min.y = base::readFloat(); m_result->m_boundingBox.min.z = base::readFloat(); m_result->m_boundingBox.max.x = base::readFloat(); m_result->m_boundingBox.max.y = base::readFloat(); m_result->m_boundingBox.max.z = base::readFloat(); atUint32 sectionCount = base::readUint32(); atInt32 materialCount = base::readInt32(); m_sectionSizes.resize(sectionCount); if (m_result->m_flags & 0x10) { base::readUint32(); atUint32 visGroupCount = base::readUint32(); while((visGroupCount--) > 0) { atUint32 len = base::readUint32(); base::readString(len); } base::readUint32(); base::readUint32(); base::readUint32(); base::readUint32(); base::readUint32(); } m_result->m_materialSets.resize(materialCount); for (atUint32 i = 0; i < sectionCount; i++) m_sectionSizes[i] = base::readUint32(); base::seekAlign32(); const atUint32 sectionBias = ((m_result->m_version == CModelFile::DKCR || m_result->m_version == CModelFile::MetroidPrime3) ? 1 : materialCount); Athena::io::MemoryReader sectionReader(new atUint8[2], 2); sectionReader.setEndian(endian()); for (atUint32 i = 0; i < sectionCount; i++) { if (m_sectionSizes[i] == 0) continue; if (materialCount > 0) { if (m_result->m_version != CModelFile::DKCR && m_result->m_version != CModelFile::MetroidPrime3) { atUint8* data = base::readUBytes(m_sectionSizes[i]); CMaterialReader reader(data, m_sectionSizes[i]); CMaterialSet materialSet; switch(m_result->m_version) { case CModelFile::MetroidPrime1: materialSet = reader.read(CMaterial::MetroidPrime1); break; case CModelFile::MetroidPrime2: materialSet = reader.read(CMaterial::MetroidPrime2); break; default: break; } m_result->m_materialSets[i] = materialSet; materialCount--; continue; } else { atUint8* data = base::readUBytes(m_sectionSizes[i]); CMaterialReader reader(data, m_sectionSizes[i]); atUint32 setIdx = 0; while ((materialCount--) > 0) { CMaterialSet& materialSet = m_result->m_materialSets[setIdx]; switch(m_result->m_version) { case CModelFile::MetroidPrime3: materialSet = reader.read(CMaterial::MetroidPrime3); break; case CModelFile::DKCR: materialSet = reader.read(CMaterial::DKCR); default: break; } setIdx++; } continue; } } else { SectionType section = (SectionType)(i - sectionBias); atUint8* data = base::readUBytes(m_sectionSizes[i]); sectionReader.setData(data, m_sectionSizes[i]); switch(section) { case SectionType::Vertices: { if (m_result->m_flags & 0x20) readVertices(sectionReader, true); else readVertices(sectionReader); } break; case SectionType::Normals: readNormals(sectionReader); break; case SectionType::Colors: readColors(sectionReader); break; case SectionType::TexCoord0: readTexCoords(0, sectionReader); break; case SectionType::TexCoord1orMeshOffsets: { if (m_result->m_flags & EFormatFlags::TexCoord1 || m_result->m_version == CModelFile::DKCR) readTexCoords(1, sectionReader); else readMeshOffsets(sectionReader); break; } case SectionType::MeshInfo: { if (m_meshOffsets.size() == 0) { readMeshOffsets(sectionReader); break; } } default: if ((i - sectionBias) >= 5) readMesh(sectionReader); break; } } } } catch(...) { delete m_result; m_result = nullptr; throw; } return m_result; }
void G3D::readContent(AbstractReader& reader, GeometryAoS& geometry) { geometry.indices = readIndices(reader, geometry.info); geometry.vertices = readVertices(reader, geometry.info); }