1

我正在尝试找到一种使用 DirectX 9 将 2D 图像平移到位的有效方法

我在下面附上了一张图片,希望能解释我想要做什么。基本上,我想在纹理上滚动所有四边形顶点的tutv坐标,以产生 2D 纹理的“原地滚动”效果。

下面的第一张图片代表我加载的纹理。第二个图像是纹理,tu每个tv角的四个顶点的坐标显示标准渲染图像。第三张图片说明了我想要发生的事情;我想以这样一种方式移动顶点,即渲染的框跨越图像的末端并以这样的方式回绕,即纹理将被渲染,如图所示,云的两半分开。第四个图像显示了我的临时(浪费)解决方案;我只是将图像翻倍并平移,直到到达最右边的边缘,此时我重置顶点的 tu 和 tv 以便被渲染的框回到最右边。

有没有一种合法的方法可以做到这一点,而不会将所有内容分成两个单独的四边形?

平移云

我在下面添加了我的设置和渲染代码的详细信息,如果这有助于阐明我当前设计的解决方案的路径。

我有一个为 2D 渲染设置 DirectX 的函数,如下所示。我已按照建议将包装属性添加到纹理阶段 0:

VOID SetupDirectXFor2DRender()
{
    pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_POINT );
    pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_POINT );
    pd3dDevice->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_POINT );

    // Set for wrapping textures to enable panning sprite render
    pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP );
    pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP );

    pd3dDevice->SetRenderState( D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL );
    pd3dDevice->SetRenderState( D3DRS_ALPHAREF, 0 );

    pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, true );
    pd3dDevice->SetRenderState( D3DRS_ALPHATESTENABLE, false );

    pd3dDevice->SetRenderState( D3DRS_SRCBLEND , D3DBLEND_SRCALPHA );
    pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA) ;

    pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
    pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
    pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );

    pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE );
    pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
    pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );

    pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
    pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
    pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );

    return;
}

在每一帧上,我渲染如下:

VOID RenderAllEntities()
{
    HRESULT hResult;
    // Void pointer for DirectX buffer locking
    VOID* pVoid;

    hResult = pd3dDevice->Clear( 0,
                                 NULL,
                                 D3DCLEAR_TARGET,
                                 0x0,
                                 1.0f,
                                 0 );

    hResult = pd3dDevice->BeginScene();

    // Do rendering on the back buffer here
    hResult = pd3dDevice->SetFVF( CUSTOMFVF );

    hResult = pd3dDevice->SetStreamSource( 0, pVertexBuffer, 0, sizeof(CUSTOM_VERTEX) );

    for ( std::vector<RenderContext>::iterator renderContextIndex = queuedContexts.begin(); renderContextIndex != queuedContexts.end(); ++renderContextIndex )
    {
        // Render each sprite
        for ( UINT uiIndex = 0; uiIndex < (*renderContextIndex).uiNumSprites; ++uiIndex )
        {
            // Lock the vertex buffer into memory
            hResult = pVertexBuffer->Lock( 0, 0, &pVoid, 0 );
            // Copy our vertex buffer to memory
            ::memcpy( pVoid, &renderContextIndex->vertexLists[uiIndex], sizeof(vertexList) );
            // Unlock buffer
            hResult = pVertexBuffer->Unlock();

            hResult = pd3dDevice->SetTexture( 0, (*renderContextIndex).textures[uiIndex]->GetTexture() );   
            hResult = pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 6 );
        }
    }

    // Complete and present the rendered scene
    hResult = pd3dDevice->EndScene();
    hResult = pd3dDevice->Present( NULL, NULL, NULL, NULL );

    return;
}

为了测试 SetTransform,我尝试在调用之前在渲染代码中添加以下(草率但临时的)代码块DrawPrimitive

{
    static FLOAT di = 0.0f;
    static FLOAT dy = 0.0f;

    di += 0.03f;
    dy += 0.03f;
    // Build and set translation matrix
    D3DXMATRIX ret;

    D3DXMatrixIdentity(&ret);
    ret(3, 0) = di;
    ret(3, 1) = dy;
    //ret(3, 2) = dz;

    hResult = pd3dDevice->SetTransform( D3DTS_TEXTURE0, &ret );
}

这不会使我渲染的任何精灵平移。我一直在研究 DirectX 教程并阅读 MS 文档以赶上事情,但我的知识肯定存在漏洞,所以我希望我没有做任何过于脑残的事情。

任何帮助都非常感谢。

谢谢!

4

1 回答 1

1

使用一个四边形应该很容易做到这一点。

假设您使用带有固定功能管道的 DX9,您可以使用IDirect3DDevice9::SetTransform( doc ) 将纹理转换为D3DTRANSFORMSTATETYPE( doc ) 和 2D 转换矩阵。您必须确保您的采样器状态D3DSAMP_ADDRESSUD3DSAMP_ADDRESSV( doc ) 设置为D3DTADDRESS_WRAP( doc )。这实际上平铺了纹理,因此负 uv 值或大于 1 的值被映射到纹理的无限重复。

如果您使用着色器或其他版本的 directx,您可以在着色器中自行转换纹理坐标或操作顶点的 uv 值。

于 2013-06-07T08:23:17.033 回答