#ifdef WIN32
#pragma warning( disable : 4786 ) // Disable stupid STL warnings.
#endif
#include <maya/MIOStream.h>
#include <math.h>
#include <maya/MString.h>
#include <maya/MPlug.h>
#include <maya/MDataBlock.h>
#include <maya/MDataHandle.h>
#include <maya/MArrayDataHandle.h>
#include <maya/MFnNumericAttribute.h>
#include <maya/MFnTypedAttribute.h>
#include <maya/MFnLightDataAttribute.h>
#include <maya/MFloatVector.h>
#include <maya/MFnPlugin.h>
#include <maya/MFnStringData.h>
#include <maya/MGlobal.h>
#include <maya/MPoint.h>
#include <maya/MMatrix.h>
#include <maya/MVector.h>
#include <maya/MEulerRotation.h>
#include <maya/MDagPath.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MSceneMessage.h>
#if defined(OSMac_MachO_)
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#else
#include <GL/gl.h>
#include <GL/glu.h>
#endif
#include "hwUnlitShader.h"
#include "ShadingConnection.h"
MTypeId hwUnlitShader::id( 0x00105440 );
MObject hwUnlitShader::color;
MObject hwUnlitShader::colorR;
MObject hwUnlitShader::colorG;
MObject hwUnlitShader::colorB;
MObject hwUnlitShader::transparency;
MObject hwUnlitShader::transparencyR;
MObject hwUnlitShader::transparencyG;
MObject hwUnlitShader::transparencyB;
void hwUnlitShader::postConstructor( )
{
setMPSafe(false);
}
void hwUnlitShader::printGlError( const char *call )
{
GLenum error;
while( (error = glGetError()) != GL_NO_ERROR ) {
assert(0);
cerr << call << ":" << error << " is " << (const char *)gluErrorString( error ) << "\n";
}
}
hwUnlitShader::hwUnlitShader()
{
m_pTextureCache = MTextureCache::instance();
attachSceneCallbacks();
}
hwUnlitShader::~hwUnlitShader()
{
detachSceneCallbacks();
}
void hwUnlitShader::releaseEverything()
{
m_pTextureCache->release();
if(!MTextureCache::getReferenceCount())
{
m_pTextureCache = 0;
}
}
void hwUnlitShader::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 hwUnlitShader::releaseCallback(void* clientData)
{
hwUnlitShader *pThis = (hwUnlitShader*) clientData;
pThis->releaseEverything();
}
void hwUnlitShader::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;
}
void * hwUnlitShader::creator()
{
return new hwUnlitShader();
}
MStatus initializePlugin( MObject obj )
{
MStatus status;
const MString UserClassify( "shader/surface/utility" );
MFnPlugin plugin( obj, PLUGIN_COMPANY, "4.0.1", "Any");
status = plugin.registerNode( "hwUnlitShader", hwUnlitShader::id,
hwUnlitShader::creator, hwUnlitShader::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( hwUnlitShader::id );
if (!status) {
status.perror("deregisterNode");
return status;
}
return MS::kSuccess;
}
MStatus hwUnlitShader::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);
transparencyR = nAttr.create( "transparencyR", "itr",MFnNumericData::kFloat);
nAttr.setStorable(true);
nAttr.setKeyable(true);
nAttr.setDefault(1.0f);
transparencyG = nAttr.create( "transparencyG", "itg",MFnNumericData::kFloat);
nAttr.setStorable(true);
nAttr.setKeyable(true);
nAttr.setDefault(0.5f);
transparencyB = nAttr.create( "transparencyB", "itb",MFnNumericData::kFloat);
nAttr.setStorable(true);
nAttr.setKeyable(true);
nAttr.setDefault(0.5f);
transparency = nAttr.create( "transparency", "it", transparencyR, transparencyG, transparencyB);
nAttr.setStorable(true);
nAttr.setKeyable(true);
nAttr.setDefault(0.0001f, 0.0001f, 0.0001f);
nAttr.setUsedAsColor(true);
addAttribute(color);
addAttribute(transparency);
return MS::kSuccess;
}
MStatus hwUnlitShader::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);
MDataHandle outColorHandle = block.outputValue( outColor );
MFloatVector& outColor = outColorHandle.asFloatVector();
outColor = resultColor;
outColorHandle.setClean();
return MS::kSuccess;
}
MStatus hwUnlitShader::getFloat3(MObject attr, float value[3])
{
MPlug plug(thisMObject(), attr);
MObject object;
MStatus status = plug.getValue(object);
if (!status)
{
status.perror("hwUnlitShader::bind plug.getValue.");
return status;
}
MFnNumericData data(object, &status);
if (!status)
{
status.perror("hwUnlitShader::bind construct data.");
return status;
}
status = data.getData(value[0], value[1], value[2]);
if (!status)
{
status.perror("hwUnlitShader::bind get values.");
return status;
}
return MS::kSuccess;
}
MStatus hwUnlitShader::getString(MObject attr, MString &str)
{
MPlug plug(thisMObject(), attr);
MStatus status = plug.getValue( str );
return MS::kSuccess;
}
void hwUnlitShader::updateTransparencyFlags(MString objectPath)
{
MString transparencyName = "";
ShadingConnection transparencyConnection(thisMObject(), objectPath, "transparency");
if (transparencyConnection.type() == ShadingConnection::CONSTANT_COLOR)
{
MColor tc = transparencyConnection.constantColor();
fConstantTransparency = (tc.r + tc.g + tc.b) / 3.0f;
}
else
fConstantTransparency = 0.0f;
}
MStatus hwUnlitShader::bind(const MDrawRequest& request,
M3dView& view)
{
MStatus status;
float bgColor[4] = {1,1,1,1};
currentObjectPath = request.multiPath();
MString currentPathName( currentObjectPath.partialPathName() );
updateTransparencyFlags(currentPathName);
MString decalName = "";
ShadingConnection colorConnection(thisMObject(), currentPathName, "color");
if (colorConnection.type() == ShadingConnection::TEXTURE &&
colorConnection.texture().hasFn(MFn::kFileTexture))
{
MFnDependencyNode textureNode(colorConnection.texture());
MPlug filenamePlug( colorConnection.texture(), textureNode.attribute(MString("fileTextureName")) );
filenamePlug.getValue(decalName);
if (decalName == "")
getFloat3(color, bgColor);
}
else
{
decalName = "";
getFloat3(color, bgColor);
}
assert(glGetError() == GL_NO_ERROR);
view.beginGL();
glPushAttrib( GL_ALL_ATTRIB_BITS );
glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
float alpha = 1.0f - fConstantTransparency;
glColor4f(bgColor[0], bgColor[1], bgColor[2], alpha);
if (decalName.length() != 0)
{
glEnable(GL_TEXTURE_2D);
assert(glGetError() == GL_NO_ERROR);
if(m_pTextureCache)
m_pTextureCache->bind(colorConnection.texture(), MTexture::RGBA, false);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
}
glDisable(GL_LIGHTING);
view.endGL();
return MS::kSuccess;
}
MStatus hwUnlitShader::unbind(const MDrawRequest& request,
M3dView& view)
{
view.beginGL();
glPopClientAttrib();
glPopAttrib();
view.endGL();
return MS::kSuccess;
}
MStatus hwUnlitShader::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)
{
view.beginGL();
glVertexPointer(3, GL_FLOAT, 0, vertexArray);
glEnableClientState(GL_VERTEX_ARRAY);
if (normalCount > 0)
{
glNormalPointer(GL_FLOAT, 0, normalArrays[0]);
glEnableClientState(GL_NORMAL_ARRAY);
}
if (texCoordCount > 0)
{
glTexCoordPointer(2, GL_FLOAT, 0, texCoordArrays[0]);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
}
glDrawElements(prim, indexCount, GL_UNSIGNED_INT, indexArray);
view.endGL();
return MS::kSuccess;
}
int hwUnlitShader::normalsPerVertex()
{
return 1;
}
int hwUnlitShader::texCoordsPerVertex()
{
return 1;
}
bool hwUnlitShader::hasTransparency()
{
return true;
}