📄 dxmutmesh.cs
字号:
{
if (systemMemoryMesh != null)
systemMemoryMesh.ComputeNormals();
if (localMemoryMesh != null)
localMemoryMesh.ComputeNormals();
}
}
/// <summary>Updates the mesh to a new vertex declaration</summary>
public void SetVertexDeclaration(Device device, VertexElement[] decl)
{
Mesh tempSystemMesh = null;
Mesh tempLocalMesh = null;
VertexElement[] oldDecl = null;
using(systemMemoryMesh)
{
using (localMemoryMesh)
{
// Clone the meshes
if (systemMemoryMesh != null)
{
oldDecl = systemMemoryMesh.Declaration;
tempSystemMesh = systemMemoryMesh.Clone(systemMemoryMesh.Options.Value,
decl, device);
}
if (localMemoryMesh != null)
{
tempLocalMesh = localMemoryMesh.Clone(localMemoryMesh.Options.Value,
decl, device);
}
}
}
// Store the new meshes
systemMemoryMesh = tempSystemMesh;
localMemoryMesh = tempLocalMesh;
bool hadNormal = false;
// Check if the old declaration contains a normal.
for(int i = 0; i < oldDecl.Length; i++)
{
if (oldDecl[i].DeclarationUsage == DeclarationUsage.Normal)
{
hadNormal = true;
break;
}
}
// Check to see if the new declaration has a normal
bool hasNormalNow = false;
for(int i = 0; i < decl.Length; i++)
{
if (decl[i].DeclarationUsage == DeclarationUsage.Normal)
{
hasNormalNow = true;
break;
}
}
// Compute normals if they are being requested and the old mesh didn't have them
if ( !hadNormal && hasNormalNow )
{
if (systemMemoryMesh != null)
systemMemoryMesh.ComputeNormals();
if (localMemoryMesh != null)
localMemoryMesh.ComputeNormals();
}
}
/// <summary>Occurs after the device has been reset</summary>
private void OnResetDevice(object sender, EventArgs e)
{
Device device = sender as Device;
if (systemMemoryMesh == null)
throw new InvalidOperationException("There is no system memory mesh. Nothing to do here.");
// Make a local memory version of the mesh. Note: because we are passing in
// no flags, the default behavior is to clone into local memory.
localMemoryMesh = systemMemoryMesh.Clone((systemMemoryMesh.Options.Value & ~MeshFlags.SystemMemory),
systemMemoryMesh.VertexFormat, device);
}
/// <summary>Occurs before the device is going to be reset</summary>
private void OnLostDevice(object sender, EventArgs e)
{
if (localMemoryMesh != null)
localMemoryMesh.Dispose();
localMemoryMesh = null;
}
/// <summary>Renders this mesh</summary>
public void Render(Device device, bool canDrawOpaque, bool canDrawAlpha)
{
if (localMemoryMesh == null)
throw new InvalidOperationException("No local memory mesh.");
// Frist, draw the subsets without alpha
if (canDrawOpaque)
{
for (int i = 0; i < meshMaterials.Length; i++)
{
if (isUsingMeshMaterials)
{
if (meshMaterials[i].DiffuseColor.Alpha < 1.0f)
continue; // Only drawing opaque right now
// set the device material and texture
device.Material = meshMaterials[i];
device.SetTexture(0, meshTextures[i]);
}
localMemoryMesh.DrawSubset(i);
}
}
// Then, draw the subsets with alpha
if (canDrawAlpha)
{
for (int i = 0; i < meshMaterials.Length; i++)
{
if (meshMaterials[i].DiffuseColor.Alpha == 1.0f)
continue; // Only drawing non-opaque right now
// set the device material and texture
device.Material = meshMaterials[i];
device.SetTexture(0, meshTextures[i]);
localMemoryMesh.DrawSubset(i);
}
}
}
/// <summary>Renders this mesh</summary>
public void Render(Device device) { Render(device, true, true); }
// TODO: Render with effect
/// <summary>Compute a bounding sphere for this mesh</summary>
public float ComputeBoundingSphere(out Vector3 center)
{
if (systemMemoryMesh == null)
throw new InvalidOperationException("There is no system memory mesh. Nothing to do here.");
// Get the object declaration
int strideSize = VertexInformation.GetFormatSize(systemMemoryMesh.VertexFormat);
// Lock the vertex buffer
GraphicsStream data = null;
try
{
data = systemMemoryMesh.LockVertexBuffer(LockFlags.ReadOnly);
// Now compute the bounding sphere
return Geometry.ComputeBoundingSphere(data, systemMemoryMesh.NumberVertices,
systemMemoryMesh.VertexFormat, out center);
}
finally
{
// Make sure to unlock the vertex buffer
if (data != null)
systemMemoryMesh.UnlockVertexBuffer();
}
}
#endregion
#region IDisposable Members
/// <summary>Cleans up any resources required when this object is disposed</summary>
public void Dispose()
{
OnLostDevice(null, EventArgs.Empty);
if (meshTextures != null)
{
for(int i = 0; i < meshTextures.Length; i++)
{
if (meshTextures[i] != null)
meshTextures[i].Dispose();
}
}
meshTextures = null;
meshMaterials = null;
if (systemMemoryMesh != null)
systemMemoryMesh.Dispose();
systemMemoryMesh = null;
}
/// <summary>Cleans up any resources required when this object is disposed</summary>
private void OnDeviceDisposing(object sender, EventArgs e)
{
// Just dispose of our class
Dispose();
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -