0

I'm working on a simple program where I start by initializing an array of VertexPos structures that each carry an XMFLOAT3 and XMFLOAT2 inside the cube's constructor. Then, the RenderCube function reads these vertices and displays the cube to the screen.

The code I have works perfectly fine when I declare an array (of the same type) within the RenderCube function like so:

VertexPos vertices[] = {
            {XMFLOAT3( -1.0f,  1.0f, -1.0f ), XMFLOAT2( 0.0f, 0.0f )},
...

Problem is, when I attempt to read vertices[] from the cube object, defined and initialized below (Only the first vertex of the cube is defined below for brevity), I see crazy values in the debugger when looking at cube->vertices, such as:

cube->vertices[0].pos.x=5.736e-039#DEN

RenderCube():

bool Render::RenderCube(Cube* cube)
{
...
D3D11_SUBRESOURCE_DATA resourceData;
    ZeroMemory(&resourceData, sizeof(resourceData));
    resourceData.pSysMem = cube->vertices;
}

Cube.h

struct VertexPos
{
    XMFLOAT3 pos;
    XMFLOAT2 tex0;
    VertexPos(){}
    VertexPos(XMFLOAT3 p, XMFLOAT2 t)
    {
        pos = p;
        tex0 = t;
    }
};

class Cube
{
    public:
        Cube::Cube(LPCWSTR textureFileName);
        VertexPos vertices[NUMBER_OF_VERTICES];
        LPCWSTR getTextureFileName();

    private:
        LPCWSTR textureFileName;
};

Cube.cpp:

Cube::Cube(LPCWSTR textureFileName)
{
    //set texture
    this->textureFileName = textureFileName;
    vertices[0] = VertexPos(XMFLOAT3( -1.0f,  1.0f, -1.0f ), XMFLOAT2( 0.0f, 0.0f ));

}

LPCWSTR Cube::getTextureFileName(){
    return textureFileName;
}

What am I doing wrong?

2
  • You initialize the array vertices[] one by one? Commented Oct 23, 2013 at 16:11
  • Not in my actual code- this is just a simplified example that illustrates the same problem Commented Oct 23, 2013 at 16:26

2 Answers 2

1

You need initialize the element in the array one by one. For example:

VertexPos vertices[3] = {VertexPos(XMFLOAT3( -1.0f,  1.0f, -1.0f ), XMFLOAT2( 0.0f, 0.0f ))}

The first element vertices[0] will be initialize as VertexPos(XMFLOAT3( -1.0f, 1.0f, -1.0f ), XMFLOAT2( 0.0f, 0.0f )), but other element will be initialize with the default constructor, as the default constructor is empty, so you get garbage contents in the struct.

Sign up to request clarification or add additional context in comments.

1 Comment

That doesn't seem to work in this case, I've changed my code to what you see below, and have the same results: struct VertexPos { XMFLOAT3 pos; XMFLOAT2 tex0; VertexPos(){ pos = XMFLOAT3(0.0f, 0.0f, 0.0f); tex0 = XMFLOAT2(0.0f, 0.0f); } VertexPos(XMFLOAT3 p, XMFLOAT2 t) { pos = XMFLOAT3(p.x, p.y, p.z); tex0 = XMFLOAT2(t.x, t.y); } };
1

I finally got it- was using only a reference to initialize my vector of cubes, not a new Cube

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.