~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/dlls/d3drm/math.c

Version: ~ [ wine-1.1.3 ] ~ [ wine-1.1.2 ] ~ [ wine-1.1.1 ] ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~ [ wine-1.0-rc5 ] ~ [ wine-1.0-rc4 ] ~ [ wine-1.0-rc3 ] ~ [ wine-1.0-rc2 ] ~ [ wine-1.0-rc1 ] ~ [ wine-0.9.61 ] ~ [ wine-0.9.60 ] ~ [ wine-0.9.59 ] ~ [ wine-0.9.58 ] ~ [ wine-0.9.57 ] ~ [ wine-0.9.56 ] ~ [ wine-0.9.55 ] ~ [ wine-0.9.54 ] ~ [ wine-0.9.53 ] ~ [ wine-0.9.52 ] ~ [ wine-0.9.51 ] ~ [ wine-0.9.50 ] ~ [ wine-0.9.49 ] ~ [ wine-0.9.48 ] ~ [ wine-0.9.47 ] ~ [ wine-0.9.46 ] ~ [ wine-0.9.45 ] ~ [ wine-0.9.44 ] ~ [ wine-0.9.43 ] ~ [ wine-0.9.42 ] ~ [ wine-0.9.41 ] ~ [ wine-0.9.40 ] ~ [ wine-0.9.39 ] ~ [ wine-0.9.38 ] ~ [ wine-0.9.37 ] ~ [ wine-0.9.36 ] ~ [ wine-0.9.35 ] ~ [ wine-0.9.34 ] ~ [ wine-0.9.33 ] ~ [ wine-0.9.32 ] ~ [ wine-0.9.31 ] ~ [ wine-0.9.30 ] ~ [ wine-0.9.29 ] ~ [ wine-0.9.28 ] ~ [ wine-0.9.27 ] ~ [ wine-0.9.26 ] ~ [ wine-0.9.25 ] ~ [ wine-0.9.24 ] ~ [ wine-0.9.23 ] ~ [ wine-0.9.22 ] ~ [ wine-0.9.21 ] ~ [ wine-0.9.20 ] ~ [ wine-0.9.19 ] ~ [ wine-0.9.18 ] ~ [ wine-0.9.17 ] ~ [ wine-0.9.16 ] ~ [ wine-0.9.15 ] ~ [ wine-0.9.14 ] ~ [ wine-0.9.13 ] ~ [ wine-0.9.12 ] ~ [ wine-0.9.11 ] ~ [ wine-0.9.10 ] ~ [ wine-0.9.9 ] ~ [ wine-0.9.8 ] ~ [ wine-0.9.7 ] ~ [ wine-0.9.6 ] ~ [ wine-0.9.5 ] ~ [ wine-0.9.4 ] ~ [ wine-0.9.3 ] ~ [ wine-0.9.2 ] ~ [ wine-0.9.1 ] ~ [ wine-0.9 ] ~ [ wine20050930 ] ~ [ wine20050830 ] ~ [ wine20050725 ] ~ [ wine20050628 ] ~ [ wine20050524 ] ~ [ wine20050419 ] ~ [ wine20050310 ] ~ [ wine20050211 ] ~ [ wine20050111 ] ~ [ wine20041201 ] ~ [ wine20041019 ] ~ [ wine20040914 ] ~ [ wine20040813 ] ~ [ wine20040716 ] ~ [ wine20040615 ] ~ [ wine20040505 ] ~ [ wine20040408 ] ~ [ wine20040309 ] ~ [ wine20040213 ] ~ [ wine20040121 ] ~ [ wine20031212 ] ~ [ wine20031118 ] ~ [ wine20031016 ] ~ [ wine20030911 ] ~ [ wine20030813 ] ~ [ wine20030709 ] ~ [ wine20030618 ] ~ [ wine20030508 ] ~ [ wine20030408 ] ~ [ wine20030318 ] ~ [ wine20030219 ] ~ [ wine20030115 ] ~ [ wine20021219 ] ~ [ wine20021125 ] ~ [ wine20021031 ] ~ [ wine20021007 ] ~ [ wine20020904 ] ~ [ wine20020804 ] ~ [ wine20020710 ] ~ [ wine20020605 ] ~ [ wine20020509 ] ~ [ wine20020411 ] ~ [ wine20020310 ] ~ [ wine20020228 ] ~ [ wine20011226 ] ~ [ wine20011108 ] ~ [ wine20011004 ] ~ [ wine20010824 ] ~ [ wine20010731 ] ~ [ wine20010629 ] ~ [ wine20010510 ] ~ [ wine20010418 ] ~ [ wine20010326 ] ~ [ wine20010305 ] ~ [ wine20010216 ] ~ [ wine20010112 ] ~ [ wine20001222 ] ~ [ wine20001202 ] ~ [ wine20001026 ] ~ [ wine20001002 ] ~ [ wine20000909 ] ~ [ wine20000821 ] ~ [ wine20000801 ] ~ [ wine20000716 ] ~ [ wine20000326 ] ~ [ wine20000227 ] ~ [ wine20000130 ] ~ [ wine20000109 ] ~

  1 /*
  2  * Copyright 2007 David Adam
  3  * Copyright 2007 Vijay Kiran Kamuju
  4  *
  5  * This library is free software; you can redistribute it and/or
  6  * modify it under the terms of the GNU Lesser General Public
  7  * License as published by the Free Software Foundation; either
  8  * version 2.1 of the License, or (at your option) any later version.
  9  *
 10  * This library is distributed in the hope that it will be useful,
 11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  * Lesser General Public License for more details.
 14  *
 15  * You should have received a copy of the GNU Lesser General Public
 16  * License along with this library; if not, write to the Free Software
 17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 18  */
 19 
 20 #define NONAMELESSUNION
 21 
 22 #include <stdio.h>
 23 #include <stdlib.h>
 24 #include <stdarg.h>
 25 #include <assert.h>
 26 #include <math.h>
 27 
 28 #include "windef.h"
 29 #include "winbase.h"
 30 #include "wingdi.h"
 31 #include "d3drmdef.h"
 32 
 33 #include "wine/debug.h"
 34 
 35 WINE_DEFAULT_DEBUG_CHANNEL(d3drm);
 36 
 37 /* Create a RGB color from its components */
 38 D3DCOLOR WINAPI D3DRMCreateColorRGB(D3DVALUE red, D3DVALUE green, D3DVALUE blue)
 39 {
 40     return (D3DRMCreateColorRGBA(red, green, blue, 255.0));
 41 }
 42 /* Create a RGBA color from its components */
 43 D3DCOLOR WINAPI D3DRMCreateColorRGBA(D3DVALUE red, D3DVALUE green, D3DVALUE blue, D3DVALUE alpha)
 44 {
 45     int Red, Green, Blue, Alpha;
 46     Red=floor(red*255);
 47     Green=floor(green*255);
 48     Blue=floor(blue*255);
 49     Alpha=floor(alpha*255);
 50     if (red < 0) Red=0;
 51     if (red > 1) Red=255;
 52     if (green < 0) Green=0;
 53     if (green > 1) Green=255;
 54     if (blue < 0) Blue=0;
 55     if (blue > 1) Blue=255;
 56     if (alpha < 0) Alpha=0;
 57     if (alpha > 1) Alpha=255;
 58     return (RGBA_MAKE(Red, Green, Blue, Alpha));
 59 }
 60 
 61 /* Determine the alpha part of a color */
 62 D3DVALUE WINAPI D3DRMColorGetAlpha(D3DCOLOR color)
 63 {
 64     return (RGBA_GETALPHA(color)/255.0);
 65 }
 66 
 67 /* Determine the blue part of a color */
 68 D3DVALUE WINAPI D3DRMColorGetBlue(D3DCOLOR color)
 69 {
 70     return (RGBA_GETBLUE(color)/255.0);
 71 }
 72 
 73 /* Determine the green part of a color */
 74 D3DVALUE WINAPI D3DRMColorGetGreen(D3DCOLOR color)
 75 {
 76     return (RGBA_GETGREEN(color)/255.0);
 77 }
 78 
 79 /* Determine the red part of a color */
 80 D3DVALUE WINAPI D3DRMColorGetRed(D3DCOLOR color)
 81 {
 82     return (RGBA_GETRED(color)/255.0);
 83 }
 84 
 85 /* Product of 2 quaternions */
 86 LPD3DRMQUATERNION WINAPI D3DRMQuaternionMultiply(LPD3DRMQUATERNION q, LPD3DRMQUATERNION a, LPD3DRMQUATERNION b)
 87 {
 88     D3DVECTOR cross_product;
 89     D3DRMVectorCrossProduct(&cross_product, &a->v, &b->v);
 90     q->s = a->s * b->s - D3DRMVectorDotProduct(&a->v, &b->v);
 91     q->v.u1.x = a->s * b->v.u1.x + b->s * a->v.u1.x + cross_product.u1.x;
 92     q->v.u2.y = a->s * b->v.u2.y + b->s * a->v.u2.y + cross_product.u2.y;
 93     q->v.u3.z = a->s * b->v.u3.z + b->s * a->v.u3.z + cross_product.u3.z;
 94     return q;
 95 }
 96 
 97 /* Matrix for the Rotation that a unit quaternion represents */
 98 void WINAPI D3DRMMatrixFromQuaternion(D3DRMMATRIX4D m, LPD3DRMQUATERNION q)
 99 {
100     D3DVALUE w,x,y,z;
101     w = q->s;
102     x = q->v.u1.x;
103     y = q->v.u2.y;
104     z = q->v.u3.z;
105     m[0][0] = 1.0-2.0*(y*y+z*z);
106     m[1][1] = 1.0-2.0*(x*x+z*z);
107     m[2][2] = 1.0-2.0*(x*x+y*y);
108     m[1][0] = 2.0*(x*y+z*w);
109     m[0][1] = 2.0*(x*y-z*w);
110     m[2][0] = 2.0*(x*z-y*w);
111     m[0][2] = 2.0*(x*z+y*w);
112     m[2][1] = 2.0*(y*z+x*w);
113     m[1][2] = 2.0*(y*z-x*w);
114     m[3][0] = 0.0;
115     m[3][1] = 0.0;
116     m[3][2] = 0.0;
117     m[0][3] = 0.0;
118     m[1][3] = 0.0;
119     m[2][3] = 0.0;
120     m[3][3] = 1.0;
121 }
122 
123 /* Return a unit quaternion that represents a rotation of an angle around an axis */
124 LPD3DRMQUATERNION WINAPI D3DRMQuaternionFromRotation(LPD3DRMQUATERNION q, LPD3DVECTOR v, D3DVALUE theta)
125 {
126     q->s = cos(theta/2.0);
127     D3DRMVectorScale(&q->v, D3DRMVectorNormalize(v), sin(theta/2.0));
128     return q;
129 }
130 
131 /* Interpolation between two quaternions */
132 LPD3DRMQUATERNION WINAPI D3DRMQuaternionSlerp(LPD3DRMQUATERNION q, LPD3DRMQUATERNION a, LPD3DRMQUATERNION b, D3DVALUE alpha)
133 {
134     D3DVALUE epsilon=1.0;
135     D3DVECTOR sca1,sca2;
136     if (a->s * b->s + D3DRMVectorDotProduct(&a->v, &b->v) < 0.0) epsilon = -1.0;
137     q->s = (1.0 - alpha) * a->s + epsilon * alpha * b->s;
138     D3DRMVectorAdd(&q->v, D3DRMVectorScale(&sca1, &a->v, 1.0 - alpha),
139                    D3DRMVectorScale(&sca2, &b->v, epsilon * alpha));
140     return q;
141 }
142 
143 /* Add Two Vectors */
144 LPD3DVECTOR WINAPI D3DRMVectorAdd(LPD3DVECTOR d, LPD3DVECTOR s1, LPD3DVECTOR s2)
145 {
146     d->u1.x=s1->u1.x + s2->u1.x;
147     d->u2.y=s1->u2.y + s2->u2.y;
148     d->u3.z=s1->u3.z + s2->u3.z;
149     return d;
150 }
151 
152 /* Subtract Two Vectors */
153 LPD3DVECTOR WINAPI D3DRMVectorSubtract(LPD3DVECTOR d, LPD3DVECTOR s1, LPD3DVECTOR s2)
154 {
155     d->u1.x=s1->u1.x - s2->u1.x;
156     d->u2.y=s1->u2.y - s2->u2.y;
157     d->u3.z=s1->u3.z - s2->u3.z;
158     return d;
159 }
160 
161 /* Cross Product of Two Vectors */
162 LPD3DVECTOR WINAPI D3DRMVectorCrossProduct(LPD3DVECTOR d, LPD3DVECTOR s1, LPD3DVECTOR s2)
163 {
164     d->u1.x=s1->u2.y * s2->u3.z - s1->u3.z * s2->u2.y;
165     d->u2.y=s1->u3.z * s2->u1.x - s1->u1.x * s2->u3.z;
166     d->u3.z=s1->u1.x * s2->u2.y - s1->u2.y * s2->u1.x;
167     return d;
168 }
169 
170 /* Dot Product of Two vectors */
171 D3DVALUE WINAPI D3DRMVectorDotProduct(LPD3DVECTOR s1, LPD3DVECTOR s2)
172 {
173     D3DVALUE dot_product;
174     dot_product=s1->u1.x * s2->u1.x + s1->u2.y * s2->u2.y + s1->u3.z * s2->u3.z;
175     return dot_product;
176 }
177 
178 /* Norm of a vector */
179 D3DVALUE WINAPI D3DRMVectorModulus(LPD3DVECTOR v)
180 {
181     D3DVALUE result;
182     result=sqrt(v->u1.x * v->u1.x + v->u2.y * v->u2.y + v->u3.z * v->u3.z);
183     return result;
184 }
185 
186 /* Normalize a vector.  Returns (1,0,0) if INPUT is the NULL vector. */
187 LPD3DVECTOR WINAPI D3DRMVectorNormalize(LPD3DVECTOR u)
188 {
189     D3DVALUE modulus = D3DRMVectorModulus(u);
190     if(modulus)
191     {
192         D3DRMVectorScale(u,u,1.0/modulus);
193     }
194     else
195     {
196         u->u1.x=1.0;
197         u->u2.y=0.0;
198         u->u3.z=0.0;
199     }
200     return u;
201 }
202 
203 /* Returns a random unit vector */
204 LPD3DVECTOR WINAPI D3DRMVectorRandom(LPD3DVECTOR d)
205 {
206     d->u1.x = rand();
207     d->u2.y = rand();
208     d->u3.z = rand();
209     D3DRMVectorNormalize(d);
210     return d;
211 }
212 
213 /* Reflection of a vector on a surface */
214 LPD3DVECTOR WINAPI D3DRMVectorReflect(LPD3DVECTOR r, LPD3DVECTOR ray, LPD3DVECTOR norm)
215 {
216     D3DVECTOR sca;
217     D3DRMVectorSubtract(r, D3DRMVectorScale(&sca, norm, 2.0*D3DRMVectorDotProduct(ray,norm)), ray);
218     return r;
219 }
220 
221 /* Rotation of a vector */
222 LPD3DVECTOR WINAPI D3DRMVectorRotate(LPD3DVECTOR r, LPD3DVECTOR v, LPD3DVECTOR axis, D3DVALUE theta)
223 {
224     D3DRMQUATERNION quaternion,quaternion1, quaternion2, quaternion3, resultq;
225     D3DVECTOR NORM;
226 
227     quaternion1.s = cos(theta*.5);
228     quaternion2.s = cos(theta*.5);
229     NORM = *D3DRMVectorNormalize(axis);
230     D3DRMVectorScale(&quaternion1.v, &NORM, sin(theta * .5));
231     D3DRMVectorScale(&quaternion2.v, &NORM, -sin(theta * .5));
232     quaternion3.s = 0.0;
233     quaternion3.v = *v;
234     D3DRMQuaternionMultiply(&quaternion, &quaternion1, &quaternion3);
235     D3DRMQuaternionMultiply(&resultq, &quaternion, &quaternion2);
236     *r = *D3DRMVectorNormalize(&resultq.v);
237     return r;
238 }
239 
240 /* Scale a vector */
241 LPD3DVECTOR WINAPI D3DRMVectorScale(LPD3DVECTOR d, LPD3DVECTOR s, D3DVALUE factor)
242 {
243     d->u1.x=factor * s->u1.x;
244     d->u2.y=factor * s->u2.y;
245     d->u3.z=factor * s->u3.z;
246     return d;
247 }
248 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.