#ifdef WIN32
#pragma warning( disable : 4786 ) // Disable STL warnings.
#endif
#include <maya/MIOStream.h>
#include <math.h>
#include <maya/MString.h>
#include <maya/MStringArray.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/MFloatVector.h>
#include <maya/MFnStringData.h>
#include <maya/MFnPlugin.h>
#include <maya/MGlobal.h>
#include <maya/MPoint.h>
#include <maya/MMatrix.h>
#include <maya/MVector.h>
#include <maya/MEulerRotation.h>
#include <maya/MQuaternion.h>
#include <maya/MDagPath.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MSceneMessage.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 "hwReflectBumpShader_NV20.h"
#include "ShadingConnection.h"
MTypeId hwReflectBumpShader_NV20::id( 0x00105442 );
void hwReflectBumpShader_NV20::postConstructor( )
{
setMPSafe(false);
}
MObject hwReflectBumpShader_NV20::color;
MObject hwReflectBumpShader_NV20::colorR;
MObject hwReflectBumpShader_NV20::colorG;
MObject hwReflectBumpShader_NV20::colorB;
MObject hwReflectBumpShader_NV20::bump;
MObject hwReflectBumpShader_NV20::bumpR;
MObject hwReflectBumpShader_NV20::bumpG;
MObject hwReflectBumpShader_NV20::bumpB;
MObject hwReflectBumpShader_NV20::uCoord;
MObject hwReflectBumpShader_NV20::vCoord;
MObject hwReflectBumpShader_NV20::uvCoord;
MObject hwReflectBumpShader_NV20::uBias;
MObject hwReflectBumpShader_NV20::vBias;
MObject hwReflectBumpShader_NV20::uvFilterSize;
MObject hwReflectBumpShader_NV20::uvFilterSizeX;
MObject hwReflectBumpShader_NV20::uvFilterSizeY;
char gszErrMsg[100];
void hwReflectBumpShader_NV20::printGlError( const char *call )
{
GLenum error;
while( (error = glGetError()) != GL_NO_ERROR ) {
cerr << call << ":" << error << " is " << (const char *)gluErrorString( error ) << "\n";
}
}
void hwReflectBumpShader_NV20::verify_shader_config(M3dView& view)
{
int consistent;
view.beginGL();
glActiveTextureARB( GL_TEXTURE0_ARB );
glGetTexEnviv(GL_TEXTURE_SHADER_NV, GL_SHADER_CONSISTENT_NV, & consistent);
if(consistent == GL_FALSE)
cerr << "Shader stage 0 is inconsistent!" << endl;
glActiveTextureARB( GL_TEXTURE1_ARB );
glGetTexEnviv(GL_TEXTURE_SHADER_NV, GL_SHADER_CONSISTENT_NV, & consistent);
if(consistent == GL_FALSE)
cerr << "Shader stage 1 is inconsistent!" << endl;
glActiveTextureARB( GL_TEXTURE2_ARB );
glGetTexEnviv(GL_TEXTURE_SHADER_NV, GL_SHADER_CONSISTENT_NV, & consistent);
if(consistent == GL_FALSE)
cerr << "Shader stage 2 is inconsistent!" << endl;
glActiveTextureARB( GL_TEXTURE3_ARB );
glGetTexEnviv(GL_TEXTURE_SHADER_NV, GL_SHADER_CONSISTENT_NV, & consistent);
if(consistent == GL_FALSE)
cerr << "Shader stage 3 is inconsistent!" << endl;
glActiveTextureARB( GL_TEXTURE0_ARB );
view.endGL();
}
char superEasyVertexProgramString[] =
"!!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];"
"DP4 R4.x, c[4], v[0];"
"DP4 R4.y, c[5], v[0];"
"DP4 R4.z, c[6], v[0];"
"DP4 R4.w, c[7], v[0];"
"DP3 R7.x, c[4], v[1];"
"DP3 R7.y, c[5], v[1];"
"DP3 R7.z, c[6], v[1];"
"DP3 R7.w, c[7], v[1];"
"DP3 o[TEX1].x, R7, c[24];"
"DP3 o[TEX2].y, R7, c[25];"
"DP3 o[TEX3].z, R7, c[26];"
"DP4 o[TEX1].w, R4, c[24];"
"DP4 o[TEX2].w, R4, c[25];"
"DP4 o[TEX3].w, R4, c[26];"
"MOV o[TEX0], v[2];"
"END";
char originalVertexProgramString[] =
"!!VP1.0\n"
"DP4 R4.x, c[4], v[0];"
"DP4 R4.y, c[5], v[0];"
"DP4 R4.z, c[6], v[0];"
"DP4 R4.w, c[7], v[0];"
"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];"
"DP3 R6.x, c[4], v[4];"
"DP3 R6.y, c[5], v[4];"
"DP3 R6.z, c[6], v[4];"
"DP3 R6.w, c[7], v[4];"
"DP3 R7.x, c[4], v[1];"
"DP3 R7.y, c[5], v[1];"
"DP3 R7.z, c[6], v[1];"
"DP3 R7.w, c[7], v[1];"
"MUL R5, R6.zxyw, R7.yzxw;"
"MAD R5, R6.yzxw, R7.zxyw, -R5;"
"DP3 R5.w, R5, R5;"
"RSQ R5.w, R5.w;"
"MUL R5.xyz, R5, R5.w;"
"MUL R5.xyz, R5, v[4].w;"
"DP3 o[TEX1].x, -R5, c[24];"
"DP3 o[TEX2].x, -R5, c[25];"
"DP3 o[TEX3].x, -R5, c[26];"
"DP3 o[TEX1].y, -R6, c[24];"
"DP3 o[TEX2].y, -R6, c[25];"
"DP3 o[TEX3].y, -R6, c[26];"
"DP3 o[TEX1].z, R7, c[24];"
"DP3 o[TEX2].z, R7, c[25];"
"DP3 o[TEX3].z, R7, c[26];"
"DP4 o[TEX1].w, -R4, c[24];"
"DP4 o[TEX2].w, -R4, c[25];"
"DP4 o[TEX3].w, -R4, c[26];"
"DP3 o[COL0], R7, c[23];"
"MOV o[COL0].w, c[50];"
"MOV o[TEX0], v[3];"
"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 hwReflectBumpShader_NV20::loadVertexProgramGL()
{
if (vertex_program_id == 0)
initVertexProgram(originalVertexProgramString, &vertex_program_id);
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, 20, 1.0, 1.0, 1.0, 1.0);
glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 21, 1.0, 1.0, 1.0, 1.0);
glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 22, 1.0, 1.0, 1.0, 1.0);
glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 23, 0.0, 0.0, 1.0, 0.0);
GLfloat modelViewMatrix[16];
glGetFloatv(GL_MODELVIEW_MATRIX, modelViewMatrix);
float stupidMatrix[4][4];
for (int i=0; i<16; i++)
{
stupidMatrix[i/4][i%4] = modelViewMatrix[i];
}
MMatrix mvMatrix(stupidMatrix);
MMatrix mv = m_ModelMatrix.inverse() * mvMatrix;
mv = mv.transpose();
glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 24, mv[0][0], mv[1][0], mv[2][0], mv[3][0]);
glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 25, mv[0][1], mv[1][1], mv[2][1], mv[3][1]);
glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 26, mv[0][2], mv[1][2], mv[2][2], mv[3][2]);
glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 27, mv[0][3], mv[1][3], mv[2][3], mv[3][3]);
}
void hwReflectBumpShader_NV20::init_ext(const char * ext)
{
if(!glh_init_extension(ext))
{ cerr << "Failed to initialize " << ext << "!" << endl; exit(0); }
}
hwReflectBumpShader_NV20::hwReflectBumpShader_NV20()
{
m_pTextureCache = MTextureCache::instance();
init_ext("GL_ARB_multitexture");
init_ext("GL_NV_register_combiners");
init_ext("GL_NV_texture_shader");
init_ext("GL_NV_vertex_program");
bumpScale = 1.0;
cubeMapOnly = FALSE;
texNames[0] = texNames[1] = texNames[2] = texNames[3] = texNames[4] = texNames[5] = 0;
currentTextureNames[0] = "";
currentTextureNames[1] = "";
currentTextureNames[2] = "";
currentTextureNames[3] = "";
currentTextureNames[4] = "";
currentTextureNames[5] = "";
attachSceneCallbacks();
vertex_program_id = 0;
}
hwReflectBumpShader_NV20::~hwReflectBumpShader_NV20()
{
detachSceneCallbacks();
}
void releaseVertexProgram(GLuint* pVertexProgramId)
{
if (*pVertexProgramId > 0)
{
glBindProgramNV(GL_VERTEX_PROGRAM_NV, 0);
glDeleteProgramsNV(1, pVertexProgramId);
*pVertexProgramId = 0;
}
}
void hwReflectBumpShader_NV20::releaseEverything()
{
if (texNames[0] != 0)
glDeleteTextures(6, &texNames[0]);
releaseVertexProgram(&vertex_program_id);
m_pTextureCache->release();
if(!MTextureCache::getReferenceCount())
{
m_pTextureCache = 0;
}
}
void hwReflectBumpShader_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 hwReflectBumpShader_NV20::releaseCallback(void* clientData)
{
hwReflectBumpShader_NV20 *pThis = (hwReflectBumpShader_NV20*) clientData;
pThis->releaseEverything();
}
void hwReflectBumpShader_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.0", "Any");
status = plugin.registerNode( "hwReflectBumpShader_NV20", hwReflectBumpShader_NV20::id,
hwReflectBumpShader_NV20::creator, hwReflectBumpShader_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( hwReflectBumpShader_NV20::id );
if (!status) {
status.perror("deregisterNode");
return status;
}
return MS::kSuccess;
}
void * hwReflectBumpShader_NV20::creator()
{
return new hwReflectBumpShader_NV20();
}
MStatus hwReflectBumpShader_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);
bumpR = nAttr.create( "bumpR", "c2r",MFnNumericData::kFloat);
nAttr.setStorable(true);
nAttr.setKeyable(true);
nAttr.setDefault(1.0f);
bumpG = nAttr.create( "bumpG", "c2g",MFnNumericData::kFloat);
nAttr.setStorable(true);
nAttr.setKeyable(true);
nAttr.setDefault(1.0f);
bumpB = nAttr.create( "bumpB", "c2b",MFnNumericData::kFloat);
nAttr.setStorable(true);
nAttr.setKeyable(true);
nAttr.setDefault(1.0f);
bump = nAttr.create( "bump", "c2", bumpR, bumpG, bumpB);
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);
addAttribute(color);
addAttribute(bump);
addAttribute(uvCoord);
addAttribute(uBias);
addAttribute(vBias);
addAttribute(uvFilterSize);
attributeAffects (colorR, outColor);
attributeAffects (colorG, outColor);
attributeAffects (colorB, outColor);
attributeAffects (color, outColor);
attributeAffects (bumpR, outColor);
attributeAffects (bumpG, outColor);
attributeAffects (bumpB, outColor);
attributeAffects (bump, outColor);
attributeAffects (uCoord, outColor);
attributeAffects (vCoord, outColor);
attributeAffects (uvCoord, outColor);
attributeAffects (uBias, outColor);
attributeAffects (vBias, outColor);
return MS::kSuccess;
}
MStatus hwReflectBumpShader_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;
MDataHandle outColorHandle = block.outputValue( outColor );
MFloatVector& outColor = outColorHandle.asFloatVector();
outColor.x = 1.0;
outColor.y = 0.5;
outColor.z = 0.5;
outColorHandle.setClean();
return MS::kSuccess;
}
MStatus hwReflectBumpShader_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("hwReflectBumpShader_NV20::bind plug.getValue.");
return status;
}
MFnNumericData data(object, &status);
if (!status)
{
status.perror("hwReflectBumpShader_NV20::bind construct data.");
return status;
}
status = data.getData(value[0], value[1], value[2]);
if (!status)
{
status.perror("hwReflectBumpShader_NV20::bind get values.");
return status;
}
return status;
}
MStatus hwReflectBumpShader_NV20::getString(MObject attr, MString &str)
{
MPlug plug(thisMObject(), attr);
MStatus status = plug.getValue( str );
return status;
}
MStatus hwReflectBumpShader_NV20::bind(const MDrawRequest& request,
M3dView& view)
{
MStatus status;
bool isHeightFieldMap = true;
m_ModelMatrix = request.multiPath().inclusiveMatrix();
MStringArray decalNames;
MString decalName;
MString bumpName;
ShadingConnection colorConnection(thisMObject(),
request.multiPath().partialPathName(), "color");
ShadingConnection bumpConnection (thisMObject(),
request.multiPath().partialPathName(), "bump");
bool gotAllEnvironmentMaps = TRUE;
if (colorConnection.type() == ShadingConnection::TEXTURE &&
colorConnection.texture().hasFn(MFn::kEnvCube))
{
MFnDependencyNode textureNode(colorConnection.texture());
MString attributeName;
MString envNames[6] = { "top", "bottom", "left", "right", "front", "back" };
for (int i=0; i<6; i++)
{
ShadingConnection conn(colorConnection.texture(), request.multiPath().partialPathName(),
envNames[i]);
if (conn.type() == ShadingConnection::TEXTURE &&
conn.texture().hasFn(MFn::kFileTexture))
{
MFnDependencyNode envNode(conn.texture());
MPlug filenamePlug( conn.texture(), envNode.attribute(MString("fileTextureName")) );
filenamePlug.getValue(decalName);
if (decalName.length() == 0)
decalName = "D:/chameleon/textures/Cham_body_color_real.tga";
decalNames.append( decalName );
}
else
{
decalName = "D:/chameleon/textures/Cham_body_color_real.tga";
decalNames.append( decalName );
}
}
}
else
{
decalName = "D:/chameleon/textures/Cham_body_color_real.tga";
for (int i=0; i<6; i++)
{
decalNames.append( decalName );
}
}
if (bumpConnection.type() == ShadingConnection::TEXTURE &&
bumpConnection.texture().hasFn(MFn::kFileTexture))
{
MFnDependencyNode textureNode(bumpConnection.texture());
MPlug filenamePlug( bumpConnection.texture(), textureNode.attribute(MString("fileTextureName")) );
filenamePlug.getValue(bumpName);
}
else
{
bumpName = "";
}
cubeMapOnly = (bumpName.length() == 0);
unsigned int width, height;
bool reload = FALSE;
for (int i=0; i<6; i++)
{
if (currentTextureNames[i] != decalNames[i])
{
reload = TRUE;
break;
}
}
if ( reload )
{
MString xpTexName(decalNames[2]);
MString xnTexName(decalNames[3]);
MString ypTexName(decalNames[0]);
MString ynTexName(decalNames[1]);
MString zpTexName(decalNames[4]);
MString znTexName(decalNames[5]);
MStatus stat;
if (! (stat = theImage_XP.readFromFile(xpTexName)) )
return MS::kFailure;
stat = theImage_XP.getSize( width, height );
if (! (stat = theImage_XN.readFromFile(xnTexName)) )
return MS::kFailure;
if (! (stat = theImage_YP.readFromFile(ypTexName)) )
return MS::kFailure;
if (! (stat = theImage_YN.readFromFile(ynTexName)) )
return MS::kFailure;
if (! (stat = theImage_ZP.readFromFile(zpTexName)) )
return MS::kFailure;
if (! (stat = theImage_ZN.readFromFile(znTexName)) )
return MS::kFailure;
if (texNames[0] == 0)
glGenTextures(6, &texNames[0]);
glBindTexture( GL_TEXTURE_2D, texNames[0] );
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, theImage_XP.pixels() );
glBindTexture( GL_TEXTURE_2D, texNames[1] );
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, theImage_XN.pixels() );
glBindTexture( GL_TEXTURE_2D, texNames[2] );
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, theImage_YP.pixels() );
glBindTexture( GL_TEXTURE_2D, texNames[3] );
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, theImage_YN.pixels() );
glBindTexture( GL_TEXTURE_2D, texNames[4] );
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, theImage_ZP.pixels() );
glBindTexture( GL_TEXTURE_2D, texNames[5] );
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, theImage_ZN.pixels() );
for (i=0; i<6; i++) {
currentTextureNames[i] = decalNames[i];
}
}
MDagPath cameraPath;
status = view.getCamera( cameraPath );
MVector camAxis;
double camTheta;
MMatrix mmatrix = cameraPath.inclusiveMatrix( &status );
MTransformationMatrix tmatrix( mmatrix );
m_CameraRotation = tmatrix.rotation();
m_CameraRotation.getAxisAngle( camAxis, camTheta );
camTheta *= 57.295779513082320876798154814105;
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, 1, 1, 1 );
if (cubeMapOnly)
{
glActiveTextureARB( GL_TEXTURE0_ARB );
glEnable(GL_TEXTURE_CUBE_MAP_ARB);
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
glEnable(GL_TEXTURE_GEN_R);
for (i=0; i<6; i++)
glBindTexture( GL_TEXTURE_2D, texNames[i] );
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glMatrixMode( GL_TEXTURE );
glPushMatrix();
glLoadIdentity();
glRotated( 180.0, 1.0, 0.0, 0.0 );
glRotated( camTheta, camAxis[0], camAxis[1], camAxis[2]);
glMatrixMode( GL_MODELVIEW );
}
else
{
loadVertexProgramGL();
glEnable(GL_TEXTURE_SHADER_NV);
glActiveTextureARB( GL_TEXTURE0_ARB );
glEnable(GL_TEXTURE_2D);
if( isHeightFieldMap ) {
if(m_pTextureCache)
m_pTextureCache->bind(bumpConnection.texture(), MTexture::NMAP, true);
}
else {
if(m_pTextureCache)
m_pTextureCache->bind(bumpConnection.texture(), MTexture::RGBA, true);
}
glTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
glActiveTextureARB( GL_TEXTURE1_ARB );
glTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_DOT_PRODUCT_NV);
glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB);
glActiveTextureARB( GL_TEXTURE2_ARB );
glTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_DOT_PRODUCT_NV);
glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB);
glActiveTextureARB( GL_TEXTURE3_ARB );
glEnable(GL_TEXTURE_CUBE_MAP_ARB);
glTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_DOT_PRODUCT_REFLECT_CUBE_MAP_NV);
glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
for (i=0; i<6; i++)
glBindTexture( GL_TEXTURE_2D, texNames[i] );
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glActiveTextureARB( GL_TEXTURE0_ARB );
float white_color[4] = {1.0, 1.0, 1.0, 1.0};
glCombinerParameterfvNV(GL_CONSTANT_COLOR0_NV, white_color);
glEnable(GL_REGISTER_COMBINERS_NV);
glCombinerParameteriNV(GL_NUM_GENERAL_COMBINERS_NV, 1);
glCombinerInputNV(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_A_NV, GL_TEXTURE3_ARB, 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_ZERO, GL_UNSIGNED_IDENTITY_NV, GL_RGB);
glCombinerInputNV(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_D_NV, GL_ZERO, GL_UNSIGNED_IDENTITY_NV, GL_RGB);
glCombinerOutputNV(GL_COMBINER0_NV, GL_RGB, GL_SPARE0_NV, GL_DISCARD_NV, GL_DISCARD_NV,
GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE);
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_SPARE0_NV, GL_UNSIGNED_IDENTITY_NV, GL_RGB);
verify_shader_config( view );
}
view.endGL();
return MS::kSuccess;
}
MStatus hwReflectBumpShader_NV20::unbind(const MDrawRequest& request,
M3dView& view)
{
view.beginGL();
if (cubeMapOnly)
{
glActiveTextureARB( GL_TEXTURE0_ARB );
glMatrixMode( GL_TEXTURE );
glPopMatrix();
glMatrixMode( GL_MODELVIEW );
glDisable( GL_TEXTURE_GEN_S );
glDisable( GL_TEXTURE_GEN_T );
glDisable( GL_TEXTURE_GEN_R );
glDisable( GL_TEXTURE_CUBE_MAP_ARB );
}
else
{
glDisable(GL_TEXTURE_SHADER_NV);
glActiveTextureARB( GL_TEXTURE0_ARB );
glDisable(GL_TEXTURE_2D);
glDisable(GL_TEXTURE_CUBE_MAP_ARB);
glActiveTextureARB( GL_TEXTURE1_ARB );
glDisable(GL_TEXTURE_2D);
glDisable(GL_TEXTURE_CUBE_MAP_ARB);
glActiveTextureARB( GL_TEXTURE2_ARB );
glDisable(GL_TEXTURE_2D);
glDisable(GL_TEXTURE_CUBE_MAP_ARB);
glActiveTextureARB( GL_TEXTURE3_ARB );
glDisable(GL_TEXTURE_2D);
glDisable(GL_TEXTURE_CUBE_MAP_ARB);
glDisable(GL_REGISTER_COMBINERS_NV);
glDisable(GL_VERTEX_PROGRAM_NV);
}
glActiveTextureARB( GL_TEXTURE0_ARB );
glPopClientAttrib();
glPopAttrib();
view.endGL();
return MS::kSuccess;
}
MStatus hwReflectBumpShader_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::kFailure;
view.beginGL();
glEnable(GL_VERTEX_ARRAY);
if (cubeMapOnly)
{
glVertexPointer(3, GL_FLOAT, 0, vertexArray);
glEnable(GL_VERTEX_ARRAY);
if (normalCount > 0)
{
glNormalPointer(GL_FLOAT, 0, normalArrays[0]);
glEnable(GL_NORMAL_ARRAY);
}
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, indexArray);
glDisable(GL_VERTEX_ARRAY);
glDisable(GL_NORMAL_ARRAY);
}
else
{
glBindProgramNV(GL_VERTEX_PROGRAM_NV, vertex_program_id);
glEnable(GL_VERTEX_PROGRAM_NV);
glVertexAttribPointerNV( 0, 3, GL_FLOAT, 0, vertexArray );
glVertexAttribPointerNV( 1, 3, GL_FLOAT, 0, normalArrays[0] );
glVertexAttribPointerNV( 2, 2, GL_FLOAT, 0, texCoordArrays[0] );
glVertexAttribPointerNV( 3, 2, GL_FLOAT, 0, texCoordArrays[0] );
glVertexAttribPointerNV( 4, 3, GL_FLOAT, 0, normalArrays[2] );
glEnableClientState( GL_VERTEX_ATTRIB_ARRAY0_NV );
glEnableClientState( GL_VERTEX_ATTRIB_ARRAY1_NV );
glEnableClientState( GL_VERTEX_ATTRIB_ARRAY2_NV );
glEnableClientState( GL_VERTEX_ATTRIB_ARRAY3_NV );
glEnableClientState( GL_VERTEX_ATTRIB_ARRAY4_NV );
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, indexArray);
glDisableClientState( GL_VERTEX_ATTRIB_ARRAY0_NV );
glDisableClientState( GL_VERTEX_ATTRIB_ARRAY1_NV );
glDisableClientState( GL_VERTEX_ATTRIB_ARRAY2_NV );
glDisableClientState( GL_VERTEX_ATTRIB_ARRAY3_NV );
glDisableClientState( GL_VERTEX_ATTRIB_ARRAY4_NV );
glDisable(GL_VERTEX_PROGRAM_NV);
}
glDisable(GL_VERTEX_ARRAY);
view.endGL();
return MS::kSuccess;
}
int hwReflectBumpShader_NV20::normalsPerVertex()
{
return 3;
}
int hwReflectBumpShader_NV20::texCoordsPerVertex()
{
return 1;
}