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 * box.cpp - This file contains the functions for dealing with boxes. 48 */ 49 50 #include "machine.hpp" 51 #include "types.hpp" 52 #include "macros.hpp" 53 #include "box.hpp" 54 #include "vector.hpp" 55 #include "intersect.hpp" 56 #include "util.hpp" 57 58 int box_bbox(void *obj, vector *min, vector *max) { 59 box *b = (box *)obj; 60 61 *min = b->min; 62 *max = b->max; 63 64 return 1; 65 } 66 67 static object_methods box_methods = { (void (*)(void *, void *))(box_intersect), 68 (void (*)(void *, void *, void *, void *))(box_normal), 69 box_bbox, 70 free }; 71 72 box *newbox(void *tex, vector min, vector max) { 73 box *b; 74 75 b = (box *)rt_getmem(sizeof(box)); 76 memset(b, 0, sizeof(box)); 77 b->methods = &box_methods; 78 b->tex = (texture *)tex; 79 b->min = min; 80 b->max = max; 81 82 return b; 83 } 84 85 void box_intersect(box *bx, ray *ry) { 86 flt a, tx1, tx2, ty1, ty2, tz1, tz2; 87 flt tnear, tfar; 88 89 tnear = -FHUGE; 90 tfar = FHUGE; 91 92 if (ry->d.x == 0.0) { 93 if ((ry->o.x < bx->min.x) || (ry->o.x > bx->max.x)) 94 return; 95 } 96 else { 97 tx1 = (bx->min.x - ry->o.x) / ry->d.x; 98 tx2 = (bx->max.x - ry->o.x) / ry->d.x; 99 if (tx1 > tx2) { 100 a = tx1; 101 tx1 = tx2; 102 tx2 = a; 103 } 104 if (tx1 > tnear) 105 tnear = tx1; 106 if (tx2 < tfar) 107 tfar = tx2; 108 } 109 if (tnear > tfar) 110 return; 111 if (tfar < 0.0) 112 return; 113 114 if (ry->d.y == 0.0) { 115 if ((ry->o.y < bx->min.y) || (ry->o.y > bx->max.y)) 116 return; 117 } 118 else { 119 ty1 = (bx->min.y - ry->o.y) / ry->d.y; 120 ty2 = (bx->max.y - ry->o.y) / ry->d.y; 121 if (ty1 > ty2) { 122 a = ty1; 123 ty1 = ty2; 124 ty2 = a; 125 } 126 if (ty1 > tnear) 127 tnear = ty1; 128 if (ty2 < tfar) 129 tfar = ty2; 130 } 131 if (tnear > tfar) 132 return; 133 if (tfar < 0.0) 134 return; 135 136 if (ry->d.z == 0.0) { 137 if ((ry->o.z < bx->min.z) || (ry->o.z > bx->max.z)) 138 return; 139 } 140 else { 141 tz1 = (bx->min.z - ry->o.z) / ry->d.z; 142 tz2 = (bx->max.z - ry->o.z) / ry->d.z; 143 if (tz1 > tz2) { 144 a = tz1; 145 tz1 = tz2; 146 tz2 = a; 147 } 148 if (tz1 > tnear) 149 tnear = tz1; 150 if (tz2 < tfar) 151 tfar = tz2; 152 } 153 if (tnear > tfar) 154 return; 155 if (tfar < 0.0) 156 return; 157 158 add_intersection(tnear, (object *)bx, ry); 159 add_intersection(tfar, (object *)bx, ry); 160 } 161 162 void box_normal(box *bx, vector *pnt, ray *incident, vector *N) { 163 vector a, b, c; 164 flt t; 165 166 c.x = (bx->max.x + bx->min.x) / 2.0; 167 c.y = (bx->max.y + bx->min.y) / 2.0; 168 c.z = (bx->max.z + bx->min.z) / 2.0; 169 170 VSub((vector *)pnt, &c, N); 171 b = (*N); 172 173 a.x = fabs(N->x); 174 a.y = fabs(N->y); 175 a.z = fabs(N->z); 176 177 N->x = 0.0; 178 N->y = 0.0; 179 N->z = 0.0; 180 181 t = MYMAX(a.x, MYMAX(a.y, a.z)); 182 183 if (t == a.x) 184 N->x = b.x; 185 186 if (t == a.y) 187 N->y = b.y; 188 189 if (t == a.z) 190 N->z = b.z; 191 192 VNorm(N); 193 } 194