[PATCH 1/2] d3dx9: Implement D3DXCreateBox()

Gediminas Jakutis gediminas at varciai.lt
Thu Feb 27 04:38:38 CST 2014


---
 dlls/d3dx9_36/mesh.c       | 104 +++++++++++++++++++++++++++++++++++++++++----
 dlls/d3dx9_36/tests/mesh.c |  14 +++---
 2 files changed, 103 insertions(+), 15 deletions(-)

diff --git a/dlls/d3dx9_36/mesh.c b/dlls/d3dx9_36/mesh.c
index 118baf7..6c102f8 100644
--- a/dlls/d3dx9_36/mesh.c
+++ b/dlls/d3dx9_36/mesh.c
@@ -4525,20 +4525,108 @@ cleanup:
     return hr;
 }
 
-HRESULT WINAPI D3DXCreateBox(struct IDirect3DDevice9 *device, float width, float height,
-        float depth, struct ID3DXMesh **mesh, struct ID3DXBuffer **adjacency)
-{
-    FIXME("(%p, %f, %f, %f, %p, %p): stub\n", device, width, height, depth, mesh, adjacency);
-
-    return E_NOTIMPL;
-}
-
 struct vertex
 {
     D3DXVECTOR3 position;
     D3DXVECTOR3 normal;
 };
 
+HRESULT WINAPI D3DXCreateBox(struct IDirect3DDevice9 *device, float width, float height,
+        float depth, struct ID3DXMesh **mesh, struct ID3DXBuffer **adjacency)
+{
+    HRESULT hr;
+    ID3DXMesh *box;
+    struct vertex *vertices;
+    WORD (*faces)[3];
+    DWORD *adjacency_buf;
+    unsigned int i, face;
+    static const float sign_x[24] = {-1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f,  1.0f,  1.0f,
+                                      1.0f,  1.0f,  1.0f,  1.0f, -1.0f, -1.0f,  1.0f,  1.0f,
+                                     -1.0f,  1.0f,  1.0f, -1.0f, -1.0f, -1.0f,  1.0f,  1.0f};
+    static const float sign_y[24] = {-1.0f, -1.0f,  1.0f,  1.0f,  1.0f,  1.0f,  1.0f,  1.0f,
+                                      1.0f,  1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f,
+                                     -1.0f, -1.0f,  1.0f,  1.0f, -1.0f,  1.0f,  1.0f, -1.0f};
+    static const float sign_z[24] = {-1.0f,  1.0f,  1.0f, -1.0f, -1.0f,  1.0f,  1.0f, -1.0f,
+                                     -1.0f,  1.0f,  1.0f, -1.0f,  1.0f, -1.0f, -1.0f,  1.0f,
+                                      1.0f,  1.0f,  1.0f,  1.0f, -1.0f, -1.0f, -1.0f, -1.0f};
+    static const float normal_x[6] = { -1.0f,  0.0f,  1.0f,  0.0f,  0.0f,  0.0f};
+    static const float normal_y[6] = {  0.0f,  1.0f,  0.0f, -1.0f,  0.0f,  0.0f};
+    static const float normal_z[6] = {  0.0f,  0.0f,  0.0f,  0.0f,  1.0f, -1.0f};
+    static const DWORD adjacency_table[36]= {6,  9,  1, 2, 10,  0,  1,  9,  3, 4, 10,  2,
+                                             3,  8,  5, 7, 11,  4,  0, 11,  7, 5,  8,  6,
+                                             7,  4,  9, 2,  0,  8,  1,  3, 11, 5,  6, 10};
+
+    TRACE("(%p, %f, %f, %f, %p, %p)\n", device, width, height, depth, mesh, adjacency);
+
+    if (!device || width < 0.0f || height < 0.0f || depth < 0.0f || !mesh)
+    {
+        return D3DERR_INVALIDCALL;
+    }
+
+    hr = D3DXCreateMeshFVF(12, 24, D3DXMESH_MANAGED, D3DFVF_XYZ | D3DFVF_NORMAL, device, &box);
+
+    if (FAILED(hr))
+    {
+        return hr;
+    }
+
+    if (FAILED(hr = box->lpVtbl->LockVertexBuffer(box, 0, (void **)&vertices)))
+    {
+        box->lpVtbl->Release(box);
+        return hr;
+    }
+
+    if (FAILED(hr = box->lpVtbl->LockIndexBuffer(box, 0, (void **)&faces)))
+    {
+        box->lpVtbl->UnlockVertexBuffer(box);
+        box->lpVtbl->Release(box);
+        return hr;
+    }
+
+    width /= 2.0f;
+    height /= 2.0f;
+    depth /= 2.0f;
+
+    for (i = 0; i < 24; i++)
+    {
+        vertices[i].position.x = width * sign_x[i];
+        vertices[i].position.y = height * sign_y[i];
+        vertices[i].position.z = depth * sign_z[i];
+        vertices[i].normal.x = normal_x[i / 4];
+        vertices[i].normal.y = normal_y[i / 4];
+        vertices[i].normal.z = normal_z[i / 4];
+    }
+
+    face = 0;
+    for (i = 0; i < 12; i++)
+    {
+        faces[i][0] = face++;
+        faces[i][1] = face++;
+        faces[i][2] = (i % 2) ? face - 4 : face;
+    }
+
+    box->lpVtbl->UnlockIndexBuffer(box);
+    box->lpVtbl->UnlockVertexBuffer(box);
+
+    if (adjacency)
+    {
+        hr = D3DXCreateBuffer(36 * sizeof(DWORD), adjacency);
+
+        if (FAILED(hr))
+        {
+            box->lpVtbl->Release(box);
+            return hr;
+        }
+
+        adjacency_buf = ID3DXBuffer_GetBufferPointer(*adjacency);
+        memcpy(adjacency_buf, adjacency_table, 36 * sizeof(DWORD));
+    }
+
+    *mesh = box;
+
+    return D3D_OK;
+}
+
 typedef WORD face[3];
 
 struct sincos_table
diff --git a/dlls/d3dx9_36/tests/mesh.c b/dlls/d3dx9_36/tests/mesh.c
index 1c3748f..d32f159 100644
--- a/dlls/d3dx9_36/tests/mesh.c
+++ b/dlls/d3dx9_36/tests/mesh.c
@@ -2482,23 +2482,23 @@ static void D3DXCreateBoxTest(void)
     }
 
     hr = D3DXCreateBox(device,2.0f,20.0f,4.9f,NULL, &ppBuffer);
-    todo_wine ok(hr==D3DERR_INVALIDCALL, "Expected D3DERR_INVALIDCALL, received %#x\n", hr);
+    ok(hr==D3DERR_INVALIDCALL, "Expected D3DERR_INVALIDCALL, received %#x\n", hr);
 
     hr = D3DXCreateBox(NULL,22.0f,20.0f,4.9f,&box, &ppBuffer);
-    todo_wine ok(hr==D3DERR_INVALIDCALL, "Expected D3DERR_INVALIDCALL, received %#x\n", hr);
+    ok(hr==D3DERR_INVALIDCALL, "Expected D3DERR_INVALIDCALL, received %#x\n", hr);
 
     hr = D3DXCreateBox(device,-2.0f,20.0f,4.9f,&box, &ppBuffer);
-    todo_wine ok(hr==D3DERR_INVALIDCALL, "Expected D3DERR_INVALIDCALL, received %#x\n", hr);
+    ok(hr==D3DERR_INVALIDCALL, "Expected D3DERR_INVALIDCALL, received %#x\n", hr);
 
     hr = D3DXCreateBox(device,22.0f,-20.0f,4.9f,&box, &ppBuffer);
-    todo_wine ok(hr==D3DERR_INVALIDCALL, "Expected D3DERR_INVALIDCALL, received %#x\n", hr);
+    ok(hr==D3DERR_INVALIDCALL, "Expected D3DERR_INVALIDCALL, received %#x\n", hr);
 
     hr = D3DXCreateBox(device,22.0f,20.0f,-4.9f,&box, &ppBuffer);
-    todo_wine ok(hr==D3DERR_INVALIDCALL, "Expected D3DERR_INVALIDCALL, received %#x\n", hr);
+    ok(hr==D3DERR_INVALIDCALL, "Expected D3DERR_INVALIDCALL, received %#x\n", hr);
 
     ppBuffer = NULL;
     hr = D3DXCreateBox(device,10.9f,20.0f,4.9f,&box, &ppBuffer);
-    todo_wine ok(hr==D3D_OK, "Expected D3D_OK, received %#x\n", hr);
+    ok(hr==D3D_OK, "Expected D3D_OK, received %#x\n", hr);
 
     if (FAILED(hr))
     {
@@ -2508,7 +2508,7 @@ static void D3DXCreateBoxTest(void)
 
     buffer = ID3DXBuffer_GetBufferPointer(ppBuffer);
     for(i=0; i<36; i++)
-        todo_wine ok(adjacency[i]==buffer[i], "expected adjacency %d: %#x, received %#x\n",i,adjacency[i], buffer[i]);
+        ok(adjacency[i]==buffer[i], "expected adjacency %d: %#x, received %#x\n",i,adjacency[i], buffer[i]);
 
     box->lpVtbl->Release(box);
 
-- 
1.8.3.2





More information about the wine-patches mailing list