/****************************************************************************** * Copyright 1986-2004 by mental images GmbH, Fasanenstr. 81, D-10623 Berlin, * Germany. All rights reserved. ****************************************************************************** * Created: 23.07.04 * Module: baseshader * Purpose: base shaders for Phenomenon writers * * Exports: * mib_amb_occlusion * mib_amb_occlusion_version * * History: * 23.07.04: copied from misss_simple_occlusion * * Description: * A simple ambient occlusion shader. * *****************************************************************************/ #ifdef HPUX #pragma OPT_LEVEL 1 /* workaround for HP/UX optimizer bug, +O2 and +O3 */ #endif #include #include /* for abs */ #include /* for FLT_MAX */ #include #include #include #include "shader.h" /* Simple ambient occlusion */ /* Simple ambient occlusion parameter struct */ struct mib_amb_occlusion_p { int samples; miColor bright; miColor dark; miScalar spread; miScalar max_distance; miBoolean reflective; int return_type; miBoolean occlusion_in_alpha; }; DLLEXPORT int mib_amb_occlusion_version(void) {return(1);} DLLEXPORT miBoolean mib_amb_occlusion( miColor *result, miState *state, struct mib_amb_occlusion_p *paras) { double sample[3], near_clip, far_clip; int counter = 0; miUint samples = *mi_eval_integer(¶s->samples); miScalar clipdist = *mi_eval_scalar(¶s->max_distance); miBoolean reflecto = *mi_eval_boolean(¶s->reflective); miBoolean ret_type = *mi_eval_integer(¶s->return_type); miVector orig_normal, trace_dir; miScalar output = 0.0, samplesdone = 0.0; miScalar spread = *mi_eval_scalar(¶s->spread) * 2.0; miColor env_total; /* environment Total */ miVector norm_total; /* Used for adding up normals */ miBoolean occ_alpha = *mi_eval_boolean(¶s->occlusion_in_alpha); /* If called as user area light source, return "no more samples" for any call beyond the first */ if (state->type == miRAY_LIGHT && state->count > 0) return (miBoolean)2; /* Used for adding up environment */ env_total.r = env_total.g = env_total.b = env_total.a = 0; far_clip = near_clip = clipdist; orig_normal = state->normal; norm_total = state->normal; /* Begin by standard normal */ /* Displacement? Makes no sense */ if (state->type == miRAY_DISPLACE) { result->r = result->g = result->b = result->a = 0.0; return (miTRUE); } if (clipdist > 0.0) mi_ray_falloff(state, &near_clip, &far_clip); while (mi_sample(sample, &counter, state, 3, &samples)) { trace_dir.x = orig_normal.x + (sample[0] - 0.5) * spread; trace_dir.y = orig_normal.y + (sample[1] - 0.5) * spread; trace_dir.z = orig_normal.z + (sample[2] - 0.5) * spread; mi_vector_normalize(&trace_dir); if (reflecto) { miVector ref; /* Calculate the reflection direction */ state->normal = trace_dir; mi_reflection_dir(&ref, state); state->normal = orig_normal; trace_dir = ref; } if (mi_vector_dot(&trace_dir, &state->normal_geom) < 0.0) continue; output += 1.0; /* Add one */ samplesdone += 1.0; if (state->options->shadow && mi_trace_probe(state, &trace_dir, &state->point)) { /* we hit something */ if (clipdist == 0.0) output -= 1.0; else if (state->child->dist < clipdist) { miScalar f = state->child->dist / clipdist; output -= (1.0 - f); norm_total.x += trace_dir.x * f; norm_total.y += trace_dir.y * f; norm_total.z += trace_dir.z * f; switch (ret_type) { case 1: { /* Environment sampling */ miColor envsample; mi_trace_environment(&envsample, state, &trace_dir); env_total.r += envsample.r * f; env_total.g += envsample.g * f; env_total.b += envsample.b * f; } break; default: /* Most return types need no special stuff */ break; } } } else { /* We hit nothing */ norm_total.x += trace_dir.x; norm_total.y += trace_dir.y; norm_total.z += trace_dir.z; switch (ret_type) { case 1: /* Environment sampling */ { miColor envsample; mi_trace_environment(&envsample, state, &trace_dir); env_total.r += envsample.r; env_total.g += envsample.g; env_total.b += envsample.b; } break; default: /* Most return types need no special treatment */ break; } } } if (clipdist > 0.0) mi_ray_falloff(state, &near_clip, &far_clip); if (samplesdone <= 0.0) /* No samples? */ samplesdone = 1.0; /* 1.0 to not to break divisons below */ switch (ret_type) { case -1: /* Plain old occlusion with untouched normal*/ case 0: /* Plain old occlusion */ default: /* (also the default for out-of-bounds values) */ { miVector old_dir = state->dir; output /= (miScalar) samplesdone; if (ret_type == -1) norm_total = state->normal; else { mi_vector_normalize(&norm_total); /* If the color shaders use the normal.... give them the bent one... */ state->normal = norm_total; state->dir = norm_total; } if (output == 0.0) *result = *mi_eval_color(¶s->dark); else if (output >= 1.0) *result = *mi_eval_color(¶s->bright); else { miColor bright, dark; bright = *mi_eval_color(¶s->bright); dark = *mi_eval_color(¶s->dark); result->r = bright.r * output + dark.r * (1.0 - output); result->g = bright.g * output + dark.g * (1.0 - output); result->b = bright.b * output + dark.b * (1.0 - output); if (occ_alpha) result->a = output; else result->a = bright.a * output + dark.a * (1.0 - output); } state->normal = orig_normal; state->dir = old_dir; } break; case 1: /* Sampled environment */ { miColor br = *mi_eval_color(¶s->bright), drk = *mi_eval_color(¶s->dark); result->r = drk.r + (br.r * env_total.r / samplesdone); result->g = drk.g + (br.g * env_total.g / samplesdone); result->b = drk.b + (br.b * env_total.b / samplesdone); if (occ_alpha) result->a = output/ samplesdone; else result->a = 1.0; } break; case 2: /* Bent normals */ case 3: /* Bent normals */ { miVector retn; /* returned Normal */ mi_vector_normalize(&norm_total); if (ret_type == 2) mi_vector_to_world(state, &retn, &norm_total); if (ret_type == 3) mi_vector_to_camera(state, &retn, &norm_total); result->r = (retn.x + 1.0) / 2.0; result->g = (retn.y + 1.0) / 2.0; result->b = (retn.z + 1.0) / 2.0; if (occ_alpha) result->a = output/ samplesdone; else result->a = 1.0; } break; } if (state->type == miRAY_LIGHT) { /* Are we a light shader? */ int type; mi_query(miQ_FUNC_CALLTYPE, state, 0, &type); /* Make sure we are called as light shader */ if (type == miSHADER_LIGHT) { /* If so, move ourselves to above the point... */ state->org.x = state->point.x + state->normal.x; state->org.y = state->point.y + state->normal.y; state->org.z = state->point.z + state->normal.z; /* ...and set dot_nd to 1.0 to illuminate fully */ state->dot_nd = 1.0; } } return miTRUE; }