Professional training 9.7

Source: Internet
Author: User

3d Basics of WPF

About the 3D aspect of things, I just tasted, never used in the project, I believe many people are also based on an interest to learn. Here are a few basic WPF 3D programming that hopefully will help beginners.

To help understand, it's all built with C # code, not written in XAML, and interesting to download the source code here. Let's take a look at the next few.

Here's a basic explanation for the first one.

3D has three important elements, models, lights, and horizons. So we're going to see an object, first of all, to exist (that is, to construct a model), and then to have a light shining on the object (that is, to construct a light, to light it on the model, otherwise to see the model), and then we have to have a vision, To see with the eyes (that is, we have to construct a vision, and the model to be seen in this field of view).

The model consists of two parts, a mesh and a texture (attached to the material). The mesh is the skeleton, the texture is the skin, both are indispensable. The grid is made up of many triangles, and the following code shows you how to construct a WPF 3D model.

A cube grid consisting of 12 triangles.

MeshGeometry3D MESHG = new MeshGeometry3D (); Point3dcollection pc = new Point3dcollection ();p c. ADD (New Point3D ( -10,10,0));p C. ADD (New Point3D (10,10,0));p C. ADD (New Point3D (10,-10,0));p C. ADD (New Point3D ( -10,-10,0));p C. ADD (New Point3D ( -10,10,-10));p C. ADD (New Point3D (10,10,-10));p C. ADD (New Point3D (10,-10,-10));p C. ADD (New Point3D ( -10,-10,-10)); meshg. positions = pc;int32collection IC = new Int32collection (), IC. ADD (0); IC. ADD (2); IC. ADD (1); IC. ADD (0); IC. ADD (3); IC. ADD (2); IC. ADD (4); IC. ADD (6); IC. ADD (5); IC. ADD (4); IC. ADD (7); IC. ADD (6); IC. ADD (4); IC. ADD (3); IC. ADD (0); IC. ADD (4); IC. ADD (7); IC. ADD (3); IC. ADD (5); IC. ADD (2); IC. ADD (1); IC. ADD (5); IC. ADD (6); IC. ADD (2); IC. ADD (4); IC. ADD (1); IC. ADD (5); IC. ADD (4); IC. ADD (0); IC. ADD (1); IC. ADD (7); IC. ADD (2); IC. ADD (6); IC. ADD (7); IC. ADD (3); IC. ADD (2); meshg. triangleindices = IC;

Next, we construct a material object, where a simple brush is used as the texture of the material. A 3D model is then constructed using this material and the mesh constructed above.

DiffuseMaterial dm = new diffusematerial ();   Dm. Brush = Brushes.cyan;   GeometryModel3D gm = new GeometryModel3D ();    gm. Geometry = MESHG;   gm. Material = DM;

Now that the 3D model has been built, we add light to it.

DirectionalLight dl = new DirectionalLight ();  Dl. Color = Colors.blue;  Dl. Direction = new Vector3D (0, 0,-1);

Next, put the model and lights in a 3D collection. My understanding is that the model and lighting are physical, this kind needs to have a place to put, so need to put this 3D set into a space, here is ModelVisual3D.

Model3DGroup mg = new Model3DGroup ();  mg. Children.add (DL);  mg. Children.add (GM);  ModelVisual3D mv = new ModelVisual3D ();  Mv. Content = mg;

At this point, all the good things have been done, the rest is a pair of beautiful eyes found. Now, just add the eye. Such a complete field of vision is constructed.

Viewport3D myviewport = new Viewport3D ();  MYVIEWPORT.CHILDREN.ADD (MV);  Perspectivecamera camera = new Perspectivecamera ();  Camera. Position = new Point3D (0, 0, N);  Camera. Updirection = new Vector3D (0, 1, 0);  Camera. Lookdirection = new Vector3D (0, 0, -20);  Myviewport.camera = Camera;

Well, now put this field of view in our form, MYGRID.CHILDREN.ADD (myviewport), you can see this cube. Here is the form code.

<window x:class= "_3dtest.window1"                xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation         " xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"        title= "Window1" height= "width=" "525" >    < Grid name= "Mygrid" >    </Grid></Window>

Nani, you only see a square? That's right, our vision now is in front of the cube, see the natural only positive. Now let's turn this cube around and you can see a few more faces.

RotateTransform3D mytransform = new RotateTransform3D (); Mytransform.rotation = new Axisanglerotation3d (new Vector3D (0, 1, 0), 45); Mv. Transform = Mytransform;

Blog forwarded from Http://www.cnblogs.com/Fuss/archive/2012/11/29/2794780.html (has been sought by bloggers to agree)

Professional training 9.7

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.