See the below code.
Model3DGroup modelGroup;
... //Create MODEL3DGROUP modelGroup = new Model3DGroup();
//Add GEOMETRYMODEL3D in MODEL3DGROUP modelGroup.Children.Add(geoModel); |
See the image below.
THIRD: Add collection of Model3DGroup(cubeM3DGroup) in ModelVisual3D(solidCubeMV3D).
See the code below.
solidCubeMV3D = new ModelVisual3D();
Model3DGroup cubeM3DGroup = new Model3DGroup(); //front side triangles cubeM3DGroup.Children.Add(CreateTriangleFacet(p3, p2, p6)); cubeM3DGroup.Children.Add(CreateTriangleFacet(p3, p6, p7)); //right side triangles cubeM3DGroup.Children.Add(CreateTriangleFacet(p2, p1, p5)); cubeM3DGroup.Children.Add(CreateTriangleFacet(p2, p5, p6)); //back side triangles cubeM3DGroup.Children.Add(CreateTriangleFacet(p1, p0, p4)); cubeM3DGroup.Children.Add(CreateTriangleFacet(p1, p4, p5)); //left side triangles cubeM3DGroup.Children.Add(CreateTriangleFacet(p0, p3, p7)); cubeM3DGroup.Children.Add(CreateTriangleFacet(p0, p7, p4)); //top side triangles cubeM3DGroup.Children.Add(CreateTriangleFacet(p7, p6, p5)); cubeM3DGroup.Children.Add(CreateTriangleFacet(p7, p5, p4)); //bottom side triangles cubeM3DGroup.Children.Add(CreateTriangleFacet(p2, p3, p0)); cubeM3DGroup.Children.Add(CreateTriangleFacet(p2, p0, p1));
//Add MODEL3DGROUP in MODELVISUAL3D solidCubeMV3D.Content = cubeM3DGroup;
// Store the instance of Transform3DGroup class in Transform Property of "solidCubeMV3D" // it will later use to rotate the object solidCubeMV3D.Transform = new Transform3DGroup(); |
NOTE: Assign transform property of (solidCubeMV3D) to later use in rotation of object.
See the below image.
FOURTH: Add ModelVisual3D(solidCubeMV3D) in the Viewport3D(mainViewPort)
See the code below.
//Add MODELVISUAL3D in VIEWPORT3D this.mainViewPort.Children.Add(solidCube); |
See the image below.
4. Draw Wireframe Cube
In order to draw wireframe object you need 3DTool.dll.
I have added it in my DemoProject "3DTOOL DLL" folder.
So, just add reference to that .dll if you don't have.
To add reference of 3DTool in your project Following the steps:
1.Go to the Solution Explorer
2.Right Click on References
3.Select: Add References… will open new dialogue box
4.Click on Browse
5.Navigate to 3DTool.dll
6.Click on OK. That's it
Now, how to draw wireframe cube.
FIRST: Create the instance of ScreenSpaceLines3D(wireFrameCube) and set it's three properties and that is "Thickness", "Color", "Points".
And also set the "Transform" property to later use in Rotation of "wireFrameCube".
See the code below.
wireFrameCube = new ScreenSpaceLines3D();
Color c = Colors.Orange; int width = 5;
wireFrameCube.Thickness = width; wireFrameCube.Color = c;
wireFrameCube.Points.Add(p0); wireFrameCube.Points.Add(p1);
wireFrameCube.Points.Add(p1); wireFrameCube.Points.Add(p2);
wireFrameCube.Points.Add(p2); wireFrameCube.Points.Add(p3);
wireFrameCube.Points.Add(p3); wireFrameCube.Points.Add(p0);
wireFrameCube.Points.Add(p4); wireFrameCube.Points.Add(p5);
wireFrameCube.Points.Add(p5); wireFrameCube.Points.Add(p6);
wireFrameCube.Points.Add(p6); wireFrameCube.Points.Add(p7);
wireFrameCube.Points.Add(p7); wireFrameCube.Points.Add(p4);
wireFrameCube.Points.Add(p0); wireFrameCube.Points.Add(p4);
wireFrameCube.Points.Add(p1); wireFrameCube.Points.Add(p5);
wireFrameCube.Points.Add(p2); wireFrameCube.Points.Add(p6); wireFrameCube.Points.Add(p3); wireFrameCube.Points.Add(p7); wireFrameCube.Transform = new Transform3DGroup(); |
See the image below.
SECOND: Simply and directly add the ScreenSpaceLines3D(wireFrameCube) to the Viewport3D(mainViewPort).
See the code below.
this.mainViewPort.Children.Add(wireFrameCube);
That's it you have created wireframe cube. |
See the image below.
NOTE: Suppose a line is being drawn using four points then two intermediate points will be used two times or we add intermediate points twice.
See the below image to clsrify the idea.
Code to draw this line in viewport is as below.
NOTE: after using the code below you just see straight line in ViewPort3D.
You will not see Red points and labels of points.
ScreenSpaceLines3D wireFrameCube = new ScreenSpaceLines3D();
ScreenSpaceLines3D wireFrameCube = new ScreenSpaceLines3D(); Color c = Colors.Orange; int width = 5;
wireFrameCube.Thickness = width; wireFrameCube.Color = c;
wireFrameCube.Points.Add(P1); wireFrameCube.Points.Add(P2);
wireFrameCube.Points.Add(P2); wireFrameCube.Points.Add(P3);
wireFrameCube.Points.Add(P3); wireFrameCube.Points.Add(P4); this.mainViewPort.Children.Add(wireFrameCube);
|
You can see that points P3 and P4 are used twise. To represent straight line.
5.TRANSFORM
To transform objects in Viewport3D we have two options.
5.1. Transformation of object or
5.2. Transformation of camera.
To make it clear let's take one example.
Suppose,
a. One cube is in your hand and you are rotating it with your fingers.
Then that is the first one---> [Transformation of object]
b. One cube is on table and you are rotating around the cube or table,
Then that this is the second one ---> [Transformation of camera]
Here, you can compare yourself as a camera.
5.1 Transformation of object
In order to transform an object you have to decide two things
1.Rotation Axis and
2.Angle to rotate around specified axis.
We can decide Rotation Axis and angle using the mouse movement and some calculations.
See the image below...
RotationAxis:
In ordert to calculate rotationAxis we will first calculate mouseMoveAngle after adding 90 degree in it; that will give us rotationAxisAngle and then as shown in the image below using some basic mathematics we can find rotationAxis.
Angle of Rotation or StepAngle:
You can give any constant value to Angle of Rotation (stepAngle).
Or you can also use the following formula.
double stepAngle = 0.01 * Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2));
|
Now, we have Axis and Angle so we can now rotate the object using any one of Code Block below.
A. Transform using QuaternionRotation3D:
Transform3DGroup group = solidCubeMV3D.Transform as Transform3DGroup;
QuaternionRotation3D qr = new QuaternionRotation3D(new Quaternion(axisArg, stepAngleArg* 180 Math.PI));
RotateTransform3D r = new RotateTransform3D(qr);
group.Children.Add(r); |
B. Transform Using AxisAngleRotation3D:
Transform3DGroup group = solidCubeMV3D.Transform as Transform3DGroup;
AxisAngleRotation3D a = new AxisAngleRotation3D(); a.Axis = axisArg; a.Angle = stepAngleArg *(180 / Math.PI);
RotateTransform3D r1 = new RotateTransform3D(a);
group.Children.Add(r1);
|
5.2 Transformation of camera.
We can transform objects using camera's position and up direction also.
I have put one button in my demo project for transforming an object using camera.
And as above you can use QuaternionRotation3D or AxisAngleRotation3D.
Vector3D rotationAxis = Vector3D.CrossProduct((Vector3D)camera.Position, camera.UpDirection);
#region - QuaternionRotation3D - //RotateTransform3D rotationTransform = new RotateTransform3D(new QuaternionRotation3D(new Quaternion(rotationAxis, 1.0))); #endregion
#region - AxisAngleRotation3D - RotateTransform3D rotationTransform = new RotateTransform3D(new AxisAngleRotation3D(rotationAxis, 1.0)); #endregion
camera.Position = rotationTransform.Transform(camera.Position); camera.UpDirection = rotationTransform.Transform(camera.UpDirection); |