将纹理应用于索引基元。 C ++ DX9

时间:2011-05-01 02:06:56

标签: c++ textures directx-9

我有一个基本程序,用48个四面体绘制一个立方体。更准确地说,它使用27个顶点和一个索引缓冲区来绘制这些四面体。我想将纹理应用到所有的四面体,但是我发现的关于纹理的所有教程都没有使用索引进行渲染,我的程序中的每个顶点都用在其他16个四面体中,所以我甚至无法弄清楚如何定位纹理。该程序本身太长而且太杂乱,我无法发布它但如果有人可以请告诉我是否可以将纹理放到索引的原语上,并且还给我一个教程的链接,我将不胜感激。

编辑:代码ici:

void setVertices(FLOAT cubeYOffset, FLOAT cubeXOffset, FLOAT cubeZOffset, int tetraRender[]){
CUSTOMVERTEX vertices[] = { 
    { cubeXOffset+1.0f, cubeYOffset+0.0f, cubeZOffset-1.0f, 0.0f, 0.5f, 0.0f, 0.0f, 1.0f, },//Center top = 0
    { cubeXOffset+0.0f, cubeYOffset+0.0f, cubeZOffset-1.0f, -0.5f, 0.5f, 0.0f, -1.0f, 1.0f, },
    { cubeXOffset+0.0f, cubeYOffset+0.0f, cubeZOffset+0.0f, -0.5f, 0.5f, 0.5f, -1.0f, 1.0f, },
    { cubeXOffset+1.0f, cubeYOffset+0.0f, cubeZOffset+0.0f, 0.0f, 0.5f, 0.5f, 0.0f, 1.0f, },
    { cubeXOffset+2.0f, cubeYOffset+0.0f, cubeZOffset+0.0f, 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, },
    { cubeXOffset+2.0f, cubeYOffset+0.0f, cubeZOffset-1.0f, 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, },
    { cubeXOffset+2.0f, cubeYOffset+0.0f, cubeZOffset-2.0f, 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, },
    { cubeXOffset+1.0f, cubeYOffset+0.0f, cubeZOffset-2.0f, 0.0f, 0.5f, -0.5f, 0.0f, 1.0f, },
    { cubeXOffset+0.0f, cubeYOffset+0.0f, cubeZOffset-2.0f, -0.5f, 0.5f, -0.5f, -1.0f, 0.0f, },

    { cubeXOffset+1.0f, cubeYOffset-1.0f, cubeZOffset-1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, },//Center middle = 9
    { cubeXOffset+0.0f, cubeYOffset-1.0f, cubeZOffset-1.0f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, },
    { cubeXOffset+0.0f, cubeYOffset-1.0f, cubeZOffset+0.0f, -0.5f, 0.0f, 0.5f, -1.0f, 0.0f, },
    { cubeXOffset+1.0f, cubeYOffset-1.0f, cubeZOffset+0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, },
    { cubeXOffset+2.0f, cubeYOffset-1.0f, cubeZOffset+0.0f, 0.5f, 0.0f, 0.5f, 1.0f, 0.0f, },
    { cubeXOffset+2.0f, cubeYOffset-1.0f, cubeZOffset-1.0f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, },
    { cubeXOffset+2.0f, cubeYOffset-1.0f, cubeZOffset-2.0f, 0.5f, 0.0f, -0.5f, 1.0f, 0.0f, },
    { cubeXOffset+1.0f, cubeYOffset-1.0f, cubeZOffset-2.0f, 0.0f, 0.0f, -0.5f, 0.0f, 0.0f, },
    { cubeXOffset+0.0f, cubeYOffset-1.0f, cubeZOffset-2.0f, -0.5f, 0.0f, -0.5f, -1.0f, 0.0f, },

    { cubeXOffset+1.0f, cubeYOffset-2.0f, cubeZOffset-1.0f, 0.0f, -0.5f, 0.0f, 0.0f, -1.0f, },//Center bottom = 18
    { cubeXOffset+0.0f, cubeYOffset-2.0f, cubeZOffset-1.0f, -0.5f, -0.5f, 0.0f, -1.0f, -1.0f, },
    { cubeXOffset+0.0f, cubeYOffset-2.0f, cubeZOffset+0.0f, -0.5f, -0.5f, 0.5f, -1.0f, -1.0f, },
    { cubeXOffset+1.0f, cubeYOffset-2.0f, cubeZOffset+0.0f, 0.0f, -0.5f, 0.5f, 0.0f, -1.0f, },
    { cubeXOffset+2.0f, cubeYOffset-2.0f, cubeZOffset+0.0f, 0.5f, -0.5f, 0.5f, 1.0f, -1.0f, },
    { cubeXOffset+2.0f, cubeYOffset-2.0f, cubeZOffset-1.0f, 0.5f, -0.5f, 0.0f, 1.0f, -1.0f, },
    { cubeXOffset+2.0f, cubeYOffset-2.0f, cubeZOffset-2.0f, 0.5f, -0.5f, -0.5f, 1.0f, -1.0f, },
    { cubeXOffset+1.0f, cubeYOffset-2.0f, cubeZOffset-2.0f, 0.0f, -0.5f, -0.5f, 0.0f, -1.0f, },
    { cubeXOffset+0.0f, cubeYOffset-2.0f, cubeZOffset-2.0f, -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, },//26(actually 27th)
};
d3ddev->CreateVertexBuffer(27*sizeof(CUSTOMVERTEX),
                           0,
                           CUSTOMFVF,
                           D3DPOOL_MANAGED,
                           &v_buffer,
                           NULL);

VOID* pVoid;
v_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, vertices, sizeof(vertices));
v_buffer->Unlock();

short tetra[48][12] = {
    //tetra 1
    0, 1, 2,
    0, 1, 11,
    0, 2, 11,
    1, 2, 11,

    //tetro 2
    0, 2, 3,
    0, 2, 11,
    0 , 3, 11,
    2, 3, 11,

    //tetro 3
    0, 3, 4,
    0, 3, 13,
    0, 4, 13,
    3, 4, 13,

    //tetro 4
    0, 4, 5,
    0, 4, 13,
    0, 5, 13,
    4, 5, 13,

    //tetro 5
    0, 5, 6,
    0, 5, 15,
    0, 6, 15,
    5, 6, 15,

    //tetro 6
    0, 6, 7,
    0, 6, 15,
    0, 7, 15,
    6, 7, 15,

    //tetro 7
    0, 7, 8,
    0, 7, 17,
    0, 8, 17,
    7, 8, 17,

    //tetro 8
    0, 8, 1,
    0, 8, 17,
    0, 1, 17,
    8, 1, 17,

    //tetro 9
    0, 1, 11,
    0, 1, 10,
    0, 10, 11,
    1, 10, 11,

    //tetro 10
    0, 3, 11,
    0, 3, 12,
    0, 11, 12,
    3, 11, 12,

    //tetro 11
    0, 3, 13,
    0, 3, 12,
    0, 12, 13,
    3, 12, 13,

    //tetro 12
    0, 5, 13,
    0, 5, 14,
    0, 13, 14,
    5, 13, 14,

    //tetro 13
    0, 5, 15,
    0, 5, 14,
    0, 14, 15,
    5, 14, 15,

    //tetro 14
    0, 7, 15,
    0, 7, 16,
    0, 15, 16,
    7, 15, 16,

    //tetro 15
    0, 7, 17,
    0, 7, 16,
    0, 16, 17,
    7, 16, 17,

    //tetro 16
    0, 1, 17,
    0, 1, 10,
    0, 17, 10,
    1, 17, 10,

    //tetro 17
    0, 10, 11,
    0, 9, 10,
    0, 9, 11,
    9, 10, 11,

    //tetro 18
    0, 11, 12,
    0, 9, 11,
    0, 9, 12,
    9, 11, 12,

    //tetro 19
    0, 12, 13,
    0, 9, 12,
    0, 9, 13,
    9, 12, 13,

    //tetro 20
    0, 13, 14,
    0, 9, 13,
    0, 9, 14,
    9, 13, 14,

    //tetro 21
    0, 14, 15,
    0, 9, 14,
    0, 9, 15,
    9, 14, 15,

    //tetro 22
    0, 15, 16,
    0, 9, 15,
    0, 9, 16,
    9, 15, 16,

    //tetro 23
    0, 16, 17,
    0, 9, 16,
    0, 9, 17,
    9, 16, 17,

    //tetro 24
    0, 17, 10,
    0, 9, 17,
    0, 9, 10,
    9, 17, 10,

    //tetro 17
    9, 10, 11,
    9, 18, 10,
    9, 18, 11,
    18, 10, 11,

    //tetro 18
    9, 11, 12,
    9, 18, 11,
    9, 18, 12,
    18, 11, 12,

    //tetro 19
    9, 12, 13,
    9, 18, 12,
    9, 18, 13,
    18, 12, 13,

    //tetro 20
    9, 13, 14,
    9, 18, 13,
    9, 18, 14,
    18, 13, 14,

    //tetro 21
    9, 14, 15,
    9, 18, 14,
    9, 18, 15,
    18, 14, 15,

    //tetro 22
    9, 15, 16,
    9, 18, 15,
    9, 18, 16,
    18, 15, 16,

    //tetro 23
    9, 16, 17,
    9, 18, 16,
    9, 18, 17,
    18, 16, 17,

    //tetro 24
    9, 17, 10,
    9, 18, 17,
    9, 18, 10,
    18, 17, 10,

    //tetro 9
    18, 19, 11,
    18, 19, 10,
    18, 10, 11,
    19, 10, 11,

    //tetro 10
    18, 21, 11,
    18, 21, 12,
    18, 11, 12,
    21, 11, 12,

    //tetro 11
    18, 21, 13,
    18, 21, 12,
    18, 12, 13,
    21, 12, 13,

    //tetro 12
    18, 23, 13,
    18, 23, 14,
    18, 13, 14,
    23, 13, 14,

    //tetro 13
    18, 23, 15,
    18, 23, 14,
    18, 14, 15,
    23, 14, 15,

    //tetro 14
    18, 25, 15,
    18, 25, 16,
    18, 15, 16,
    25, 15, 16,

    //tetro 15
    18, 25, 17,
    18, 25, 16,
    18, 16, 17,
    25, 16, 17,

    //tetro 16
    18, 19, 17,
    18, 19, 10,
    18, 17, 10,
    19, 17, 10,

    //tetro 19
    18, 19, 20,
    18, 19, 11,
    18, 20, 11,
    19, 20, 11,

    //tetro 20
    18, 20, 21,
    18, 20, 11,
    18 , 21, 11,
    20, 21, 11,

    //tetro 21
    18, 21, 22,
    18, 21, 13,
    18, 22, 13,
    21, 22, 13,

    //tetro 22
    18, 22, 23,
    18, 22, 13,
    18, 23, 13,
    22, 23, 13,

    //tetro 23
    18, 23, 24,
    18, 23, 15,
    18, 24, 15,
    23, 24, 15,

    //tetro 24
    18, 24, 25,
    18, 24, 15,
    18, 25, 15,
    24, 25, 15,

    //tetro 25
    18, 25, 26,
    18, 25, 17,
    18, 26, 17,
    25, 26, 17,

    //tetro 26
    18, 26, 19,
    18, 26, 17,
    18, 19, 17,
    26, 19, 17,
};
short indices [576];
int i = 0;
int i2 = 0;
ind = 0;
int ic;
for(i; i < 48; i++){
    if (tetraRender[i] == 1){
        for(i2; i2 < 12; i2++){     
            if((ind == 0)&&(i2 == 0)){ 
                ic = 0;
            }else{
                ic = ind*12+i2;
            }
            indices[ic] = tetra[i][i2]; 
        }
        i2 = 0;
        ind++;
    }
}

if (ind > 0) {
d3ddev->CreateIndexBuffer(12*ind*sizeof(short),
                            0,
                            D3DFMT_INDEX16,
                            D3DPOOL_MANAGED,
                            &i_buffer,
                            NULL);
i_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, indices, 12*ind*2);
i_buffer->Unlock();
}
    }

好的,这就是顶点和索引的声明只是为了给你一个想法。它非常混乱,所以我很抱歉,如果你看不到它的作用是声明一个螺旋状的顶点集合,然后声明四面体的所有指数。然后它使用函数中的int数组来仅绘制指定的四面体。

是的,我正在使用3D照明,即使我没有最简洁的原因。

2 个答案:

答案 0 :(得分:1)

我不是DirectX专家,但是根据经验说,我认为它使用的方法与OpenGL非常相似。

如果要对对象进行纹理处理,除了position属性和其他可能(正常,切向量,...)之外,还需要每个顶点的纹理坐标。 当您使用索引缓冲区来处理顶点数据时,我假设您在顶点缓冲区中有后者。因此,通过向每个顶点添加纹理坐标,您可以将纹理包裹到四面体上。但请注意,每个顶点不能有不同的纹理坐标,如果索引缓冲区显示“我想要索引0,1和2的三角形”,您将始终从索引0,1获取位置和纹理坐标数据,2。

您可以做的是首先通过对它们应用变换矩阵来修改每个四面体的纹理坐标。此转换与您应用于您的位置属性的转换是分开的。

答案 1 :(得分:1)

你不应该再使用FVF系统了。使用IDirect3DVertexDeclaration9系统,这实际上更灵活。 FVF的问题在于,当您将标记设置在一起时,顺序是不可指定的,而VERTEXELEMENT9 []系统能够指定元素的顺序以及包含在内的元素的顺序。

如何生成纹理坐标的简单答案非常简单 - 当你考虑一个顶点,然后它在3D空间中的位置,因此最终它的纹理坐标是固定的,无论它实际上最终是哪个三角形一部分。因此,除非你打算模拟一些非常锋利的边缘,在这种情况下你应该复制顶点,每个顶点有一个tex-coord是完全可以接受的。

至于渲染系统而不会崩溃,那么是时候使用着色器了,主要是因为固定功能已经死了,而且几乎所有的现代渲染都使用着色器,而且你必须在很久以前就已经学会了这些东西知道如何用它。幸运的是,基本的纹理是基本的,不需要任何特别高级的东西。

D3DVERTEXELEMENT9 vertexDecl[] = {
    { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
    { 0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0 },
    { 0, 24, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
    D3DDECL_END()
};
struct CustomVertex {
    float position[3];
    float normal[3];
    float texcoords[2];
};

IDirect3DVertexDeclaration9* vertexdecl;
d3ddev->CreateVertexDeclaration(vertexDecl, &vertexdecl);

至于着色器,那么非常简单的事情就可以了。

// Stuff we send to the shader from C++
// This is not per-vertex. Anything we loaded into the 
// vertex data itself is input to the vertex shader.
// Direct3D 9.0c also supports hardware instancing, but I'll
// leave you to work that one out yourself.

// World * View * Projection matrix gives the result in Homogenous Clip 
// Co-ordinates, which is what Direct3D wants from us as output
uniform extern float4x4 WVPMatrix;
// WorldInverseTranspose transforms the normal into world space
// successfully, even with non-linear transformations as the World
uniform extern float4x4 WorldInverseTransposeMatrix;
// This is just a 2D texture that we can change at any time
uniform extern texture MyTexture;
// The sampler state determines how the texture is filtered.
sampler TexS = sampler_state
{
    Texture = <MyTexture>;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};
// What we output from the vertex shader. This is basically the position
// of the vertex in HCC (the first two), and anything we want to pass into
// the pixel shader (the second two).
struct VS_OUTPUT {
    float4 position : POSITION0;
    float3 normal : NORMAL0;
    float2 texcoords : TEXCOORD0;
};
// What we're putting in to the vertex shader. This is basically
// our vertex structure from C++.
struct VS_INPUT {
    float3 position : POSITION0;
    float3 normal : NORMAL0;
    float2 texcoords : TEXCOORD0;
};
VS_OUTPUT VertexShader(VS_INPUT in) {
    VS_OUTPUT out = (VS_OUTPUT)0;
    // just pass texcoords on, we're not interested
    out.texcoords = in.texcoords;
    // get the resulting vertex position that we need
    out.position = mul(float4(in.position, 1.0f), WVPMatrix);
    // transform the normal into world space
    out.normal = mul(float4(in.normal, 0.0f), WorldInverseTransposeMatrix).xyz;
}
float4 PixelShader(float3 normal : NORMAL0, float2 texcoords : TEXCOORD0) {
    return tex2D(TexS, texcoords);
}
technique BasicShader 
{
    pass p0
    {            
        vertexShader = compile vs_3_0 VertexShader();
        pixelShader  = compile ps_3_0 PixelShader();
    }
}

我这里没有进行任何光照计算,但我确实将法线传递给像素着色器。