| 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 |
* light.cpp - This file contains declarations and defines for light sources. |
| 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 LIGHT_PRIVATE |
| 58 |
#include "light.hpp" |
| 59 |
|
| 60 |
static object_methods light_methods = { (void (*)(void *, void *))(light_intersect), |
| 61 |
(void (*)(void *, void *, void *, void *))(light_normal), |
| 62 |
light_bbox, |
| 63 |
free }; |
| 64 |
|
| 65 |
point_light *newlight(void *tex, vector ctr, flt rad) { |
| 66 |
point_light *l; |
| 67 |
|
| 68 |
l = (point_light *)rt_getmem(sizeof(point_light)); |
| 69 |
memset(l, 0, sizeof(point_light)); |
| 70 |
l->methods = &light_methods; |
| 71 |
|
| 72 |
l->tex = (texture *)tex; |
| 73 |
l->ctr = ctr; |
| 74 |
l->rad = rad; |
| 75 |
|
| 76 |
return l; |
| 77 |
} |
| 78 |
|
| 79 |
static int light_bbox(void *obj, vector *min, vector *max) { |
| 80 |
return 0; /* lights are unbounded currently */ |
| 81 |
} |
| 82 |
|
| 83 |
static void light_intersect(point_light *l, ray *ry) { |
| 84 |
flt b, disc, t1, t2, temp; |
| 85 |
vector V; |
| 86 |
|
| 87 |
/* Lights do not cast shadows.. */ |
| 88 |
if (ry->flags & RT_RAY_SHADOW) |
| 89 |
return; |
| 90 |
|
| 91 |
VSUB(l->ctr, ry->o, V); |
| 92 |
VDOT(b, V, ry->d); |
| 93 |
VDOT(temp, V, V); |
| 94 |
|
| 95 |
disc = b * b + l->rad * l->rad - temp; |
| 96 |
|
| 97 |
if (disc <= 0.0) |
| 98 |
return; |
| 99 |
disc = sqrt(disc); |
| 100 |
|
| 101 |
t2 = b + disc; |
| 102 |
if (t2 <= SPEPSILON) |
| 103 |
return; |
| 104 |
add_intersection(t2, (object *)l, ry); |
| 105 |
|
| 106 |
t1 = b - disc; |
| 107 |
if (t1 > SPEPSILON) |
| 108 |
add_intersection(t1, (object *)l, ry); |
| 109 |
} |
| 110 |
|
| 111 |
static void light_normal(point_light *l, vector *pnt, ray *incident, vector *N) { |
| 112 |
VSub((vector *)pnt, &(l->ctr), N); |
| 113 |
|
| 114 |
VNorm(N); |
| 115 |
|
| 116 |
if (VDot(N, &(incident->d)) > 0.0) { |
| 117 |
N->x = -N->x; |
| 118 |
N->y = -N->y; |
| 119 |
N->z = -N->z; |
| 120 |
} |
| 121 |
} |