/// <summary> /// Initializes a new Panel containing one 3D Mesh /// </summary> /// <param name="XFile">The .x File you want to display</param> /// <param name="txtrmap"> /// A replacementmap for Filenames. This hashtable mappes the Textures Filename /// as stored in the xFile, to another Filename or to a Stream containing the Texture /// </param> public void Init(Control parent, Point pos, Size size, Stream xfile, Hashtable txtrmap, ViewportSetting vp) { this.Size = size; this.Location = pos; this.Parent = parent; this.xfile = xfile; this.txtrmap = txtrmap; if (!InitializeGraphics()) // Initialize Direct3D { throw new Exception("Could not initialize Direct3D."); } try { OnResetDevice(device, null); } #if DEBUG catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace + "\n\n" + ex.Source); return; } #else catch (Exception) { return; } #endif if (vp == null) { this.vp = new ViewportSetting(); } else { this.vp = vp; } Monitor.Enter(render); Render(); Monitor.Enter(panelaccess); //the first Panel is created if (panels == null) { panels = new ArrayList(); panels.Add(this); InitThread(); } else { WaitForThreadRenderEnd(); panels.Add(this); } Monitor.Exit(panelaccess); Monitor.Exit(render); }
/// <summary> /// Initializes a new Panel containing one 3D Mesh /// </summary> /// <param name="XFile">The name of the .x File you want to display</param> /// <param name="txtrmap"> /// A replacementmap for Filenames. This hashtable mappes the Textures Filename /// as stored in the xFile, to another Filename or to a Stream containing the Texture /// </param> /// <param name="vp">the viewport Settings to start with</param> public Panel3D(Control parent, Point pos, Size size, string xfile, Hashtable txtrmap, ViewportSetting vp) { System.IO.FileStream fs = System.IO.File.OpenRead(xfile); Init(parent, pos, size, fs, txtrmap, vp); }
/// <summary> /// Used to Transfor the View of the Scene /// </summary> void SetupMatrices(bool bb) { if (vp == null) { vp = new ViewportSetting(); } if (true || (!bb)) { device.Transform.World = Matrix.Multiply( Matrix.Translation(-vp.ObjectCenter.X, -vp.ObjectCenter.Y, -vp.ObjectCenter.Z), Matrix.Multiply( Matrix.Scaling(-vp.Scale, vp.Scale, vp.Scale), Matrix.Multiply( Matrix.RotationZ(vp.AngelZ), Matrix.Multiply( Matrix.RotationX(vp.AngelX), Matrix.Multiply( Matrix.RotationY(vp.AngelY), Matrix.Translation(vp.X + vp.ObjectCenter.X, vp.Y + vp.ObjectCenter.Y, vp.Z + vp.ObjectCenter.Z) ) ) ) ) ); } else { device.Transform.World = Matrix.Multiply( Matrix.Scaling(vp.Scale, vp.Scale, vp.Scale), Matrix.Multiply( Matrix.Translation(-vp.X, -vp.Y, -vp.Z), Matrix.Multiply( Matrix.RotationZ(vp.AngelZ), Matrix.Multiply( Matrix.RotationX(vp.AngelX), Matrix.Multiply( Matrix.RotationY(vp.AngelY), Matrix.Multiply( Matrix.Scaling(vp.Scale, vp.Scale, vp.Scale), Matrix.Translation(vp.X, vp.Y, vp.Z) ) ) ) ) ) ); } device.Transform.View = Matrix.Multiply( Matrix.RotationX(0.3f), Matrix.Multiply( Matrix.RotationY(-0.2f), Matrix.Multiply( Matrix.RotationZ(0), Matrix.LookAtLH(vp.CameraPosition, vp.CameraTarget, vp.CameraUpVector) ) ) ); // For the projection matrix, we set up a perspective transform (which // transforms geometry from 3D view space to 2D viewport space, with // a perspective divide making objects smaller in the distance). To build // a perpsective transform, we need the field of view (1/4 pi is common), // the aspect ratio, and the near and far clipping planes (which define at // what distances geometry should be no longer be rendered). device.Transform.Projection = Matrix.PerspectiveFovLH(vp.FoV, vp.Aspect, vp.NearPlane, vp.FarPlane * 1000f); }
public void ResetDefaultViewport() { if (vp == null) { vp = new ViewportSetting(); } vp.Reset(); try { Vector3 min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue); Vector3 max = new Vector3(float.MinValue, float.MinValue, float.MinValue); int[] rank = { mesh.NumberVertices }; if (mesh.VertexBuffer.Description.VertexFormat == (VertexFormats)0x112) { CustomVertex.PositionNormalTextured[] vertices = (CustomVertex.PositionNormalTextured[])mesh.LockVertexBuffer(typeof(CustomVertex.PositionNormalTextured), LockFlags.None, rank); try { foreach (CustomVertex.PositionNormalTextured v in vertices) { if (v.X < min.X) { min.X = v.X; } if (v.Y < min.Y) { min.Y = v.Y; } if (v.Z < min.Z) { min.Z = v.Z; } if (v.X > max.X) { max.X = v.X; } if (v.Y > max.Y) { max.Y = v.Y; } if (v.Z > max.Z) { max.Z = v.Z; } } } finally { mesh.UnlockVertexBuffer(); } } else if (mesh.VertexBuffer.Description.VertexFormat == VertexFormats.PositionNormal) { CustomVertex.PositionNormal[] vertices = (CustomVertex.PositionNormal[])mesh.LockVertexBuffer(typeof(CustomVertex.PositionNormalTextured), LockFlags.None, rank); try { foreach (CustomVertex.PositionNormal v in vertices) { if (v.X < min.X) { min.X = v.X; } if (v.Y < min.Y) { min.Y = v.Y; } if (v.Z < min.Z) { min.Z = v.Z; } if (v.X > max.X) { max.X = v.X; } if (v.Y > max.Y) { max.Y = v.Y; } if (v.Z > max.Z) { max.Z = v.Z; } } } finally { mesh.UnlockVertexBuffer(); } } if (min == max) { max.X = min.X + 1; max.Y = min.Y + 1; max.Z = min.Z + 1; } Vector3 center = new Vector3((float)((max.X + min.X) / 2.0), (float)((max.Y + min.Y) / 2.0), (float)((max.Z + min.Z) / 2.0)); vp.Aspect = this.Size.Width / this.Size.Height; Vector3 boundingSphereRadius = new Vector3(min.X - center.X, min.Y - center.Y, min.Z - center.Z); double radius = boundingSphereRadius.Length() / vp.Aspect; double dist = radius / Math.Sin(vp.FoV / 2.0); //for (int i=0; i< mesh.NumberVertices; i++) vertices[i].Subtract(center); vp.ObjectCenter = center; vp.X = -center.X; vp.Y = -center.Y; vp.Z = -center.Z; vp.CameraTarget = new Vector3(0, 0, 0); vp.CameraPosition = new Vector3( 0, //center.X, 0, //center.Y, (float)dist); // + center.Z); vp.NearPlane = 0.01f; //(float)(dist-radius); vp.FarPlane = (float)((dist + radius)); //bbox = Mesh.Box(device, max.X-min.X, max.Y-min.Y, max.Z-min.Z); #if DEBUG bbox = Mesh.Sphere(device, 0.05f, 12, 4); #endif //Unlock the buffer } catch {} }
/// <summary> /// Initializes a new Panel containing one 3D Mesh /// </summary> /// <param name="XFile">The .x File you want to display</param> /// <param name="txtrmap"> /// A replacementmap for Filenames. This hashtable mappes the Textures Filename /// as stored in the xFile, to another Filename or to a Stream containing the Texture /// </param> /// <param name="vp">the viewport Settings to start with</param> public Panel3D(Control parent, Point pos, Size size, Stream xfile, Hashtable txtrmap, ViewportSetting vp) { Init(parent, pos, size, xfile, txtrmap, vp); }