public TrifanInfo ParseTrifanInfo() { TrifanInfo trifan = new TrifanInfo(); trifan.TrifanIndex = m_sourceFile.ReadUInt16(); trifan.VertexCount = m_sourceFile.ReadByte(); trifan.Flags = m_sourceFile.ReadByte(); trifan.Unknown4 = m_sourceFile.ReadUInt32(); trifan.Unknown5 = m_sourceFile.ReadUInt32(); //bool isSolidColor = (set.TrifanData[i].Flags & 0x04) == 0x04; //if (isSolidColor) //{ // set.TrifanData[i].TextureIndex = (set.TrifanData[i].Unknown4 & 0xFFFF); //} //else //{ // set.TrifanData[i].TextureIndex = (set.TrifanData[i].Unknown5 & 0xFFFF); //} //set.TrifanData[i].UVDepth = (ushort)(set.TrifanData[i].Unknown4 & 0xFFFF); //if (set.TrifanData[i].Flags != 0x04) // if flag 0x04 is not set //{ trifan.TextureIndex = (trifan.Unknown5 & 0xFFFF); //} //else //{ // set.TrifanData[i].TextureIndex = UInt32.MaxValue; // set to invalid value //} trifan.UVDepth = (ushort)(trifan.Unknown4 & 0xFFFF); if (trifan.UVDepth == 0) { trifan.UVDepth = 1; } trifan.VertexIndices = new ushort[trifan.VertexCount]; for (int j = 0; j < trifan.VertexCount; j++) { trifan.VertexIndices[j] = m_sourceFile.ReadUInt16(); } if (trifan.Flags != 0x04) { for (int d = 0; d < trifan.UVDepth; d++) { trifan.UVIndex = m_sourceFile.ReadBytes(trifan.VertexCount); } } if (m_sourceFile.DatType != DatType.Portal_ToD) { m_sourceFile.AlignToDwordBoundary(); } return(trifan); }
/// <summary> /// Initialized the vertex & index buffers for this mesh scene /// </summary> public void ReInitialize() { if (m_noTexturing) { ReInitializeWithoutTexturing(); return; } // prepare vars m_coloredTrifans.Clear(); m_texturedTrifans.Clear(); // loop through the texturing info and determine for each of them what // type of decoration it is. m_decorations = new MeshDecoration[m_simpleMesh.TextureInfo.Length]; m_textures = new Texture[m_decorations.Length]; for (int i = 0; i < m_decorations.Length; i++) { m_decorations[i] = new MeshDecoration(m_simpleMesh.TextureInfo[i]); if (m_decorations[i].DecorationType == DecorationType.Texture) { Bitmap bmp = m_decorations[i].Texture.GetBitmap(); m_textures[i] = new Texture(m_device, bmp, 0, Pool.Managed); } } // we'll have to create two sets of index buffers: // one for textured trifans and one for colored trifans // we'll have to determine for each trifan to which of these it belongs List <VertexLookup> texturedVertexTable = new List <VertexLookup>(); List <VertexLookup> coloredVertexTable = new List <VertexLookup>(); List <ushort> texturedIndexList = new List <ushort>(); List <ushort> coloredIndexList = new List <ushort>(); for (int i = 0; i < (int)m_simpleMesh.SecondTrifanSet.TrifanCount; i++) { TrifanInfo trifan = m_simpleMesh.SecondTrifanSet.TrifanData[i]; bool drawSolidColor = false; drawSolidColor = (trifan.Flags & 0x04) == 0x04; // draw solid when this trifan is drawn using a solid color drawSolidColor = drawSolidColor || (m_decorations[(int)trifan.TextureIndex].DecorationType == DecorationType.SolidColor); if (drawSolidColor) { Color color = m_decorations[(int)trifan.TextureIndex].SolidColor; TrifanDrawInfo trifanInfo = new TrifanDrawInfo(); trifanInfo.IndexBufferStart = (ushort)coloredIndexList.Count; trifanInfo.PrimitiveCount = trifan.VertexCount - 2; for (int j = 0; j < trifan.VertexIndices.Length; j++) { VertexLookup lookup = new VertexLookup(trifan.VertexIndices[j], 0, color); ushort vertexIndex; if (!coloredVertexTable.Contains(lookup)) { coloredVertexTable.Add(lookup); } vertexIndex = (ushort)coloredVertexTable.IndexOf(lookup); coloredIndexList.Add(vertexIndex); } m_coloredTrifans.Add(i, trifanInfo); } else { TrifanDrawInfo trifanInfo = new TrifanDrawInfo(); trifanInfo.IndexBufferStart = (ushort)texturedIndexList.Count; trifanInfo.PrimitiveCount = trifan.VertexCount - 2; for (int j = 0; j < trifan.VertexIndices.Length; j++) { VertexLookup lookup = new VertexLookup(trifan.VertexIndices[j], trifan.UVIndex[j]); ushort vertexIndex; if (!texturedVertexTable.Contains(lookup)) { texturedVertexTable.Add(lookup); } vertexIndex = (ushort)texturedVertexTable.IndexOf(lookup); texturedIndexList.Add(vertexIndex); } m_texturedTrifans.Add(i, trifanInfo); } } // now create the actual index & vertex buffers // texture vb & ib if (texturedVertexTable.Count > 0) { m_vbTextured = new VertexBuffer(typeof(CustomVertex.PositionNormalTextured), texturedVertexTable.Count, m_device, Usage.WriteOnly, CustomVertex.PositionNormalTextured.Format, Pool.Managed); GraphicsStream stream = m_vbTextured.Lock(0, 0, LockFlags.None); foreach (VertexLookup lookup in texturedVertexTable) { VertexInfo vertex = m_simpleMesh.VertexInfo[(int)lookup.VertexId]; stream.Write(new CustomVertex.PositionNormalTextured(vertex.X, vertex.Y, vertex.Z, vertex.NX, vertex.NY, vertex.NZ, vertex.CUVData[lookup.UVIndex].U, vertex.CUVData[lookup.UVIndex].V)); } m_vbTextured.Unlock(); m_ibTextured = new IndexBuffer(typeof(ushort), texturedIndexList.Count, m_device, Usage.WriteOnly, Pool.Managed); stream = m_ibTextured.Lock(0, 0, LockFlags.None); stream.Write(texturedIndexList.ToArray()); m_ibTextured.Unlock(); } m_vbTexturedLength = texturedVertexTable.Count; // color vb & ib if (coloredVertexTable.Count > 0) { m_vbColored = new VertexBuffer(typeof(CustomVertex.PositionNormalColored), coloredVertexTable.Count, m_device, Usage.WriteOnly, CustomVertex.PositionNormalColored.Format, Pool.Managed); GraphicsStream stream = m_vbColored.Lock(0, 0, LockFlags.None); foreach (VertexLookup lookup in coloredVertexTable) { VertexInfo vertex = m_simpleMesh.VertexInfo[(int)lookup.VertexId]; stream.Write(new CustomVertex.PositionNormalColored(vertex.X, vertex.Y, vertex.Z, vertex.NX, vertex.NY, vertex.NZ, lookup.Color.ToArgb())); } m_vbColored.Unlock(); m_ibColored = new IndexBuffer(typeof(ushort), coloredIndexList.Count, m_device, Usage.WriteOnly, Pool.Managed); stream = m_ibColored.Lock(0, 0, LockFlags.None); stream.Write(coloredIndexList.ToArray()); m_ibColored.Unlock(); } m_vbColoredLength = coloredVertexTable.Count; }
public void ReInitializeWithoutTexturing() { // prepare vars m_coloredTrifans.Clear(); m_texturedTrifans.Clear(); // loop through the texturing info and determine for each of them what // type of decoration it is. m_decorations = new MeshDecoration[0]; m_textures = new Texture[0]; // create a single trifan set of red trifans List <VertexLookup> coloredVertexTable = new List <VertexLookup>(); List <ushort> coloredIndexList = new List <ushort>(); for (int i = 0; i < (int)m_simpleMesh.SecondTrifanSet.TrifanCount; i++) { TrifanInfo trifan = m_simpleMesh.SecondTrifanSet.TrifanData[i]; Color color = Color.Red; TrifanDrawInfo trifanInfo = new TrifanDrawInfo(); trifanInfo.IndexBufferStart = (ushort)coloredIndexList.Count; trifanInfo.PrimitiveCount = trifan.VertexCount - 2; for (int j = 0; j < trifan.VertexIndices.Length; j++) { VertexLookup lookup = new VertexLookup(trifan.VertexIndices[j], 0, color); ushort vertexIndex; if (!coloredVertexTable.Contains(lookup)) { coloredVertexTable.Add(lookup); } vertexIndex = (ushort)coloredVertexTable.IndexOf(lookup); coloredIndexList.Add(vertexIndex); } m_coloredTrifans.Add(i, trifanInfo); } // now create the actual index & vertex buffers // texture vb & ib m_vbTexturedLength = 0; // color vb & ib if (coloredVertexTable.Count > 0) { m_vbColored = new VertexBuffer(typeof(CustomVertex.PositionNormalColored), coloredVertexTable.Count, m_device, Usage.WriteOnly, CustomVertex.PositionNormalColored.Format, Pool.Managed); GraphicsStream stream = m_vbColored.Lock(0, 0, LockFlags.None); foreach (VertexLookup lookup in coloredVertexTable) { VertexInfo vertex = m_simpleMesh.VertexInfo[(int)lookup.VertexId]; stream.Write(new CustomVertex.PositionNormalColored(vertex.X, vertex.Y, vertex.Z, vertex.NX, vertex.NY, vertex.NZ, lookup.Color.ToArgb())); } m_vbColored.Unlock(); m_ibColored = new IndexBuffer(typeof(ushort), coloredIndexList.Count, m_device, Usage.WriteOnly, Pool.Managed); stream = m_ibColored.Lock(0, 0, LockFlags.None); stream.Write(coloredIndexList.ToArray()); m_ibColored.Unlock(); } m_vbColoredLength = coloredVertexTable.Count; }