c++ – Direct3D 11 How to create a vertex buffer from different arrays without allocating additional memory?

Question:

There are 3 buffers

std::vector< vec3 > verticies;
std::vector< vec2 > tcoords;
std::vector< vec3 > normals;

When creating a Vertex buffer, filling in the D3D11_SUBRESOURCE_DATA structure, a pointer to the array from which the D3D11 buffer will be created is indicated

vData.pSysMem   =   verticies.data(); // сюда нужно послать ещё tcoords и normals

The problem is that there are three arrays, and the pointer is needed 1.

I've tried constructs like

struct Vertex{
    Vertex(){}
    Vertex( vec3* _pos, vec2* _texCoord,    vec3* _norm){
    pos = _pos;
    texCoord = _texCoord;
    norm = _norm;
}

   vec3* pos;
   vec2* texCoord;
   vec3* norm;
};

std::vector< Vertex >           verts;

this->verts.push_back( 
   Vertex( verticies.data(),
           tcoords.data(),
           normals.data() ));

vData.pSysMem   =   this->verts.data();

somehow unsuccessfully

Maybe there are other ways, for example, using the capabilities of C ++, or Direct3D 11 itself? On OpenGL, everything is created without problems with separate functions, mb and in D3D11 there is something?

Answer:

On the knee, something like this:

#include <iostream>
#include <vector>
#include <algorithm>

struct A 
{
    A( int p_a ) : _a( p_a ) {}
    int _a;
};

using    VecSrcData = std::vector<A>;
using VecPtrSrcData = std::vector<A const*>;

class B
{
public:
    template<size_t N>
    B( VecSrcData* const ( &vecs )[N] )
    {
        size_t commonSize = 0;
        std::for_each( vecs, vecs + N, [&commonSize]( VecSrcData const* vec )
        {
            commonSize += vec->size();
        });

        std::cout << "\ncommon_size=" << commonSize << std::endl;

        m_srcPData.reserve( commonSize );

        /*for( size_t j = 0; j < N; ++j )
        {
            for( size_t i = 0; i < vecs[j]->size() ; ++i )
            {
                std::cout << "\nnum:" << i;
                m_srcPData.push_back( &( vecs[j]->at(i) ) );
            }
        }     */   

        std::for_each( vecs, vecs + N, [this]( VecSrcData const* vec )
        {
            for( VecSrcData::const_iterator it = vec->cbegin(); it != vec->cend(); ++it )
                m_srcPData.push_back( &(*it) );
        });
    }

    VecPtrSrcData m_srcPData;

    void show()
    {
        for( VecPtrSrcData::const_iterator it = m_srcPData.begin(); it != m_srcPData.end(); ++it )
        {
            std::cout << "\nEl[" << it - m_srcPData.begin() << "] = " << (*it)->_a;
        }
    }

};

int main()
{

    VecSrcData srcDataA( 10, A( 2 ) );
    VecSrcData srcDataB( 20, A( 3 ) );
    VecSrcData srcDataC( 10, A( 4 ) );

    VecSrcData* vecs[] = { &srcDataA, &srcDataB, &srcDataC };

    B b = B( vecs );

    b.show();

    getchar();
    return 0;

}
Scroll to Top
AllEscort