在wpf中旋转3D变换 [英] Rotate 3D transformations in wpf

查看:91
本文介绍了在wpf中旋转3D变换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



最近我开始了WPF / C#3D开发。我正在尝试旋转矩形。但问题是当我旋转它时,其他轴也会旋转。



例如,如果我沿x轴旋转90°,y轴和z轴也会旋转。即y轴指向负z轴,z轴指向y轴。现在,如果我想围绕y轴旋转它,我必须实际围绕z轴旋转它,然后它将在UI上绕y旋转。



I我猜我必须跟踪每个轴,如果我没有遗漏任何东西。任何人都可以帮助我吗?



我已经研究了四元数但是我必须跟踪轴。



这是我正在使用的代码



 public void RotateXAxis(RotateDirection rotaryDirection)
{
AxisAngleRotation3D axisAngleRotation = new AxisAngleRotation3D(new Vector3D(1,0,0),90 * x *(int)rotaryDirection);
RotateTransform3D rotation = new RotateTransform3D(axisAngleRotation,new Point3D(7.5,7.5,7.5));
this.modelVisual3D.Transform = rotation;

}

///< summary>
///旋转切片a-Axis
///< / summary>
///< param name =rotaryDirection>< / param>
public void RotateYAxis(RotateDirection rotaryDirection)
{
AxisAngleRotation3D axisAngleRotation = new AxisAngleRotation3D(new Vector3D(0,1,0), - 90 * y *(int)rotaryDirection);
RotateTransform3D rotation = new RotateTransform3D(axisAngleRotation,new Point3D(7.5,7.5,7.5));

this.modelVisual3D.Transform = rotation;

}





问候,

zain ul abideen

解决方案

有趣。这可能是几件事。 :-)



A.



代码中的某个地方,你正在使用这个旋转值和坚持它在某个地方。但是你使用它的地方可能需要与你输入的格式不同(例如你可能会把你的价值观放在错误的地方)。



B. br />


这可能是WPF中的一个错误。这样的小错误,在整个C#中散落(例如,当使用foreach循环时,你无法修改值,因为这会导致程序崩溃)。所以你可能想谷歌这个,看看这是一个众所周知的问题。



C.



也许你使用了错误的旋转技术(没有违法行为)。查看MSDN( http://www.msdn.microsoft.com/ ),

和查看库中的System.Windows部分( http://msdn.microsoft.com/en-us/library/gg145013.aspx ),并在类中搜索看看是否有更合适的3D旋转方式。



希望这会有所帮助。



: - )


在OpenGL和DirectX世界中,您推模型视图矩阵,然后执行转换(平移和/或旋转),然后弹出矩阵。最初的OpenGL编程指南在绕地太阳运行的第3章中有一个很好的例子,展示了它们之间旋转和转换的推动和弹出。 (在C#中为类似的OpenGL或DirectX项目搜索planet或solar system。)



然而,我一直在努力尝试用WPF / C#3D实现同样的目的。如果我想弄清楚如何在WPF中推和弹出模型视图矩阵,我会发布代码。



值得重复的是矩阵乘法不是可交换的,所以翻译和旋转的顺序至关重要。


zain ul abideen



参见如何:将多个转换应用于三维模型以获取示例代码。



https://docs.microsoft.com/en-us/dotnet/framework/wpf /图形多媒体/如何对应用-多转换到A-3-d-模型

Hi All

Recently I started WPF/C# 3D development. I am trying to rotate a rectangle. But the problem is when I rotate it, other axis also get rotated.

For example if I rotate it along x-axis by 90, y and z axes also get rotated. that is y-axis point towards negative z-axis and z-axis point to y-axis. Now if I want to rotate it around y-axis, I have to rotate it actually around z-axis only then it will rotate around y on the UI.

I'm guessing i have to keep track of each axis if I'm not missing anything. Can anybody help me ?

I have looked into quaternions but even then I have to keep track of axes.

this is the code I'm using

public void RotateXAxis(RotateDirection rotaryDirection)
        {
            AxisAngleRotation3D axisAngleRotation = new AxisAngleRotation3D(new Vector3D(1,0,0), 90 * x * (int)rotaryDirection);
            RotateTransform3D rotation = new RotateTransform3D(axisAngleRotation, new Point3D(7.5, 7.5, 7.5));
            this.modelVisual3D.Transform = rotation;
         
        }

        /// <summary>
        /// Rotates the slice arount Y-Axis
        /// </summary>
        /// <param name="rotaryDirection"></param>
        public void RotateYAxis(RotateDirection rotaryDirection)
        {
            AxisAngleRotation3D axisAngleRotation = new AxisAngleRotation3D(new Vector3D(0, 1, 0), -90 * y * (int)rotaryDirection);
            RotateTransform3D rotation = new RotateTransform3D(axisAngleRotation, new Point3D(7.5, 7.5, 7.5));

            this.modelVisual3D.Transform = rotation;

        }



Regards,
zain ul abideen

解决方案

Interesting. This could be several things. :-)

A.

Somewhere in your code, you are using this rotation value and sticking it in somewhere. But the place you are using it could need a different format than what you put in (e.g. You may be sticking your values in the wrong place).

B.

This could be a bug in WPF. There are small bugs like that, littered throughout C# (e.g. when using foreach loop, you cannot modify the values, because that will cause the program to crash). So you might want to Google this, and see if this is a well-known problem.

C.

Maybe you are using the wrong technique to rotate (no offense intended). Check on MSDN (http://www.msdn.microsoft.com/),
and look in the Library, in the System.Windows section (http://msdn.microsoft.com/en-us/library/gg145013.aspx), and search through the classes to see if there is a more appropriate way to do 3D rotation.

Hope this helps.

:-)


In the OpenGL and DirectX world, you "push" the model view matrix, then perform the transformation (translation and/or rotation), then "pop" the matrix. The original OpenGL programming guide had a great example in Chapter 3 of the earth orbiting the sun, demonstrating pushing and popping with the rotating and transforming between them. (Do a search for "planetary" or "solar system" for similar OpenGL or DirectX projects in C#.)

However, I/ve been struggling for some time trying to achieve the same thing with WPF/C# 3D. If I ever figure out how to "push" and "pop" the model view matrix in WPF, I'll post the code.

It's worth repeating that matrix multiplication is not commutative, so the order in which you translate and rotate is crucial.


zain ul abideen

See "How to: Apply Multiple Transformations to a 3-D Model" for sample code.

https://docs.microsoft.com/en-us/dotnet/framework/wpf/graphics-multimedia/how-to-apply-multiple-transformations-to-a-3-d-model


这篇关于在wpf中旋转3D变换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆