在DirectX9 / C ++中为OBJMesh加载纹理坐标

时间:2013-09-02 15:20:36

标签: c++ directx texture-mapping directx-9

我正在尝试在我的OBJMesh加载器上运行纹理。到目前为止,我已经看过一些关于如何做到这一点的在线教程,最明显的例子是我理解的立方体/盒子示例。但是我遇到了这个问题,其中顶点可能使用/具有多于1个纹理坐标。

例如:

f 711/1/1 712/2/2 709/3/3
f 711/9/1 704/10/9 712/11/2

正如你所看到的,索引顶点数711使用纹理坐标编号1和9,索引顶点编号712使用纹理坐标编号2和11.所以我的问题是如何让它使用多个纹理坐标?我可以使用某种缓冲区,如顶点缓冲区和索引缓冲区吗?

我正在使用索引,我有以下顶点声明:

D3DVERTEXELEMENT9 vertexElement[] = {   {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
                                        {0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
                                        D3DDECL_END()};

device->CreateVertexDeclaration(vertexElement, &vertexDec);

我的这里是我的顶点结构:

struct VERTEX{
    D3DXVECTOR3 position;
    D3DXVECTOR2 uv;

    D3DCOLOR color;
};

不太清楚在这里展示的是什么其他代码,但是如果我错过了什么或者你们需要看到帮助我的东西,请告诉我。

由于

编辑:如果我使用FVF而不是顶点声明会更好吗?

1 个答案:

答案 0 :(得分:0)

使用DX9,您需要使用分割法线和纹理坐标取消实例化位置。

这是一个示例代码:

#include <array>
#include <vector>
#include <utility>
#include <tuple>
#include <map>

struct Vec3f { /* put your beloved implementation here */ };
struct Vec2f { /* put your beloved implementation here */ };

// imagine your obj loaded as these set of vectors : 

// the vertices separate values
using Positions = std::vector<Vec3f>;
using Normals = std::vector<Vec3f>;
using Texcoords = std::vector<Vec2f>;

// and faces information
struct VertexTriplet {
    int _pos;
    int _normal;
    int _texcoord;
    friend bool operator< ( VertexTriplet const & a, VertexTriplet const & b ) {
        return    a._pos < b._pos 
               || ( a._pos == b._pos && a._normal < b._normal )
               || ( a._pos == b._pos && a._normal == b._normal && a._texcoord < b._texcoord );
    }
};

using Triangle = std::array<VertexTriplet,3>;
using Faces = std::vector<Triangle>;

// imagine your GPU friendly geometry as these two vectors
using Vertex = std::tuple<Vec3f,Vec3f,Vec2f>;
using Index = unsigned int;

using VertexBuffer = std::vector<Vertex>;
using IndexBuffer = std::vector<Index>;

using GpuObject = std::pair<VertexBuffer,IndexBuffer>;
// you can now implement the conversion :
GpuObject Convert( Positions const & positions, Normals const & normals, Texcoords const & texcoords, Faces const & faces ) {
    GpuObject result;

    // first, we create unique index for each existing triplet
    auto & indexBuffer = result.second;
    indexBuffer.reserve( faces.size() * 3 );

    std::map<VertexTriplet, Index> remap;
    Index freeIndex = 0;
    for ( auto & triangle : faces ) {
        for ( auto & vertex : triangle ) {
            auto it = remap.find( vertex );
            if ( it != remap.end() ) { // index already exists
                indexBuffer.push_back( it->second );
            } else { // create new index
                indexBuffer.push_back( freeIndex );
                remap[vertex] = freeIndex++;
            }
        }
    }

    // we now have the index buffer, we can fill the vertex buffer
    // we start by reversing the mapping from triplet to index
    // so wee can fill the memory with monotonic increasing address
    std::map< Index, VertexTriplet > reverseRemap;
    for ( auto & pair : remap ) {
        reverseRemap[pair.second] = pair.first;
    }

    auto & vertexBuffer = result.first;
    vertexBuffer.reserve( reverseRemap.size() );
    for ( auto & pair : reverseRemap ) {
        auto & triplet = pair.second;
        vertexBuffer.push_back( std::make_tuple( positions[triplet.m_pos], normals[triplet.m_normal], texcoords[triplet.m_texcoord] ) );
    }
    return result;
}
int main() {
    // read your obj file into these four vectors
    Positions positions;
    Normals normals;
    Texcoords texcoords;
    Faces faces;

    /* read the obj here */

    // then convert
    auto gpuObject = Convert( positions, normals, texcoords, faces );

    return 0;
}

当然,我们可以设想对索引和顶点缓冲区进行后期处理以优化性能。将数据打包到比float和unsigned更小的类型,优化了gpu post转换缓存,......

使用DX10 / 11,我们可以想象使用Faces向量作为顶点缓冲区,提供三个带有输入布局(顶点声明新名称)的索引,并将位置,法线和texcoords作为缓冲区类型的三个着色器资源视图传递给顶点着色器并直接执行查找。与旧的学校顶点交错相比,它不应该是如此不理想,但你应该坚持旧学校的解决方案。