public McnkChunk_s parseMcnkChunk(MemoryStream ms, Terrain_s terrain) { long mcnk_off = ms.Position; McnkChunk_s mcnk_chunk = new McnkChunk_s(); mcnk_chunk.Read(ms); // set stream position to MCVT chunk ms.Position = mcnk_off + mcnk_chunk.mcvtOff; McvtChunk_s mcvt_chunk = new McvtChunk_s(); mcvt_chunk.Read(ms, "MCVT"); // read all height values uint num_heights = mcvt_chunk.size / sizeof(float); HeightMap_t height_map = terrain.heightMap; height_map.Capacity = (int) num_heights; for (int i = 0; i < num_heights; i++) { height_map.Add(ms.ReadFloat()); } // set stream position to MCNR chunk McnrChunk_s mcnr_chunk = new McnrChunk_s(); ms.Position = mcnk_off + mcnk_chunk.mcnrOff; mcnr_chunk.Read(ms); // read all normal values //int num_normals = mcnr_chunk.size / sizeof( char ); // size is wrong Normals_t normals = terrain.normals; normals.Capacity = (int) num_heights; byte normal; for (int i = 0; i < num_heights; i++) { Vector3 v = new Vector3(); normal = (byte) ms.ReadByte(); v.X = normal * 0.00787401574803149606299212598425f; normal = (byte)ms.ReadByte(); v.Z = normal * 0.00787401574803149606299212598425f; normal = (byte)ms.ReadByte(); v.Y = normal * 0.00787401574803149606299212598425f; normals.Add(v); } ms.Position = mcnk_off + 0x8 + mcnk_chunk.size; return mcnk_chunk; }
public bool Load(mpq.Wrapper mpq_h, string name) { byte[] buf = mpq_h.GetFile(name + ".adt"); if (buf == null) return false; MemoryStream ms = new MemoryStream(buf); int chunk_size = 0; chunk_size = (int) _mverChunk.Read(ms); chunk_size = (int)_mhdrChunk.Read(ms); if (_mhdrChunk.mh2oOff != 0) { chunk_size = (int)_mh2oChunk.Read(ms); } _adtTerrain.Capacity = 256; for (int i = 0; i < 256; i++) { Terrain_s terrain = new Terrain_s(); McnkChunk_s mcnk_chunk = parseMcnkChunk(ms, terrain); terrain.areaId = mcnk_chunk.areaId; terrain.position = mcnk_chunk.position; terrain.holes = mcnk_chunk.holes ; // deal w/ water if (i < _mh2oChunk.headers.Count) { Mh2oChunk_s.Header_s hdr = _mh2oChunk.headers[i]; terrain.GenerateTerrain(); if ( hdr.renderMap == 0 && hdr.numLayers > 0 && hdr.infos[0].type == 0x2 ) hdr.renderMap = 0xffffffffffffffff; if (hdr.renderMap != 0) { for (int wy = 0; wy < 8; wy++) { for (int wx = 0; wx < 8; wx++) { int cell_idx = wy * 8 + wx; // water cell index // Do we have water in this cell? if ((hdr.renderMap & (1UL << cell_idx)) != 0) { int terr_idx = cell_idx; // terrain index // A terrain cell is made up of 12 indices which describe // 4 polygons that make up a cell in our map chunk. for (int ii = 0; ii < 12; ii++) { terrain.indices[terr_idx * 12 + ii] = -1; // mark every index with uint max } } } } } Util.removeGeometry(ref terrain.indices, ref terrain.vertices, ref terrain.normals); } _adtTerrain.Add(terrain); } return true; }