using System; using System.Collections.Generic; using System.Text; using Vector3 = Nuclex.Math.Vector3; using Matrix44 = Nuclex.Math.Matrix44; using D3D = Microsoft.DirectX.Direct3D; using DX = Microsoft.DirectX; namespace Nuclex.Opal.SimpleExample { public class Entity { public Entity(D3D.Device device, Solid visualizedSolid) { m_visualizedSolid = visualizedSolid; // The entities in our example will only ever have one shape // otherwise this code would be badly broken :) switch(m_visualizedSolid.Data.GetShapeData(0).Type) { case ShapeTypes.Box: { BoxShapeData boxShape = (m_visualizedSolid.Data.GetShapeData(0) as BoxShapeData); m_mesh = D3D.Mesh.Box( device, boxShape.Dimensions.X, boxShape.Dimensions.Y, boxShape.Dimensions.Z ); break; } case ShapeTypes.Sphere: { SphereShapeData sphereShape = (m_visualizedSolid.Data.GetShapeData(0) as SphereShapeData); m_mesh = D3D.Mesh.Sphere(device, sphereShape.Radius, 32, 32); break; } case ShapeTypes.Capsule: { CapsuleShapeData capsuleShape = (m_visualizedSolid.Data.GetShapeData(0) as CapsuleShapeData); m_mesh = D3D.Mesh.Cylinder( device, capsuleShape.Radius, capsuleShape.Radius, capsuleShape.Length, 32, 32 ); break; } default: { break; } } mtrl = new D3D.Material(); mtrl.AmbientColor = randomColor(); mtrl.EmissiveColor = randomColor(); mtrl.DiffuseColor = randomColor(); mtrl.SpecularColor = randomColor(); } private DX.ColorValue randomColor() { System.Random r = new Random(); return new DX.ColorValue( (float)r.NextDouble(), (float)r.NextDouble(), (float)r.NextDouble() ); } public void Render() { Matrix44 transform = m_visualizedSolid.Transform; //transform = Matrix44.BuildZRotation(Nuclex.Math.Trigonometry.ToRadians(90.0f)) * transform; transform = Matrix44.BuildYRotation(Nuclex.Math.Trigonometry.ToRadians(90.0f)) * transform; DX.Matrix matrix = new DX.Matrix(); matrix.M11 = transform[0, 0]; matrix.M12 = transform[0, 1]; matrix.M13 = transform[0, 2]; matrix.M14 = transform[0, 3]; matrix.M21 = transform[1, 0]; matrix.M22 = transform[1, 1]; matrix.M23 = transform[1, 2]; matrix.M24 = transform[1, 3]; matrix.M31 = transform[2, 0]; matrix.M32 = transform[2, 1]; matrix.M33 = transform[2, 2]; matrix.M34 = transform[2, 3]; matrix.M41 = transform[3, 0]; matrix.M42 = transform[3, 1]; matrix.M43 = transform[3, 2]; matrix.M44 = transform[3, 3]; matrix = DX.Matrix.TransposeMatrix(matrix); m_mesh.Device.Material = mtrl; m_mesh.Device.Transform.World = matrix; m_mesh.DrawSubset(0); } private Solid m_visualizedSolid; private D3D.Mesh m_mesh; private D3D.Material mtrl; } } // namespace Nuclex.Opal.SimpleExample