渲染由平面定义的立方体(Valve 贴图文件格式)

Rendering a cube defined by planes (Valve map file format)

目前我正在尝试在单一游戏应用程序中渲染立方体。 我正在使用 Valve 地图文件格式。地图格式在以下维基页面中描述:

https://developer.valvesoftware.com/wiki/MAP_file_format.

导入格式很简单,完全没有问题。 重要的部分是飞机的描述。格式如下所示:

{
    ( x1 y1 z1) ( x2 y2 z2) ( x3 y3 z3) texture_name  ...
    ( x1 y1 z1) ( x2 y2 z2) ( x3 y3 z3) texture_name  ...
    ( x1 y1 z1) ( x2 y2 z2) ( x3 y3 z3) texture_name  ...
    ( x1 y1 z1) ( x2 y2 z2) ( x3 y3 z3) texture_name  ...
    ( x1 y1 z1) ( x2 y2 z2) ( x3 y3 z3) texture_name  ...
    ( x1 y1 z1) ( x2 y2 z2) ( x3 y3 z3) texture_name  ...
}

每条线定义一个平面。要定义一个实际平面,必须给出三个点(由 x1、y1、z1、x2、y2、z2 和 x3、y3、z3 显示)。

plane ((vertex) (vertex) (vertex))

这定义了将用于设置平面在三维世界中的方向和位置的三个点。第一个标记脸部的左下角,第二个标记左上角,第三个标记右上角。下面是通过 CSG 从六个平面构建一个简单画笔的动画。第一、二、三平面点用红、绿、蓝点表示。

所以我正在加载这些平面并将它们渲染为三角形。 我的 monogame 应用程序中的渲染结果是这样的:

很明显,立方体中缺少一些部分。

我正在使用 worldcraft 锤子编辑器创建地图文件。所以结果应该是这样的:

顶点创建方法:

将画笔和面转换为 xna VertexPositionColor 类型。

    private VertexPositionColor[] CreateVertexPositions(Brush brush)
    {
        VertexPositionColor[] vertices = new VertexPositionColor[brush.Faces.Count * 3];

        int j = 0;
        for (int i = 0; i < brush.Faces.Count; i++)
        {
            Face brushFace = brush.Faces[i];
            Color color = ColorUtils.GenerateRandomColor(Color.Wheat);

            vertices[i + j + 0] = new VertexPositionColor( // bottom left of the face
                new Vector3(brushFace.V1.X, brushFace.V1.Y, brushFace.V1.Z), color
            );
            vertices[i + j + 1] = new VertexPositionColor( // top left of the face
                new Vector3(brushFace.V2.X, brushFace.V2.Y, brushFace.V2.Z), color
            );
            vertices[i + j + 2] = new VertexPositionColor( // top right of the face
                new Vector3(brushFace.V3.X, brushFace.V3.Y, brushFace.V3.Z), color
            );

            j = j + 2;
        }

        return vertices;
    }

刷脸模型:

public sealed class Face
{
    public Vertex3 V1 { get; set; }
    public Vertex3 V2 { get; set; }
    public Vertex3 V3 { get; set; }
    public string TextureName { get; set; }
    public Plane P1 { get; set; }
    public Plane P2 { get; set; }
    public int Rotation { get; set; }
    public float XScale { get; set; }
    public float YScale { get; set; }
}

渲染方法:

    public override void Render(ICamera camera)
    {
        _effect.Projection = camera.Projection;
        _effect.View = camera.View;
        _effect.World = camera.World;

        RasterizerState rasterizerState = new RasterizerState();
        rasterizerState.CullMode = CullMode.None;
        rasterizerState.FillMode = FillMode.Solid;
        GraphicsDevice.RasterizerState = rasterizerState;

        foreach (EffectPass pass in _effect.CurrentTechnique.Passes)
        {
            pass.Apply();
            GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, _vertices.ToArray(), 0, (_vertices.Count / 3), VertexPositionColor.VertexDeclaration);
        }
    }

我缺少一个基础部分。我怎样才能用给定的平面创建一个立方体?

我不是要完整的代码或完成的解决方案,而是要有人将我推向正确的方向。

提前致谢。

更新

我为此做了一个非常简单的项目并将其上传到我的 OneDrive 帐户:

OneDrive Link

一个立方体由 6 个四边形( 6 个矩形)组成。四边形是两个三角形,而不是一个。由于四边形是 2 个三角形,因此它有 6 个顶点。所以要渲染一个立方体,你必须处理36个顶点。

构成 36 个顶点的所有信息都在构成您的平面的那 18 个顶点中。

对于所有的36个顶点,只有6个唯一的分量值。对于组成立方体的 36 个顶点中的任何一个,只能有 2 个可能的 X 值、2 个可能的 Y 值和 2 个可能的 Z 值。所有 36 个顶点均由这 6 个唯一值的各种组合组成。

你的任务是遍历 6 个平面(18 个顶点)并提取 6 个唯一的分量值,然后用这 6 个分量值组成你的 36 个顶点。然后用 36 个顶点填充 VertexBuffer。