0
\$\begingroup\$

This code is DirectX 9, software rendering. I have next piece of code to simple draw textured cube (see below). Cube geometry draws correctly, but texturing is affine - with distortions (my cube is rotate). How to apply perspective correct texturemapping in my sample? I wrote same code in DirectX 6 and it works perfectly - with perspective correnct texturemapping. Whats wrong in my DX9 sample?

struct MY_VERTEX
{
    D3DXVECTOR4 p;
    D3DXVECTOR2 t;

    static const DWORD FVF;
};


const DWORD MY_VERTEX::FVF = D3DFVF_XYZRHW | D3DFVF_TEX1;



void Render_Scene()
{

//...........................

//cube model has 24 vertices with tex coords
for ( int i = 0; i < 24; i++ )
{
    MY_VERTEX v1;
    MY_VERTEX v2;

    D3DXVec4Transform(&v1.p, &vert_buff[i].p, &MxRotateY);
    D3DXVec4Transform(&v2.p, &v1.p, &MxWorld);
    D3DXVec4Transform(&v1.p, &v2.p, &MxProj);
            
    v1.p.x = v1.p.x / v1.p.w;
    v1.p.y = v1.p.y / v1.p.w;

    v1.p.x = v1.p.x * rc.right / 2.0f + rc.right / 2.0f;
    v1.p.y = -v1.p.y * rc.bottom / 2.0f + rc.bottom / 2.0f;

    v1.t.x = vert_buff[i].t.x;
    v1.t.y = vert_buff[i].t.y;

    vert_buff_transformed[i] = v1;
}


HRESULT hr;

hr = p_d3d_Device->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 60, 100, 150), 1.0f, 0 );
if(FAILED(hr)) return;

hr = p_d3d_Device->BeginScene();
if(FAILED(hr)) return;

p_d3d_Device->SetTexture(0, g_pTexture);

p_d3d_Device->SetFVF( MY_VERTEX::FVF );

p_d3d_Device->DrawIndexedPrimitiveUP( D3DPT_TRIANGLELIST,0,24,12, index_buff,D3DFMT_INDEX16, vert_buff_transformed, sizeof(MY_VERTEX));


hr = p_d3d_Device->EndScene();
if(FAILED(hr)) return;
    
hr = p_d3d_Device->Present( NULL, NULL, NULL, NULL );
if(FAILED(hr)) return;

}

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I did put some additional lines in loop and it works fine:

v1.p.x = v1.p.x / v1.p.w;
v1.p.y = v1.p.y / v1.p.w;
v1.p.z = v1.p.z / v1.p.w;    //added
v1.p.w = 1.0f / v1.p.w;      //added
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Did this solve the problem? \$\endgroup\$
    – DMGregory
    Mar 15 at 11:55
  • \$\begingroup\$ Did solve, yes. Thank you. \$\endgroup\$
    – black4
    Mar 15 at 13:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .