ABERDEEN SHANG
  • About
  • Real-Time VFX
  • Houdini
  • Fun Things
  • Blogs
    • Mentor Project
    • MatchtoLive
    • VSFX 755 Class Blog >
      • C: debris procedural primitive
      • C++: side-mask pattern node
      • OSL: renderman moom shading
  • Other Art Works
  • Award
/* 
 AberSideMask.cpp
 Generated by Cutter from:
     "/home/zshang20/mount/stuhome/maya/rfm_scripts/nodes/Args/AberSideMask.args"
 at 8.39:25am 4.23.2019
 Basic code for use with RPS 22.
*/
#include <RixPattern.h> 
#include <RixShadingUtils.h>
//#include <RixInterfaces.h>
//#include <RixShading.h>
#include <RixPredefinedStrings.hpp>
#include <sstream>

class AberSideMask : public RixPattern {
public:

    AberSideMask();
    virtual ~AberSideMask() { }
    virtual int Init(RixContext &ctx, RtUString pluginpath);
    virtual RixSCParamInfo const *GetParamTable();
    virtual void Synchronize(RixContext &ctx, RixSCSyncMsg sync_msg,
                            RixParameterList const *plist) { }
    virtual int CreateInstanceData(RixContext &ctx, RtUString const str,
                            RixParameterList const *plist, InstanceData *data)
    {
        return -1;
    }
    virtual void Finalize(RixContext &) { }
    virtual int ComputeOutputParams(RixShadingContext const *ctx,
                                    RtInt *noutputs, 
                                    OutputSpec **outputs,
                                    RtPointer instanceData,
                                    RixSCParamInfo const *ignored);
    virtual bool Bake2dOutput(RixBakeContext const *ctx, Bake2dSpec &spec, RtPointer ptr) { return false; }
    virtual bool Bake3dOutput(RixBakeContext const *ctx, Bake3dSpec &spec, RtPointer ptr) { return false; }
private:
RixMessages *m_msg;
RixShadeFunctions *m_shd;  // Shading functions in RixInterfaces.h
RtColorRGB const m_front_color;
RtColorRGB const m_rear_color;
RtFloat const m_blur;
RtInt const m_swap_colors;
};

AberSideMask::AberSideMask():
m_msg(NULL),
m_shd(NULL),
m_front_color(1,0,0),
m_rear_color(1,1,1),
m_blur(0.05),
m_swap_colors(0)
{ }

int AberSideMask::Init(RixContext &ctx, RtUString const pluginpath) {
    m_msg = (RixMessages*)ctx.GetRixInterface(k_RixMessages);
    m_shd = (RixShadeFunctions*)ctx.GetRixInterface(k_RixShadeFunctions);

    // Uncomment the next three lines if a rib Option will be queried.
    //RixRenderState *rstate = (RixRenderState*)ctx.GetRixInterface(k_RixRenderState);
    //RixRenderState::Type optType;
    //RtInt optNumValues, err;
    // Example of using messaging,
    //    m_msg->Info("%f\n", a_float_value);
    return (!m_msg) ? 1 : 0;
    }

RixSCParamInfo const *AberSideMask::GetParamTable() {
    static RixSCParamInfo s_ptable[] = {
// Outputs
RixSCParamInfo(RtUString("resultRGB"), k_RixSCColor, k_RixSCOutput),
RixSCParamInfo(RtUString("resultF"), k_RixSCFloat, k_RixSCOutput),
// Inputs
RixSCParamInfo(RtUString("front_color"), k_RixSCColor),
RixSCParamInfo(RtUString("rear_color"), k_RixSCColor),
RixSCParamInfo(RtUString("blur"), k_RixSCFloat),
RixSCParamInfo(RtUString("swap_colors"), k_RixSCInteger),
RixSCParamInfo() // end of table
        };
    return &s_ptable[0];
    }

enum paramIndex {
k_resultRGB = 0,
k_resultF,
k_front_color,
k_rear_color,
k_blur,
k_swap_colors
    };
int AberSideMask::ComputeOutputParams(RixShadingContext const *ctx,
RtInt *noutputs, 
OutputSpec **outputs,
RtPointer instanceData,
RixSCParamInfo const *ignored) {

// Uncomment the next three lines if a rib Attribute will be queried. Note
// that Rib Options should be queried in the init() method - not here!
//RixRenderState *rstate = (RixRenderState*)ctx->GetRixInterface(k_RixRenderState);
//RixRenderState::Type attrType;
//RtInt attrNumValues, err;

// OUTPUTS BEGIN____________________________________
// Allocate memory for the OutputSpec data structure.
RixShadingContext::Allocator pool(ctx);
OutputSpec *outSpec = pool.AllocForPattern<OutputSpec>(2);
*outputs = outSpec;

// Allocate memory for each output.
RtColorRGB *resultRGB = pool.AllocForPattern<RtColorRGB>(ctx->numPts);
RtFloat *resultF = pool.AllocForPattern<RtFloat>(ctx->numPts);

// Connect the output(s) to the OutputSpec.
*noutputs = 2;
outSpec[0].paramId = k_resultRGB;
outSpec[0].detail = k_RixSCVarying;
outSpec[0].value = resultRGB;
outSpec[1].paramId = k_resultF;
outSpec[1].detail = k_RixSCVarying;
outSpec[1].value = resultF;

// INPUTS BEGIN____________________________________
bool varying = true;
bool uniform = false;
// Declare a pointer for each input then obtain their values
// using EvalParam().
RtColorRGB const *front_color;
RtColorRGB const *rear_color;
RtFloat const *blur;
RtInt const *swap_colorsPtr;
ctx->EvalParam(k_front_color, -1, &front_color, &m_front_color, varying);
ctx->EvalParam(k_rear_color, -1, &rear_color, &m_rear_color, varying);
ctx->EvalParam(k_blur, -1, &blur, &m_blur, varying);
ctx->EvalParam(k_swap_colors, -1, &swap_colorsPtr, &m_swap_colors, uniform);


// GRAB THE NECESSARY GEOMETRIC DATA___________________________
//Grab the value of the swap checkbox
RtInt doSwap = *swap_colorsPtr;
// Access the primitive variables 'N' and the viewing vevtor 'V' so 
// we can measure the angle between them in order to decide which "side"
// of a surface we are shading.

// Assign an array of normalized shading normals to Nn.
RtNormal3 const  *Nn;
ctx->GetBuiltinVar(RixShadingContext::k_Nn, &Nn);

// Assign an array of normalized view vectors to Vn 
    // pointing away from the shading points.
RtVector3 const  *Vn;
ctx->GetBuiltinVar(RixShadingContext::k_Vn, &Vn);

// Assign an array of surface positions to P.
RtPoint3 const  *P;
ctx->GetBuiltinVar(RixShadingContext::k_P, &P);


// Assign values to the output(s).
for(int i = 0; i < ctx->numPts; i++) {
RtFloat sstep = RixSmoothStep(0 - blur[i]/2, blur[i]/2, Dot(Nn[i], Vn[i]));
if(doSwap == 0){
resultRGB[i] = RixMix(front_color[i], rear_color[i], sstep);
resultF[i] = RixMix(1, 0, sstep);
}
else{
resultRGB[i] = RixMix(rear_color[i], front_color[i], sstep);
resultF[i] = RixMix(0, 1, sstep);
}
}
return 0;
}
RIX_PATTERNCREATE {
return new AberSideMask();
}
RIX_PATTERNDESTROY {
delete((AberSideMask*)pattern);
}

  • About
  • Real-Time VFX
  • Houdini
  • Fun Things
  • Blogs
    • Mentor Project
    • MatchtoLive
    • VSFX 755 Class Blog >
      • C: debris procedural primitive
      • C++: side-mask pattern node
      • OSL: renderman moom shading
  • Other Art Works
  • Award