| 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 |
* sphere.cpp - This file contains the functions for dealing with spheres. |
| 48 |
*/ |
| 49 |
|
| 50 |
#include "machine.hpp" |
| 51 |
#include "types.hpp" |
| 52 |
#include "macros.hpp" |
| 53 |
#include "vector.hpp" |
| 54 |
#include "intersect.hpp" |
| 55 |
#include "util.hpp" |
| 56 |
|
| 57 |
#define SPHERE_PRIVATE |
| 58 |
#include "sphere.hpp" |
| 59 |
|
| 60 |
static object_methods sphere_methods = { (void (*)(void *, void *))(sphere_intersect), |
| 61 |
(void (*)(void *, void *, void *, void *))(sphere_normal), |
| 62 |
sphere_bbox, |
| 63 |
free }; |
| 64 |
|
| 65 |
object *newsphere(void *tex, vector ctr, flt rad) { |
| 66 |
sphere *s; |
| 67 |
|
| 68 |
s = (sphere *)rt_getmem(sizeof(sphere)); |
| 69 |
memset(s, 0, sizeof(sphere)); |
| 70 |
s->methods = &sphere_methods; |
| 71 |
|
| 72 |
s->tex = (texture *)tex; |
| 73 |
s->ctr = ctr; |
| 74 |
s->rad = rad; |
| 75 |
|
| 76 |
return (object *)s; |
| 77 |
} |
| 78 |
|
| 79 |
static int sphere_bbox(void *obj, vector *min, vector *max) { |
| 80 |
sphere *s = (sphere *)obj; |
| 81 |
|
| 82 |
min->x = s->ctr.x - s->rad; |
| 83 |
min->y = s->ctr.y - s->rad; |
| 84 |
min->z = s->ctr.z - s->rad; |
| 85 |
max->x = s->ctr.x + s->rad; |
| 86 |
max->y = s->ctr.y + s->rad; |
| 87 |
max->z = s->ctr.z + s->rad; |
| 88 |
|
| 89 |
return 1; |
| 90 |
} |
| 91 |
|
| 92 |
static void sphere_intersect(sphere *spr, ray *ry) { |
| 93 |
flt b, disc, t1, t2, temp; |
| 94 |
vector V; |
| 95 |
|
| 96 |
VSUB(spr->ctr, ry->o, V); |
| 97 |
VDOT(b, V, ry->d); |
| 98 |
VDOT(temp, V, V); |
| 99 |
|
| 100 |
disc = b * b + spr->rad * spr->rad - temp; |
| 101 |
|
| 102 |
if (disc <= 0.0) |
| 103 |
return; |
| 104 |
disc = sqrt(disc); |
| 105 |
|
| 106 |
t2 = b + disc; |
| 107 |
if (t2 <= SPEPSILON) |
| 108 |
return; |
| 109 |
add_intersection(t2, (object *)spr, ry); |
| 110 |
|
| 111 |
t1 = b - disc; |
| 112 |
if (t1 > SPEPSILON) |
| 113 |
add_intersection(t1, (object *)spr, ry); |
| 114 |
} |
| 115 |
|
| 116 |
static void sphere_normal(sphere *spr, vector *pnt, ray *incident, vector *N) { |
| 117 |
VSub((vector *)pnt, &(spr->ctr), N); |
| 118 |
|
| 119 |
VNorm(N); |
| 120 |
|
| 121 |
if (VDot(N, &(incident->d)) > 0.0) { |
| 122 |
N->x = -N->x; |
| 123 |
N->y = -N->y; |
| 124 |
N->z = -N->z; |
| 125 |
} |
| 126 |
} |