【问题标题】:DirectX: Cannot translate a rectangleDirectX:无法平移矩形
【发布时间】:2018-01-21 20:15:37
【问题描述】:

我一直在关注这个 DirectX 教程http://www.directxtutorial.com/Lesson.aspx?lessonid=9-4-5。我刚刚用 C++ 启动了 DirectX。我在屏幕上绘制了两个矩形,但无法翻译它们。矩形在屏幕上的位置不同,并存储在数组 OurVertices 中。我一直在阅读教程,它说您首先必须应用世界变换才能将其转换为 3d,但我不想进入 3d,因为我有两个简单的 2d 矩形。如何在 DirectX C++ 中移动简单的二维矩形。如果我使用教程应用世界变换,我不知道为什么我的相机位置倾斜。我的代码如下:

CUSTOMVERTEX OurVertices[] =
    {
    //  1
        { 0, 0, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 0), },   //meaning x,y,z,Dword
        { 100, 0, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 0), },
        { 0, 100, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 0), },
        { 100, 100, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 0), },

        { 200, 200, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), },
        { 400, 200, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), },
        { 200, 400, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },
        { 400, 400, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), },
    };

d3ddev->CreateVertexBuffer(8 * sizeof(CUSTOMVERTEX),
        0,
        CUSTOMFVF,
        D3DPOOL_MANAGED,
        &v_buffer,
        NULL);
    VOID* pVoid;    // the void* we were talking about

    v_buffer->Lock(0, 0, (void**)&pVoid, 0);    // locks v_buffer, the buffer we made earlier

    memcpy(pVoid, OurVertices, sizeof(OurVertices));
    v_buffer->Unlock();    // unlock v_buffer

    d3ddev->SetFVF(CUSTOMFVF);



void render_frame()
{ 
    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

    d3ddev->BeginScene();

    // select which vertex format we are using
    // select the vertex buffer to display
    d3ddev->SetFVF(CUSTOMFVF);

        // SET UP THE PIPELINE

    D3DXMATRIX matTranslate;    
    static float index = 0.0f; 
    index += 0.01f;    // an ever-increasing float value

                                                      // build a matrix to rotate the model based on the increasing float value
    D3DXMatrixTranslation(&matTranslate, 0, index , 0.0f);

        // tell Direct3D about our matrix
    d3ddev->SetTransform(D3DTS_WORLD, &matTranslate);

    D3DXMATRIX matView;    // the view transform matrix

    D3DXMatrixLookAtLH(&matView,
            &D3DXVECTOR3(0.0f, 0.0f, 10.0f),    // the camera position
            &D3DXVECTOR3(0.0f, 0.0f, 0.0f),    // the look-at position
            &D3DXVECTOR3(0.0f, 1.0f, 0.0f));    // the up direction

    d3ddev->SetTransform(D3DTS_VIEW, &matView);    // set the view transform to matView

    D3DXMATRIX matProjection;     // the projection transform matrix

    D3DXMatrixPerspectiveFovLH(&matProjection,
            D3DXToRadian(100),    // the horizontal field of view
            (FLOAT)800 / (FLOAT)600, // aspect ratio
            1.0f,    // the near view-plane
            100.0f);    // the far view-plane

    d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection);    // set the projection

                                                                   // select the vertex buffer to display
    d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));

        // copy the vertex buffer to the back buffer
    d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
    d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 2);



    d3ddev->EndScene();

    d3ddev->Present(NULL, NULL, NULL, NULL);
}

有什么办法可以平移两个矩形?

【问题讨论】:

    标签: c++ directx translation


    【解决方案1】:

    您已经在 3D 中工作,当您指定顶点的位置时,您有一个额外的 z 组件,此时设置为 0。您可以更改它并查看场景的效果。

        { 0, 0, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 0), },   //meaning x,y,z,Dword
        { 100, 0, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 0), },
        { 0, 100, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 0), },
        { 100, 100, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 0), },
    
        { 200, 200, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), },
        { 400, 200, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), },
        { 200, 400, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },
        { 400, 400, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), },
    

    所以现在每一帧你都在 y 轴上将你的顶点平移一个增加的量。

    static float index = 0.0f; 
    index += 0.01f;    // an ever-increasing float value
    
    // build a matrix to rotate the model based on the increasing float value
    D3DXMatrixTranslation(&matTranslate, 0, index , 0.0f);
    

    如果您在每一帧打印索引,您会看到他在成长,并使用此值来构建一个平移矩阵,该矩阵将在查看/投影之前应用。

    不知道你想要什么效果。 但是你场景的所有顶点都会从下到上,你可以看起来像你的相机正在下降。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-23
      • 1970-01-01
      • 2014-04-26
      • 2019-01-12
      • 2016-11-11
      • 2012-12-15
      • 2011-02-16
      • 1970-01-01
      相关资源
      最近更新 更多