1 /* 2 Copyright (c) 2005-2020 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 * intersect.cpp - This file contains code for CSG and intersection routines. 48 */ 49 50 #include "machine.hpp" 51 #include "types.hpp" 52 #include "intersect.hpp" 53 #include "light.hpp" 54 #include "util.hpp" 55 #include "global.hpp" 56 57 unsigned int new_objectid(void) { 58 return numobjects++; /* global used to generate unique object ID's */ 59 } 60 61 unsigned int max_objectid(void) { 62 return numobjects; 63 } 64 65 void add_object(object *obj) { 66 object *objtemp; 67 68 if (obj == nullptr) 69 return; 70 71 obj->id = new_objectid(); 72 73 objtemp = rootobj; 74 rootobj = obj; 75 obj->nextobj = objtemp; 76 } 77 78 void free_objects(object *start) { 79 object *cur; 80 object *cur2; 81 82 cur = start; 83 while (cur->nextobj != nullptr) { 84 cur2 = (object *)cur->nextobj; 85 cur->methods->free(cur); 86 cur = cur2; 87 } 88 free(cur); 89 } 90 91 void reset_object(void) { 92 if (rootobj != nullptr) 93 free_objects(rootobj); 94 95 rootobj = nullptr; 96 numobjects = 0; /* set number of objects back to 0 */ 97 } 98 99 void intersect_objects(ray *intray) { 100 object *cur; 101 object temp; 102 103 temp.nextobj = rootobj; /* setup the initial object pointers.. */ 104 cur = &temp; /* ready, set */ 105 106 while ((cur = (object *)cur->nextobj) != nullptr) 107 cur->methods->intersect(cur, intray); 108 } 109 110 void reset_intersection(intersectstruct *intstruct) { 111 intstruct->num = 0; 112 intstruct->list[0].t = FHUGE; 113 intstruct->list[0].obj = nullptr; 114 intstruct->list[1].t = FHUGE; 115 intstruct->list[1].obj = nullptr; 116 } 117 118 void add_intersection(flt t, object *obj, ray *ry) { 119 intersectstruct *intstruct = ry->intstruct; 120 121 if (t > EPSILON) { 122 /* if we hit something before maxdist update maxdist */ 123 if (t < ry->maxdist) { 124 ry->maxdist = t; 125 126 /* if we hit *anything* before maxdist, and we're firing a */ 127 /* shadow ray, then we are finished ray tracing the shadow */ 128 if (ry->flags & RT_RAY_SHADOW) 129 ry->flags |= RT_RAY_FINISHED; 130 } 131 132 intstruct->num++; 133 intstruct->list[intstruct->num].obj = obj; 134 intstruct->list[intstruct->num].t = t; 135 } 136 } 137 138 int closest_intersection(flt *t, object **obj, intersectstruct *intstruct) { 139 int i; 140 *t = FHUGE; 141 142 for (i = 1; i <= intstruct->num; i++) { 143 if (intstruct->list[i].t < *t) { 144 *t = intstruct->list[i].t; 145 *obj = intstruct->list[i].obj; 146 } 147 } 148 149 return intstruct->num; 150 } 151 152 int shadow_intersection(intersectstruct *intstruct, flt maxdist) { 153 int i; 154 155 if (intstruct->num > 0) { 156 for (i = 1; i <= intstruct->num; i++) { 157 if ((intstruct->list[i].t < maxdist) && 158 (intstruct->list[i].obj->tex->shadowcast == 1)) { 159 return 1; 160 } 161 } 162 } 163 164 return 0; 165 } 166