gpt4 book ai didi

c# - 围绕其自身的轴旋转 3D 图元

转载 作者:行者123 更新时间:2023-11-30 14:32:28 25 4
gpt4 key购买 nike

这几天我一直在用 XNA 做一个项目。该应用程序的主要思想是拥有一个可以单独翻转的平面彩色瓷砖网格。我遇到的问题是我无法真正弄清楚如何围绕各个图 block 的中心轴进行翻转旋转。

现在发生的事情是瓷砖围绕世界轴而不是它自己的轴旋转。

每个图 block 都包含一个顶点数组,这些顶点数组在 3D 世界中创建一个二维正方形。

我很确定问题与我弄乱矩阵有关。

这是每个单独的图 block 的代码:

public Tile(Vector3 position, int size, Matrix world)
{
this.position = position;
this.size = size;
this.world = world;

vertices = new VertexPositionColor[6];

for (int i = 0; i < vertices.Length; i++)
vertices[i].Position = position;

vertices[0].Position += new Vector3(0, 0, 0);
vertices[0].Color = Color.Pink;
vertices[1].Position += new Vector3(0, size, 0);
vertices[1].Color = Color.Yellow;
vertices[2].Position += new Vector3(0, size, size);
vertices[2].Color = Color.Green;

vertices[3].Position += new Vector3(0, size, size);
vertices[3].Color = Color.Green;
vertices[4].Position += new Vector3(0, 0f, size);
vertices[4].Color = Color.Blue;
vertices[5].Position += new Vector3(0, 0, 0);
vertices[5].Color = Color.Blue;
}

public VertexPositionColor[] Vertices
{
get{ return Functions.TransformVertices((VertexPositionColor[])vertices.Clone(), GetMatrix()); }
}

private Matrix GetMatrix()
{
return Matrix.CreateRotationZ(rot);
}


private void TransformVertices(Matrix matrix)
{
for (int i = 0; i < vertices.Length; i++)
vertices[i].Position = Vector3.Transform(vertices[i].Position, matrix);
}

绘制方法如下:

protected override void Draw( GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);

RasterizerState rasterizerState = new RasterizerState();
rasterizerState.CullMode = CullMode.None;
GraphicsDevice.RasterizerState = rasterizerState;
viewMatrix = Matrix.CreateLookAt(cameraPos, new Vector3(0, 0, 0), Vector3.Up);

effect.CurrentTechnique = effect.Techniques["ColoredNoShading"];
effect.Parameters["xView"].SetValue(viewMatrix);
effect.Parameters["xProjection"].SetValue(projectionMatrix);

Vector3 rotAxis = new Vector3(0, 0, 1);
rotAxis.Normalize();
worldMatrix = Matrix.Identity;
effect.Parameters["xWorld"].SetValue(worldMatrix);

foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();

foreach(Tile tile in tiles)
GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, tile.Vertices, 0, 2, VertexPositionColor.VertexDeclaration);
}

base.Draw(gameTime);
}

如果有帮助,我可以将整个源代码发送给您。任何帮助都是无价的!

最佳答案

要围绕其自身的轴旋转 Tile:

(1) Transform the tile back to the origin.
(2) Perform rotation.
(3) Transform back to original position.

关于c# - 围绕其自身的轴旋转 3D 图元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18446440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com