public void RenderSingleTrifan(int index) { bool isTextured = m_texturedTrifans.ContainsKey(index); if (isTextured) { m_device.SetStreamSource(0, m_vbTextured, 0); m_device.Indices = m_ibTextured; m_device.VertexFormat = CustomVertex.PositionNormalTextured.Format; int currentTextureId = -1; TrifanDrawInfo info = m_texturedTrifans[index]; if (currentTextureId != m_simpleMesh.SecondTrifanSet.TrifanData[index].TextureIndex) { currentTextureId = (int)m_simpleMesh.SecondTrifanSet.TrifanData[index].TextureIndex; m_device.SetTexture(0, m_textures[currentTextureId]); } m_device.DrawIndexedPrimitives(PrimitiveType.TriangleFan, 0, 0, m_vbTexturedLength, info.IndexBufferStart, info.PrimitiveCount); m_device.SetTexture(0, null); } else { m_device.SetStreamSource(0, m_vbColored, 0); m_device.Indices = m_ibColored; m_device.VertexFormat = CustomVertex.PositionNormalColored.Format; // render each colored trifan TrifanDrawInfo info = m_coloredTrifans[index]; m_device.DrawIndexedPrimitives(PrimitiveType.TriangleFan, 0, 0, m_vbColoredLength, info.IndexBufferStart, info.PrimitiveCount); } }
/// <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 Render() { lock (this) { if (m_renderSubMeshOnly) { RenderSingleTrifan(m_subMeshIndex); return; } if (m_vbTexturedLength > 0) { m_device.SetStreamSource(0, m_vbTextured, 0); m_device.Indices = m_ibTextured; m_device.VertexFormat = CustomVertex.PositionNormalTextured.Format; int currentTextureId = -1; /* * m_pDirect3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,true); //alpha blending enabled * m_pDirect3DDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA); //source blend factor * m_pDirect3DDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA); //destination blend factor * * m_pDirect3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG1,D3DTA_TEXTURE); //alpha from texture * */ m_device.RenderState.AlphaBlendEnable = true; m_device.RenderState.SourceBlend = Blend.SourceAlpha; m_device.RenderState.DestinationBlend = Blend.InvSourceAlpha; m_device.TextureState[0].AlphaArgument1 = TextureArgument.TextureColor; // render each textured trifan foreach (int trifanId in m_texturedTrifans.Keys) { TrifanDrawInfo info = m_texturedTrifans[trifanId]; if (currentTextureId != m_simpleMesh.SecondTrifanSet.TrifanData[trifanId].TextureIndex) { currentTextureId = (int)m_simpleMesh.SecondTrifanSet.TrifanData[trifanId].TextureIndex; m_device.SetTexture(0, m_textures[currentTextureId]); } m_device.DrawIndexedPrimitives(PrimitiveType.TriangleFan, 0, 0, m_vbTexturedLength, info.IndexBufferStart, info.PrimitiveCount); } m_device.SetTexture(0, null); } if (m_vbColoredLength > 0) { m_device.SetStreamSource(0, m_vbColored, 0); m_device.Indices = m_ibColored; m_device.VertexFormat = CustomVertex.PositionNormalColored.Format; // render each colored trifan foreach (int trifanId in m_coloredTrifans.Keys) { TrifanDrawInfo info = m_coloredTrifans[trifanId]; m_device.DrawIndexedPrimitives(PrimitiveType.TriangleFan, 0, 0, m_vbColoredLength, info.IndexBufferStart, info.PrimitiveCount); } } m_device.SetStreamSource(0, null, 0); m_device.Indices = null; } }
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; }