Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


#Actualburnt_casadilla

Posted 30 May 2013 - 11:37 PM

When I'm creating a cube object in my class and trying to use my CreateVertexBuffer and CreateIndicesBuffer, I'm getting the error "object reference not set to an instance of an object". Even tho I'm creating the cube the right way, why is this happening?

 

public class Cube
{
    public GraphicsDevice device;

    const int number_of_vertices = 8;
    public const int number_of_indices = 36;

    public VertexBuffer vertices;
    public IndexBuffer indices;

    public Cube(GraphicsDevice graphicsDevice)
    {
        device = graphicsDevice;
    }

    public void CreateCubeVertexBuffer()
    {
        VertexPositionTexture[] cubeVertices = new VertexPositionTexture[number_of_vertices];

        "VERTICES HERE"

        vertices = new VertexBuffer(device, VertexPositionTexture.VertexDeclaration, number_of_vertices, BufferUsage.WriteOnly);
        vertices.SetData<VertexPositionTexture>(cubeVertices);
    }

    public void CreateCubeIndexBuffer()
    {
        UInt16[] cubeIndices = new UInt16[number_of_indices];

        "INDICES HERE"

        indices = new IndexBuffer(device, IndexElementSize.SixteenBits, number_of_indices, BufferUsage.WriteOnly);
        indices.SetData<UInt16>(cubeIndices);

    }

    public void Draw(BasicEffect effect)
    {

        foreach (EffectPass pass in effect.CurrentTechnique.Passes)
        {
            effect.CurrentTechnique.Passes[0].Apply();

            device.SetVertexBuffer(vertices);
            device.Indices = indices;

            device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, number_of_vertices, 0, number_of_indices / 3);
        }
    }
}
}

The error is at the line indices = new IndexBuffer(blah, blah, blah);

 

            cube = new Cube(device);
            cube.CreateCubeIndexBuffer();
            cube.CreateCubeVertexBuffer();
 
Thats how I'm creating my cube. The reason for this is that I only want the buffers to be called once within the game to reduce lag

#1burnt_casadilla

Posted 30 May 2013 - 11:36 PM

When I'm creating a cube object in my class and trying to use my CreateVertexBuffer and CreateIndicesBuffer, I'm getting the error "object reference not set to an instance of an object". Even tho I'm creating the cube the right way, why is this happening?

 

public class Cube
{
    public GraphicsDevice device;

    const int number_of_vertices = 8;
    public const int number_of_indices = 36;

    public VertexBuffer vertices;
    public IndexBuffer indices;

    public Cube(GraphicsDevice graphicsDevice)
    {
        device = graphicsDevice;
    }

    public void CreateCubeVertexBuffer()
    {
        VertexPositionTexture[] cubeVertices = new VertexPositionTexture[number_of_vertices];

        "VERTICES HERE"

        vertices = new VertexBuffer(device, VertexPositionTexture.VertexDeclaration, number_of_vertices, BufferUsage.WriteOnly);
        vertices.SetData<VertexPositionTexture>(cubeVertices);
    }

    public void CreateCubeIndexBuffer()
    {
        UInt16[] cubeIndices = new UInt16[number_of_indices];

        "INDICES HERE"

        indices = new IndexBuffer(device, IndexElementSize.SixteenBits, number_of_indices, BufferUsage.WriteOnly);
        indices.SetData<UInt16>(cubeIndices);

    }

    public void Draw(BasicEffect effect)
    {

        foreach (EffectPass pass in effect.CurrentTechnique.Passes)
        {
            effect.CurrentTechnique.Passes[0].Apply();

            device.SetVertexBuffer(vertices);
            device.Indices = indices;

            device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, number_of_vertices, 0, number_of_indices / 3);
        }
    }
}
}

The error is at the line indices = new IndexBuffer(blah, blah, blah);

 

cube = new Cube(device);
            cube.CreateCubeIndexBuffer();
            cube.CreateCubeVertexBuffer();
 

PARTNERS