简体   繁体   English

Windows上WPF C#中的3D图形

[英]3D graphics in wpf c# on windows

I am new to 3d Graphics and also wpf and need to combine these two in my current project. 我是3d Graphics的新手,也是wpf的新手,需要在当前项目中将这两者结合起来。 I add points and normals to MeshGeometry3D and add MeshGeometry3D to GeometryModel3D . 我将点和法线添加到MeshGeometry3D并将MeshGeometry3D添加到GeometryModel3D Then add GeometryModel3D to ModelVisual3D and finally add ModelVisual3D to ViewPort3D . 然后将GeometryModel3D添加到ModelVisual3D ,最后将ModelVisual3D添加到ViewPort3D Now if i need to rotate i perform the required Transform either on GeometryModel3D or ModelVisual3D and add it again finally to the ViewPort3D. 现在,如果需要旋转,请在GeometryModel3D或ModelVisual3D上执行所需的Transform,然后将其最终再次添加到ViewPort3D。 I'm running into a problems: 我遇到了一个问题:

objViewPort3D.Remove(objModelVisual3D);
objGeometryModel3D.Transform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 1, 0), angle += 15));
objModelVisual3D.Content = objGeometryModel3D;
objViewPort3D.Children.Add(objModelVisual3D);

to rotate it everytime by 15 degrees why must i do angle += 15 and not just 15 ? 每次将其旋转15度,为什么我必须将angle += 15而不仅仅是15 It seems that the stored model is not transformed by Transform operation but transformation is applied only when displaying by ViewPort3D . 似乎存储的模型不是通过Transform操作进行Transform而是仅在通过ViewPort3D显示时才应用转换。 I want the transformation to actually change the coordinates in the stored MeshGeometry3D object so that when i do the transform next time it does on the previously transformed model and not the original model. 我希望转换实际上改变存储的MeshGeometry3D对象中的坐标,以便下次我进行转换时,它对先前转换的模型而不是原始模型进行转换。 How do i obtain this behaviour? 我如何获得这种行为?

I think you can use Animation 我想你可以使用动画

some pseudo-code: 一些伪代码:

angle = 0 function onClick: new_angle = angle + 30 Animate(angle, new_angle) angle = new_angle 角度= 0函数onClick:new_angle =角度+ 30 Animate(angle,new_angle)角度= new_angle

Correct, the position of the mesh is not transformed by the "Transform" operation. 正确,网格的位置未通过“变换”操作进行变换。 Instead the Transform property defines the world transform of the mesh during rendering. 相反,Transform属性定义了渲染期间网格物体的世界变换。

In 3d graphics the world transform transforms the points of the mesh from object space to world space during the render of the object. 在3d图形中,世界变换在对象渲染期间将网格的点从对象空间转换为世界空间。

对象空间到世界空间图

(Image from World, View and Projection Matrix Unveiled ) (图片来自世界,视角和投影矩阵

It's much faster to set the world transform and let the renderer draw the mesh in a single transform than transforming each vertex of a mesh, like you want. 设置世界变换并使渲染器在单个变换中绘制网格比按需要变换网格的每个顶点要快得多。

You have to do angle += 15 because you're applying a new RotateTransform3D each time. 您必须做角度+ = 15,因为每次都要应用一个新的RotateTransform3D。

This might help: 这可能会有所帮助:

public RotateTransform3D MyRotationTransform { get; set; }
...
//constructor
public MyClass()
{
     MyRotationTransform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 1, 0), 0));

}

//in your method
MyRotationTransform.Rotation += 15;
objGeometryModel3D.Transform = MyRotationTransform;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM