WPF constructs three-dimensional graphics with triangular meshes

Source: Internet
Author: User

While WPF supports only a subset of three-dimensional models, it is also sufficient (non-game development) from the perspective of application feature development. WPF's three-dimensional graphics, to speak simple, there are two.

1, the two-dimensional objects into three-dimensional space, this should be better to do, such as image control, Shape type, or some of our common controls can be put into three-dimensional space, building a model in this way may be more practical, or at least without the help of specialized modeling tools.

2, the complete use of coordinate modeling. It is actually composed of N triangles to form a three-dimensional model.

Although you will see that the base type Visual3D derives several types, but in general it is divided into the above two categories. Viewport2dvisual3d class is specifically to put two-dimensional objects into three-dimensional space, and ModelVisual3D and UIElement3D almost, the difference is that UIElement3D can capture user input behavior, such as mouse keyboard touch, If your three-dimensional model wants to interact with the user, you can use this class.

This article focuses on the second method of modeling, which is to use triangles to form. Why do you use triangles? We've all learned geometry before, you know, how many points are needed to determine a face? It's a three. Two points can only be determined by a line, a point on the forget, that is infinite ray. It is so, so the so-called polygon in the three-dimensional graph, at least three points to determine down, so, to use the triangle as a graphical primitive.

Before the introduction of triangular coordinate settings, the old week first to use nonsense to add a little stuff.

Hosting a three-dimensional model in WPF is like this: first, you should declare a Viewport3D object, which contains the model we want to build, some days ago, the old week wrote an article, introduced the camera in the three-dimensional type, is Viewport3D camera properties, you must set up a camera, In order to see things in the three-dimensional model, it is like we look at objective things through the viewfinder of the physical camera, which can be viewed here.

After that, Viewport3D will contain a collection of child elements, each of which must be a subclass of Visual3D, and because it is a collection, you can add N to it.

Using the coordinates to build the model, you should use the ModelVisual3D class, this class is fun, it itself exposes a set, you can add ModelVisual3D object. That is, the ModelVisual3D object can be nested like a tree structure, which makes it easy for you to build complex three-dimensional graphics and effectively group them. Many people will feel that the three-dimensional model of WPF is very complex and difficult to understand, in addition to the complexity of the three-dimensional space itself, it may be because of this visual element of the nesting between the first contact when it is prone to dizziness. But don't be afraid, there is no one born in the world will be, do not hurry, learning, do not build too complex models, so the idea is easy to adapt, as long as you are familiar with the very easy, and then complex things, but also by a variety of simple elements of the composition.

ModelVisual3D class has a content property, which is really used to set the model coordinates, its type is Model3D, it is an abstract class, its derived class has two chunks: one is GeometryModel3D, used to define the coordinates of the three-dimensional graphics. In addition, light, which is used to illuminate the three-dimensional model of the lights, because three-dimensional graphics are simulating the reality of things, so will take into account the problem of light exposure, its implementation types such as environmental light (ambientlight), this is like the headlights in our room, it can illuminate all the corners of the house Direction Light (directionallight), like a flashlight light bar, has the direction of irradiation ...

In the GeometryModel3D class, three properties are also exposed:

Geometry: Set the coordinate points of the three-dimensional graph, not only the point, but also the index, normal vector, texture mapping and so on.

Material: The material on the front of the graphic, like a metal surface, reflects a mirror-like effect.

Backmaterial: There is a front side, which refers to the material on the back of the graphic.

Here is the key to building the model, the geometry attribute, which exposes only one implementation class--meshgeometry3d, which is the use of triangular networks to build the surface of an object. There are also three attributes that are more important than the other.

Positions: This is a very important collection of various coordinate points that make up a graphical surface.

Triangleindices: This is also important, it's a set of integers, starting with 0, which is specifying which three points make up a triangle.

Texturecoordinates: Texture mapping. A bit like you put a picture of a sister on a glass, the picture is equivalent to the texture, each point of the corresponding relationship with the glass. For example, in general, we will point the top left corner of the photo to the point in the upper left corner of the glass, and the point in the upper right corner to the top right corner of the glass. If the point in the upper left corner of the photo is aligned to the lower right corner of the glass, the point at the top left corner of the photo is aligned to the lower left corner of the glass so that the photo is reversed.

Normals: The normal vector of each coordinate point that makes up the graph, by default it is all 0,0,1. This thing really does not know how to explain, generally we can not specify, with the default value can be, probably refers to the relationship between the face and the surface.

Let's take a look at the demo.

                        <GeometryModel3D>                            <Geometrymodel3d.geometry>                                 <MeshGeometry3D  positions= " -2,2,0 3,-2,2 5,3,3 7,1,-2"  Triangleindices= "0,1,2,1,3,2"  texturecoordinates= "0,0 1,0 1,0"  Normals= "0,0,1 0,0,1 0,0,1 0,0,1"/>                             </Geometrymodel3d.geometry>                            <geometrymodel3d.material>                                <diffusematerial>                                    <Diffusematerial.brush>                                        ...                                    </Diffusematerial.brush>                                </diffusematerial>                            </geometrymodel3d.material>                        </GeometryModel3D>

In the above code, four coordinate points are defined, note the value of the Triangleindices property, which has six values, indicating that the four points can form two triangles. That is, it is composed of two faces.

0-1-2 represents the first point, the second point, and the third point, which make up a triangle of three points. Just like this.

Then the 1-3-2, the second point, the fourth point, and the third point form a triangular shape.

Everyone should pay attention to a rule-the point that makes up a triangle, if it is sorted counterclockwise (as in the example above), represents the front of the object, and if it is clockwise, the opposite side of the object is represented.

For example, let's write this again, this time there are five points.

<positions= "0,3,0-3,-2,0-1,-3,1 1,-3,1 3,-2,0"  triangleindices= " 0,1,2,0,2,3,0,3,4 "  texturecoordinates=" 0,0"/>

The first point is at the top and the remaining four are below, constituting three triangles.

Well, because of the serious lack of money on the cast, the old week can only be said here today. In order to get more money to write the back of the article, the old week to play "crash" to earn cash.

WPF constructs three-dimensional graphics with triangular meshes

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.