From 7bfd634286bd3a9afdcd790fee3af3050671b6e2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=B3zef=20Kucia?= Date: Sun, 28 Oct 2012 23:20:08 +0100 Subject: [PATCH] d3dx9: Handle NULL viewport in D3DXVec3Unproject. --- dlls/d3dx9_36/math.c | 21 +++++++++++++-------- dlls/d3dx9_36/tests/math.c | 4 ++++ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/dlls/d3dx9_36/math.c b/dlls/d3dx9_36/math.c index 5342bc93ded..117bb848c88 100644 --- a/dlls/d3dx9_36/math.c +++ b/dlls/d3dx9_36/math.c @@ -1928,22 +1928,27 @@ D3DXVECTOR3* WINAPI D3DXVec3TransformNormalArray(D3DXVECTOR3* out, UINT outstrid D3DXVECTOR3* WINAPI D3DXVec3Unproject(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DVIEWPORT9 *pviewport, CONST D3DXMATRIX *pprojection, CONST D3DXMATRIX *pview, CONST D3DXMATRIX *pworld) { D3DXMATRIX m; - D3DXVECTOR3 out; TRACE("(%p, %p, %p, %p, %p, %p)\n", pout, pv, pviewport, pprojection, pview, pworld); - if (pworld) { + if (pworld) + { D3DXMatrixMultiply(&m, pworld, pview); D3DXMatrixMultiply(&m, &m, pprojection); - } else { + } + else + { D3DXMatrixMultiply(&m, pview, pprojection); } D3DXMatrixInverse(&m, NULL, &m); - out.x = 2.0f * ( pv->x - pviewport->X ) / pviewport->Width - 1.0f; - out.y = 1.0f - 2.0f * ( pv->y - pviewport->Y ) / pviewport->Height; - out.z = ( pv->z - pviewport->MinZ) / ( pviewport->MaxZ - pviewport->MinZ ); - D3DXVec3TransformCoord(&out, &out, &m); - *pout = out; + *pout = *pv; + if (pviewport) + { + pout->x = 2.0f * ( pout->x - pviewport->X ) / pviewport->Width - 1.0f; + pout->y = 1.0f - 2.0f * ( pout->y - pviewport->Y ) / pviewport->Height; + pout->z = ( pout->z - pviewport->MinZ) / ( pviewport->MaxZ - pviewport->MinZ ); + } + D3DXVec3TransformCoord(pout, pout, &m); return pout; } diff --git a/dlls/d3dx9_36/tests/math.c b/dlls/d3dx9_36/tests/math.c index 166b14a5039..a066ea8aef3 100644 --- a/dlls/d3dx9_36/tests/math.c +++ b/dlls/d3dx9_36/tests/math.c @@ -1381,6 +1381,10 @@ static void D3DXVector3Test(void) D3DXMatrixMultiply(&mat,&world,&view); D3DXVec3Unproject(&gotvec,&u,&viewport,&projection,&mat,NULL); expect_vec3(expectedvec,gotvec); + /* Viewport can be omitted */ + expectedvec.x = -11.018396f; expectedvec.y = 3.218991f; expectedvec.z = 1.380329f; + D3DXVec3Unproject(&gotvec,&u,NULL,&projection,&view,&world); + expect_vec3(expectedvec,gotvec); } static void D3DXVector4Test(void) -- 2.11.4.GIT