/// <summary> /// Create the primitives to be drawed /// X ---- Width /// Z ---- Height /// </summary> public override void Initialize( ) { if (rectangleMesh != null) { rectangleMesh.Dispose(); } model = base.source as RectangleModel; //Create the mesh rectangleMesh = new AutoMesh(d3d, Mesh.Box(d3d.Dx, model.Width, model.Height, 0.0f)); //Get the bounding box rectangleMesh.BoundingBox(out minVector3, out maxVector3); minVector3.TransformCoordinate(model.WorldMatrix); maxVector3.TransformCoordinate(model.WorldMatrix); Vector3 minClone = minVector3; Vector3 maxClone = maxVector3; minVector3.X = minClone.X < maxClone.X ? minClone.X : maxClone.X; minVector3.Y = minClone.Y < maxClone.Y ? minClone.Y : maxClone.Y; minVector3.Z = minClone.Z < maxClone.Z ? minClone.Z : maxClone.Z; maxVector3.X = minClone.X < maxClone.X ? maxClone.X : minClone.X; maxVector3.Y = minClone.Y < maxClone.Y ? maxClone.Y : minClone.Y; maxVector3.Z = minClone.Z < maxClone.Z ? maxClone.Z : minClone.Z; model.MaxVector3 = maxVector3; model.MinVector3 = minVector3; }
/// <summary> /// Create the primitives to be drawed /// Z ---- length /// </summary> public override void Initialize( ) { if (roundMesh != null) { roundMesh.Dispose(); } model = source as RoundModel; //Create the mesh roundMesh = new AutoMesh(d3d, Mesh.Cylinder(d3d.Dx, model.Radius, model.Radius, 0.0f, Direct3dRender.DefaultSlices, Direct3dRender.DefaultStacks)); //Get the bounding box roundMesh.BoundingBox(out minVector3, out maxVector3); minVector3.TransformCoordinate(model.WorldMatrix); maxVector3.TransformCoordinate(model.WorldMatrix); Vector3 minClone = minVector3; Vector3 maxClone = maxVector3; minVector3.X = minClone.X < maxClone.X ? minClone.X : maxClone.X; minVector3.Y = minClone.Y < maxClone.Y ? minClone.Y : maxClone.Y; minVector3.Z = minClone.Z < maxClone.Z ? minClone.Z : maxClone.Z; maxVector3.X = minClone.X < maxClone.X ? maxClone.X : minClone.X; maxVector3.Y = minClone.Y < maxClone.Y ? maxClone.Y : minClone.Y; maxVector3.Z = minClone.Z < maxClone.Z ? maxClone.Z : minClone.Z; model.MinVector3 = minVector3; model.MaxVector3 = maxVector3; }
/// <summary> /// Create the primitives to be drawed /// </summary> public override void Initialize() { if (sphereMesh != null) { sphereMesh.Dispose(); } model = source as SphereModel; //Create the mesh sphereMesh = new AutoMesh(d3d, Mesh.Sphere(d3d.Dx, model.Radius, Direct3dRender.DefaultSlices, Direct3dRender.DefaultStacks)); //Get the bounding box sphereMesh.BoundingBox(out minVector3, out maxVector3); minVector3.TransformCoordinate(model.WorldMatrix); maxVector3.TransformCoordinate(model.WorldMatrix); model.MinVector3 = minVector3; model.MaxVector3 = maxVector3; }
/// <summary> /// Create the primitives to be drawed /// X ---- Width /// Y ---- Depth /// Z ---- Height /// </summary> public override void Initialize( ) { if (cuboidMesh != null) { cuboidMesh.Dispose(); } model = source as CuboidModel; //Create the mesh cuboidMesh = new AutoMesh(d3d, Mesh.Box(d3d.Dx, model.Width, model.Depth, model.Height)); //Get the bounding box cuboidMesh.BoundingBox(out minVector3, out maxVector3); minVector3.TransformCoordinate(model.WorldMatrix); maxVector3.TransformCoordinate(model.WorldMatrix); model.MinVector3 = minVector3; model.MaxVector3 = maxVector3; }
public override void Initialize( ) { //Create a plane,on which camera Plane cameraPlane = Plane.FromPointNormal(camera.Eye * 2.0f, -camera.Look); //top left corner Vector3 topleftNear = new Vector3(0, 0, 0); Vector3 topleftFar = new Vector3(0, 0, 1); topleftNear.Unproject(d3d.Dx.Viewport, camera.ProjectionMatrix, camera.ViewMatrix, Matrix.Identity); topleftFar.Unproject(d3d.Dx.Viewport, camera.ProjectionMatrix, camera.ViewMatrix, Matrix.Identity); Vector3 topleft_cameraPlane_Intersect = Plane.IntersectLine(cameraPlane, topleftNear, topleftFar); //bottom right Vector3 bottomrightNear = new Vector3(d3d.ClientSize.Width, d3d.ClientSize.Height, 0); Vector3 bottomrightFar = new Vector3(d3d.ClientSize.Width, d3d.ClientSize.Height, 1); bottomrightNear.Unproject(d3d.Dx.Viewport, camera.ProjectionMatrix, camera.ViewMatrix, Matrix.Identity); bottomrightFar.Unproject(d3d.Dx.Viewport, camera.ProjectionMatrix, camera.ViewMatrix, Matrix.Identity); Vector3 bottomright_cameraPlane_Intersect = Plane.IntersectLine(cameraPlane, bottomrightNear, bottomrightFar); //Use two points to make four plane List <Plane> planes = new List <Plane> ( ); planes.Add(cameraPlane); planes.Add(Plane.FromPointNormal(topleft_cameraPlane_Intersect, -camera.Right)); planes.Add(Plane.FromPointNormal(topleft_cameraPlane_Intersect, camera.Up)); planes.Add(Plane.FromPointNormal(bottomright_cameraPlane_Intersect, -camera.Up)); planes.Add(Plane.FromPointNormal(bottomright_cameraPlane_Intersect, camera.Right)); //Process the x axis Vector3 xAxisDirection = new Vector3(1, 0, 0); List <Vector3> xAxisIntersects = new List <Vector3> ( ); // Console.WriteLine ( "NEW INTIALIZE!" ); for (int i = 0; i < planes.Count; i++) { if (Math.Abs(Plane.DotNormal(planes[i], xAxisDirection)) <= 1e-6) //parallel { //Console.WriteLine ( "PLANE = {0} Parallel" , i ); continue; } Vector3 inter = Plane.IntersectLine(planes[i], origin, origin + new Vector3(10000f, 0, 0)); if (inter.X < 0) { continue; } //Vector3 interProj = Vector3.Project ( inter , d3d.Dx.Viewport , camera.ProjectionMatrix , camera.ViewMatrix , Matrix.Identity ); //interProj must be in the range of the viewport // if (Math.Floor ( interProj.X ) + 1 >= 0 && Math.Floor ( interProj.X ) + 1 <= d3d.Dx.Viewport.Width // && Math.Floor ( interProj.Y ) + 1 >= 0 && Math.Floor ( interProj.Y ) + 1 <= d3d.Dx.Viewport.Height) { xAxisIntersects.Add(inter); //Console.WriteLine ( "PLANE = {3} , X = {0},Y = {1},Z = {2} , dot = {4}" , inter.X , inter.Y , inter.Z , i , Math.Abs ( Plane.DotNormal ( planes[i] , xAxisDirection ) ) ); } } xAxisIntersects.Sort(CompareVector3ByLength); //Process the y axis Vector3 yAxisDirection = new Vector3(0, 1, 0); List <Vector3> yAxisIntersects = new List <Vector3> ( ); for (int i = 0; i < planes.Count; i++) { if (Math.Abs(Plane.DotNormal(planes[i], yAxisDirection)) <= 1e-6) //parallel { continue; } Vector3 inter = Plane.IntersectLine(planes[i], origin, origin + new Vector3(0, 10000f, 0)); if (inter.Y < 0) { continue; } //Vector3 interProj = Vector3.Project ( inter , d3d.Dx.Viewport , camera.ProjectionMatrix , camera.ViewMatrix , Matrix.Identity ); //interProj must be in the range of the viewport // if (Math.Floor ( interProj.X ) + 1 >= 0 && Math.Floor ( interProj.X ) + 1 <= d3d.Dx.Viewport.Width // && Math.Floor ( interProj.Y ) + 1 >= 0 && Math.Floor ( interProj.Y ) + 1 <= d3d.Dx.Viewport.Height) { yAxisIntersects.Add(inter); } } yAxisIntersects.Sort(CompareVector3ByLength); //Process the z axis1 Vector3 zAxisDirection = new Vector3(0, 0, 1); List <Vector3> zAxisIntersects = new List <Vector3> ( ); for (int i = 0; i < planes.Count; i++) { if (Math.Abs(Plane.DotNormal(planes[i], zAxisDirection)) <= 1e-6) //parallel { continue; } Vector3 inter = Plane.IntersectLine(planes[i], origin, origin + new Vector3(0, 0, 10000f)); if (inter.Z < 0) { continue; } //Vector3 interProj = Vector3.Project ( inter , d3d.Dx.Viewport , camera.ProjectionMatrix , camera.ViewMatrix , Matrix.Identity ); //interProj must be in the range of the viewport // if (Math.Floor ( interProj.X ) + 1 >= 0 && Math.Floor ( interProj.X ) + 1 <= d3d.Dx.Viewport.Width // && Math.Floor ( interProj.Y ) + 1 >= 0 && Math.Floor ( interProj.Y ) + 1 <= d3d.Dx.Viewport.Height) { zAxisIntersects.Add(inter); } } zAxisIntersects.Sort(CompareVector3ByLength); //Process the origin Vector3 originProj = Vector3.Project(origin, d3d.Dx.Viewport, camera.ProjectionMatrix, camera.ViewMatrix, Matrix.Identity); xAxis.Clear( ); yAxis.Clear( ); zAxis.Clear( ); if (xAxisCone != null) { xAxisCone.Dispose( ); xAxisCone = null; } if (yAxisCone != null) { yAxisCone.Dispose( ); yAxisCone = null; } if (zAxisCone != null) { zAxisCone.Dispose( ); zAxisCone = null; } if (originProj.X >= 0 && originProj.X <= d3d.Dx.Viewport.Width && originProj.Y >= 0 && originProj.Y <= d3d.Dx.Viewport.Height) { //Origin is in the range of viewport //X Axis if (xAxisIntersects.Count >= 1) { float xAxisLength = Vector3.Subtract(xAxisIntersects[0], origin).Length( ); xAxis.Add(origin); xAxis.Add(origin + new Vector3(xAxisLength * 0.9f, 0, 0)); } //Y Axis if (yAxisIntersects.Count >= 1) { float yAxisLength = Vector3.Subtract(yAxisIntersects[0], origin).Length( ); yAxis.Add(origin); yAxis.Add(origin + new Vector3(0, yAxisLength * 0.9f, 0)); } //Z Axis if (zAxisIntersects.Count >= 1) { float zAxisLength = Vector3.Subtract(zAxisIntersects[0], origin).Length( ); zAxis.Add(origin); zAxis.Add(origin + new Vector3(0, 0, zAxisLength * 0.9f)); } } else { //origin is out of the range of viewport //X Axis if (xAxisIntersects.Count >= 2) { float xAxisLength = Vector3.Subtract(xAxisIntersects[1], xAxisIntersects[0]).Length( ); xAxis.Add(xAxisIntersects[0]); xAxis.Add(xAxisIntersects[0] + new Vector3(xAxisLength * 0.9f, 0, 0)); } //Y Axis if (yAxisIntersects.Count >= 2) { float yAxisLength = Vector3.Subtract(yAxisIntersects[1], yAxisIntersects[0]).Length( ); yAxis.Add(yAxisIntersects[0]); yAxis.Add(yAxisIntersects[0] + new Vector3(0, yAxisLength * 0.9f, 0)); } //Z Axis if (zAxisIntersects.Count >= 2) { float zAxisLength = Vector3.Subtract(zAxisIntersects[1], zAxisIntersects[0]).Length( ); zAxis.Add(zAxisIntersects[0]); zAxis.Add(zAxisIntersects[0] + new Vector3(0, 0, zAxisLength * 0.9f)); } } if (xAxis.Count == 2) { float radius = camera.ViewportWidth * 0.008f; xAxisCone = new AutoMesh(d3d, Mesh.Cylinder(d3d.Dx, radius, 0, radius * 3, Direct3dRender.DefaultSlices, Direct3dRender.DefaultStacks)); xAxisConeMatrix = Matrix.RotationY((float)Math.PI / 2) * Matrix.Translation(xAxis[1] + new Vector3(radius * 1.5f, 0, 0)); xAxisText = xAxis[1] + new Vector3(0, radius * 2.0f, 0); xAxisText.Project(d3d.Dx.Viewport, camera.ProjectionMatrix, camera.ViewMatrix, Matrix.Identity); } if (yAxis.Count == 2) { float radius = camera.ViewportWidth * 0.008f; yAxisCone = new AutoMesh(d3d, Mesh.Cylinder(d3d.Dx, radius, 0, radius * 3, Direct3dRender.DefaultSlices, Direct3dRender.DefaultStacks)); yAxisConeMatrix = Matrix.RotationX(-(float)Math.PI / 2) * Matrix.Translation(yAxis[1] + new Vector3(0, radius * 1.5f, 0)); yAxisText = yAxis[1] + new Vector3(radius * 2.0f, 0, 0); yAxisText.Project(d3d.Dx.Viewport, camera.ProjectionMatrix, camera.ViewMatrix, Matrix.Identity); } if (zAxis.Count == 2) { float radius = camera.ViewportWidth * 0.008f; zAxisCone = new AutoMesh(d3d, Mesh.Cylinder(d3d.Dx, radius, 0, radius * 3, Direct3dRender.DefaultSlices, Direct3dRender.DefaultStacks)); zAxisConeMatrix = Matrix.Translation(zAxis[1] + new Vector3(0, 0, radius * 1.5f)); zAxisText = zAxis[1] + new Vector3(radius * 2.0f, 0, 0); zAxisText.Project(d3d.Dx.Viewport, camera.ProjectionMatrix, camera.ViewMatrix, Matrix.Identity); } }
/// <summary> /// User cancels loading the mesh /// </summary> private void buttonCancel_Click(object sender, EventArgs e) { Hide(); if (mMesh != null) mMesh.Dispose(); }
/// <summary> /// Setup the form by loading the mesh /// </summary> private void FormViewMesh_Shown(object sender, EventArgs e) { // ToDo: Show a message form while loading AutoMesh autoMesh = null; // Load the mesh (or fail and exit) try { autoMesh = AutoMesh.LoadFromXFile(mFileName, MeshFlags.SystemMemory, d3dModel); } catch { // Dispose whatever we have try { autoMesh.Dispose(); } catch { } autoMesh = null; MessageBox.Show(this, "Error loading mesh"); Hide(); return; } // Detect unsupported texture formats VertexFormats format = autoMesh.M.VertexFormat; if ( (format & VertexFormats.TextureCountMask) != VertexFormats.Texture0 && (format & VertexFormats.Texture1) != VertexFormats.Texture1 ) { MessageBox.Show(this, "Multiple textures not supported"); autoMesh.Dispose(); Hide(); return; } // Unsupported vertex formats VertexFormats unsupported = VertexFormats.Specular | VertexFormats.LastBetaD3DColor | VertexFormats.LastBetaUByte4 | VertexFormats.PointSize | VertexFormats.Transformed; if ( (int)(format & unsupported) != 0) { MessageBox.Show(this, "Unsupported vertex format"); autoMesh.Dispose(); Hide(); return; } // No position (sometimes this happens) if ( (format & VertexFormats.Position) != VertexFormats.Position ) { MessageBox.Show(this, "Unsupported vertex format (no position detected!)"); autoMesh.Dispose(); Hide(); return; } // Clone to new format, using 32 bit pixel format. try { mMesh = autoMesh.Clone(d3dModel, MeshFlags.Managed, autoMesh.M.VertexFormat, Format.A8R8G8B8, Usage.AutoGenerateMipMap, Pool.Managed); } catch { MessageBox.Show(this, "Error cloning mesh"); try { autoMesh.Dispose(); } catch { } try { mMesh.Dispose(); } catch { } Hide(); return; } autoMesh.Dispose(); autoMesh = null; // Display mesh info DisplayMeshInfo(); }