为什么我的原始图形相互叠加而不是相互叠加?

时间:2015-11-28 14:40:17

标签: xna-4.0

我试图绘制立方体基元和轴穿过它。轴不得与立方体一起旋转。我不知道为什么它不能那样工作。如果我删除axises.ApplyPasses();并调用cube.ApplyPasses();轴,则会通过多维数据集,但它们会随之旋转。

原始类包含PrimitiveType,VertexData,BasicEffect对象等的字段。这里没有数学或其他任何内容。

*.ApplyPasses();等于Effect.CurrentTechnique.Passes[0].Apply();

我得到了什么: enter image description here

我正在尝试做什么: enter image description here

多维数据集创建:

Primitive<VertexPositionColor> cube = new Primitive<VertexPositionColor>(_graphics.GraphicsDevice);
cube.VertexOffset = 0;
cube.Type = PrimitiveType.TriangleList;
cube.VertexData = new VertexPositionColor[]
{
    new VertexPositionColor(new Vector3(-0.5f, -0.5f, 0.5f), Color.Red),
    new VertexPositionColor(new Vector3(-0.5f, 0.5f, 0.5f), Color.Red),
    new VertexPositionColor(new Vector3(0.5f, 0.5f, 0.5f), Color.Red),

    new VertexPositionColor(new Vector3(0.5f, 0.5f, 0.5f), Color.Red),
    new VertexPositionColor(new Vector3(0.5f, -0.5f, 0.5f), Color.Red),
    new VertexPositionColor(new Vector3(-0.5f, -0.5f, 0.5f), Color.Red),

    new VertexPositionColor(new Vector3(-0.5f, -0.5f, 0.5f), Color.Green),
    new VertexPositionColor(new Vector3(-0.5f, -0.5f, -0.5f), Color.Green),
    new VertexPositionColor(new Vector3(-0.5f, 0.5f, -0.5f), Color.Green),

    new VertexPositionColor(new Vector3(-0.5f, 0.5f, -0.5f), Color.Green),
    new VertexPositionColor(new Vector3(-0.5f, 0.5f, 0.5f), Color.Green),
    new VertexPositionColor(new Vector3(-0.5f, -0.5f, 0.5f), Color.Green),

    new VertexPositionColor(new Vector3(-0.5f, -0.5f, 0.5f), Color.White),
    new VertexPositionColor(new Vector3(0.5f, -0.5f, 0.5f), Color.White),
    new VertexPositionColor(new Vector3(0.5f, -0.5f, -0.5f), Color.White),

    new VertexPositionColor(new Vector3(0.5f, -0.5f, -0.5f), Color.White),
    new VertexPositionColor(new Vector3(-0.5f, -0.5f, -0.5f), Color.White),
    new VertexPositionColor(new Vector3(-0.5f, -0.5f, 0.5f), Color.White),

    new VertexPositionColor(new Vector3(-0.5f, -0.5f, 0.5f), Color.White),
    new VertexPositionColor(new Vector3(0.5f, -0.5f, 0.5f), Color.White),
    new VertexPositionColor(new Vector3(0.5f, -0.5f, -0.5f), Color.White),

    new VertexPositionColor(new Vector3(0.5f, -0.5f, -0.5f), Color.Blue),
    new VertexPositionColor(new Vector3(0.5f, 0.5f, -0.5f), Color.Blue),
    new VertexPositionColor(new Vector3(-0.5f, 0.5f, -0.5f), Color.Blue),

    new VertexPositionColor(new Vector3(-0.5f, 0.5f, -0.5f), Color.Blue),
    new VertexPositionColor(new Vector3(-0.5f, -0.5f, -0.5f), Color.Blue),
    new VertexPositionColor(new Vector3(0.5f, -0.5f, -0.5f), Color.Blue),

    new VertexPositionColor(new Vector3(0.5f, -0.5f, -0.5f), Color.Black),
    new VertexPositionColor(new Vector3(0.5f, -0.5f, 0.5f), Color.Black),
    new VertexPositionColor(new Vector3(0.5f, 0.5f, 0.5f), Color.Black),

    new VertexPositionColor(new Vector3(0.5f, 0.5f, 0.5f), Color.Black),
    new VertexPositionColor(new Vector3(0.5f, 0.5f, -0.5f), Color.Black),
    new VertexPositionColor(new Vector3(0.5f, -0.5f, -0.5f), Color.Black),

    new VertexPositionColor(new Vector3(0.5f, -0.5f, -0.5f), Color.Black),
    new VertexPositionColor(new Vector3(0.5f, -0.5f, 0.5f), Color.Black),
    new VertexPositionColor(new Vector3(0.5f, 0.5f, 0.5f), Color.Black),

    new VertexPositionColor(new Vector3(0.5f, 0.5f, 0.5f), Color.Yellow),
    new VertexPositionColor(new Vector3(-0.5f, 0.5f, 0.5f), Color.Yellow),
    new VertexPositionColor(new Vector3(-0.5f, 0.5f, -0.5f), Color.Yellow),

    new VertexPositionColor(new Vector3(-0.5f, 0.5f, -0.5f), Color.Yellow),
    new VertexPositionColor(new Vector3(0.5f, 0.5f, -0.5f), Color.Yellow),
    new VertexPositionColor(new Vector3(0.5f, 0.5f, 0.5f), Color.Yellow)
};

cubeMatrixes = new Matrixes();
cubeMatrixes.World = Matrix.Identity;
cubeMatrixes.View = Matrix.CreateLookAt(new Vector3(0.0f, 1.0f, 2.0f), Vector3.Zero, Vector3.Up);
cubeMatrixes.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(110.0f), 4.0f / 3.0f, 0.001f, 10000.0f);

创造轴心:

Primitive<VertexPositionColor> axises = new Primitive<VertexPositionColor>(_graphics.GraphicsDevice);
axises.Type = PrimitiveType.LineList;
axises.VertexOffset = 0;
axises.VertexData = new VertexPositionColor[]
{
    new VertexPositionColor(new Vector3(-5, 0, 0), Color.Red),
    new VertexPositionColor(new Vector3(5, 0, 0), Color.Red),
    new VertexPositionColor(new Vector3(0, -5, 0), Color.Green),
    new VertexPositionColor(new Vector3(0, 5, 0), Color.Green),
    new VertexPositionColor(new Vector3(0, 0, -5), Color.Blue),
    new VertexPositionColor(new Vector3(0, 0, 5), Color.Blue),
};

axisesMatrixes = new Matrixes();
axisesMatrixes.World = Matrix.Identity;
axisesMatrixes.View = Matrix.CreateLookAt(new Vector3(0.0f, 1.0f, 5.0f), Vector3.Zero, Vector3.Up);
axisesMatrixes.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), 4.0f / 3.0f, 0.001f, 10000.0f);

翻译:

cube.Effect.World
    = Matrix.CreateRotationX(MathHelper.ToRadians(cubeAngle.X))
    * Matrix.CreateRotationY(MathHelper.ToRadians(cubeAngle.Y))
    * Matrix.CreateRotationZ(MathHelper.ToRadians(cubeAngle.Z))
    * cubeMatrixes.World;
cube.Effect.View = cubeMatrixes.View;
cube.Effect.Projection = cubeMatrixes.Projection;

axises.Effect.World
    = Matrix.CreateRotationX(MathHelper.ToRadians(0.0f))
    * Matrix.CreateRotationY(MathHelper.ToRadians(-60.0f))
    * Matrix.CreateRotationZ(MathHelper.ToRadians(0.0f))
    * axisesMatrixes.World;
axises.Effect.View
    = axisesMatrixes.View;
axises.Effect.Projection
    = axisesMatrixes.Projection;

图:

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

    axises.ApplyPasses();
    _graphics.GraphicsDevice.DrawUserPrimitives(axises.Type, axises.VertexData, axises.VertexOffset, axises.Count);

    cube.ApplyPasses();
    _graphics.GraphicsDevice.DrawUserPrimitives(cube.Type, cube.VertexData, cube.VertexOffset, cube.Count);     

    base.Draw(gameTime);
}

1 个答案:

答案 0 :(得分:0)

这个问题的答案是我创建了单独的投影矩阵。

当更改为两个轴和立方体的单个投影矩阵时,一切都按预期工作。

翻译:

cube.Effect.World
    = Matrix.CreateRotationX(MathHelper.ToRadians(cubeAngle.X))
    * Matrix.CreateRotationY(MathHelper.ToRadians(cubeAngle.Y))
    * Matrix.CreateRotationZ(MathHelper.ToRadians(cubeAngle.Z))
    * cubeMatrixes.World;
cube.Effect.View = cubeMatrixes.View;
cube.Effect.Projection = cubeMatrixes.Projection;

axises.Effect.World
    = Matrix.CreateRotationX(MathHelper.ToRadians(0.0f))
    * Matrix.CreateRotationY(MathHelper.ToRadians(-60.0f))
    * Matrix.CreateRotationZ(MathHelper.ToRadians(0.0f))
    * axisesMatrixes.World;
axises.Effect.View
    = axisesMatrixes.View;
axises.Effect.Projection
    = cubeMatrixes.Projection; // instead axisesMatrixes.Projection;