public ChunkAttach(byte[] file, int address, uint imageBase, Dictionary <int, string> labels) : this() { if (labels.ContainsKey(address)) { Name = labels[address]; } else { Name = "attach_" + address.ToString("X8"); } ChunkType ctype; int tmpaddr = ByteConverter.ToInt32(file, address); if (tmpaddr != 0) { tmpaddr = (int)unchecked ((uint)tmpaddr - imageBase); Vertex = new List <VertexChunk>(); if (labels.ContainsKey(tmpaddr)) { VertexName = labels[tmpaddr]; } else { VertexName = "vertex_" + tmpaddr.ToString("X8"); } ctype = (ChunkType)(ByteConverter.ToUInt32(file, tmpaddr) & 0xFF); while (ctype != ChunkType.End) { VertexChunk chunk = new VertexChunk(file, tmpaddr); Vertex.Add(chunk); tmpaddr += (chunk.Size * 4) + 4; ctype = (ChunkType)(ByteConverter.ToUInt32(file, tmpaddr) & 0xFF); } } tmpaddr = ByteConverter.ToInt32(file, address + 4); if (tmpaddr != 0) { tmpaddr = (int)unchecked ((uint)tmpaddr - imageBase); Poly = new List <PolyChunk>(); if (labels.ContainsKey(tmpaddr)) { PolyName = labels[tmpaddr]; } else { PolyName = "poly_" + tmpaddr.ToString("X8"); } PolyChunk chunk = PolyChunk.Load(file, tmpaddr); while (chunk.Type != ChunkType.End) { //if (chunk.Type != ChunkType.Null) Poly.Add(chunk); tmpaddr += chunk.ByteSize; chunk = PolyChunk.Load(file, tmpaddr); } } Bounds = new BoundingSphere(file, address + 8); }
private List <MeshInfo> ProcessPolyList(string name, List <PolyChunk> strips, int start) { List <MeshInfo> result = new List <MeshInfo>(); for (int i = start; i < strips.Count; i++) { PolyChunk chunk = strips[i]; MaterialBuffer.UpdateFromPolyChunk(chunk); switch (chunk.Type) { case ChunkType.Bits_CachePolygonList: byte cachenum = ((PolyChunkBitsCachePolygonList)chunk).List; #if modellog Extensions.Log("Caching Poly List " + name + "[" + (i + 1) + "] to cache #" + cachenum + Environment.NewLine); #endif PolyCache[cachenum] = new CachedPoly(name, strips, i + 1); return(result); case ChunkType.Bits_DrawPolygonList: cachenum = ((PolyChunkBitsDrawPolygonList)chunk).List; CachedPoly cached = PolyCache[cachenum]; #if modellog Extensions.Log("Drawing Poly List " + cached.Name + "[" + cached.Index + "] from cache #" + cachenum + Environment.NewLine); #endif result.AddRange(ProcessPolyList(cached.Name, cached.Polys, cached.Index)); break; case ChunkType.Strip_Strip: case ChunkType.Strip_StripUVN: case ChunkType.Strip_StripUVH: case ChunkType.Strip_StripNormal: case ChunkType.Strip_StripUVNNormal: case ChunkType.Strip_StripUVHNormal: case ChunkType.Strip_StripColor: case ChunkType.Strip_StripUVNColor: case ChunkType.Strip_StripUVHColor: case ChunkType.Strip_Strip2: case ChunkType.Strip_StripUVN2: case ChunkType.Strip_StripUVH2: { PolyChunkStrip c2 = (PolyChunkStrip)chunk; #if modellog Extensions.Log("Strip " + c2.Type + Environment.NewLine); #endif bool hasVColor = false; switch (chunk.Type) { case ChunkType.Strip_StripColor: case ChunkType.Strip_StripUVNColor: case ChunkType.Strip_StripUVHColor: hasVColor = true; break; } bool hasUV = false; switch (chunk.Type) { case ChunkType.Strip_StripUVN: case ChunkType.Strip_StripUVH: case ChunkType.Strip_StripUVNColor: case ChunkType.Strip_StripUVHColor: case ChunkType.Strip_StripUVN2: case ChunkType.Strip_StripUVH2: hasUV = true; break; } List <Poly> polys = new List <Poly>(); List <VertexData> verts = new List <VertexData>(); #if modellog List <ushort> indexes = new List <ushort>(); #endif foreach (PolyChunkStrip.Strip strip in c2.Strips) { #if modellog indexes.AddRange(strip.Indexes); #endif Strip str = new Strip(strip.Indexes.Length, strip.Reversed); for (int k = 0; k < strip.Indexes.Length; k++) { str.Indexes[k] = (ushort)verts.Count; verts.Add(new VertexData( VertexBuffer[strip.Indexes[k]].Position, VertexBuffer[strip.Indexes[k]].Normal, hasVColor ? (Color?)strip.VColors[k] : VertexBuffer[strip.Indexes[k]].Color, hasUV ? strip.UVs[k] : null)); } polys.Add(str); } if (!hasVColor) { hasVColor = verts.Any(a => a.Color.HasValue && a.Color.Value != Color.White); } #if modellog indexes = new List <ushort>(System.Linq.Enumerable.Distinct(indexes)); indexes.Sort(); StringBuilder sb = new StringBuilder("Vertex Usage: "); ushort ist = indexes[0]; for (int k = 0; k < indexes.Count - 1; k++) { if (indexes[k + 1] != indexes[k] + 1) { sb.Append(" " + ist); if (indexes[k] != ist) { sb.Append("-" + indexes[k]); } ist = indexes[++k]; } } sb.Append(" " + ist); if (indexes[indexes.Count - 1] != ist) { sb.Append("-" + indexes[indexes.Count - 1]); } sb.Append(Environment.NewLine); Extensions.Log(sb.ToString()); #endif result.Add(new MeshInfo(MaterialBuffer, polys.ToArray(), verts.ToArray(), hasUV, hasVColor)); MaterialBuffer = new NJS_MATERIAL(MaterialBuffer); } break; } } return(result); }
public void UpdateFromPolyChunk(PolyChunk chunk) { switch (chunk) { case PolyChunkBitsBlendAlpha bba: SourceAlpha = bba.SourceAlpha; DestinationAlpha = bba.DestinationAlpha; break; case PolyChunkBitsMipmapDAdjust bmda: MipmapDAdjust = bmda.MipmapDAdjust; break; case PolyChunkBitsSpecularExponent bse: Exponent = bse.SpecularExponent; break; case PolyChunkTinyTextureID ttid: MipmapDAdjust = ttid.MipmapDAdjust; ClampU = ttid.ClampU; ClampV = ttid.ClampV; FilterMode = ttid.FilterMode; FlipU = ttid.FlipU; FlipV = ttid.FlipV; SuperSample = ttid.SuperSample; TextureID = ttid.TextureID; break; case PolyChunkMaterial mat: SourceAlpha = mat.SourceAlpha; DestinationAlpha = mat.DestinationAlpha; if (mat.Ambient.HasValue) { AmbientColor = mat.Ambient.Value; } if (mat.Diffuse.HasValue) { DiffuseColor = mat.Diffuse.Value; } if (mat.Specular.HasValue) { SpecularColor = mat.Specular.Value; Exponent = mat.SpecularExponent; } break; case PolyChunkStrip str: DoubleSided = str.DoubleSide; EnvironmentMap = str.EnvironmentMapping; FlatShading = str.FlatShading; IgnoreLighting = str.IgnoreLight; IgnoreSpecular = str.IgnoreSpecular; IgnoreAmbient = str.IgnoreAmbient; UseAlpha = str.UseAlpha; UseTexture = EnvironmentMap; switch (chunk.Type) { case ChunkType.Strip_StripUVN: case ChunkType.Strip_StripUVH: case ChunkType.Strip_StripUVNColor: case ChunkType.Strip_StripUVHColor: case ChunkType.Strip_StripUVN2: case ChunkType.Strip_StripUVH2: UseTexture = true; break; } break; } }