Search code examples
directx-11unhandled-exception

In directx11 when I create vertexbuffer I have unhandled exception error


//change directory
_chdir("Models");

std::ifstream InFile;
InFile.open(strFilename);
if (!InFile)
{
    MessageBox(0, L"file does not exist", 0, 0);
    return 0;
}

char strCommand[256] = { 0 };

for (;;)
{
    InFile >> strCommand;
    if (!InFile)
        break;

    if (0 == strcmp(strCommand, "#"))
    {
        // Comment
    }
    else if (0 == strcmp(strCommand, "v"))
    {
        // Vertex Position
        float x, y, z;
        InFile >> x >> y >> z;
        Positions->push_back(XMFLOAT3(x, y, z));
        mVCount++;
    }
    else if (0 == strcmp(strCommand, "vt"))
    {
        // Vertex TexCoord
        float u, v;
        InFile >> u >> v;
        TexCoords->push_back(XMFLOAT2(u, v));
        mTCount++;
    }
    else if (0 == strcmp(strCommand, "vn"))
    {
        // Vertex Normal
        float x, y, z;
        InFile >> x >> y >> z;
        Normals->push_back(XMFLOAT3(x, y, z));
        mNCount++;
    }
    else if (0 == strcmp(strCommand, "f"))
    {
        // Face
        UINT iPosition, iTexCoord, iNormal;
        VERTEX::objVertex vertex;

        for (UINT iFace = 0; iFace < 3; iFace++)
        {
            ZeroMemory(&vertex, sizeof(VERTEX::objVertex));

            // OBJ format uses 1-based arrays
            InFile >> iPosition;
            vertex.position = (*Positions)[iPosition - 1];
            /////////////////////////////////////////////////////////////
            vIndices->push_back(iPosition - 1);
            mICount++;
            /////////////////////////////////////////////////////////////

            if ('/' == InFile.peek())
            {
                InFile.ignore();

                if ('/' != InFile.peek())
                {
                    // Optional texture coordinate
                    InFile >> iTexCoord;
                    vertex.texcoord = (*TexCoords)[iTexCoord - 1];
                }

                if ('/' == InFile.peek())
                {
                    InFile.ignore();

                    // Optional vertex normal
                    InFile >> iNormal;
                    vertex.normal = (*Normals)[iNormal - 1];
                }
            }
        }
    }
    else
    {
        //skip without vertex
    }

    //InFile.ignore(1000, '\n');
}

InFile.close();
_chdir("..");

return S_OK;

this is when i try to load geometry from .obj file.

D3D11_BUFFER_DESC vbd;
vbd.Usage = D3D11_USAGE_IMMUTABLE;
vbd.ByteWidth = sizeof(VERTEX::objVertex) * mVCount;
vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vbd.CPUAccessFlags = 0;
vbd.MiscFlags = 0;
vbd.StructureByteStride = 0;

D3D11_SUBRESOURCE_DATA vinitData;
vinitData.pSysMem = Positions;
HR(Device->CreateBuffer(&vbd, &vinitData, &pmVB));

D3D11_BUFFER_DESC ibd;
ibd.Usage = D3D11_USAGE_IMMUTABLE;
ibd.ByteWidth = sizeof(UINT)* mICount;
ibd.BindFlags = D3D11_BIND_INDEX_BUFFER;
ibd.CPUAccessFlags = 0;
ibd.MiscFlags = 0;
D3D11_SUBRESOURCE_DATA iinitData;
iinitData.pSysMem = vindices;
HR(Device->CreateBuffer(&ibd, &iinitData, &pmIB));

return S_OK;

and this is when i try to create buffer. when i try to debug, vs said unhandled exception at HR() function. i have this problem in most three days and i don't know why this happening. please give me some adivise.


Solution

  • As per the comments, the solution was two-fold:

    a) Passing a pointer to a std::vector is incorrect as the underlying data of a std::vector doesn't live at that address.

    b) Once corrected, passing data that only contains the position attributes when trying to initialise a vertex buffer large enough to contain all the attributes will fail. D3D will try to read more data than the 'initial data' is providing and is likely the cause of the exception. The solution is to provide initial data that contains enough data for all attributes of all the vertices.