/* * Copyright (C) 2008-2018 TrinityCore * Copyright (C) 2005-2011 MaNGOS * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along * with this program. If not, see . */ #include "vmapexport.h" #include "model.h" #include "wmo.h" #include "adtfile.h" #include "cascfile.h" #include #include #include extern CASC::StorageHandle CascStorage; Model::Model(std::string &filename) : filename(filename), vertices(0), indices(0) { memset(&header, 0, sizeof(header)); } bool Model::open() { CASCFile f(CascStorage, filename.c_str()); if (f.isEof()) { f.close(); // Do not show this error on console to avoid confusion, the extractor can continue working even if some models fail to load //printf("Error loading model %s\n", filename.c_str()); return false; } _unload(); uint32 m2start = 0; char const* ptr = f.getBuffer(); while (m2start + 4 < f.getSize() && *reinterpret_cast(ptr) != '02DM') { ++m2start; ++ptr; if (m2start + sizeof(ModelHeader) > f.getSize()) return false; } memcpy(&header, f.getBuffer() + m2start, sizeof(ModelHeader)); if (header.nBoundingTriangles > 0) { f.seek(m2start); f.seekRelative(header.ofsBoundingVertices); vertices = new Vec3D[header.nBoundingVertices]; f.read(vertices,header.nBoundingVertices*12); for (uint32 i=0; i 0) { for (uint32 i = 0; i < nIndexes; ++i) { if ((i % 3) - 1 == 0 && i + 1 < nIndexes) { uint16 tmp = indices[i]; indices[i] = indices[i + 1]; indices[i + 1] = tmp; } } fwrite(indices, sizeof(unsigned short), nIndexes, output); } fwrite("VERT", 4, 1, output); wsize = sizeof(int) + sizeof(float) * 3 * nVertices; fwrite(&wsize, sizeof(int), 1, output); fwrite(&nVertices, sizeof(int), 1, output); if (nVertices >0) { for (uint32 vpos = 0; vpos < nVertices; ++vpos) { float tmp = vertices[vpos].y; vertices[vpos].y = -vertices[vpos].z; vertices[vpos].z = tmp; } fwrite(vertices, sizeof(float)*3, nVertices, output); } fclose(output); return true; } Vec3D fixCoordSystem(Vec3D v) { return Vec3D(v.x, v.z, -v.y); } Vec3D fixCoordSystem2(Vec3D v) { return Vec3D(v.x, v.z, v.y); } ModelInstance::ModelInstance(CASCFile& f, char const* ModelInstName, uint32 mapID, uint32 tileX, uint32 tileY, uint32 originalMapId, FILE* pDirfile, std::vector* dirfileCache) : id(0), scale(0), flags(0) { float ff[3]; f.read(&id, 4); f.read(ff, 12); pos = fixCoords(Vec3D(ff[0], ff[1], ff[2])); f.read(ff, 12); rot = Vec3D(ff[0], ff[1], ff[2]); f.read(&scale, 2); f.read(&flags, 2); // scale factor - divide by 1024. blizzard devs must be on crack, why not just use a float? sc = scale / 1024.0f; char tempname[512]; sprintf(tempname, "%s/%s", szWorkDirWmo, ModelInstName); FILE* input = fopen(tempname, "r+b"); if (!input) { //printf("ModelInstance::ModelInstance couldn't open %s\n", tempname); return; } fseek(input, 8, SEEK_SET); // get the correct no of vertices int nVertices; int count = fread(&nVertices, sizeof (int), 1, input); fclose(input); if (count != 1 || nVertices == 0) return; uint16 adtId = 0;// not used for models uint32 tcflags = MOD_M2; if (tileX == 65 && tileY == 65) tcflags |= MOD_WORLDSPAWN; if (mapID != originalMapId) tcflags |= MOD_PARENT_SPAWN; //write mapID, tileX, tileY, Flags, ID, Pos, Rot, Scale, name fwrite(&mapID, sizeof(uint32), 1, pDirfile); fwrite(&tileX, sizeof(uint32), 1, pDirfile); fwrite(&tileY, sizeof(uint32), 1, pDirfile); fwrite(&tcflags, sizeof(uint32), 1, pDirfile); fwrite(&adtId, sizeof(uint16), 1, pDirfile); fwrite(&id, sizeof(uint32), 1, pDirfile); fwrite(&pos, sizeof(float), 3, pDirfile); fwrite(&rot, sizeof(float), 3, pDirfile); fwrite(&sc, sizeof(float), 1, pDirfile); uint32 nlen = strlen(ModelInstName); fwrite(&nlen, sizeof(uint32), 1, pDirfile); fwrite(ModelInstName, sizeof(char), nlen, pDirfile); if (dirfileCache) { dirfileCache->emplace_back(); ADTOutputCache& cacheModelData = dirfileCache->back(); cacheModelData.Flags = tcflags & ~MOD_PARENT_SPAWN; cacheModelData.Data.resize( sizeof(uint16) + // adtId sizeof(uint32) + // id sizeof(float) * 3 + // pos sizeof(float) * 3 + // rot sizeof(float) + // sc sizeof(uint32) + // nlen nlen); // ModelInstName uint8* cacheData = cacheModelData.Data.data(); #define CACHE_WRITE(value, size, count, dest) memcpy(dest, value, size * count); dest += size * count; CACHE_WRITE(&adtId, sizeof(uint16), 1, cacheData); CACHE_WRITE(&id, sizeof(uint32), 1, cacheData); CACHE_WRITE(&pos, sizeof(float), 3, cacheData); CACHE_WRITE(&rot, sizeof(float), 3, cacheData); CACHE_WRITE(&sc, sizeof(float), 1, cacheData); CACHE_WRITE(&nlen, sizeof(uint32), 1, cacheData); CACHE_WRITE(ModelInstName, sizeof(char), nlen, cacheData); #undef CACHE_WRITE } /* int realx1 = (int) ((float) pos.x / 533.333333f); int realy1 = (int) ((float) pos.z / 533.333333f); int realx2 = (int) ((float) pos.x / 533.333333f); int realy2 = (int) ((float) pos.z / 533.333333f); fprintf(pDirfile,"%s/%s %f,%f,%f_%f,%f,%f %f %d %d %d,%d %d\n", MapName, ModelInstName, (float) pos.x, (float) pos.y, (float) pos.z, (float) rot.x, (float) rot.y, (float) rot.z, sc, nVertices, realx1, realy1, realx2, realy2 ); */ }