1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
|
/**
** sipp - SImple Polygon Processor
**
** A general 3d graphic package
**
** Copyright Equivalent Software HB 1992
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 1, or any later version.
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
** You can receive a copy of the GNU General Public License from the
** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**/
/**
** lightsource.c - Functions that handles lightsources.
**/
#include <stdio.h>
#include <math.h>
#include <values.h>
#ifndef MAXFLOAT
# define MAXFLOAT ((float)3.40282346638528860e+38)
#endif
#include <sipp.h>
#include <lightsource.h>
#include <smalloc.h>
Lightsource *lightsrc_stack; /* Stack of installed lightsources. */
extern int depthmap_size;
static int rand_index = 0;
static double rand_no[256];
static double
shadow_sample _ANSI_ARGS_((Shadow_info *sh,
Vector *pos));
/*
* Create a new lightsource in the scene.
*/
Lightsource *
lightsource_create(x, y, z, red, grn, blu, type)
double x, y, z;
double red, grn, blu;
int type;
{
Lightsource *lp;
Dir_light_info *ip;
if (type != LIGHT_DIRECTION && type != LIGHT_POINT) {
return NULL;
}
lp = (Lightsource *)scalloc(1, sizeof(Lightsource));
ip = (Dir_light_info *)scalloc(1, sizeof(Dir_light_info));
MakeVector(ip->dir, x, y, z);
if (type == LIGHT_DIRECTION) {
vecnorm(&ip->dir);
}
lp->info = (void *)ip;
lp->type = type;
lp->color.red = red;
lp->color.grn = grn;
lp->color.blu = blu;
lp->active = TRUE;
lp->next = lightsrc_stack;
lp->shadow.active = FALSE;
lightsrc_stack = lp;
return lp;
}
/*
* Change the position of a lightsource. If it is a directional
* lightsource it's direction is changed.
*/
void
lightsource_put(lp, x, y, z)
Lightsource *lp;
double x, y, z;
{
if (lp->type != LIGHT_DIRECTION && lp->type != LIGHT_POINT) {
return;
}
MakeVector(((Dir_light_info *)(lp->info))->dir, x, y, z);
if (lp->type == LIGHT_DIRECTION) {
vecnorm(&((Dir_light_info *)(lp->info))->dir);
}
}
/*
* Define a new spotlight in the scene.
*/
Lightsource *
spotlight_create(x, y, z, to_x, to_y, to_z, fov, red, grn, blu, type, shadows)
double x, y, z;
double to_x, to_y, to_z;
double fov;
double red, grn, blu;
int type;
bool shadows;
{
Lightsource *lp;
Spot_light_info *sp;
Vector tmp;
if (type != SPOT_SHARP && type != SPOT_SOFT) {
return NULL;
}
lp = (Lightsource *)scalloc(1, sizeof(Lightsource));
sp = (Spot_light_info *)scalloc(1, sizeof(Spot_light_info));
MakeVector(sp->pos, x, y, z);
MakeVector(sp->point, to_x, to_y, to_z);
VecSub(sp->dir, sp->point, sp->pos);
vecnorm(&sp->dir);
sp->cos_fov = cos(fov * M_PI / 180.0 * 0.5);
lp->info = (void *)sp;
lp->shadow.fov_factor = tan(fov * M_PI / 180.0 * 0.5);
lp->shadow.active = shadows;
lp->shadow.d_map = NULL;
lp->type = type;
lp->color.red = red;
lp->color.grn = grn;
lp->color.blu = blu;
lp->active = TRUE;
lp->next = lightsrc_stack;
lightsrc_stack = lp;
return lp;
}
/*
* Remove the lightsource LIGHT from the list of lights and free all
* resources attached to it.
*/
void
light_destruct(light)
Lightsource *light;
{
Lightsource * lght;
/* First, we must remove the light from the list of active lights. */
while (lightsrc_stack == light)
lightsrc_stack = lightsrc_stack->next;
lght = lightsrc_stack;
while (lght != NULL) {
if (lght->next == light)
lght->next = lght->next->next;
else
lght = lght->next;
}
sfree(light->info);
sfree(light);
}
/*
* Change the position of a spotlight.
*/
void
spotlight_pos(lp, x, y, z)
Lightsource *lp;
double x, y, z;
{
Spot_light_info *sp;
if (lp->type != SPOT_SOFT && lp->type != SPOT_SHARP) {
return;
}
sp = (Spot_light_info *)(lp->info);
MakeVector(sp->pos, x, y, z);
VecSub(sp->dir, sp->point, sp->pos);
vecnorm(&sp->dir);
}
/*
* Change the point a spotlight is shining at.
*/
void
spotlight_at(lp, x, y, z)
Lightsource *lp;
double x, y, z;
{
Spot_light_info *sp;
if (lp->type != SPOT_SOFT && lp->type != SPOT_SHARP) {
return;
}
sp = (Spot_light_info *)(lp->info);
MakeVector(sp->point, x, y, z);
VecSub(sp->dir, sp->point, sp->pos);
vecnorm(&sp->dir);
}
/*
* Change the opening angle of a spotlight.
*/
void
spotlight_opening(lp, fov)
Lightsource *lp;
double fov;
{
if (lp->type != SPOT_SOFT && lp->type != SPOT_SHARP) {
return;
}
((Spot_light_info *)(lp->info))->cos_fov = cos(fov * M_PI / 180.0 * 0.5);
}
/*
* Turn on or off shadow generation from a spotlight.
*/
void
spotlight_shadows(lp, flag)
Lightsource *lp;
bool flag;
{
if (lp->type != SPOT_SOFT && lp->type != SPOT_SHARP) {
return;
}
lp->shadow.active = flag;
}
/*
* Set the color of the light from a lightsource or a spotlight.
*/
void light_color(lp, red, grn, blu)
Lightsource *lp;
double red, grn, blu;
{
lp->color.red = red;
lp->color.grn = grn;
lp->color.blu = blu;
}
/*
* Turn a lightsource or spotlight on or off.
*/
void
light_active(lp, flag)
Lightsource *lp;
bool flag;
{
lp->active = flag;
}
/*
* Evaluate how much light from the lightsource LP thas is falling
* on the point POS. Type of lightsource and shadows are taken into
* account.
* In VEC we return a vector pointing from POS to LP.
*/
double
light_eval(lp, pos, vec)
Lightsource *lp;
Vector *pos;
Vector *vec;
{
double fov_factor;
switch (lp->type) {
case LIGHT_DIRECTION:
VecCopy(*vec, ((Dir_light_info *)(lp->info))->dir);
return 1.0;
case LIGHT_POINT:
VecSub(*vec, ((Dir_light_info *)(lp->info))->dir, *pos);
vecnorm(vec);
return 1.0;
case SPOT_SOFT:
VecSub(*vec, *pos, ((Spot_light_info *)(lp->info))->pos);
vecnorm(vec);
fov_factor = VecDot(*vec, ((Spot_light_info *)(lp->info))->dir);
if (fov_factor <= 0.0
|| fov_factor < ((Spot_light_info *)(lp->info))->cos_fov)
{
VecNegate(*vec);
return 0.0;
} else {
fov_factor = (cos((1.0 - fov_factor) * M_PI
/ (1.0 - ((Spot_light_info *)
(lp->info))->cos_fov))
* 0.5 + 0.5);
if (lp->shadow.active && lp->shadow.d_map) {
VecNegate(*vec);
return fov_factor * shadow_sample(&lp->shadow, pos);
} else {
VecNegate(*vec);
return fov_factor;
}
}
case SPOT_SHARP:
VecSub(*vec, *pos, ((Spot_light_info *)(lp->info))->pos);
vecnorm(vec);
if (VecDot(*vec, ((Spot_light_info *)(lp->info))->dir)
< ((Spot_light_info *)(lp->info))->cos_fov)
{
VecNegate(*vec);
return 0.0;
} else if (lp->shadow.active && lp->shadow.d_map) {
VecNegate(*vec);
return shadow_sample(&lp->shadow, pos);
} else {
VecNegate(*vec);
return 1.0;
}
}
}
/*
* Sample a depth map and do percentage closer filtering to
* see how much shadowed POS is.
*/
#define BOXRES 0.002
static double
shadow_sample(sh, pos)
Shadow_info *sh;
Vector *pos;
{
Vector lp_view;
int lit;
int ns;
int x, y;
int i, j;
double lu, hu;
double lv, hv;
double xmin, ymin;
double boxres;
double ds, js;
double persp;
point_transform(&lp_view, pos, &sh->matrix);
persp = lp_view.z * sh->fov_factor;
lp_view.x = (lp_view.x * depthmap_size * 0.5 / persp
+ depthmap_size * 0.5);
lp_view.y = (lp_view.y * depthmap_size * 0.5 / persp
+ depthmap_size * 0.5);
boxres = BOXRES * depthmap_size;
lu = floor(lp_view.x - boxres);
lv = floor(lp_view.y - boxres);
hu = ceil(lp_view.x + boxres);
hv = ceil(lp_view.y + boxres);
if (lu >= depthmap_size || hu < 0.0
|| lv >= depthmap_size || hv < 0.0)
{
return 1.0;
}
ns = (int)(boxres * 2.0 + 0.5);
ds = 2.0 * boxres / ns;
js = ds * 0.5;
xmin = lp_view.x - boxres + js;
ymin = lp_view.y - boxres + js;
lit = ns * ns;
for (i = 0, lp_view.x = xmin; i < ns; i++, lp_view.x += ds) {
for (j = 0, lp_view.y = ymin; j < ns; j++, lp_view.y += ds) {
rand_index = (rand_index + 1) & 255;
x = lp_view.x + rand_no[rand_index] * js;
rand_index = (rand_index + 1) & 255;
y = lp_view.y + rand_no[rand_index] * js;
if (x >= 0 && x < depthmap_size
&& y >= 0 && y < depthmap_size) {
if (lp_view.z > (sh->d_map[(depthmap_size - 1 - y)
* depthmap_size + x]
+ sh->bias)) {
lit--;
}
}
}
}
return (double)lit / (double)(ns * ns);
}
/*
* Create and initialize depthmaps for all lightsources
* that have their shadow generation activated.
*/
void
depthmaps_create()
{
Lightsource *lp;
int i;
for (i = 0; i < 256; i++) {
rand_no[i] = (RANDOM() + 1.0) * 0.5;
}
rand_index = 0;
for (lp = lightsrc_stack; lp != NULL; lp = lp->next) {
if (lp->shadow.active) {
if (NULL != lp->shadow.d_map) {
sfree(lp->shadow.d_map);
}
lp->shadow.d_map = (float *)smalloc(depthmap_size * depthmap_size
* sizeof(float));
for (i = 0; i < depthmap_size * depthmap_size; i++) {
lp->shadow.d_map[i] = MAXFLOAT;
}
}
}
}
/*
* Release the memory used by the depthmaps.
*/
void
depthmaps_destruct()
{
Lightsource *lp;
for (lp = lightsrc_stack; lp != NULL; lp = lp->next) {
if (lp->shadow.active && lp->shadow.d_map != NULL) {
sfree(lp->shadow.d_map);
lp->shadow.d_map = NULL;
}
}
}
|