| 1 |
/* |
| 2 |
Copyright (c) 2005-2021 Intel Corporation |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
/* |
| 18 |
The original source for this example is |
| 19 |
Copyright (c) 1994-2008 John E. Stone |
| 20 |
All rights reserved. |
| 21 |
|
| 22 |
Redistribution and use in source and binary forms, with or without |
| 23 |
modification, are permitted provided that the following conditions |
| 24 |
are met: |
| 25 |
1. Redistributions of source code must retain the above copyright |
| 26 |
notice, this list of conditions and the following disclaimer. |
| 27 |
2. Redistributions in binary form must reproduce the above copyright |
| 28 |
notice, this list of conditions and the following disclaimer in the |
| 29 |
documentation and/or other materials provided with the distribution. |
| 30 |
3. The name of the author may not be used to endorse or promote products |
| 31 |
derived from this software without specific prior written permission. |
| 32 |
|
| 33 |
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
| 34 |
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 35 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 36 |
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 37 |
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 38 |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 39 |
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 40 |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 41 |
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 42 |
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 43 |
SUCH DAMAGE. |
| 44 |
*/ |
| 45 |
|
| 46 |
/* |
| 47 |
* vector.cpp - This file contains all of the vector arithmetic functions. |
| 48 |
*/ |
| 49 |
|
| 50 |
#include "machine.hpp" |
| 51 |
#include "types.hpp" |
| 52 |
#include "macros.hpp" |
| 53 |
|
| 54 |
flt VDot(vector *a, vector *b) { |
| 55 |
return (a->x * b->x + a->y * b->y + a->z * b->z); |
| 56 |
} |
| 57 |
|
| 58 |
void VCross(vector *a, vector *b, vector *c) { |
| 59 |
c->x = (a->y * b->z) - (a->z * b->y); |
| 60 |
c->y = (a->z * b->x) - (a->x * b->z); |
| 61 |
c->z = (a->x * b->y) - (a->y * b->x); |
| 62 |
} |
| 63 |
|
| 64 |
flt VLength(vector *a) { |
| 65 |
return (flt)sqrt((a->x * a->x) + (a->y * a->y) + (a->z * a->z)); |
| 66 |
} |
| 67 |
|
| 68 |
void VNorm(vector *a) { |
| 69 |
flt len; |
| 70 |
|
| 71 |
len = sqrt((a->x * a->x) + (a->y * a->y) + (a->z * a->z)); |
| 72 |
if (len != 0.0) { |
| 73 |
a->x /= len; |
| 74 |
a->y /= len; |
| 75 |
a->z /= len; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
void VAdd(vector *a, vector *b, vector *c) { |
| 80 |
c->x = (a->x + b->x); |
| 81 |
c->y = (a->y + b->y); |
| 82 |
c->z = (a->z + b->z); |
| 83 |
} |
| 84 |
|
| 85 |
void VSub(vector *a, vector *b, vector *c) { |
| 86 |
c->x = (a->x - b->x); |
| 87 |
c->y = (a->y - b->y); |
| 88 |
c->z = (a->z - b->z); |
| 89 |
} |
| 90 |
|
| 91 |
void VAddS(flt a, vector *A, vector *B, vector *C) { |
| 92 |
C->x = (a * A->x) + B->x; |
| 93 |
C->y = (a * A->y) + B->y; |
| 94 |
C->z = (a * A->z) + B->z; |
| 95 |
} |
| 96 |
|
| 97 |
vector Raypnt(ray *a, flt t) { |
| 98 |
vector temp; |
| 99 |
|
| 100 |
temp.x = a->o.x + (a->d.x * t); |
| 101 |
temp.y = a->o.y + (a->d.y * t); |
| 102 |
temp.z = a->o.z + (a->d.z * t); |
| 103 |
|
| 104 |
return temp; |
| 105 |
} |
| 106 |
|
| 107 |
void VScale(vector *a, flt s) { |
| 108 |
a->x *= s; |
| 109 |
a->y *= s; |
| 110 |
a->z *= s; |
| 111 |
} |
| 112 |
|
| 113 |
void ColorAddS(color *a, color *b, flt s) { |
| 114 |
a->r += b->r * s; |
| 115 |
a->g += b->g * s; |
| 116 |
a->b += b->b * s; |
| 117 |
} |
| 118 |
|
| 119 |
void ColorAccum(color *a, color *b) { |
| 120 |
a->r += b->r; |
| 121 |
a->g += b->g; |
| 122 |
a->b += b->b; |
| 123 |
} |
| 124 |
|
| 125 |
void ColorScale(color *a, flt s) { |
| 126 |
a->r *= s; |
| 127 |
a->g *= s; |
| 128 |
a->b *= s; |
| 129 |
} |