Monday, April 11, 2011

Writing mental ray shaders: Z Depth Part II

Introduction
In this part I'll create the set range and blend functions that can be found in maya. These functions will be a set of auxilliary functions that I can call upon, much like how the set range and blend nodes work in Maya. Again, I am working from, Writing mental ray® Shaders: A Perceptual Introduction (mental ray® Handbooks). Buy the book, its worth it :)

I'll further refine the zdepth shader, incorporating the set range and blend functions.

Goal
1. Develop a Set Range function and use as a library function
2. Develop a Blender function and use as a library function
3. Apply the above functions into the zdepth shader



Before Starting
Make sure you are writing to a separate miaux.c and miaux.h file, in my case, i opened a new file called jc_miaux.c and had it compiled to an object (*.o) file. And put it in your include folder.

Library Header: jc_miaux
#include "shader.h"

#ifndef __JC_MIAUX_H__
#define __JC_MIAUX_H__

double jc_miaux_set_range(
 double v,
 double oldmin,
 double oldmax,
 double newmin,
 double newmax);

double jc_miaux_blend(
 miScalar a,
 miScalar b,
 miScalar factor);

void jc_miaux_blend_colors(
 miColor *result,
 miColor *colorA,
 miColor *colorB,
 miScalar factor);

#endif

Library C Source: jc_miaux
#include "jc_miaux.h"

double jc_miaux_set_range(
 double v,
 double oldmin,
 double oldmax,
 double newmin,
 double newmax) {
 return newmin + ((v- oldmin) / (oldmax - oldmin)) * (newmax - newmin);
 }

double jc_miaux_blend(
 miScalar a,
 miScalar b,
 miScalar factor) {
 return (a * factor) + (b * (1 - factor));
 }

void jc_miaux_blend_colors(
 miColor *result,
 miColor *colorA,
 miColor *colorB,
 miScalar factor) {
 result->r = jc_miaux_blend(colorA->r, colorB->r, factor);
 result->g = jc_miaux_blend(colorA->g, colorB->g, factor);
 result->b = jc_miaux_blend(colorA->b, colorB->b, factor);
 }

Mi Source: zdepth_ver3
declare shader
 color "zdepth_ver3" (
  scalar "near" default 0,
  scalar "far" default 1,
  color "near_color" default 1 1 1,
  color "far_color" default 0 0 0
 )
apply material
end declare

C Source: zdepth_ver3
#include "shader.h"
#include "jc_miaux.h"

struct zdepth_ver3 {
 miScalar near;
 miScalar far;
 miColor near_color;
 miColor far_color;
 };

DLLEXPORT
miBoolean zdepth_ver3(miColor *result, miState *state, struct zdepth_ver3 *params) {
 miScalar near = *mi_eval_scalar(&params->near);
 miScalar far = *mi_eval_scalar(&params->far);
 miColor *near_color = mi_eval_color(&params->near_color);
 miColor *far_color = mi_eval_color(&params->far_color);
 miScalar factor;
 miVector position;
 mi_point_to_camera(state, &position, &state->point);
 factor = jc_miaux_set_range(position.z, far, near, 0, 1);
 result->r = (near_color->r * factor) + (far_color->r * (1 - factor));
 result->g = (near_color->g * factor) + (far_color->g * (1 - factor));
 result->b = (near_color->b * factor) + (far_color->b * (1 - factor));
 result->a = 1;
 return miTRUE;
}

Notes

0 comments:

Post a Comment