Pergunta

Ok guys, this one is literally killing my mind, as I've been able to render models just fine (in fact I had to in order to test out my camera).

However now that I'm trying to draw a cube from a vertex and index buffer, it just won't work. (I've been able to draw triangles and such, but never from their own class).

My end goal is to be able to build regions of 64x64x8 cubes to create the game world. (Not a minecraft clone, actually an RTS -- it'll have a "2d" feel in that the game world itself will only ever be 8 cubes deep, but I digress).

From looking over the various index & vertex tutorials all over the web, it looks to me like this should work. Here's some code.....

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

and my cube (this one is kind of long, sorry)

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

I really have no clue why this isn't working. Probably because I've been having a staredown with the code for the last 4 hours >.<

Oh, the camera starts at 0,0,50 and is facing 0,0,0. It also allows me to move around with the mouse and keyboard (like any rts cam should) with rotation (middle mouse button held down). I've searched all around just to make sure my cube wasn't somewhere outside of my view range,but all I see is blue >.<

I'd appreciate any help here.

p.s. as a secondary question, any hints on how to share buffers between cubes? I read that since the geometry of a cube never changes, that it's more efficient to share buffers but I'm not sure how to go about this...create one decently big buffer and fill it with cubes from some kind of list class that holds all the cubes that need to be rendered maybe? not even sure where to start looking. Again, thanks much for any input/advice.

Foi útil?

Solução

It looks like you have some typing issues. I got it to work by changing the following code:

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

If it still does not work, yell at me and I will check to make sure that I did not miss anything. Keep in mind, I had to use one of my own camera classes since you did not include yours.

I am not sure how to answer your second question. You might want to ask it as a separate question.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top