Thursday, May 2, 2024
HomeGame Developmentrendering - load vertaxes from an objfile

rendering – load vertaxes from an objfile


I do have a problem with loading mine vertexes.
When i debug mine code. I find out the vertexes are loaded. But when i use render doc. I found out the vertexes is not loaded. Only the first line of mine vertex gets loaded. I went thru mine coded i could not find the problem.
Render doc:
enter image description here

Mine 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);

    ```

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments