为什么我的基元绘制在彼此之上而不是彼此内部?

Why my primitives are drawn on top of each other instead inside each other?

我正在尝试绘制立方体基元和穿过它的轴。轴不得随立方体旋转。我不知道为什么它不能那样工作。如果我删除 axises.ApplyPasses(); 并调用 cube.ApplyPasses(); 轴穿过立方体,但它们会随之旋转。

Primitive class 包含 PrimitiveType、VertexData、BasicEffect 对象等字段。这里没有数学运算或其他任何操作。

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

我得到了什么:

我正在尝试做的事情:

多维数据集创建:

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);
}

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

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

翻译:

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;