如何使用C#在XNA中绘制此立方体

本文关键字:绘制 立方体 XNA 何使用 | 更新日期: 2023-09-27 18:25:47

好吧,伙计们,这一次简直要了我的命,因为我已经能够很好地渲染模型了(事实上,为了测试我的相机,我不得不这么做)。

然而,现在我正试图从顶点和索引缓冲区绘制一个立方体,它就是不起作用。(我已经能画三角形之类的了,但从来没有在他们自己的课上画过)。

我的最终目标是能够构建64x64x8立方体的区域来创建游戏世界。(不是minecraft的克隆,实际上是RTS——它会有一种"2d"的感觉,因为游戏世界本身只有8个立方体深,但我跑题了)。

从查看各种索引&网络上到处都是顶点教程,在我看来这应该可行。这里有一些代码。。。。。

game.cs

    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if(GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();
        // TODO: Add your update logic here
        float timeDifference = (float)gameTime.ElapsedGameTime.TotalMilliseconds / 1000.0f;
        cam.Update(timeDifference);
        base.Update(gameTime);
    }
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        // TODO: Add your drawing code here
        cube = new CubeShape(Color.Black, new Vector3(0, 0, 0), GraphicsDevice);

        RasterizerState rasterizerState = new RasterizerState();
        rasterizerState.CullMode = CullMode.None;
        GraphicsDevice.RasterizerState = rasterizerState;
        cube.Render(cam.viewMatrix,cam.projectionMatrix);
        base.Draw(gameTime);
    }
}

和我的立方体(这个有点长,对不起)

class CubeShape
{
    //Transform later to have static v and i buffers.
    private VertexBuffer vBuffer;
    public VertexBuffer VBuffer
    { get { return vBuffer; } set { vBuffer = value; } }
    private IndexBuffer iBuffer;
    public IndexBuffer IBuffer
    { get { return iBuffer; } set { iBuffer = value; } }
    private BasicEffect bEffect;
    public BasicEffect BEffect
    { get { return bEffect; } set { bEffect = value; } }
    private Matrix world;
    public Matrix World
    { get { return world; } set { world = value; } }
    private Matrix view;
    public Matrix View
    { get { return view; } set { view = value; } }
    private Matrix projection;
    private Matrix Projection
    { get { return projection; } set { projection = value; } }
    private Color color;
    public Color Color
    { get { return color; } set { color = value; } }
    private Vector3 position;
    public Vector3 Position
    { get { return position; } set { position = value; } }
    //Need to change this eventually to use textures.
    private VertexPositionColor[] vertices;
    byte[] indices;

    private GraphicsDevice device;
    //constructors!
    public CubeShape(Color inColor,Vector3 inPosition,GraphicsDevice inDevice)
    {
        device = inDevice;

        this.color = inColor;
        this.position = inPosition;
        SetUpVertices();
        SetUpIndices();
        //world = Matrix.CreateTranslation(position);
        world = Matrix.CreateTranslation(0, 0, 0);
        bEffect = new BasicEffect(device);
        bEffect.World = world;
        bEffect.VertexColorEnabled = true;
    }
    //end constructors!
    // >.<
    public void Render(Matrix view,Matrix projection)
    {
        bEffect.View = view;
        bEffect.Projection = projection;

        device.SetVertexBuffer(vBuffer);
        device.Indices = IBuffer;
        foreach(EffectPass pass in bEffect.CurrentTechnique.Passes)
        {
            pass.Apply();
            device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 8, 0, 12);
        }
    }
    /// <summary>
    /// Sets up the vertices for a cube using 8 unique vertices.
    /// Build order is front to back, left to up to right to down.
    /// </summary>
    private void SetUpVertices()
    {
        vertices = new VertexPositionColor[8];
        //front left bottom corner
        vertices[0] = new VertexPositionColor(new Vector3(0, 0, 0), color);
        //front left upper corner
        vertices[1] = new VertexPositionColor(new Vector3(0, 100, 0), color);
        //front right upper corner
        vertices[2] = new VertexPositionColor(new Vector3(100, 100, 0), color);
        //front lower right corner
        vertices[3] = new VertexPositionColor(new Vector3(100, 0, 0), color);
        //back left lower corner
        vertices[4] = new VertexPositionColor(new Vector3(0, 0, -100), color);
        //back left upper corner
        vertices[5] = new VertexPositionColor(new Vector3(0, 100, -100), color);
        //back right upper corner
        vertices[6] = new VertexPositionColor(new Vector3(100, 100, -100), color);
        //back right lower corner
        vertices[7] = new VertexPositionColor(new Vector3(100, 0, -100), color);
        vBuffer = new VertexBuffer(device, typeof(VertexPositionColor), 8, BufferUsage.WriteOnly);
        vBuffer.SetData<VertexPositionColor>(vertices);
    }
    /// <summary>
    /// Sets up the indices for a cube. Has 36 positions that match up
    /// to the element numbers of the vertices created earlier.
    /// Valid range is 0-7 for each value.
    /// </summary>
    private void SetUpIndices()
    {
        indices = new byte[36];
        //Front face
        //bottom right triangle
        indices[0] = 0;
        indices[1] = 3;
        indices[2] = 2;
        //top left triangle
        indices[3] = 2;
        indices[4] = 1;
        indices[5] = 0;
        //back face
        //bottom right triangle
        indices[6] = 4;
        indices[7] = 7;
        indices[8] = 6;
        //top left triangle
        indices[9] = 6;
        indices[10] = 5;
        indices[11] = 4;
        //Top face
        //bottom right triangle
        indices[12] = 1;
        indices[13] = 2;
        indices[14] = 6;
        //top left triangle
        indices[15] = 6;
        indices[16] = 5;
        indices[17] = 1;
        //bottom face
        //bottom right triangle
        indices[18] = 4;
        indices[19] = 7;
        indices[20] = 3;
        //top left triangle
        indices[21] = 3;
        indices[22] = 0;
        indices[23] = 4;
        //left face
        //bottom right triangle
        indices[24] = 4;
        indices[25] = 0;
        indices[26] = 1;
        //top left triangle
        indices[27] = 1;
        indices[28] = 5;
        indices[29] = 4;
        //right face
        //bottom right triangle
        indices[30] = 3;
        indices[31] = 7;
        indices[32] = 6;
        //top left triangle
        indices[33] = 6;
        indices[34] = 2;
        indices[35] = 3;
        iBuffer = new IndexBuffer(device, typeof(short), 36, BufferUsage.WriteOnly);
        iBuffer.SetData(indices);
    }
}

我真的不知道为什么这不起作用。可能是因为在过去的4个小时里,我一直在使用代码<

哦,摄影机从0,0,50开始,面向0,0,0。它还允许我用鼠标和键盘(就像任何rts摄像头一样)旋转(按住鼠标中键)。我四处搜索,只是为了确保我的立方体不在我的视野范围之外,但我看到的都是蓝色><

如果有人帮忙,我将不胜感激。

附言:作为第二个问题,有关于如何在多维数据集之间共享缓冲区的提示吗?我读到,由于立方体的几何结构永远不会改变,共享缓冲区更有效,但我不知道如何做到这一点。。。创建一个相当大的缓冲区,并用某种列表类中的多维数据集填充它,该类包含可能需要渲染的所有多维数据集?甚至不知道从哪里开始寻找。再次感谢您的意见和建议。

如何使用C#在XNA中绘制此立方体

看起来您有一些打字问题。我通过更改以下代码使其工作:

//byte[] indices;
short[] indices;
//indices = new byte[36];
indices = new short[36];
//iBuffer = new IndexBuffer(device, typeof(short), 36, BufferUsage.WriteOnly);
iBuffer = new IndexBuffer(device, IndexElementSize.SixteenBits, sizeof(short) * indices.Length, BufferUsage.WriteOnly);

如果它仍然不起作用,对我大喊大叫,我会检查以确保我没有错过任何东西。请记住,我不得不使用我自己的一个相机类,因为你没有包括你的相机类。

我不知道如何回答你的第二个问题。你可能想把它作为一个单独的问题来问。