comdlg32: Fix a crash in LookIn combo box WM_COMMAND message handler.
[wine/hacks.git] / dlls / d3dx9_36 / mesh.c
blob0d3eb2ced1f9c244908480332efe07f0ba09b9e6
1 /*
2 * Mesh operations specific to D3DX9.
4 * Copyright (C) 2009 David Adam
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "windef.h"
23 #include "wingdi.h"
24 #include "d3dx9.h"
27 /*************************************************************************
28 * D3DXComputeBoundingBox
30 HRESULT WINAPI D3DXComputeBoundingBox(CONST D3DXVECTOR3 *pfirstposition, DWORD numvertices, DWORD dwstride, D3DXVECTOR3 *pmin, D3DXVECTOR3 *pmax)
32 D3DXVECTOR3 vec;
33 unsigned int i;
35 if( !pfirstposition || !pmin || !pmax ) return D3DERR_INVALIDCALL;
37 *pmin = *pfirstposition;
38 *pmax = *pmin;
40 for(i=0; i<numvertices; i++)
42 vec = *( (D3DXVECTOR3*)((char*)pfirstposition + dwstride * i) );
44 if ( vec.x < pmin->x ) pmin->x = vec.x;
45 if ( vec.x > pmax->x ) pmax->x = vec.x;
47 if ( vec.y < pmin->y ) pmin->y = vec.y;
48 if ( vec.y > pmax->y ) pmax->y = vec.y;
50 if ( vec.z < pmin->z ) pmin->z = vec.z;
51 if ( vec.z > pmax->z ) pmax->z = vec.z;
54 return D3D_OK;
57 /*************************************************************************
58 * D3DXComputeBoundingSphere
60 HRESULT WINAPI D3DXComputeBoundingSphere(CONST D3DXVECTOR3* pfirstposition, DWORD numvertices, DWORD dwstride, D3DXVECTOR3 *pcenter, FLOAT *pradius)
62 D3DXVECTOR3 temp, temp1;
63 FLOAT d;
64 unsigned int i;
66 if( !pfirstposition || !pcenter || !pradius ) return D3DERR_INVALIDCALL;
68 temp.x = 0.0f;
69 temp.y = 0.0f;
70 temp.z = 0.0f;
71 temp1 = temp;
72 d = 0.0f;
73 *pradius = 0.0f;
75 for(i=0; i<numvertices; i++)
77 D3DXVec3Add(&temp1, &temp, (D3DXVECTOR3*)((char*)pfirstposition + dwstride * i));
78 temp = temp1;
81 D3DXVec3Scale(pcenter, &temp, 1.0f/((FLOAT)numvertices));
83 for(i=0; i<numvertices; i++)
85 d = D3DXVec3Length(D3DXVec3Subtract(&temp, (D3DXVECTOR3*)((char*)pfirstposition + dwstride * i), pcenter));
86 if ( d > *pradius ) *pradius = d;
88 return D3D_OK;
91 /*************************************************************************
92 * D3DXGetFVFVertexSize
94 static UINT Get_TexCoord_Size_From_FVF(DWORD FVF, int tex_num)
96 return (((((FVF) >> (16 + (2 * (tex_num)))) + 1) & 0x03) + 1);
99 UINT WINAPI D3DXGetFVFVertexSize(DWORD FVF)
101 DWORD size = 0;
102 UINT i;
103 UINT numTextures = (FVF & D3DFVF_TEXCOUNT_MASK) >> D3DFVF_TEXCOUNT_SHIFT;
105 if (FVF & D3DFVF_NORMAL) size += sizeof(D3DXVECTOR3);
106 if (FVF & D3DFVF_DIFFUSE) size += sizeof(DWORD);
107 if (FVF & D3DFVF_SPECULAR) size += sizeof(DWORD);
108 if (FVF & D3DFVF_PSIZE) size += sizeof(DWORD);
110 switch (FVF & D3DFVF_POSITION_MASK)
112 case D3DFVF_XYZ: size += sizeof(D3DXVECTOR3); break;
113 case D3DFVF_XYZRHW: size += 4 * sizeof(FLOAT); break;
114 case D3DFVF_XYZB1: size += 4 * sizeof(FLOAT); break;
115 case D3DFVF_XYZB2: size += 5 * sizeof(FLOAT); break;
116 case D3DFVF_XYZB3: size += 6 * sizeof(FLOAT); break;
117 case D3DFVF_XYZB4: size += 7 * sizeof(FLOAT); break;
118 case D3DFVF_XYZB5: size += 8 * sizeof(FLOAT); break;
119 case D3DFVF_XYZW: size += 4 * sizeof(FLOAT); break;
122 for (i = 0; i < numTextures; i++)
124 size += Get_TexCoord_Size_From_FVF(FVF, i) * sizeof(FLOAT);
127 return size;
130 /*************************************************************************
131 * D3DXIntersectTri
133 BOOL WINAPI D3DXIntersectTri(CONST D3DXVECTOR3 *p0, CONST D3DXVECTOR3 *p1, CONST D3DXVECTOR3 *p2, CONST D3DXVECTOR3 *praypos, CONST D3DXVECTOR3 *praydir, FLOAT *pu, FLOAT *pv, FLOAT *pdist)
135 D3DXMATRIX m;
136 D3DXVECTOR4 vec;
138 m.m[0][0] = p1->x - p0->x;
139 m.m[1][0] = p2->x - p0->x;
140 m.m[2][0] = -praydir->x;
141 m.m[3][0] = 0.0f;
142 m.m[0][1] = p1->y - p0->z;
143 m.m[1][1] = p2->y - p0->z;
144 m.m[2][1] = -praydir->y;
145 m.m[3][1] = 0.0f;
146 m.m[0][2] = p1->z - p0->z;
147 m.m[1][2] = p2->z - p0->z;
148 m.m[2][2] = -praydir->z;
149 m.m[3][2] = 0.0f;
150 m.m[0][3] = 0.0f;
151 m.m[1][3] = 0.0f;
152 m.m[2][3] = 0.0f;
153 m.m[3][3] = 1.0f;
155 vec.x = praypos->x - p0->x;
156 vec.y = praypos->y - p0->y;
157 vec.z = praypos->z - p0->z;
158 vec.w = 0.0f;
160 if ( D3DXMatrixInverse(&m, NULL, &m) )
162 D3DXVec4Transform(&vec, &vec, &m);
163 if ( (vec.x >= 0.0f) && (vec.y >= 0.0f) && (vec.x + vec.y <= 1.0f) && (vec.z >= 0.0f) )
165 *pu = vec.x;
166 *pv = vec.y;
167 *pdist = fabs( vec.z );
168 return TRUE;
172 return FALSE;