0
\$\begingroup\$

I do have a problem with loading my vertexes.

When I debug my code. I find out the vertexes are loaded. But when i use render doc, I found out the vertexes are not loaded. Only the first line of my vertex gets loaded. I went thru my code i could not find the problem.

Render doc: enter image description here

My code:

bool ObjFile(ID3D11Device* device, MeshD3D11* Mesh ,VertexBufferD3D11* vertex, IndexBufferD3D11* IndexBuffer, TextureSrvRtv* SendColorInformation) {
    objl::Loader LoadOBJ;
    bool ReadingFile = LoadOBJ.LoadFile("ball.obj");

    if (!ReadingFile)
    {
        std::cerr << "the file can't be open" << std::endl;
        return false;
        
    }
    else
    {
        //Below is used to get every vertax to mesh objekt. Same metoed is used in the cration of a cube in assigment 2 
        std::vector<VertaxOBJ> VertaxObjVec;
        for (int x = 0; x < LoadOBJ.LoadedVertices.size(); x++)
        {
            VertaxOBJ VertaxObj;
            //Position
            VertaxObj.pos.x = LoadOBJ.LoadedVertices[x].Position.X,
            VertaxObj.pos.y = LoadOBJ.LoadedVertices[x].Position.Y,
            VertaxObj.pos.z = LoadOBJ.LoadedVertices[x].Position.Z;

            //Normal
            VertaxObj.nor.x = LoadOBJ.LoadedVertices[x].Normal.X,
            VertaxObj.nor.y = LoadOBJ.LoadedVertices[x].Normal.Y,
            VertaxObj.nor.z = LoadOBJ.LoadedVertices[x].Normal.Z;

            //UV        
            VertaxObj.UV.x = LoadOBJ.LoadedVertices[x].TextureCoordinate.X,
            VertaxObj.UV.y = LoadOBJ.LoadedVertices[x].TextureCoordinate.Y;

            VertaxObjVec.push_back(VertaxObj);
        }

        MeshData MeshForVERTAXandINDEXandMESH;
        //vertax
        MeshForVERTAXandINDEXandMESH.vertexInfo.nrOfVerticesInBuffer = VertaxObjVec.size();
        MeshForVERTAXandINDEXandMESH.vertexInfo.sizeOfVertex = sizeof(VertaxObjVec);
        MeshForVERTAXandINDEXandMESH.vertexInfo.vertexData = VertaxObjVec.data();

        //index
        MeshForVERTAXandINDEXandMESH.indexInfo.nrOfIndicesInBuffer = LoadOBJ.LoadedIndices.size();
        MeshForVERTAXandINDEXandMESH.indexInfo.indexData = LoadOBJ.LoadedIndices.data();


        //mesh
        MeshData::SubMeshInfo MeshForMesh;
        ColorInformation CI;

        SendColorInformation->UploadingTexture(device, 1024, 576, nullptr, DXGI_FORMAT_R8G8B8A8_UNORM, D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE);
        SendColorInformation->UploadingSRV(device, false, CI.ambient);
        SendColorInformation->UploadingTexture(device, 1024, 576, nullptr, DXGI_FORMAT_R8G8B8A8_UNORM, D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE);
        SendColorInformation->UploadingSRV(device, false, CI.diffuse);
        SendColorInformation->UploadingTexture(device, 1024, 576, nullptr, DXGI_FORMAT_R8G8B8A8_UNORM, D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE);
        SendColorInformation->UploadingSRV(device, false, CI.specular);


        MeshForMesh.startIndexValue = 0;
        MeshForMesh.nrOfIndicesInSubMesh = 0;
        for (int x = 0; x < LoadOBJ.LoadedMeshes.size(); x++) {
            //offset tekniskt sätt
            MeshForMesh.nrOfIndicesInSubMesh = LoadOBJ.LoadedMeshes[x].Indices.size();
            MeshForMesh.ambientTextureSRV = CI.ambient;
            MeshForMesh.diffuseTextureSRV = CI.diffuse;
            MeshForMesh.specularTextureSRV = CI.specular;
            MeshForVERTAXandINDEXandMESH.subMeshInfo.push_back(MeshForMesh);
            //the reason the size is in the end. For i need the first element need be zero. For correct caulcation of the startindex
            MeshForMesh.startIndexValue += LoadOBJ.LoadedMeshes[x].Indices.size();
        }
        Mesh->Initialize(device, MeshForVERTAXandINDEXandMESH);
        
    }
    return true;
}

The function this file is sent to

void MeshD3D11::Initialize(ID3D11Device* device, const MeshData& meshInfo)
{

    vertexBuffer.Initialize(device, meshInfo.vertexInfo.sizeOfVertex,
        meshInfo.vertexInfo.nrOfVerticesInBuffer, meshInfo.vertexInfo.vertexData);


    indexBuffer.Initialize(device, meshInfo.indexInfo.nrOfIndicesInBuffer,
        meshInfo.indexInfo.indexData);


    for (int x = 0; x < meshInfo.subMeshInfo.size(); x++)
    {
        {
            SubMeshD3D11 SMD3D11;
            SMD3D11.Initialize
            (
                meshInfo.subMeshInfo[x].startIndexValue,
                meshInfo.subMeshInfo[x].nrOfIndicesInSubMesh,
                meshInfo.subMeshInfo[x].ambientTextureSRV,
                meshInfo.subMeshInfo[x].diffuseTextureSRV,
                meshInfo.subMeshInfo[x].specularTextureSRV
            );
            subMeshes.push_back(SMD3D11);
        }
    }
}

The render function

#include "AllHeaderFile.h"

void Render(ID3D11DeviceContext* immediateContext, TextureSrvRtv* rtvBackBUffer,
    ID3D11DepthStencilView* dsView, D3D11_VIEWPORT& viewport, ShaderD3D11* vshader,
    ShaderD3D11* pshader, InputLayoutD3D11* InputLayout, VertexBufferD3D11* vertex,
    SamplerD3D11* samplerState, /*ShaderD3D11* cshader,*/
    ID3D11UnorderedAccessView* backBufferUAV, ConstantBufferD3D11* ConstantBuffer, CameraD3D11* Cam,
    IndexBufferD3D11* IndexBuffer, /*, StructuredBufferD3D11* Structured*/ MeshD3D11* Mesh)
{
    float clearColour[4] = { 0, 0, 0, 0 };

    immediateContext->ClearRenderTargetView(rtvBackBUffer->GetRTV(), clearColour);


    immediateContext->ClearDepthStencilView(dsView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1, 0);

    /*UINT stride = sizeof(VertaxOBJ);
    UINT offset = 0;*/

    //Byt namn på det om det fungerar
    ID3D11Buffer* ConstBuffer = ConstantBuffer->GetBuffer();
    /*ID3D11Buffer* VertaxBuffer = vertex->GetBuffer();
    ID3D11Buffer* Index = IndexBuffer->GetBuffer();*/
    ID3D11SamplerState* sampler = samplerState->GetSamplerState();
    ID3D11ShaderResourceView* shaderResourceView = rtvBackBUffer->GetSRV();
    ID3D11ShaderResourceView* AmbientSRV;
    ID3D11ShaderResourceView* DiffuseSRV;
    ID3D11ShaderResourceView* SpecularSRV;

    ID3D11RenderTargetView* NewRtv = rtvBackBUffer->GetRTV();



    Mesh->BindMeshBuffers(immediateContext);


    immediateContext->IASetInputLayout(InputLayout->GetInputLayout());
    immediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
    vshader->BindShader(immediateContext, ShaderType::VERTEX_SHADER);
    immediateContext->RSSetViewports(1, &viewport);
    pshader->BindShader(immediateContext, ShaderType::PIXEL_SHADER);
    immediateContext->OMSetRenderTargets(1, &NewRtv, dsView);
    //immediateContext->OMSetRenderTargets(1, &RTVCupemaping, dsView);
    immediateContext->VSSetConstantBuffers(0, 1, &ConstBuffer);
    immediateContext->PSSetShaderResources(0, 1, &shaderResourceView);

    for (int x = 0; x < Mesh->GetNrOfSubMeshes(); x++)
    {
        AmbientSRV = Mesh->GetAmbientSRV(x);
        DiffuseSRV = Mesh->GetDiffuseSRV(x);
        SpecularSRV = Mesh->GetSpecularSRV(x);
        immediateContext->PSSetShaderResources(1, 1, &AmbientSRV);
        immediateContext->PSSetShaderResources(2, 1, &DiffuseSRV);
        immediateContext->PSSetShaderResources(3, 1, &SpecularSRV);
        Mesh->PerformSubMeshDrawCall(immediateContext, x);
    }

    immediateContext->PSSetSamplers(0, 1, &sampler);

The struct that i fill data to sent to vertex,index and mesh

struct MeshData
{
    struct VertexInfo
    {
        size_t sizeOfVertex;
        size_t nrOfVerticesInBuffer;
        void* vertexData;
    } vertexInfo;

    struct IndexInfo
    {
        size_t nrOfIndicesInBuffer;
        uint32_t* indexData;
    } indexInfo;

    struct SubMeshInfo
    {
        size_t startIndexValue;
        size_t nrOfIndicesInSubMesh;
        ID3D11ShaderResourceView* ambientTextureSRV;
        ID3D11ShaderResourceView* diffuseTextureSRV;
        ID3D11ShaderResourceView* specularTextureSRV;
    };

    std::vector<SubMeshInfo> subMeshInfo;
};
```
\$\endgroup\$
2
  • \$\begingroup\$ Note to others: This seems to have evolved from the earlier question: gamedev.stackexchange.com/questions/210130/… \$\endgroup\$
    – Kevin
    Commented Apr 19 at 1:37
  • \$\begingroup\$ Where did the MeshData type and objl Loader come from? If those are classes that you wrote, then you'll need to share them. \$\endgroup\$
    – Kevin
    Commented Apr 19 at 1:53

1 Answer 1

1
\$\begingroup\$

In the ObjFile function you create a std::vector to store the vertex data and use the index vector inside LoadOBJ to take the data pointer from them but only use that data after you return from the function and those two objects get destroyed.

That's a use-after-free (and a paddl'n).

you need to keep that vector alive until after you do initialize. Easiest is to add a std::vector for the vertex data and the index data in MeshData


In addition there is another issue in this line:

MeshForVERTAXandINDEXandMESH.vertexInfo.sizeOfVertex = sizeof(VertaxObjVec);

You give it the size of the vector object instead of the actual VertaxOBJ.

\$\endgroup\$
3
  • \$\begingroup\$ I tried it. But it won't work and another thing mine vertex and index are there with mine current code. I have looked it up in mine in other places in mine code. Like example in the render function. The size is correct and amount of vertex is correct. \$\endgroup\$ Commented Apr 19 at 13:23
  • \$\begingroup\$ use after free is undefined behavior, it can sometimes work correctly but it will break on you elsewhere when something else allocates memory and reuses that buffer \$\endgroup\$ Commented Apr 19 at 13:25
  • \$\begingroup\$ aha. i do not understand what you mean by this comment that you gave me. Your method do not work. I get same result that i got with mine code \$\endgroup\$ Commented Apr 19 at 14:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .