public static void Draw(ref Device device, ParsedModel pm)
{
for (int i = 0; i < 5; i++)
{
device.SetTexture(i, null);
}
for (int x = 0; x < pm.Display.Chunk.Count; x++)
{
int rawindex = pm.Display.Chunk[x];
device.SetStreamSource(0, pm.Display.vertexBuffer[rawindex], 0);
// device.VertexFormat = HaloVertex.FVF;
device.VertexFormat = pm.Display.vertexBuffer[rawindex].Description.VertexFormat;
// This makes the sky look a little more correct...
// device.VertexFormat = VertexFormats.Texture0 | VertexFormats.PositionNormal | VertexFormats.Specular;
device.Indices = pm.Display.indexBuffer[rawindex];
for (int xx = 0; xx < pm.RawDataMetaChunks[rawindex].SubMeshInfo.Length; xx++)
{
device.RenderState.AlphaBlendEnable = false;
device.RenderState.SpecularEnable = false;
int tempshade = pm.RawDataMetaChunks[rawindex].SubMeshInfo[xx].ShaderNumber;
ShaderInfo shad = pm.Shaders.Shader[tempshade];
Renderer.SetAlphaBlending(shad.Alpha, ref device);
device.SetTexture(0, shad.MainTexture);
if (shad.Alpha == ShaderInfo.AlphaType.None)
{
device.TextureState[0].ColorOperation = TextureOperation.SelectArg1;
device.TextureState[0].ColorArgument1 = TextureArgument.TextureColor;
if (shad.BitmapNames.Count > 0 && shad.BitmapNames[0].ToLower().Contains("specular"))
{
device.RenderState.SpecularEnable = true;
device.RenderState.SpecularMaterialSource = ColorSource.Material;
device.TextureState[0].ColorOperation = TextureOperation.Modulate;
device.TextureState[0].ColorArgument1 = TextureArgument.TextureColor;
device.TextureState[0].ColorArgument2 = TextureArgument.Specular;
}
}
else
{
device.RenderState.AlphaBlendEnable = true;
device.RenderState.AlphaTestEnable = true;
device.RenderState.ReferenceAlpha = 180;
device.RenderState.AlphaFunction = Compare.Greater;
device.RenderState.SourceBlend = Blend.SourceAlpha;
device.RenderState.DestinationBlend = Blend.One; // was Blend.InvSourceAlpha;
device.TextureState[0].ColorOperation = TextureOperation.SelectArg1;
device.TextureState[0].ColorArgument1 = TextureArgument.TextureColor;
device.TextureState[0].ColorArgument2 = TextureArgument.Diffuse;
/*
device.TextureState[0].AlphaOperation = TextureOperation.Modulate; // BlendTextureAlpha;
device.TextureState[0].AlphaArgument1 = TextureArgument.TextureColor;
device.TextureState[0].AlphaArgument2 = TextureArgument.Current;
*/
}
if (shad.BitmapTextures != null)
{
for (int xxx = 1; xxx <= shad.BitmapTextures.Length; xxx++)
{
device.SetTexture(xxx, shad.BitmapTextures[xxx - 1]);
if (shad.Alpha == ShaderInfo.AlphaType.None)
{
// device.RenderState.AlphaBlendEnable = false;
device.TextureState[xxx].AlphaOperation = TextureOperation.Disable;
device.TextureState[xxx].AlphaArgument1 = TextureArgument.TextureColor;
device.TextureState[xxx].AlphaArgument2 = TextureArgument.Current;
device.TextureState[xxx].ColorOperation = TextureOperation.Disable;
device.TextureState[xxx].ColorArgument1 = TextureArgument.TextureColor;
device.TextureState[xxx].ColorArgument2 = TextureArgument.Current;
}
else
{
// device.RenderState.AlphaBlendEnable = true;
device.RenderState.SourceBlend = Blend.SourceColor;
device.RenderState.DestinationBlend = Blend.InvSourceColor;
/*
device.TextureState[xxx].ColorOperation = TextureOperation.SelectArg1;
device.TextureState[xxx].ColorArgument1 = TextureArgument.TextureColor;
device.TextureState[xxx].ColorArgument2 = TextureArgument.Diffuse;
*/
device.TextureState[xxx].AlphaOperation = TextureOperation.Modulate;
device.TextureState[xxx].AlphaArgument1 = TextureArgument.TextureColor;
device.TextureState[xxx].AlphaArgument2 = TextureArgument.Current;
}
}
}
PrimitiveType pt;
if (pm.RawDataMetaChunks[rawindex].FaceCount * 3 != pm.RawDataMetaChunks[rawindex].IndiceCount)
{
pt = PrimitiveType.TriangleStrip;
// The -2 is because it's a Triangle Strip, which has three vertices for trianlge 1 and one for
// each following traingle.
// So the total # of triangles are IndiceCount-2 (for the first two points)
// Thanks to Prey for pointing this out
try
{
device.DrawIndexedPrimitives(
pt,
0,
0,
pm.RawDataMetaChunks[rawindex].VerticeCount,
pm.RawDataMetaChunks[rawindex].SubMeshInfo[xx].IndiceStart,
pm.RawDataMetaChunks[rawindex].SubMeshInfo[xx].IndiceCount - 2);
}
catch
{
}
}
else
{
pt = PrimitiveType.TriangleList;
device.DrawIndexedPrimitives(
pt,
0,
0,
pm.RawDataMetaChunks[rawindex].VerticeCount,
pm.RawDataMetaChunks[rawindex].SubMeshInfo[xx].IndiceStart,
pm.RawDataMetaChunks[rawindex].SubMeshInfo[xx].IndiceCount / 3);
}
}
}
}