1

This code works fine:

    Vertex cubeVertices[] = 
    {
        {XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, 0.133f, 0.333f)},
        {XMFLOAT3(-0.5f, -0.5f,  0.5f), XMFLOAT3(0.0f, 0.133f, 0.333f)},
        {XMFLOAT3(-0.5f,  0.5f, -0.5f), XMFLOAT3(0.0f, 0.133f, 0.333f)},
        {XMFLOAT3(-0.5f,  0.5f,  0.5f), XMFLOAT3(0.0f, 0.133f, 0.333f)},
        {XMFLOAT3( 0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, 0.133f, 0.333f)},
        {XMFLOAT3( 0.5f, -0.5f,  0.5f), XMFLOAT3(0.0f, 0.133f, 0.333f)},
        {XMFLOAT3( 0.5f,  0.5f, -0.5f), XMFLOAT3(0.0f, 0.133f, 0.333f)},
        {XMFLOAT3( 0.5f,  0.5f,  0.5f), XMFLOAT3(0.0f, 0.133f, 0.333f)},
    };

    D3D11_SUBRESOURCE_DATA vertexBufferData = {0};
    vertexBufferData.pSysMem = cubeVertices;
    vertexBufferData.SysMemPitch = 0;
    vertexBufferData.SysMemSlicePitch = 0;

            //THE PROBLEM IS HERE
            //if I use sizeof(m_Vertices) it no longer works.
            //I still don't understand though because m_Vertices should still be in scope
    CD3D11_BUFFER_DESC vertexBufferDesc(sizeof(cubeVertices), D3D11_BIND_VERTEX_BUFFER);
    DX::ThrowIfFailed(
        m_d3dDevice->CreateBuffer(
            &vertexBufferDesc,
            &vertexBufferData,
            &m_vertexBuffer));

It draws a lovely cube on my screen.

This code does not work:

    GeometryGenerator generator;
    GeometryGenerator::MeshData cubeData;
    generator.CreateCube(cubeData);

            //I thought it might be a scope problem
            //m_Vertices is a std::Vector<Vertex>
    m_Vertices.resize(cubeData.Vertices.size());

    for(size_t i = 0; i < cubeData.Vertices.size(); ++i)
    {
        m_Vertices[i].pos = cubeData.Vertices[i].Position;          
        m_Vertices[i].color = XMFLOAT3(0.0f, 0.133f, 0.333f);
    }

    D3D11_SUBRESOURCE_DATA vertexBufferData = {0};
    vertexBufferData.pSysMem = &m_Vertices[0];
    vertexBufferData.SysMemPitch = 0;
    vertexBufferData.SysMemSlicePitch = 0;

            //THE PROBLEM IS HERE
            //if I use sizeof(m_Vertices) it no longer works.
            //I still don't understand though because m_Vertices should still be in scope
    CD3D11_BUFFER_DESC vertexBufferDesc(sizeof(m_Vertices), D3D11_BIND_VERTEX_BUFFER);
    DX::ThrowIfFailed(
        m_d3dDevice->CreateBuffer(
            &vertexBufferDesc,
            &vertexBufferData,
            &m_vertexBuffer));

The screen clears to my clear color but no shapes draw on the screen.

I thought &m_Vertices[0] is equivalent to an array? I've checked the values in debug and both the array and my CreateCube function produce the same data.

Eric
  • 1,392
  • 17
  • 37
  • must be something else then, do you properly initialize all world and projection matrices ? – Radu Chivu Oct 21 '13 at 21:21
  • Yes, if I take the lower black and change it to the upper block and make no other changes it works. – Eric Oct 21 '13 at 21:30
  • might be because the compiler pads the structure you're using to represent a vertex, can you surround it with #pragma pack(push) #pragma pack(1) //your structure definition #pragma pack(pop) – Radu Chivu Oct 21 '13 at 21:49
  • That did not make a difference – Eric Oct 21 '13 at 22:49
  • That does look equivalent, assuming m_Vertices is std::vector as you stated. If you can't spot anything, I'd add a test loop comparing your cubeData array with your m_Vertices vector to verify they're equivalent. – holtavolt Oct 22 '13 at 03:19
  • Found the line causing the issue and it was in the next few lines of code that I didn't include. I'm editing the post now. I still don't know why it's causing the issue, but I've narrowed down the cause – Eric Oct 22 '13 at 16:05

1 Answers1

1

sizeof(m_Vertices) gives you the size of the vector object itself, not the number of elements it stores. What you probably want is m_Vertices.size() * sizeof(Vertex).

ComicSansMS
  • 51,484
  • 14
  • 155
  • 166
  • thank you! I'm converting from C# to c++ and I have trouble with these types of details – Eric Oct 22 '13 at 17:38
  • @Eric You might want to grab a [decent C++ textbook](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) on the way. C++ can be a lot of fun, but it can also be a royal pain if you try to cut corners while learning the basics. – ComicSansMS Oct 22 '13 at 18:25