Shader hair_color_fire

Click on the filename to display or download the file.

hair_color_fire.mi
declare shader 
    color "hair_color_fire" ( 
        color "transparency" default 0.5 0.5 0.5 ) 
    version 1 
    apply material 
end declare 

hair_color_fire.c
#include "shader.h" 
#include "miaux.h" 
 
struct hair_color_fire { 
    miColor transparency; 
}; 
 
DLLEXPORT 
int hair_color_fire_version(void) { return 1; } 
 
DLLEXPORT 
miBoolean hair_color_fire ( 
    miColor *result, miState *state, struct hair_color_fire *params  ) 
{ 
    miScalar keys[6] = {0.0, .2, .4, .6, .9, 1.0}; 
    miColor colors[6] = {{1.2, 1.2, .7},  
                         {1, 1, .5}, 
                         {1, .5, 0}, 
                         {1, .2, 0}, 
                         {.4, .1, 0}, 
                         {0, 0, 0}}; 
    miColor *transparency = mi_eval_color(&params->transparency); 
    miScalar age = state->tex_list[0].x; 
    int i; 
    for (i = 4; i >=0; i--) { 
        if (age >= keys[i]) { 
            miaux_color_fit(result, age,  
                            keys[i], keys[i+1], &colors[i], &colors[i+1]); 
            break; 
        } 
    } 
    miaux_blend_transparency(result, state, transparency); 
 
    return miTRUE; 
} 

hair_color_fire_util.c
void miaux_color_fit(miColor *result,  
		     miScalar f, miScalar start, miScalar end, 
		     miColor *start_color, miColor *end_color) 
{ 
    result->r = miaux_fit(f, start, end, start_color->r, end_color->r); 
    result->g = miaux_fit(f, start, end, start_color->g, end_color->g); 
    result->b = miaux_fit(f, start, end, start_color->b, end_color->b); 
} 
 
double miaux_fit( 
    double v, double oldmin, double oldmax, double newmin, double newmax)     
{ 
    return newmin + ((v - oldmin) / (oldmax - oldmin)) * (newmax - newmin); 
} 
 
void miaux_blend_transparency(miColor *result,  
			      miState *state, miColor *transparency) 
{ 
    miColor opacity, background; 
    miaux_invert_channels(&opacity, transparency); 
    mi_opacity_set(state, &opacity); 
    if (!miaux_all_channels_equal(transparency, 1.0)) { 
        mi_trace_transparent(&background, state); 
        miaux_blend_channels(result, &background, &opacity); 
    } 
} 
 
void miaux_invert_channels(miColor *result, miColor *color) 
{ 
    result->r = 1.0 - color->r; 
    result->g = 1.0 - color->g; 
    result->b = 1.0 - color->b; 
    result->a = 1.0 - color->a; 
} 
 
void miaux_blend_channels(miColor *result, 
			  miColor *blend_color, miColor *blend_fraction) 
{ 
    result->r = miaux_blend(result->r, blend_color->r, blend_fraction->r); 
    result->g = miaux_blend(result->g, blend_color->g, blend_fraction->g); 
    result->b = miaux_blend(result->b, blend_color->b, blend_fraction->b); 
} 
 
double miaux_blend(miScalar a, miScalar b, miScalar factor) 
{ 
    return a * factor + b * (1.0 - factor); 
} 

22 April 2008 23:40:46