d3dx8: Implement D3DXBoxBoundProbe.
[wine] / dlls / d3dx8 / mesh.c
1 /*
2  * Copyright 2008 David Adam
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include "windef.h"
20 #include "wingdi.h"
21 #include "d3dx8.h"
22
23 #include "wine/debug.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
26
27 BOOL WINAPI D3DXBoxBoundProbe(CONST D3DXVECTOR3 *pmin, CONST D3DXVECTOR3 *pmax, CONST D3DXVECTOR3 *prayposition, CONST D3DXVECTOR3 *praydirection)
28
29 /* Algorithm taken from the article: An Efficient and Robust Ray-Box Intersection Algoritm
30 Amy Williams             University of Utah
31 Steve Barrus             University of Utah
32 R. Keith Morley          University of Utah
33 Peter Shirley            University of Utah
34
35 International Conference on Computer Graphics and Interactive Techniques  archive
36 ACM SIGGRAPH 2005 Courses
37 Los Angeles, California
38
39 This algorithm is free of patents or of copyrights, as confirmed by Peter Shirley himself.
40
41 Algorithm: Consider the box as the intersection of three slabs. Clip the ray
42 against each slab, if there's anything left of the ray after we're
43 done we've got an intersection of the ray with the box.
44 */
45
46 {
47     FLOAT div, tmin, tmax, tymin, tymax, tzmin, tzmax;
48
49     div = 1.0f / praydirection->x;
50     if ( div >= 0.0f )
51     {
52      tmin = ( pmin->x - prayposition->x ) * div;
53      tmax = ( pmax->x - prayposition->x ) * div;
54     }
55     else
56     {
57      tmin = ( pmax->x - prayposition->x ) * div;
58      tmax = ( pmin->x - prayposition->x ) * div;
59     }
60
61     if ( tmax < 0.0f ) return FALSE;
62
63     div = 1.0f / praydirection->y;
64     if ( div >= 0.0f )
65     {
66      tymin = ( pmin->y - prayposition->y ) * div;
67      tymax = ( pmax->y - prayposition->y ) * div;
68     }
69     else
70     {
71      tymin = ( pmax->y - prayposition->y ) * div;
72      tymax = ( pmin->y - prayposition->y ) * div;
73     }
74
75     if ( ( tymax < 0.0f ) || ( tmin > tymax ) || ( tymin > tmax ) ) return FALSE;
76
77     if ( tymin > tmin ) tmin = tymin;
78     if ( tymax < tmax ) tmax = tymax;
79
80     div = 1.0f / praydirection->z;
81     if ( div >= 0.0f )
82     {
83      tzmin = ( pmin->z - prayposition->z ) * div;
84      tzmax = ( pmax->z - prayposition->z ) * div;
85     }
86     else
87     {
88      tzmin = ( pmax->z - prayposition->z ) * div;
89      tzmax = ( pmin->z - prayposition->z ) * div;
90     }
91
92     if ( (tzmax < 0.0f ) || ( tmin > tzmax ) || ( tzmin > tmax ) ) return FALSE;
93
94     return TRUE;
95 }
96
97 BOOL WINAPI D3DXSphereBoundProbe(CONST D3DXVECTOR3 *pcenter, FLOAT radius, CONST D3DXVECTOR3 *prayposition, CONST D3DXVECTOR3 *praydirection)
98 {
99     D3DXVECTOR3 difference;
100     FLOAT a, b, c, d;
101
102     a = D3DXVec3LengthSq(praydirection);
103     if (!D3DXVec3Subtract(&difference, prayposition, pcenter)) return FALSE;
104     b = D3DXVec3Dot(&difference, praydirection);
105     c = D3DXVec3LengthSq(&difference) - radius * radius;
106     d = b * b - a * c;
107
108     if ( ( d <= 0.0f ) || ( sqrt(d) <= b ) ) return FALSE;
109     return TRUE;
110 }