




已阅读5页,还剩8页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
前段时间弄d3d,看完红书和基于Visual+C#的DirectX开发实例,就自己练习了下这个程序sdk上面都有拾取,但是没有选中移动,最近的项目要用到,就简单仿了下3dmax的移动方式选中物体,再选取需要移动的平面,效果如下: 拾取代码中会有个小问题,就是会出现重复多选的问题,本例中是通过比较Intersect函数返回参数IntersectInformation中交点距离的最短值找出mesh的;大家可以用list的遍历找最小值,方法很多然后是关于平面的移动,当选取完成之后。会出现三个完全透明的平面,物体就是根据隐形平面的u v值进行移动的;重要代码如下:(我基本打成class类,后期工程的时候会加到dll中,这个只是演示,便于学习)这个是:class类c-sharpview plaincopyprint?1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Drawing; 6 using Microsoft.DirectX; 7 using Microsoft.DirectX.Direct3D; 8 9 namespace PickObject 10 11 public class Hawk_Class 12 13 public Device device; /Device 14 public float Pan_Width, Pan_High; /pannel Size 15 16 /初始化类 17 public Hawk_Class(Device device,float w,float h) 18 19 this.device = device; 20 Pan_Width = w; 21 Pan_High = h; 22 23 24 /寻找交点 25 public void Check_Point_Face(float Mouse_x, float Mouse_y, Vector3 Cam_Postion, 26 Mesh Chech_Mesh, Matrix Chech_Mesh_Postion, out bool Check_Bool, out IntersectInformation Point_Inf) 27 28 /计算摄像机投影窗口上对应的点击位置坐标 29 Vector3 SVector = new Vector3(); 30 SVector.X = 2 * Mouse_x / this.Pan_Width - 1; 31 SVector.Y = -2 * Mouse_y / this.Pan_High + 1; 32 SVector.Z = 1.0f / (float)Math.Tan(Math.PI / 8); 33 /视图矩阵 34 Matrix viewMatrix = this.device.Transform.View; 35 viewMatrix.Invert();/计算视图矩阵的逆矩阵 36 /射线位置 37 Vector3 rayPos = Vector3.TransformCoordinate(SVector, viewMatrix); 38 /射线方向 39 Vector3 rayDir = Vector3.Subtract(rayPos, Cam_Postion); 40 /碰撞检测 41 /将模型的世界矩阵进行逆变换 42 Matrix inverseWorld = Matrix.Invert(Chech_Mesh_Postion); 43 Vector3 localRayPos = Vector3.TransformCoordinate(rayPos, inverseWorld); 44 Vector3 localRayDir = Vector3.TransformNormal(rayDir, inverseWorld); 45 IntersectInformation PI; 46 bool result = Chech_Mesh.Intersect(localRayPos, localRayDir, out PI); 47 Check_Bool = result; 48 Point_Inf = PI; 49 50 51 /创建一个mesh类 52 public void Create_Mesh(Vector3 pointVectors,out Mesh M_H) 53 54 Mesh meshObject; 55 /定义顶点 56 CustomVertex.PositionColored arrayVertices = new CustomVertex.PositionColored4; 57 /定义索引 58 Int16 arrayIndices = new Int166; 59 /定义属性 60 AttributeRange attributeRange = new AttributeRange(); 61 /实例化网格对象 62 meshObject = new Mesh(arrayIndices.Length / 3, arrayVertices.Length, MeshFlags.SystemMemory, CustomVertex.PositionColored.Format, device); 63 /设置顶点坐标值 64 arrayVertices0.Position = pointVectors0; 65 arrayVertices0.Color = Color.FromArgb(108, 255, 255, 255).ToArgb(); 66 arrayVertices1.Position = pointVectors1; 67 arrayVertices1.Color = Color.FromArgb(108, 255, 255, 255).ToArgb(); 68 arrayVertices2.Position = pointVectors2; 69 arrayVertices2.Color = Color.FromArgb(108, 255, 255, 255).ToArgb(); 70 arrayVertices3.Position = pointVectors3; 71 arrayVertices3.Color = Color.FromArgb(108, 255, 255, 255).ToArgb(); 72 /设置索引,顶部三角形 73 arrayIndices0 = 0; arrayIndices1 = 1; arrayIndices2 = 3; 74 arrayIndices3 = 3; arrayIndices4 = 1; arrayIndices5 = 2; 75 /设置属性 76 attributeRange.AttributeId = 0; 77 attributeRange.FaceStart = 0; 78 attributeRange.FaceCount = arrayIndices.Length / 3; 79 attributeRange.VertexStart = 0; 80 attributeRange.VertexCount = arrayVertices.Length; 81 /设置网格对象的索引缓冲和顶点缓冲数据 82 meshObject.VertexBuffer.SetData(arrayVertices, 0, LockFlags.None); 83 meshObject.IndexBuffer.SetData(arrayIndices, 0, LockFlags.None); 84 meshObject.SetAttributeTable(new AttributeRange attributeRange ); 85 M_H = meshObject; 86 87 public void Create_Mesh_Face(Vector3 pointVectors, out Mesh M_H) 88 89 Mesh meshObject; 90 /定义顶点 91 CustomVertex.PositionColored arrayVertices = new CustomVertex.PositionColored3; 92 /定义索引 93 Int16 arrayIndices = new Int163; 94 /定义属性 95 AttributeRange attributeRange = new AttributeRange(); 96 /实例化网格对象 97 meshObject = new Mesh(arrayIndices.Length / 3, arrayVertices.Length, MeshFlags.SystemMemory, CustomVertex.PositionColored.Format, device); 98 /设置顶点坐标值 99 arrayVertices0.Position = pointVectors0; 100 arrayVertices0.Color = Color.FromArgb(0, 255, 255, 255).ToArgb(); 101 arrayVertices1.Position = pointVectors1; 102 arrayVertices1.Color = Color.FromArgb(0, 255, 255, 255).ToArgb(); 103 arrayVertices2.Position = pointVectors2; 104 arrayVertices2.Color = Color.FromArgb(0, 255, 255, 255).ToArgb(); 105 /设置索引,顶部三角形 106 arrayIndices0 = 0; arrayIndices1 = 1; arrayIndices2 = 2; 107 /设置属性 108 attributeRange.AttributeId = 0; 109 attributeRange.FaceStart = 0; 110 attributeRange.FaceCount = arrayIndices.Length / 3; 111 attributeRange.VertexStart = 0; 112 attributeRange.VertexCount = arrayVertices.Length; 113 /设置网格对象的索引缓冲和顶点缓冲数据 114 meshObject.VertexBuffer.SetData(arrayVertices, 0, LockFlags.None); 115 meshObject.IndexBuffer.SetData(arrayIndices, 0, LockFlags.None); 116 meshObject.SetAttributeTable(new AttributeRange attributeRange ); 117 M_H = meshObject; 118 119 120 /所有关于坐标的初始化 121 public void SetLine_and_Face(Mesh meshObjXYZ, Mesh checkface, out Material meshMaterialsXYZ, 122 VertexBuffer _xyzLineVertexBuffer) 123 124 /三个标示的大平面 125 Vector3 points = new Vector34; 126 points0 = new Vector3(0f, 0f, 0f); 127 points1 = new Vector3(0f, 5f, 0f); 128 points2 = new Vector3(5f, 5f, 0f); 129 points3 = new Vector3(5f, 0f, 0f); 130 Create_Mesh(points, out meshObjXYZ0); 131 132 Vector3 points1 = new Vector34; 133 points10 = new Vector3(0f, 0f, 0f); 134 points11 = new Vector3(0f, 5f, 0f); 135 points12 = new Vector3(0f, 5f, 5f); 136 points13 = new Vector3(0f, 0f, 5f); 137 Create_Mesh(points1, out meshObjXYZ1); 138 139 Vector3 points2 = new Vector34; 140 points20 = new Vector3(0f, 0f, 0f); 141 points21 = new Vector3(0f, 0f, 5f); 142 points22 = new Vector3(5f, 0f, 5f); 143 points23 = new Vector3(5f, 0f, 0f); 144 Create_Mesh(points2, out meshObjXYZ2); 145 146 /三个标示的大平面其材质 147 Material temp = new Material(); 148 temp.Diffuse = Color.FromArgb(108, 255, 255, 255); 149 temp.Ambient = Color.White; 150 temp.Specular = Color.White;/浅灰色 151 temp.SpecularSharpness = 1.0F; 152 meshMaterialsXYZ = temp; 153 154 /用于移动的三个大的透明平面 155 Vector3 points3 = new Vector33; 156 points30 = new Vector3(-500f, -500f, 0f); 157 points31 = new Vector3(-500f, 2000f, 0f); 158 points32 = new Vector3(2000f, -500f, 0f); 159 Create_Mesh_Face(points3, out checkface0); 160 161 Vector3 points4 = new Vector33; 162 points40 = new Vector3(0f, -500f, -500f); 163 points41 = new Vector3(0f, 2000f, -500f); 164 points42 = new Vector3(0f, -500f, 2000f); 165 Create_Mesh_Face(points4, out checkface1); 166 167 Vector3 points5 = new Vector33; 168 points50 = new Vector3(-500f, 0f, -500f); 169 points51 = new Vector3(-500f, 0f, 2000f); 170 points52 = new Vector3(2000f, 0f, -500f); 171 Create_Mesh_Face(points5, out checkface2); 172 173 /三根XYZ坐标的数据生成 174 using (GraphicsStream data = _xyzLineVertexBuffer.Lock(0, 0, LockFlags.None) 175 176 data.Write(new CustomVertex.PositionColored(0.0f, 0.0f, 0.0f, Color.Red.ToArgb(); 177 data.Write(new CustomVertex.PositionColored(10.0f, 0.0f, 0.0f, Color.Red.ToArgb(); 178 data.Write(new CustomVertex.PositionColored(0.0f, 0.0f, 0.0f, Color.Green.ToArgb(); 179 data.Write(new CustomVertex.PositionColored(0.0f, 10.0f, 0.0f, Color.Green.ToArgb(); 180 data.Write(new CustomVertex.PositionColored(0.0f, 0.0f, 0.0f, Color.White.ToArgb(); 181 data.Write(new CustomVertex.PositionColored(0.0f, 0.0f, 10.0f, Color.White.ToArgb(); 182 _xyzLineVertexBuffer.Unlock(); 183 184 185 186 /给予一个mesh数组以及对应mesh位置的数组,确认鼠标选中的最近的mesh 187 /(最后一个参数:选择哪个model,:选择哪个平面) 188 public int Select_Mesh_Fun(float Mouse_x, float Mouse_y,Vector3 Cam_Postion, 189 Mesh Check_Mesh, Matrix Chech_Mesh_Postion,int flag) 190 191 int Lenth_Data = Check_Mesh.Length; 192 int selected; 193 IntersectInformation Pi = new IntersectInformation3; 194 for (int i = 0; i Lenth_Data; i+) 195 196 bool result; 197 if(flag=99) 198 Check_Point_Face(Mouse_x, Mouse_y, Cam_Postion, Check_Meshi, Chech_Mesh_Postioni, out result, out Pii); 199 else 200 Check_Point_Face(Mouse_x, Mouse_y, Cam_Postion, Check_Meshi, Chech_Mesh_Postionflag, out result, out Pii); 201 if (!result) 202 Pii.Dist = 9999.0f; 203 204 205 #region /找出最短的mesh 206 float tempmin = Math.Min(Pi0.Dist, Pi1.Dist); 207 if (tempmin = Pi0.Dist); 208 else 209 selected = 1; 210 211 tempmin = Math.Min(tempmin, Pi2.Dist); 212 if (tempmin = Pi2.Dist) 213 selected = 2; 214 215 if (tempmin = 9999.0f) 216 selected = 99; 217 218 #endregion 219 return selected; 220 221 222 223 这个是对应loop吐出画面的函数:c-sharpview plaincopyprint?224 device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.DarkSlateBlue, 1.0f, 0); /清除windows界面为深蓝色 225 device.BeginScene(); 226 227 device.RenderState.CullMode = Cull.None;/显示里面的机构 228 229 device.Lights0.Type = LightType.Directional; 230 device.Lights0.Diffuse = System.Drawing.Color.Red; 231 device.Lights0.Direction = new Vector3(-1.0f, 1.0f, 1.0f); 232 device.Lights0.Enabled = true; /打开灯光 233 device.Lights1.Type = LightType.Directional; 234 device.Lights1.Diffuse = System.Drawing.Color.Green; 235 device.Lights1.Direction = new Vector3(1f, -1.0f, 1.0f); 236 device.Lights1.Enabled = true; /打开灯光 237 device.RenderState.Ambient = Color.SlateGray; 238 239 device.SetRenderState(RenderStates.AlphaBlendEnable, true);/开启透明度 240 device.RenderState.SourceBlend = Blend.SourceAlpha; 241 device.RenderState.DestinationBlend = Blend.InvSourceAlpha; 242 243 /以实体形式绘制茶壶模型 244 device.RenderState.FillMode = FillMode.Solid; 245 246 for (int i = 0; i meshObj.Length; i+) 247 248 device.Transform.World = meshPositioni; 249 if (i = selected) 250 251 device.RenderState.Lighting = false; 252 / XYZ 253 this.device.SetStreamSource(0, this._xyzLineVertexBuffer, 0); 254 this.device.VertexFormat = CustomVertex.PositionColored.Format; 255 this.device.DrawPrimitives(PrimitiveType.LineList, 0, 3); 256 257 device.RenderState.Lighting = true; 258 for (int j = 0; j 3; j+)/选中模型的选中平面显示 259 260 if (j = selectedFace) 261 device.Material = meshMaterialsXYZ; 262 else 263 device.Material = new Material(); 264 265 meshObjXYZj.D
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年事业单位招聘考试教师化学学科专业知识试卷备考资料解析
- 2025年物流师(初级)物流安全管理鉴定试卷
- 南昌二年级下数学试卷
- 2025年事业单位招聘考试综合类专业技能测试试卷(化学)
- 2025年无损检测资格证考试无损检测检测人员培训融合试卷
- 2025年事业单位招聘考试卫生类药学专业知识试卷:药学专业真题模拟案例分析题
- 临澧三中七下数学试卷
- 里湖中学九年级数学试卷
- 2025四川省焊工考试题及答案
- 2025事业基金考试题库及答案
- CJ/T 341-2010混空轻烃燃气
- 存款代为保管协议书
- 辅导班劳务合同协议
- 宋代汉族服装风格演变及其社会功能
- T∕CWEA 29-2024 水利水电工程砌石坝施工规范
- 日本签证个人信息处理同意书
- JTS-T 245-2023 水运工程土工合成材料试验规程
- 新兵培训课件模板
- 2025年初中语文教师招聘面试八年级上册逐字稿之愚公移山
- 自考《课程与教学论》考试复习题(附答案)
- 环境内审员试题及答案
评论
0/150
提交评论