Thursday, March 31, 2011

Writing mental ray shaders: Color Balance

Goal
A simple shader that combines the gain and offset shader

Methodology
1. create a color parameter called base
2. create a color parameter called gain
3. create a color parameter called offset
4. create a shader called "color_gain"
5. create a shader called "color_offset"
6. create a function where result = base * gain
7. create a function where result = result + offset

Mi File
declare shader
 color "color_balance" (
  color "base"  default 1 1 1,
  color "gain" default 1 1 1,
  color "offset" default 0 0 0,
 )
end declare

C File

#include "shader.h"

DLLEXPORT

struct color_balance {
 miColor base;
 miColor gain;
 miColor offset;
 };

DLLEXPORT

miBoolean color_balance (miColor *result, miState *state, struct color_balance *params) {
 miColor *base = mi_eval_color(&params->base);
 miColor *gain = mi_eval_color(&params->gain);
 miColor *offset = mi_eval_color(&params->offset);
 result->r = base->r * gain->r;
 result->g = base->g * gain->g;
 result->b = base->b * gain->b;
 result->r += offset->r;
 result->g += offset->g;
 result->b += offset->b;
 return miTRUE;
 }
 

0 comments:

Post a Comment