ReactOS  0.4.14-dev-49-gfb4591c
math.c
Go to the documentation of this file.
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 #include "config.h"
21 #include "wine/port.h"
22 
23 #include "d3drm_private.h"
24 
25 /* Create a RGB color from its components */
27 {
28  return D3DRMCreateColorRGBA(red, green, blue, 1.0f);
29 }
30 /* Create a RGBA color from its components */
32 {
34 
36 
37  return color;
38 }
39 
40 /* Determine the alpha part of a color */
42 {
43  return (RGBA_GETALPHA(color)/255.0);
44 }
45 
46 /* Determine the blue part of a color */
48 {
49  return (RGBA_GETBLUE(color)/255.0);
50 }
51 
52 /* Determine the green part of a color */
54 {
55  return (RGBA_GETGREEN(color)/255.0);
56 }
57 
58 /* Determine the red part of a color */
60 {
61  return (RGBA_GETRED(color)/255.0);
62 }
63 
64 /* Product of 2 quaternions */
66 {
68  D3DVECTOR cross_product;
69 
70  D3DRMVectorCrossProduct(&cross_product, &a->v, &b->v);
71  temp.s = a->s * b->s - D3DRMVectorDotProduct(&a->v, &b->v);
72  temp.v.u1.x = a->s * b->v.u1.x + b->s * a->v.u1.x + cross_product.u1.x;
73  temp.v.u2.y = a->s * b->v.u2.y + b->s * a->v.u2.y + cross_product.u2.y;
74  temp.v.u3.z = a->s * b->v.u3.z + b->s * a->v.u3.z + cross_product.u3.z;
75 
76  *q = temp;
77  return q;
78 }
79 
80 /* Matrix for the Rotation that a unit quaternion represents */
82 {
83  D3DVALUE w,x,y,z;
84  w = q->s;
85  x = q->v.u1.x;
86  y = q->v.u2.y;
87  z = q->v.u3.z;
88  m[0][0] = 1.0-2.0*(y*y+z*z);
89  m[1][1] = 1.0-2.0*(x*x+z*z);
90  m[2][2] = 1.0-2.0*(x*x+y*y);
91  m[1][0] = 2.0*(x*y+z*w);
92  m[0][1] = 2.0*(x*y-z*w);
93  m[2][0] = 2.0*(x*z-y*w);
94  m[0][2] = 2.0*(x*z+y*w);
95  m[2][1] = 2.0*(y*z+x*w);
96  m[1][2] = 2.0*(y*z-x*w);
97  m[3][0] = 0.0;
98  m[3][1] = 0.0;
99  m[3][2] = 0.0;
100  m[0][3] = 0.0;
101  m[1][3] = 0.0;
102  m[2][3] = 0.0;
103  m[3][3] = 1.0;
104 }
105 
106 /* Return a unit quaternion that represents a rotation of an angle around an axis */
108 {
109  q->s = cos(theta/2.0);
110  D3DRMVectorScale(&q->v, D3DRMVectorNormalize(v), sin(theta/2.0));
111  return q;
112 }
113 
114 /* Interpolation between two quaternions */
117 {
118  D3DVALUE dot, epsilon, temp, theta, u;
119  D3DVECTOR v1, v2;
120 
121  dot = a->s * b->s + D3DRMVectorDotProduct(&a->v, &b->v);
122  epsilon = 1.0f;
123  temp = 1.0f - alpha;
124  u = alpha;
125  if (dot < 0.0)
126  {
127  epsilon = -1.0;
128  dot = -dot;
129  }
130  if( 1.0f - dot > 0.001f )
131  {
132  theta = acos(dot);
133  temp = sin(theta * temp) / sin(theta);
134  u = sin(theta * alpha) / sin(theta);
135  }
136  q->s = temp * a->s + epsilon * u * b->s;
137  D3DRMVectorScale(&v1, &a->v, temp);
138  D3DRMVectorScale(&v2, &b->v, epsilon * u);
139  D3DRMVectorAdd(&q->v, &v1, &v2);
140  return q;
141 }
142 
143 /* Add Two Vectors */
145 {
146  D3DVECTOR temp;
147 
148  temp.u1.x=s1->u1.x + s2->u1.x;
149  temp.u2.y=s1->u2.y + s2->u2.y;
150  temp.u3.z=s1->u3.z + s2->u3.z;
151 
152  *d = temp;
153  return d;
154 }
155 
156 /* Subtract Two Vectors */
158 {
159  D3DVECTOR temp;
160 
161  temp.u1.x=s1->u1.x - s2->u1.x;
162  temp.u2.y=s1->u2.y - s2->u2.y;
163  temp.u3.z=s1->u3.z - s2->u3.z;
164 
165  *d = temp;
166  return d;
167 }
168 
169 /* Cross Product of Two Vectors */
171 {
172  D3DVECTOR temp;
173 
174  temp.u1.x=s1->u2.y * s2->u3.z - s1->u3.z * s2->u2.y;
175  temp.u2.y=s1->u3.z * s2->u1.x - s1->u1.x * s2->u3.z;
176  temp.u3.z=s1->u1.x * s2->u2.y - s1->u2.y * s2->u1.x;
177 
178  *d = temp;
179  return d;
180 }
181 
182 /* Dot Product of Two vectors */
184 {
185  D3DVALUE dot_product;
186  dot_product=s1->u1.x * s2->u1.x + s1->u2.y * s2->u2.y + s1->u3.z * s2->u3.z;
187  return dot_product;
188 }
189 
190 /* Norm of a vector */
192 {
194  result=sqrt(v->u1.x * v->u1.x + v->u2.y * v->u2.y + v->u3.z * v->u3.z);
195  return result;
196 }
197 
198 /* Normalize a vector. Returns (1,0,0) if INPUT is the NULL vector. */
200 {
202  if(modulus)
203  {
205  }
206  else
207  {
208  u->u1.x=1.0;
209  u->u2.y=0.0;
210  u->u3.z=0.0;
211  }
212  return u;
213 }
214 
215 /* Returns a random unit vector */
217 {
218  d->u1.x = rand();
219  d->u2.y = rand();
220  d->u3.z = rand();
222  return d;
223 }
224 
225 /* Reflection of a vector on a surface */
227 {
228  D3DVECTOR sca, temp;
230 
231  *r = temp;
232  return r;
233 }
234 
235 /* Rotation of a vector */
237 {
238  D3DRMQUATERNION quaternion1, quaternion2, quaternion3;
239  D3DVECTOR norm;
240 
241  quaternion1.s = cos(theta * 0.5f);
242  quaternion2.s = cos(theta * 0.5f);
243  norm = *D3DRMVectorNormalize(axis);
244  D3DRMVectorScale(&quaternion1.v, &norm, sin(theta * 0.5f));
245  D3DRMVectorScale(&quaternion2.v, &norm, -sin(theta * 0.5f));
246  quaternion3.s = 0.0;
247  quaternion3.v = *v;
248  D3DRMQuaternionMultiply(&quaternion1, &quaternion1, &quaternion3);
249  D3DRMQuaternionMultiply(&quaternion1, &quaternion1, &quaternion2);
250 
251  *r = *D3DRMVectorNormalize(&quaternion1.v);
252  return r;
253 }
254 
255 /* Scale a vector */
257 {
258  D3DVECTOR temp;
259 
260  temp.u1.x=factor * s->u1.x;
261  temp.u2.y=factor * s->u2.y;
262  temp.u3.z=factor * s->u3.z;
263 
264  *d = temp;
265  return d;
266 }
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble * u
Definition: glfuncs.h:240
_STLP_DECLSPEC complex< float > _STLP_CALL sqrt(const complex< float > &)
Definition: complex.cpp:188
struct S2 s2
D3DVALUE WINAPI D3DRMVectorModulus(D3DVECTOR *v)
Definition: math.c:191
#define RGBA_GETGREEN(rgb)
Definition: d3dtypes.h:59
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:6102
_Tp _STLP_CALL norm(const complex< _Tp > &__z)
Definition: _complex.h:741
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint factor
Definition: glfuncs.h:178
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
D3DVECTOR *WINAPI D3DRMVectorNormalize(D3DVECTOR *u)
Definition: math.c:199
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
D3DVECTOR *WINAPI D3DRMVectorReflect(D3DVECTOR *r, D3DVECTOR *ray, D3DVECTOR *norm)
Definition: math.c:226
D3DVECTOR *WINAPI D3DRMVectorAdd(D3DVECTOR *d, D3DVECTOR *s1, D3DVECTOR *s2)
Definition: math.c:144
D3DVALUE WINAPI D3DRMColorGetRed(D3DCOLOR color)
Definition: math.c:59
const GLfloat * m
Definition: glext.h:10848
valarray< _Tp > acos(const valarray< _Tp > &__x)
Definition: _valarray.h:901
GLuint color
Definition: glext.h:6243
_Check_return_ int __cdecl rand(void)
Definition: rand.c:10
D3DVECTOR *WINAPI D3DRMVectorRandom(D3DVECTOR *d)
Definition: math.c:216
GLdouble GLdouble z
Definition: glext.h:5874
float D3DVALUE
Definition: d3dtypes.h:89
_STLP_DECLSPEC complex< float > _STLP_CALL cos(const complex< float > &)
D3DVECTOR *WINAPI D3DRMVectorRotate(D3DVECTOR *r, D3DVECTOR *v, D3DVECTOR *axis, D3DVALUE theta)
Definition: math.c:236
D3DVALUE s
Definition: d3drmdef.h:42
void WINAPI D3DRMMatrixFromQuaternion(D3DRMMATRIX4D m, D3DRMQUATERNION *q)
Definition: math.c:81
D3DRMQUATERNION *WINAPI D3DRMQuaternionSlerp(D3DRMQUATERNION *q, D3DRMQUATERNION *a, D3DRMQUATERNION *b, D3DVALUE alpha)
Definition: math.c:115
D3DVECTOR *WINAPI D3DRMVectorScale(D3DVECTOR *d, D3DVECTOR *s, D3DVALUE factor)
Definition: math.c:256
GLclampf GLclampf blue
Definition: gl.h:1740
GLfloat f
Definition: glext.h:7540
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
D3DVECTOR *WINAPI D3DRMVectorSubtract(D3DVECTOR *d, D3DVECTOR *s1, D3DVECTOR *s2)
Definition: math.c:157
#define d
Definition: ke_i.h:81
D3DVALUE WINAPI D3DRMColorGetGreen(D3DCOLOR color)
Definition: math.c:53
struct S1 s1
#define WINAPI
Definition: msvc.h:8
GLclampf green
Definition: gl.h:1740
GLdouble GLdouble GLdouble GLdouble q
Definition: gl.h:2063
#define red
Definition: linetest.c:67
D3DRMQUATERNION *WINAPI D3DRMQuaternionMultiply(D3DRMQUATERNION *q, D3DRMQUATERNION *a, D3DRMQUATERNION *b)
Definition: math.c:65
D3DVALUE WINAPI D3DRMColorGetBlue(D3DCOLOR color)
Definition: math.c:47
D3DVALUE D3DRMMATRIX4D[4][4]
Definition: d3drmdef.h:39
GLdouble s
Definition: gl.h:2039
D3DVALUE WINAPI D3DRMColorGetAlpha(D3DCOLOR color)
Definition: math.c:41
D3DVALUE WINAPI D3DRMVectorDotProduct(D3DVECTOR *s1, D3DVECTOR *s2)
Definition: math.c:183
GLfloat GLfloat GLfloat v2
Definition: glext.h:6063
#define RGBA_GETBLUE(rgb)
Definition: d3dtypes.h:60
const GLdouble * v
Definition: gl.h:2040
static calc_node_t temp
Definition: rpn_ieee.c:38
static void d3drm_set_color(D3DCOLOR *color, float r, float g, float b, float a)
D3DRMQUATERNION *WINAPI D3DRMQuaternionFromRotation(D3DRMQUATERNION *q, D3DVECTOR *v, D3DVALUE theta)
Definition: math.c:107
GLint GLint GLint GLint GLint GLint y
Definition: gl.h:1548
#define RGBA_GETALPHA(rgb)
Definition: d3dtypes.h:57
D3DCOLOR WINAPI D3DRMCreateColorRGBA(D3DVALUE red, D3DVALUE green, D3DVALUE blue, D3DVALUE alpha)
Definition: math.c:31
GLclampf GLclampf GLclampf alpha
Definition: gl.h:1740
D3DVECTOR v
Definition: d3drmdef.h:43
_STLP_DECLSPEC complex< float > _STLP_CALL sin(const complex< float > &)
GLboolean GLboolean GLboolean GLboolean a
Definition: glext.h:6204
D3DCOLOR WINAPI D3DRMCreateColorRGB(D3DVALUE red, D3DVALUE green, D3DVALUE blue)
Definition: math.c:26
GLuint64EXT * result
Definition: glext.h:11304
GLfloat GLfloat v1
Definition: glext.h:6062
#define RGBA_GETRED(rgb)
Definition: d3dtypes.h:58
D3DVECTOR *WINAPI D3DRMVectorCrossProduct(D3DVECTOR *d, D3DVECTOR *s1, D3DVECTOR *s2)
Definition: math.c:170