#ifdef WIN32
#pragma warning( disable : 4786 ) // Disable STL warnings.
#endif
#include <maya/MIOStream.h>
#include <math.h>
#include <maya/MString.h>
#include <maya/MPlug.h>
#include <maya/MDagPath.h>
#include <maya/MDataBlock.h>
#include <maya/MDataHandle.h>
#include <maya/MArrayDataHandle.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MFnNumericAttribute.h>
#include <maya/MFnTypedAttribute.h>
#include <maya/MFnLightDataAttribute.h>
#include <maya/MFloatVector.h>
#include <maya/MFnLight.h>
#include <maya/MFnStringData.h>
#include <maya/MFnPlugin.h>
#include <maya/MGlobal.h>
#include <maya/MSceneMessage.h>
#include <maya/MPoint.h>
#include <maya/MMatrix.h>
#include <maya/MVector.h>
#include <maya/MEulerRotation.h>
#include <maya/MFnNonAmbientLight.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <gl/glext.h>
#define GLH_EXT_SINGLE_FILE
#include "glh_extensions.h"
#undef GL_NV_vertex_array_range
#include "glh_genext.h"
#include "glh_obs.h"
using namespace glh;
#include "hwToonShader_NV20.h"
#include "ShadingConnection.h"
MTypeId hwToonShader_NV20::id( 0x00105443 );
const unsigned int hwToonShader_NV20::lookup_texture_size(256);
void hwToonShader_NV20::postConstructor( )
{
setMPSafe(false);
}
MObject hwToonShader_NV20::color;
MObject hwToonShader_NV20::colorR;
MObject hwToonShader_NV20::colorG;
MObject hwToonShader_NV20::colorB;
MObject hwToonShader_NV20::lightModel;
MObject hwToonShader_NV20::lightModelR;
MObject hwToonShader_NV20::lightModelG;
MObject hwToonShader_NV20::lightModelB;
MObject hwToonShader_NV20::camera;
MObject hwToonShader_NV20::cameraX;
MObject hwToonShader_NV20::cameraY;
MObject hwToonShader_NV20::cameraZ;
MObject hwToonShader_NV20::uCoord;
MObject hwToonShader_NV20::vCoord;
MObject hwToonShader_NV20::uvCoord;
MObject hwToonShader_NV20::uBias;
MObject hwToonShader_NV20::vBias;
MObject hwToonShader_NV20::uvFilterSize;
MObject hwToonShader_NV20::uvFilterSizeX;
MObject hwToonShader_NV20::uvFilterSizeY;
MObject hwToonShader_NV20::shininess;
MObject hwToonShader_NV20::lightColor;
MObject hwToonShader_NV20::lightColorR;
MObject hwToonShader_NV20::lightColorG;
MObject hwToonShader_NV20::lightColorB;
void hwToonShader_NV20::printGlError( const char *call )
{
GLenum error;
while( (error = glGetError()) != GL_NO_ERROR ) {
assert(0);
cerr << call << ":" << error << " is " << (const char *)gluErrorString( error ) << "\n";
}
}
char vertexProgramString[] =
"!!VP1.0\n"
"DP4 o[HPOS].x, c[0], v[0];"
"DP4 o[HPOS].y, c[1], v[0];"
"DP4 o[HPOS].z, c[2], v[0];"
"DP4 o[HPOS].w, c[3], v[0];"
"MOV R1, v[2];"
"DP3 R1.w, R1, R1;"
"RSQ R1.w, R1.w;"
"MUL R1.xyz, R1, R1.w;"
"MOV R2, c[11];"
"DP3 R2.w, R2, R2;"
"RSQ R2.w, R2.w;"
"MUL R2.xyz, R2, R2.w;"
"ADD R0, c[12], -v[0];"
"DP3 R0.w, R0, R0;"
"RSQ R0.w, R0.w;"
"MUL R0.xyz, R0, R0.w;"
"DP3 o[TEX0].x, R1, R2;"
"DP3 o[TEX0].y, R1, R0;"
"MOV o[TEX1], v[9];"
#ifndef DEBUGGING_VERTEX_PROGRAM
"MUL o[COL0], v[3], c[9];"
#else
"MOV o[COL0], R2;"
#endif
"END";
char vertexProgramStringPoint[] =
"!!VP1.0\n"
"DP4 o[HPOS].x, c[0], v[0];"
"DP4 o[HPOS].y, c[1], v[0];"
"DP4 o[HPOS].z, c[2], v[0];"
"DP4 o[HPOS].w, c[3], v[0];"
"MOV R1, v[2];"
"DP3 R1.w, R1, R1;"
"RSQ R1.w, R1.w;"
"MUL R1.xyz, R1, R1.w;"
"ADD R2, c[11], -v[0];"
"DP3 R2.w, R2, R2;"
"RSQ R2.w, R2.w;"
"MUL R2.xyz, R2, R2.w;"
"ADD R0, c[12], -v[0];"
"DP3 R0.w, R0, R0;"
"RSQ R0.w, R0.w;"
"MUL R0.xyz, R0, R0.w;"
"DP3 o[TEX0].x, R1, R2;"
"DP3 o[TEX0].y, R1, R0;"
"MOV o[TEX1], v[9];"
#ifndef DEBUGGING_VERTEX_PROGRAM
"MUL o[COL0], v[3], c[9];"
#else
"MOV o[COL0], R2;"
#endif
"END";
char vertexProgramStringPointDecay[] =
"!!VP1.0\n"
"DP4 o[HPOS].x, c[0], v[0];"
"DP4 o[HPOS].y, c[1], v[0];"
"DP4 o[HPOS].z, c[2], v[0];"
"DP4 o[HPOS].w, c[3], v[0];"
"MOV R1, v[2];"
"DP3 R1.w, R1, R1;"
"RSQ R1.w, R1.w;"
"MUL R1.xyz, R1, R1.w;"
"ADD R2, c[11], -v[0];"
"DP3 R2.w, R2, R2;"
"RSQ R2.w, R2.w;"
"MUL R2.xyz, R2, R2.w;"
"ADD R0, c[12], -v[0];"
"DP3 R0.w, R0, R0;"
"RSQ R0.w, R0.w;"
"MUL R0.xyz, R0, R0.w;"
"DP3 o[TEX0].x, R1, R2;"
"DP3 o[TEX0].y, R1, R0;"
"MOV o[TEX1], v[9];"
#ifndef DEBUGGING_VERTEX_PROGRAM
"MOV R4, c[9];"
"MOV R5, c[9];"
"MUL R5.xyz, R4, R2.w;"
"MUL o[COL0], v[3], R5;"
#else
"MOV o[COL0], R2;"
#endif
"END";
void initVertexProgram(const char vertexProgramCode[], GLuint* pVertexProgramId)
{
glGenProgramsNV(1, pVertexProgramId);
GLenum error = glGetError();
assert(error == GL_NO_ERROR);
unsigned int length = strlen(vertexProgramCode);
glLoadProgramNV(GL_VERTEX_PROGRAM_NV, *pVertexProgramId, length,
(const GLubyte *) vertexProgramCode);
error = glGetError();
if (error != GL_NO_ERROR)
{
if (error == GL_INVALID_OPERATION)
{
int error_position = -2;
glGetIntegerv(GL_PROGRAM_ERROR_POSITION_NV, &error_position);
assert(0);
}
}
}
void hwToonShader_NV20::loadVertexProgramGL()
{
GLenum error = glGetError();
assert(!error);
if (!fVertexProgramsLoaded)
{
initVertexProgram(vertexProgramString, &fVertexProgramDirectional);
initVertexProgram(vertexProgramStringPointDecay, &fVertexProgramPointDecay);
initVertexProgram(vertexProgramStringPoint, &fVertexProgramPointNoDecay);
fVertexProgramsLoaded = true;
}
glTrackMatrixNV(GL_VERTEX_PROGRAM_NV, 0, GL_MODELVIEW_PROJECTION_NV, GL_IDENTITY_NV);
glTrackMatrixNV(GL_VERTEX_PROGRAM_NV, 4, GL_MODELVIEW, GL_IDENTITY_NV);
glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 8, 1, 1, 1, 1);
glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 9, 1, 1, 1, 1);
glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 10, 1, 1, 1, 1);
glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 11, -lightRotation[0], -lightRotation[1], -lightRotation[2], 0);
}
void hwToonShader_NV20::bind_lookup_table()
{
lookup_texture->bind();
if (fLookupTextureReprocessed)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, lookup_texture_size, lookup_texture_size, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, lookup_table);
fLookupTextureReprocessed = false;
}
lookup_texture->parameter(GL_TEXTURE_MIN_FILTER, GL_NEAREST);
lookup_texture->parameter(GL_TEXTURE_MAG_FILTER, GL_LINEAR);
lookup_texture->parameter(GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
lookup_texture->parameter(GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
void hwToonShader_NV20::init_ext(const char * ext)
{
if(!glh_init_extension(ext))
{ cerr << "Failed to initialize " << ext << "!" << endl; exit(0); }
}
hwToonShader_NV20::hwToonShader_NV20()
{
m_pTextureCache = MTextureCache::instance();
init_ext("GL_ARB_multitexture");
init_ext("GL_NV_register_combiners");
init_ext("GL_NV_vertex_program");
isDirectionalLight = true;
currentShininessValue = -1.0;
currentShininessScale = -1.0;
lookup_texture = NULL;
lookup_table = NULL;
fLookupTextureReprocessed = false;
fBeforeNewCB = 0;
fBeforeOpenCB = 0;
fBeforeRemoveReferenceCB = 0;
fMayaExitingCB = 0;
attachSceneCallbacks();
fVertexProgramsLoaded = false;
fVertexProgramDirectional = 0;
fVertexProgramPointDecay = 0;
fVertexProgramPointNoDecay = 0;
}
hwToonShader_NV20::~hwToonShader_NV20()
{
detachSceneCallbacks();
}
void releaseVertexProgram(GLuint* pVertexProgramId)
{
if (*pVertexProgramId > 0)
{
glBindProgramNV(GL_VERTEX_PROGRAM_NV, 0);
glDeleteProgramsNV(1, pVertexProgramId);
*pVertexProgramId = 0;
}
}
void hwToonShader_NV20::releaseEverything()
{
release_lookup_texture();
if (fVertexProgramsLoaded)
{
releaseVertexProgram(&fVertexProgramDirectional);
releaseVertexProgram(&fVertexProgramPointDecay);
releaseVertexProgram(&fVertexProgramPointNoDecay);
fVertexProgramsLoaded = false;
}
m_pTextureCache->release();
if(!MTextureCache::getReferenceCount())
{
m_pTextureCache = 0;
}
}
void hwToonShader_NV20::attachSceneCallbacks()
{
fBeforeNewCB = MSceneMessage::addCallback(MSceneMessage::kBeforeNew, releaseCallback, this);
fBeforeOpenCB = MSceneMessage::addCallback(MSceneMessage::kBeforeOpen, releaseCallback, this);
fBeforeRemoveReferenceCB = MSceneMessage::addCallback(MSceneMessage::kBeforeRemoveReference,
releaseCallback, this);
fMayaExitingCB = MSceneMessage::addCallback(MSceneMessage::kMayaExiting, releaseCallback, this);
}
void hwToonShader_NV20::releaseCallback(void* clientData)
{
hwToonShader_NV20 *pThis = (hwToonShader_NV20*) clientData;
pThis->releaseEverything();
}
void hwToonShader_NV20::detachSceneCallbacks()
{
if (fBeforeNewCB)
MMessage::removeCallback(fBeforeNewCB);
if (fBeforeOpenCB)
MMessage::removeCallback(fBeforeOpenCB);
if (fBeforeRemoveReferenceCB)
MMessage::removeCallback(fBeforeRemoveReferenceCB);
if (fMayaExitingCB)
MMessage::removeCallback(fMayaExitingCB);
fBeforeNewCB = 0;
fBeforeOpenCB = 0;
fBeforeRemoveReferenceCB = 0;
fMayaExitingCB = 0;
}
MStatus initializePlugin( MObject obj )
{
MStatus status;
const MString UserClassify( "shader/surface/utility" );
MFnPlugin plugin( obj, PLUGIN_COMPANY, "4.5", "Any");
status = plugin.registerNode( "hwToonShader_NV20", hwToonShader_NV20::id,
hwToonShader_NV20::creator, hwToonShader_NV20::initialize,
MPxNode::kHwShaderNode, &UserClassify );
if (!status) {
status.perror("registerNode");
return status;
}
return MS::kSuccess;
}
MStatus uninitializePlugin( MObject obj )
{
MStatus status;
MFnPlugin plugin( obj );
plugin.deregisterNode( hwToonShader_NV20::id );
if (!status) {
status.perror("deregisterNode");
return status;
}
return MS::kSuccess;
}
void * hwToonShader_NV20::creator()
{
return new hwToonShader_NV20();
}
MStatus hwToonShader_NV20::initialize()
{
MFnNumericAttribute nAttr;
MStatus status;
MFnTypedAttribute sAttr;
colorR = nAttr.create( "colorR", "cr",MFnNumericData::kFloat);
nAttr.setStorable(true);
nAttr.setKeyable(true);
nAttr.setDefault(1.0f);
colorG = nAttr.create( "colorG", "cg",MFnNumericData::kFloat);
nAttr.setStorable(true);
nAttr.setKeyable(true);
nAttr.setDefault(0.5f);
colorB = nAttr.create( "colorB", "cb",MFnNumericData::kFloat);
nAttr.setStorable(true);
nAttr.setKeyable(true);
nAttr.setDefault(0.5f);
color = nAttr.create( "color", "c", colorR, colorG, colorB);
nAttr.setStorable(true);
nAttr.setKeyable(true);
nAttr.setDefault(1.0f, 0.5f, 0.5f);
nAttr.setUsedAsColor(true);
lightModelR = nAttr.create( "lightModelR", "c2r",MFnNumericData::kFloat);
nAttr.setStorable(true);
nAttr.setKeyable(true);
nAttr.setDefault(1.0f);
lightModelG = nAttr.create( "lightModelG", "c2g",MFnNumericData::kFloat);
nAttr.setStorable(true);
nAttr.setKeyable(true);
nAttr.setDefault(1.0f);
lightModelB = nAttr.create( "lightModelB", "c2b",MFnNumericData::kFloat);
nAttr.setStorable(true);
nAttr.setKeyable(true);
nAttr.setDefault(1.0f);
lightModel = nAttr.create( "lightModel", "c2", lightModelR, lightModelG, lightModelB);
nAttr.setStorable(true);
nAttr.setKeyable(true);
nAttr.setDefault(1.0f, 1.0f, 1.0f);
nAttr.setUsedAsColor(true);
uCoord = nAttr.create( "uCoord", "u", MFnNumericData::kFloat);
nAttr.setStorable(true);
nAttr.setKeyable(true);
nAttr.setDefault(0.5f);
vCoord = nAttr.create( "vCoord", "v", MFnNumericData::kFloat);
nAttr.setStorable(true);
nAttr.setKeyable(true);
nAttr.setDefault(0.5f);
uvCoord = nAttr.create( "uvCoord","uv", uCoord, vCoord);
nAttr.setStorable(true);
nAttr.setKeyable(true);
nAttr.setDefault(0.5f, 0.5f );
nAttr.setHidden(true);
uBias = nAttr.create( "uBias", "bu", MFnNumericData::kFloat);
nAttr.setStorable(true);
nAttr.setMin(0.0f);
nAttr.setMax(1.0f);
nAttr.setKeyable(true);
nAttr.setDefault(0.5f);
vBias = nAttr.create( "vBias", "bv", MFnNumericData::kFloat);
nAttr.setStorable(true);
nAttr.setKeyable(true);
nAttr.setMin(0.0f);
nAttr.setMax(1.0f);
nAttr.setDefault(0.5f);
uvFilterSizeX = nAttr.create( "uvFilterSizeX", "fsx", MFnNumericData::kFloat);
nAttr.setStorable(false);
nAttr.setReadable(true);
nAttr.setWritable(true);
nAttr.setHidden(true);
uvFilterSizeY = nAttr.create( "uvFilterSizeY", "fsy", MFnNumericData::kFloat);
nAttr.setStorable(false);
nAttr.setReadable(true);
nAttr.setWritable(true);
nAttr.setHidden(true);
uvFilterSize = nAttr.create("uvFilterSize","fs",uvFilterSizeX,uvFilterSizeY);
nAttr.setStorable(false);
nAttr.setReadable(true);
nAttr.setWritable(true);
nAttr.setHidden(true);
cameraX = nAttr.create( "cameraX", "camx",MFnNumericData::kFloat);
nAttr.setStorable(true);
nAttr.setKeyable(true);
nAttr.setDefault(0.0f);
cameraY = nAttr.create( "cameraY", "camy",MFnNumericData::kFloat);
nAttr.setStorable(true);
nAttr.setKeyable(true);
nAttr.setDefault(0.0f);
cameraZ = nAttr.create( "cameraZ", "camz",MFnNumericData::kFloat);
nAttr.setStorable(true);
nAttr.setKeyable(true);
nAttr.setDefault(1.0f);
camera = nAttr.create( "camera", "cam", cameraX, cameraY, cameraZ);
nAttr.setStorable(true);
nAttr.setKeyable(true);
nAttr.setDefault(0.0f, 0.0f, 1.0f);
shininess = nAttr.create( "shininess", "sn", MFnNumericData::kFloat);
nAttr.setStorable(true);
nAttr.setKeyable(true);
nAttr.setMin(0.0f);
nAttr.setMax(1.0f);
nAttr.setDefault(0.5f);
lightColorR = nAttr.create( "lightColorR", "lcr", MFnNumericData::kFloat);
nAttr.setStorable(true);
nAttr.setKeyable(true);
nAttr.setDefault(1.0f);
lightColorG = nAttr.create( "lightColorG", "lcg", MFnNumericData::kFloat);
nAttr.setStorable(true);
nAttr.setKeyable(true);
nAttr.setDefault(1.0f);
lightColorB = nAttr.create( "lightColorB", "lcb", MFnNumericData::kFloat);
nAttr.setStorable(true);
nAttr.setKeyable(true);
nAttr.setDefault(1.0f);
lightColor = nAttr.create( "lightColor", "lc", lightColorR, lightColorG, lightColorB);
nAttr.setStorable(true);
nAttr.setKeyable(true);
nAttr.setDefault(1.0f, 1.0f, 1.0f);
nAttr.setUsedAsColor(true);
addAttribute(color);
addAttribute(lightModel);
addAttribute(uvCoord);
addAttribute(uBias);
addAttribute(vBias);
addAttribute(uvFilterSize);
addAttribute(camera);
addAttribute(shininess);
addAttribute(lightColor);
attributeAffects (colorR, outColor);
attributeAffects (colorG, outColor);
attributeAffects (colorB, outColor);
attributeAffects (color, outColor);
attributeAffects (lightModelR, outColor);
attributeAffects (lightModelG, outColor);
attributeAffects (lightModelB, outColor);
attributeAffects (lightModel, outColor);
attributeAffects (uCoord, outColor);
attributeAffects (vCoord, outColor);
attributeAffects (uvCoord, outColor);
attributeAffects (uBias, outColor);
attributeAffects (vBias, outColor);
attributeAffects (cameraX, outColor);
attributeAffects (cameraY, outColor);
attributeAffects (cameraZ, outColor);
attributeAffects (camera, outColor);
attributeAffects (shininess, outColor);
attributeAffects (lightColorR, outColor);
attributeAffects (lightColorG, outColor);
attributeAffects (lightColorB, outColor);
attributeAffects (lightColor, outColor);
return MS::kSuccess;
}
MStatus hwToonShader_NV20::compute(
const MPlug& plug,
MDataBlock& block )
{
bool k = false;
k |= (plug==outColor);
k |= (plug==outColorR);
k |= (plug==outColorG);
k |= (plug==outColorB);
if( !k ) return MS::kUnknownParameter;
MFloatVector resultColor(0.0,0.0,0.0);
float& u = block.inputValue( uCoord ).asFloat();
float& v = block.inputValue( vCoord ).asFloat();
float& bu = block.inputValue( uBias ).asFloat();
float& bv = block.inputValue( vBias ).asFloat();
if ( bu <= 0.0 ) bu = 0.001f;
if ( bv <= 0.0 ) bv = 0.001f;
MFloatVector& surfaceColor = block.inputValue( color ).asFloatVector();
MFloatVector& surfaceColor2 = block.inputValue( lightModel ).asFloatVector();
u = u - int(u);
v = v - int(v);
resultColor = surfaceColor;
MDataHandle outColorHandle = block.outputValue( outColor );
MFloatVector& outColor = outColorHandle.asFloatVector();
outColor = resultColor;
outColorHandle.setClean();
return MS::kSuccess;
}
MStatus hwToonShader_NV20::getFloat3(MObject attr, float value[3])
{
MStatus status = MS::kSuccess;
MPlug plug(thisMObject(), attr);
MObject object;
status = plug.getValue(object);
if (!status)
{
status.perror("hwToonShader_NV20::getFloat3 plug.getValue.");
return status;
}
MFnNumericData data(object, &status);
if (!status)
{
status.perror("hwToonShader_NV20::getFloat3 construct data.");
return status;
}
status = data.getData(value[0], value[1], value[2]);
if (!status)
{
status.perror("hwToonShader_NV20::getFloat3 get values.");
return status;
}
return status;
}
MStatus hwToonShader_NV20::getString(MObject attr, MString &str)
{
MPlug plug(thisMObject(), attr);
MStatus status = plug.getValue( str );
return status;
}
MStatus hwToonShader_NV20::bind(const MDrawRequest& request, M3dView& view)
{
MStatus status;
MString decalName = "";
MString lightModelName = "";
ShadingConnection colorConnection(thisMObject(), request.multiPath().partialPathName(), "color");
ShadingConnection lightModelConnection (thisMObject(), request.multiPath().partialPathName(), "lightModel");
if (lightModelConnection.type() == ShadingConnection::TEXTURE &&
lightModelConnection.texture().hasFn(MFn::kFileTexture))
{
MFnDependencyNode textureNode(lightModelConnection.texture());
MPlug filenamePlug( lightModelConnection.texture(), textureNode.attribute(MString("fileTextureName")) );
filenamePlug.getValue(lightModelName);
}
if (lightModelName.length() == 0 )
{
view.beginGL();
glPushAttrib( GL_ALL_ATTRIB_BITS );
glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
view.endGL();
return MS::kSuccess;
}
view.beginGL();
glPushAttrib( GL_ALL_ATTRIB_BITS );
glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT);
glActiveTextureARB( GL_TEXTURE0_ARB );
glEnable(GL_TEXTURE_2D);
if(m_pTextureCache)
m_pTextureCache->bind(lightModelConnection.texture(), MTexture::RGBA, false);
if (colorConnection.type() != ShadingConnection::TEXTURE)
{
MColor color = colorConnection.constantColor();
glColor4f(color.r, color.g, color.b, color.a);
}
glEnable(GL_REGISTER_COMBINERS_NV);
glCombinerParameteriNV(GL_NUM_GENERAL_COMBINERS_NV, 1);
float constColor0[4];
constColor0[0] = constColor0[1] = constColor0[2] = constColor0[3] = 1.0;
glCombinerParameterfvNV(GL_CONSTANT_COLOR0_NV, constColor0);
#ifndef DEBUGGING_VERTEX_PROGRAM
glCombinerInputNV(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_A_NV, GL_PRIMARY_COLOR_NV, GL_UNSIGNED_IDENTITY_NV, GL_RGB);
glCombinerInputNV(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_B_NV, GL_TEXTURE0_ARB, GL_UNSIGNED_IDENTITY_NV, GL_RGB);
glCombinerInputNV(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_C_NV, GL_TEXTURE0_ARB, GL_UNSIGNED_IDENTITY_NV, GL_ALPHA);
glCombinerInputNV(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_D_NV, GL_TEXTURE0_ARB, GL_UNSIGNED_IDENTITY_NV, GL_ALPHA);
glCombinerOutputNV(GL_COMBINER0_NV, GL_RGB, GL_SPARE1_NV, GL_DISCARD_NV, GL_DISCARD_NV,
GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE);
#else
glCombinerInputNV(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_A_NV, GL_PRIMARY_COLOR_NV, GL_UNSIGNED_IDENTITY_NV, GL_RGB);
glCombinerInputNV(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_B_NV, GL_CONSTANT_COLOR0_NV, GL_UNSIGNED_IDENTITY_NV, GL_RGB);
glCombinerInputNV(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_C_NV, GL_TEXTURE0_ARB, GL_UNSIGNED_IDENTITY_NV, GL_ALPHA);
glCombinerInputNV(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_D_NV, GL_TEXTURE3_ARB, GL_UNSIGNED_IDENTITY_NV, GL_ALPHA);
glCombinerOutputNV(GL_COMBINER0_NV, GL_RGB, GL_SPARE1_NV, GL_DISCARD_NV, GL_DISCARD_NV,
GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE);
#endif // DEBUGGING_VERTEX_PROGRAM
glFinalCombinerInputNV(GL_VARIABLE_A_NV, GL_ZERO, GL_UNSIGNED_IDENTITY_NV, GL_RGB);
glFinalCombinerInputNV(GL_VARIABLE_B_NV, GL_ZERO, GL_UNSIGNED_IDENTITY_NV, GL_RGB);
glFinalCombinerInputNV(GL_VARIABLE_C_NV, GL_ZERO, GL_UNSIGNED_IDENTITY_NV, GL_RGB);
glFinalCombinerInputNV(GL_VARIABLE_D_NV, GL_SPARE1_NV, GL_UNSIGNED_IDENTITY_NV, GL_RGB);
view.endGL();
return MS::kSuccess;
}
MStatus hwToonShader_NV20::unbind(const MDrawRequest& request,
M3dView& view)
{
view.beginGL();
glDisable(GL_REGISTER_COMBINERS_NV);
glActiveTextureARB( GL_TEXTURE0_ARB );
glDisable(GL_TEXTURE_2D);
glPopClientAttrib();
glPopAttrib();
view.endGL();
return MS::kSuccess;
}
MStatus hwToonShader_NV20::geometry( const MDrawRequest& request,
M3dView& view,
int prim,
unsigned int writable,
int indexCount,
const unsigned int * indexArray,
int vertexCount,
const int * vertexIDs,
const float * vertexArray,
int normalCount,
const float ** normalArrays,
int colorCount,
const float ** colorArrays,
int texCoordCount,
const float ** texCoordArrays)
{
if (prim != GL_TRIANGLES)
return MS::kSuccess;
view.beginGL();
isDirectionalLight = true;
isNonAmbientLight = false;
boolean useDefaultLight = false;
unsigned int numLights;
MDagPath lightPath;
view.getLightCount( numLights );
if (numLights)
{
M3dView::LightingMode mode;
view.getLightingMode(mode);
if (mode == M3dView::kLightDefault)
{
useDefaultLight = true;
isDirectionalLight = true;
}
else
{
view.getLightPath( 0, lightPath );
MObject lightObj = lightPath.node();
isDirectionalLight = lightObj.hasFn( MFn::kDirectionalLight );
isNonAmbientLight = lightObj.hasFn( MFn::kNonAmbientLight );
if (isNonAmbientLight)
{
MFnNonAmbientLight mNonAmbientLight(lightObj);
if (mNonAmbientLight.decayRate() == 0)
isNonAmbientLight = false;
}
}
}
loadVertexProgramGL();
if (isDirectionalLight)
glBindProgramNV(GL_VERTEX_PROGRAM_NV, fVertexProgramDirectional);
else if (isNonAmbientLight)
glBindProgramNV(GL_VERTEX_PROGRAM_NV, fVertexProgramPointDecay);
else
glBindProgramNV(GL_VERTEX_PROGRAM_NV, fVertexProgramPointNoDecay);
GLenum error = glGetError();
assert(error == GL_NO_ERROR);
glEnable(GL_VERTEX_PROGRAM_NV);
MDagPath objPath = request.multiPath();
MMatrix objMatrix = objPath.inclusiveMatrixInverse();
if (numLights)
{
if (useDefaultLight )
{
glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 11, 0, 0, 1, 1);
glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 9, 1,1,1,1);
}
else
{
view.getLightPath( 0, lightPath );
MMatrix matrix = lightPath.inclusiveMatrix();
isDirectionalLight = lightPath.node().hasFn( MFn::kDirectionalLight );
if (isDirectionalLight)
{
MVector lightDir(0,0,1);
lightDir *= matrix;
lightDir *= objMatrix;
glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 11, lightDir.x, lightDir.y, lightDir.z, 1);
}
else
{
MPoint lightPos(0,0,0);
lightPos *= matrix;
lightPos *= objMatrix;
glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 11, lightPos.x, lightPos.y, lightPos.z, 1);
}
MFnLight mLight(lightPath.node());
MColor lightColor = mLight.color();
float intensity = mLight.intensity();
lightColor.r *= intensity;
lightColor.g *= intensity;
lightColor.b *= intensity;
glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 9, lightColor.r,
lightColor.g, lightColor.b, 1);
}
}
else
{
glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 11, 0, 0, 1, 1);
glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 9, 1.0, 1.0, 1.0, 1);
}
float cameraPos[4] = {0.0f, 0.0f, 0.0f, 0.0f};
{
MDagPath camDag;
view.getCamera(camDag);
MPoint cameraInObject(0,0,0);
MMatrix cameraToWorldMatrix = camDag.inclusiveMatrix();
cameraInObject *= cameraToWorldMatrix;
cameraInObject *= objMatrix;
glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 12, cameraInObject.x, cameraInObject.y, cameraInObject.z, 1);
}
glVertexAttribPointerNV( 0, 3, GL_FLOAT, 0, vertexArray );
glVertexAttribPointerNV( 2, 3, GL_FLOAT, 0, normalArrays[0] );
glEnableClientState( GL_VERTEX_ATTRIB_ARRAY0_NV );
glEnableClientState( GL_VERTEX_ATTRIB_ARRAY2_NV );
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, indexArray);
glDisableClientState( GL_VERTEX_ATTRIB_ARRAY0_NV );
glDisableClientState( GL_VERTEX_ATTRIB_ARRAY2_NV );
glDisable(GL_VERTEX_PROGRAM_NV);
glClientActiveTextureARB(GL_TEXTURE0_ARB);
view.endGL();
return MS::kSuccess;
}
int hwToonShader_NV20::normalsPerVertex()
{
return 1;
}
int hwToonShader_NV20::texCoordsPerVertex()
{
return 1;
}
void hwToonShader_NV20::release_lookup_texture()
{
if (lookup_table)
{
delete lookup_table;
lookup_table = NULL;
}
if (lookup_texture)
{
delete lookup_texture;
lookup_table = NULL;
}
}