源码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using SlimDX; 6 using RGeos.SlimScene.Core; 7 using SlimDX.Direct3D9; 8 using System.Drawing; 9
10 namespace RGeos.SlimScene.Renderable 11 { 12 public class Axis : IRenderable 13 { 14 public bool IsOn = true; 15 public bool IsInitialize = false; 16 public Mesh mMeshArrow = null; 17 public Mesh mMeshStick = null; 18 private Material material;//定义材质变量
19 private Material materialStick;//定义材质变量
20 public void Initialize(DrawArgs drawArgs) 21 { 22 if (IsOn && !IsInitialize) 23 { 24 mMeshArrow = Mesh.CreateCylinder(drawArgs.Device, 1.5f, 0.0f, 5.0f, 12, 5); 25 mMeshStick = Mesh.CreateCylinder(drawArgs.Device, 0.5f, 0.5f, 10.0f, 6, 6); 26 LoadTexturesAndMaterials(drawArgs); 27 } 28 IsInitialize = true; 29 } 30 private void LoadTexturesAndMaterials(DrawArgs drawArgs)//导入贴图和材质
31 { 32 material = new Material(); 33 material.Diffuse = Color.Red; 34 material.Ambient = Color.White; 35 material.Specular = Color.Yellow; 36 material.Power = 15.0F; 37
38 materialStick = new Material(); 39 materialStick.Diffuse = Color.Yellow; 40 materialStick.Ambient = Color.White; 41 materialStick.Specular = Color.Yellow; 42 materialStick.Power = 15.0F; 43 } 44 public void Update(DrawArgs drawArgs) 45 { 46 if (IsOn && !IsInitialize) 47 { 48 Initialize(drawArgs); 49 } 50 } 51
52 public void Render(DrawArgs drawArgs) 53 { 54 Matrix world = drawArgs.Device.GetTransform(TransformState.World); 55 int currentColorOp = drawArgs.Device.GetTextureStageState(0, TextureStage.ColorOperation); 56 int lightCur = drawArgs.Device.GetRenderState(RenderState.Lighting); 57 try
58 { 59 drawArgs.Device.SetRenderState(RenderState.Lighting, true); 60 drawArgs.Device.EnableLight(0, true); 61 AxisX(drawArgs); 62 AxisY(drawArgs); 63 AxisZ(drawArgs); 64
65 } 66 catch (Exception) 67 { 68 } 69 finally
70 { 71 drawArgs.Device.EnableLight(0, false); 72 drawArgs.Device.SetTextureStageState(0, TextureStage.ColorOperation, currentColorOp); 73 drawArgs.Device.SetTransform(TransformState.World, world); 74 drawArgs.Device.SetRenderState(RenderState.Lighting, lightCur); 75 } 76
77 } 78
79 public void AxisX(DrawArgs drawArgs) 80 { 81 Light light = new Light(); 82 light.Type = LightType.Spot; 83 light.Diffuse = Color.White; 84 light.Direction = new Vector3(mPosition.X, mPosition.Y + 2, mPosition.Z + 4); 85 drawArgs.Device.SetLight(0, light); 86
87 //drawArgs.Device.SetTextureStageState(0, TextureStage.ColorOperation, TextureOperation.SelectArg1);
88 drawArgs.Device.SetRenderState(RenderState.Ambient, Color.FromArgb(Color.Red.ToArgb()).ToArgb()); 89
90 drawArgs.Device.Material = material; 91
92 Matrix MoveModel = Matrix.Translation(new Vector3(12.5f, 0f, 0f)); 93 MoveModel = Matrix.RotationY((float)Math.PI / 2) * MoveModel;//右乘
94 Matrix tmp = Matrix.RotationZ((float)mAngle); 95 tmp = tmp * Matrix.Translation(mPosition.X, mPosition.Y, mPosition.Z); 96 tmp = MoveModel * tmp; 97 drawArgs.Device.SetTransform(TransformState.World, tmp); 98 //设置顶点格式
99 mMeshArrow.DrawSubset(0); 100
101 Light light2 = new Light(); 102 light2.Type = LightType.Directional; 103 light2.Diffuse = Color.White; 104 light.Direction = new Vector3(mPosition.X, mPosition.Y + 2, mPosition.Z + 4); 105 drawArgs.Device.SetLight(0, light2); 106
107 drawArgs.Device.SetRenderState(RenderState.Ambient, Color.FromArgb(Color.Red.ToArgb()).ToArgb()); 108
109 drawArgs.Device.Material = materialStick; 110
111 Matrix MoveModel2 = Matrix.Translation(new Vector3(5, 0, 0)); 112 MoveModel2 = Matrix.RotationY((float)Math.PI / 2) * MoveModel2; 113 Matrix tmp2 = Matrix.RotationZ((float)mAngle); 114 tmp2 = tmp2 * Matrix.Translation(mPosition.X, mPosition.Y, mPosition.Z); 115 tmp2 = MoveModel2 * tmp2; 116 drawArgs.Device.SetTransform(TransformState.World, tmp2); 117 mMeshStick.DrawSubset(0); 118 } 119
120 public void AxisY(DrawArgs drawArgs) 121 { 122
123 Light light = new Light(); 124 light.Type = LightType.Spot; 125 light.Diffuse = Color.White; 126 light.Direction = new Vector3(mPosition.X, mPosition.Y + 2, mPosition.Z + 4); 127 drawArgs.Device.SetLight(0, light); 128
129 //drawArgs.Device.SetTextureStageState(0, TextureStage.ColorOperation, TextureOperation.SelectArg1);
130 drawArgs.Device.SetRenderState(RenderState.Ambient, Color.FromArgb(Color.Green.ToArgb()).ToArgb()); 131
132 drawArgs.Device.Material = material; 133
134 Matrix MoveModel = Matrix.Translation(new Vector3(0f, 12.5f, 0f)); 135 MoveModel = Matrix.RotationX((float)-Math.PI / 2) * MoveModel;//右乘
136 Matrix tmp = Matrix.RotationZ((float)mAngle); 137 tmp = tmp * Matrix.Translation(mPosition.X, mPosition.Y, mPosition.Z); 138 tmp = MoveModel * tmp; 139 drawArgs.Device.SetTransform(TransformState.World, tmp); 140 //设置顶点格式
141 mMeshArrow.DrawSubset(0); 142
143 Light light2 = new Light(); 144 light2.Type = LightType.Directional; 145 light2.Diffuse = Color.White; 146 light.Direction = new Vector3(mPosition.X, mPosition.Y + 2, mPosition.Z + 4); 147 drawArgs.Device.SetLight(0, light2); 148
149 drawArgs.Device.SetRenderState(RenderState.Ambient, Color.FromArgb(Color.Green.ToArgb()).ToArgb()); 150
151 drawArgs.Device.Material = materialStick; 152
153 Matrix MoveModel2 = Matrix.Translation(new Vector3(0, 5, 0)); 154 MoveModel2 = Matrix.RotationX((float)-Math.PI / 2) * MoveModel2; 155 Matrix tmp2 = Matrix.RotationZ((float)mAngle); 156 tmp2 = tmp2 * Matrix.Translation(mPosition.X, mPosition.Y, mPosition.Z); 157 tmp2 = MoveModel2 * tmp2; 158 drawArgs.Device.SetTransform(TransformState.World, tmp2); 159 mMeshStick.DrawSubset(0); 160 } 161
162 public void AxisZ(DrawArgs drawArgs) 163 { 164
165 Light light = new Light(); 166 light.Type = LightType.Spot; 167 light.Diffuse = Color.White; 168 light.Direction = new Vector3(mPosition.X, mPosition.Y + 2, mPosition.Z + 4); 169 drawArgs.Device.SetLight(0, light); 170
171 //drawArgs.Device.SetTextureStageState(0, TextureStage.ColorOperation, TextureOperation.SelectArg1);
172 drawArgs.Device.SetRenderState(RenderState.Ambient, Color.FromArgb(Color.Blue.ToArgb()).ToArgb()); 173
174 drawArgs.Device.Material = material; 175
176 Matrix MoveModel = Matrix.Translation(new Vector3(0f, 0f, 12.5f)); 177 // MoveModel = Matrix.RotationX((float)Math.PI) * MoveModel;//右乘
178 Matrix tmp = Matrix.RotationZ((float)mAngle); 179 tmp = tmp * Matrix.Translation(mPosition.X, mPosition.Y, mPosition.Z); 180 tmp = MoveModel * tmp; 181 drawArgs.Device.SetTransform(TransformState.World, tmp); 182 //设置顶点格式
183 mMeshArrow.DrawSubset(0); 184
185 Light light2 = new Light(); 186 light2.Type = LightType.Directional; 187 light2.Diffuse = Color.White; 188 light.Direction = new Vector3(mPosition.X, mPosition.Y + 2, mPosition.Z + 4); 189 drawArgs.Device.SetLight(0, light2); 190
191 drawArgs.Device.SetRenderState(RenderState.Ambient, Color.FromArgb(Color.Blue.ToArgb()).ToArgb()); 192
193 drawArgs.Device.Material = materialStick; 194
195 Matrix MoveModel2 = Matrix.Translation(new Vector3(0, 0, 5)); 196 // MoveModel2 = Matrix.RotationX((float)Math.PI) * MoveModel2;
197 Matrix tmp2 = Matrix.RotationZ((float)mAngle); 198 tmp2 = tmp2 * Matrix.Translation(mPosition.X, mPosition.Y, mPosition.Z); 199 tmp2 = MoveModel2 * tmp2; 200 drawArgs.Device.SetTransform(TransformState.World, tmp2); 201 mMeshStick.DrawSubset(0); 202 } 203
204
205 public void Dispose() 206 { 207 if (mMeshArrow != null && !mMeshArrow.Disposed) 208 { 209 mMeshArrow.Dispose(); 210 mMeshArrow = null; 211 } 212 if (mMeshStick != null && !mMeshStick.Disposed) 213 { 214 mMeshStick.Dispose(); 215 mMeshStick = null; 216 } 217 IsInitialize = false; 218 } 219
220 public double mAngle 221 { 222 get; 223 set; 224 } 225
226 public Vector3 mPosition 227 { 228 get; 229 set; 230 } 231
232 public void SetTransform(Vector3 position) 233 { 234 mPosition = position; 235 } 236
237 public void SetRotateZ(double angle) 238 { 239 mAngle = angle; 240 } 241 } 242 }
结果: