DirectX9:如何使用1个vertexBuffer在一个场景中使用不同的纹理

时间:2019-07-19 13:58:24

标签: c++ visual-c++ directx

我有方法,可以创建3个多维数据集并为其加载3个纹理。 问题是-我不知道如何为不同的多维数据集设置不同的纹理。

创建多维数据集代码:

void CreateCubes(
    LPDIRECT3DDEVICE9 &ptr_dxDevice, 
    LPDIRECT3DINDEXBUFFER9 &ptr_indexBuffer, 
    LPDIRECT3DVERTEXBUFFER9 &ptr_vertexBuffer
)
{
    int cubeVertex = 8;
    int cubesCount = 3;
    int cubesFaces = cubesCount * 36;

    //load textures
    LoadTexture(ptr_dxDevice, ptr_dxCubeTexture, cubeTextureName);
    LoadTexture(ptr_dxDevice, ptr_dxSphere1Texture, sphere1TextureName);
    LoadTexture(ptr_dxDevice, ptr_dxSphere2Texture, sphere2TextureName);

    // create the vertices using the CUSTOMVERTEX struct
    CUSTOMVERTEX vertices[] =
    {
        { -0.2f,  0.2f, -0.2f, dxRed,   ((FLOAT)0) / (24 - 1), 1.0f },
        {  0.2f,  0.2f, -0.2f, dxGreen, ((FLOAT)1) / (24 - 1), 0.0f },
        { -0.2f, -0.2f, -0.2f, dxRed,   ((FLOAT)2) / (24 - 1), 1.0f },
        {  0.2f, -0.2f, -0.2f, dxGreen, ((FLOAT)3) / (24 - 1), 0.0f },
        { -0.2f,  0.2f,  0.2f, dxRed,   ((FLOAT)4) / (24 - 1), 1.0f },
        {  0.2f,  0.2f,  0.2f, dxGreen, ((FLOAT)5) / (24 - 1), 0.0f },
        { -0.2f, -0.2f,  0.2f, dxRed,   ((FLOAT)6) / (24 - 1), 1.0f },
        {  0.2f, -0.2f,  0.2f, dxGreen, ((FLOAT)7) / (24 - 1), 0.0f },

        {  0.8f,  0.2f+0.5f, -0.2f-0.4f, dxRed,        ((FLOAT)0+8) / (24 - 1), 1.0f },
        {  1.0f,  0.2f + 0.5f, -0.2f - 0.4f, dxYellow, ((FLOAT)1+8) / (24 - 1), 0.0f },
        {  0.8f, -0.2f + 0.5f, -0.2f - 0.4f, dxRed,    ((FLOAT)2+8) / (24 - 1), 1.0f },
        {  1.0f, -0.2f + 0.5f, -0.2f - 0.4f, dxYellow, ((FLOAT)3+8) / (24 - 1), 0.0f },
        {  0.8f,  0.2f + 0.5f,  0.2f - 0.4f, dxRed,    ((FLOAT)4+8) / (24 - 1), 1.0f },
        {  1.0f,  0.2f + 0.5f,  0.2f - 0.4f, dxYellow, ((FLOAT)5+8) / (24 - 1), 0.0f },
        {  0.8f, -0.2f + 0.5f,  0.2f - 0.4f, dxRed,    ((FLOAT)6+8) / (24 - 1), 1.0f },
        {  1.0f, -0.2f + 0.5f,  0.2f - 0.4f, dxYellow, ((FLOAT)7+8) / (24 - 1), 0.0f },

        {  0.8f+1.0f,  0.2f + 0.8f, -0.2f + 0.4f, dxBlue,     ((FLOAT)0+16) / (24 - 1), 1.0f },
        {  1.0f + 1.0f,  0.2f + 0.8f, -0.2f + 0.4f, dxYellow, ((FLOAT)1+16) / (24 - 1), 0.0f },
        {  0.8f + 1.0f, -0.2f + 0.8f, -0.2f + 0.4f, dxBlue,   ((FLOAT)2+16) / (24 - 1), 1.0f },
        {  1.0f + 1.0f, -0.2f + 0.8f, -0.2f + 0.4f, dxYellow, ((FLOAT)3+16) / (24 - 1), 0.0f },
        {  0.8f + 1.0f,  0.2f + 0.8f,  0.2f + 0.4f, dxBlue,   ((FLOAT)4+16) / (24 - 1), 1.0f },
        {  1.0f + 1.0f,  0.2f + 0.8f,  0.2f + 0.4f, dxYellow, ((FLOAT)5+16) / (24 - 1), 0.0f },
        {  0.8f + 1.0f, -0.2f + 0.8f,  0.2f + 0.4f, dxBlue,   ((FLOAT)6+16) / (24 - 1), 1.0f },
        {  1.0f + 1.0f, -0.2f + 0.8f,  0.2f + 0.4f, dxYellow, ((FLOAT)7+16) / (24 - 1), 0.0f },
    };

    // create a vertex buffer interface called v_buffer
    ptr_dxDevice->CreateVertexBuffer(
        cubesCount * cubeVertex * sizeof(CUSTOMVERTEX),
        0,
        drawFVF,
        D3DPOOL_MANAGED,
        &ptr_vertexBuffer,
        NULL);

    VOID* pVoid;    // a void pointer

    // lock v_buffer and load the vertices into it
    ptr_vertexBuffer->Lock(0, 0, (void**)&pVoid, 0);
    memcpy(pVoid, vertices, sizeof(vertices));
    ptr_vertexBuffer->Unlock();

    // create the indices using an int array
    short indices[] =
    {
        0, 1, 2,    // side 1        //cube 1
        2, 1, 3,
        4, 0, 6,    // side 2
        6, 0, 2,
        7, 5, 6,    // side 3
        6, 5, 4,
        3, 1, 7,    // side 4
        7, 1, 5,
        4, 5, 0,    // side 5
        0, 5, 1,
        3, 7, 2,    // side 6
        2, 7, 6,
        0 + 8, 1 + 8, 2 + 8,    // side 1   // cube 2
        2 + 8, 1 + 8, 3 + 8,
        4 + 8, 0 + 8, 6 + 8,    // side 2
        6 + 8, 0 + 8, 2 + 8,
        7 + 8, 5 + 8, 6 + 8,    // side 3
        6 + 8, 5 + 8, 4 + 8,
        3 + 8, 1 + 8, 7 + 8,    // side 4
        7 + 8, 1 + 8, 5 + 8,
        4 + 8, 5 + 8, 0 + 8,    // side 5
        0 + 8, 5 + 8, 1 + 8,
        3 + 8, 7 + 8, 2 + 8,    // side 6
        2 + 8, 7 + 8, 6 + 8,
        0 + 16, 1 + 16, 2 + 16,    // side 1   // cube 3
        2 + 16, 1 + 16, 3 + 16,
        4 + 16, 0 + 16, 6 + 16,    // side 2
        6 + 16, 0 + 16, 2 + 16,
        7 + 16, 5 + 16, 6 + 16,    // side 3
        6 + 16, 5 + 16, 4 + 16,
        3 + 16, 1 + 16, 7 + 16,    // side 4
        7 + 16, 1 + 16, 5 + 16,
        4 + 16, 5 + 16, 0 + 16,    // side 5
        0 + 16, 5 + 16, 1 + 16,
        3 + 16, 7 + 16, 2 + 16,    // side 6
        2 + 16, 7 + 16, 6 + 16,
    };

    // create an index buffer interface called i_buffer
    ptr_dxDevice->CreateIndexBuffer(cubesFaces * sizeof(short),
        0,
        D3DFMT_INDEX16,
        D3DPOOL_MANAGED,
        &ptr_indexBuffer,
        NULL);


    // lock i_buffer and load the indices into it
    ptr_indexBuffer->Lock(0, 0, (void**)&pVoid, 0);
    memcpy(pVoid, indices, sizeof(indices));
    ptr_indexBuffer->Unlock();

    ptr_dxDevice->SetFVF(drawFVF);

    // select the vertex and index buffers to use
    ptr_dxDevice->SetStreamSource(0, vertexBuffer, 0, sizeof(CUSTOMVERTEX));
    ptr_dxDevice->SetIndices(ptr_indexBuffer);

....

接下来,我尝试使用纹理,但是结果是每个多维数据集仅应用了1个纹理。 代码:

//-
//texture1 
ptr_dxDevice->SetTexture(0, ptr_dxCubeTexture);
ptr_dxDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
ptr_dxDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
ptr_dxDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
ptr_dxDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
//texture2
ptr_dxDevice->SetTexture(1, ptr_dxSphere1Texture);
ptr_dxDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE);
ptr_dxDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
ptr_dxDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
ptr_dxDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
//+


D3DXMATRIXA16 mTextureTransform;
D3DXMATRIXA16 mProj;
D3DXMATRIXA16 mTrans;
D3DXMATRIXA16 mScale;

ptr_dxDevice->GetTransform(D3DTS_PROJECTION, &mProj);
D3DXMatrixTranslation(&mTrans, 0.5f, 0.5f, 0.0f);
D3DXMatrixScaling(&mScale, 0.5f, -0.5f, 1.0f);
mTextureTransform = mProj * mScale * mTrans;

ptr_dxDevice->SetTransform(D3DTS_TEXTURE0, &mTextureTransform);
ptr_dxDevice->SetTextureStageState(1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT4 | D3DTTFF_PROJECTED);
ptr_dxDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEPOSITION);
//textures-

// draw the cubes
ptr_dxDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8 * cubesCount, 0, 12* cubesCount);
}

(功能结束)

因此,我想了解如何为每个多维数据集设置不同的纹理。 我有3个立方体和3个纹理,我想在一个场景中使用它们。

谢谢

0 个答案:

没有答案