#include <maya/MSimple.h>
#include <maya/MIOStream.h>
#include <maya/MRenderView.h>
#include <maya/M3dView.h>
#include <math.h>
#include <maya/MSyntax.h>
#include <maya/MArgDatabase.h>
#include <maya/MGlobal.h>
#include <maya/MDGMessage.h>
#include <maya/MTime.h>
#include <maya/MAnimControl.h>
#include <stdlib.h>
class renderViewInteractiveRender :
public MPxCommand
{
public:
renderViewInteractiveRender() {};
~renderViewInteractiveRender() {};
static void* creator();
static MStatus updateRenderViewDefault();
static void timeChangeCB(
MTime &time,
void *clientData);
static MStatus updateRenderViewIpr();
static const char *cmdName;
private:
};
namespace {
bool fullRefresh;
bool immediateRefresh;
bool doNotClearBackground = true;
bool verbose;
bool editMode = false;
bool useRandomColors = false;
double radius;
unsigned int size[2];
unsigned int tileSize[2];
unsigned int numberLoops;
bool iprPaused = false;
bool iprMode = false;
unsigned int width = 640;
unsigned int height = 480;
unsigned int left = 0;
unsigned int right = width;
unsigned int top = height;
unsigned int bottom = 0;
MCallbackId timeChangeCallbackID;
const char *kVerbose = "-v";
const char *kVerboseLong = "-verbose";
const char *kDoNotClearBackground = "-b";
const char *kDoNotClearBackgroundLong = "-background";
const char *kRadius = "-r";
const char *kRadiusLong = "-radius";
const char *kSizeX = "-sx";
const char *kSizeXLong = "-sizeX";
const char *kSizeY = "-sy";
const char *kSizeYLong = "-sizeY";
const char *kSizeTileX = "-tx";
const char *kSizeTileXLong = "-sizeTileX";
const char *kSizeTileY = "-ty";
const char *kSizeTileYLong = "-sizeTileY";
const char *kNumberLoops = "-nl";
const char *kNumberLoopsLong = "-numberLoops";
const char *kImmediateRefresh = "-ir";
const char *kImmediateRefreshLong = "-immediateRefresh";
const char *kFullRefresh = "-fr";
const char *kFullRefreshLong = "-fullRefresh";
const char *kIprMode = "-ipr";
const char *kIprModeLong = "-iprMode";
const char *kPause = "-p";
const char *kPauseLong = "-pause";
const char *kLeft = "-lft";
const char *kLeftLong = "-leftEdge";
const char *kRight = "-rgt";
const char *kRightLong = "-rightEdge";
const char *kTop = "-tp";
const char *kTopLong = "-topEdge";
const char *kBottom = "-bot";
const char *kBottomLong = "-bottomEdge";
const char *kUseRandomColors = "-rc";
const char *kUseRandomColorsLong = "-useRandomColors";
const char *kEditMode = "-e";
const char *kEditModeLong = "-editMode";
};
const char * renderViewInteractiveRender::cmdName = "renderViewInteractiveRender";
void* renderViewInteractiveRender::creator()
{
return new renderViewInteractiveRender;
}
MSyntax renderViewInteractiveRender::newSyntax()
{
syntax.
addFlag(kDoNotClearBackground, kDoNotClearBackgroundLong);
syntax.
addFlag(kEditMode, kEditModeLong);
syntax.
addFlag(kVerbose, kVerboseLong);
syntax.
addFlag(kImmediateRefresh, kImmediateRefreshLong);
syntax.
addFlag(kFullRefresh, kFullRefreshLong);
return syntax;
}
{
doNotClearBackground = argData.
isFlagSet( kDoNotClearBackground );
fullRefresh = argData.
isFlagSet( kFullRefresh );
immediateRefresh = argData.
isFlagSet( kImmediateRefresh );
editMode = false;
radius = 50.;
size[0] = width;
size[1] = height;
tileSize[0] = 16;
tileSize[1] = 16;
numberLoops = 10;
}
void renderViewInteractiveRender::timeChangeCB(
MTime &time,
void *clientData)
{
if(iprMode && !iprPaused)
{
doNotClearBackground = true;
renderViewInteractiveRender::updateRenderView();
}
}
{
MFnPlugin plugin( obj, PLUGIN_COMPANY,
"4.5" );
stat = plugin.registerCommand( renderViewInteractiveRender::cmdName,
renderViewInteractiveRender::creator,
renderViewInteractiveRender::newSyntax);
if ( !stat )
stat.
perror(
"registerCommand" );
plugin.registerUI("registerSampleRenderer", "");
if( !stat )
timeChangeCallbackID = 0;
return stat;
}
{
stat = plugin.deregisterCommand( renderViewInteractiveRender::cmdName );
if ( !stat )
stat.
perror(
"deregisterCommand" );
if ( !stat )
stat.
perror(
"removeCallback" );
return stat;
}
RV_PIXEL renderViewInteractiveRender::evaluate(
int x,
int y)
{
double distance = sqrt(double((x*x) + (y*y))) / radius;
float percent = (float)(cos(distance*2*3.1415927)/2.+.5);
pixel.
r = color1.r * percent + color2.r * (1-percent);
pixel.
g = color1.g * percent + color2.g * (1-percent);
pixel.
b = color1.b * percent + color2.b * (1-percent);
return pixel;
}
MStatus renderViewInteractiveRender::updateRenderView()
{
{
displayError(
"Cannot renderViewInteractiveRender in batch render mode.\n"
"Run in interactive mode, so that the render editor exists." );
};
if(iprMode)
{
stat = updateRenderViewIpr();
}
else
{
stat = updateRenderViewDefault();
}
{
displayError( "renderViewInteractiveRender: error occurred in endRender." );
}
return stat;
}
MStatus renderViewInteractiveRender::updateRenderViewDefault()
{
unsigned int image_width = size[0], image_height = size[1];
doNotClearBackground,
{
displayError("renderViewInteractiveRender: error occurred in startRender.");
}
static float colors[] = { 255, 150, 69,
255, 84, 112,
255, 94, 249,
86, 62, 255,
46, 195, 255,
56, 255, 159,
130, 255, 64 };
int indx1 = 0;
int indx2 = 3*3;
for (unsigned int loopId = 0 ; loopId < numberLoops ; loopId++ )
{
color1.
r = colors[indx1];
color1.g = colors[indx1+1];
color1.b = colors[indx1+2];
color1.a = 255;
indx1 += 3; if (indx1 >= 21) indx1 -= 21;
color2.r = colors[indx2];
color2.g = colors[indx2+1];
color2.b = colors[indx2+2];
color2.a = 255;
indx2 += 6; if (indx2 >= 21) indx2 -= 21;
for (unsigned int min_y = 0; min_y < size[1] ; min_y += tileSize[1] )
{
unsigned int max_y = min_y + tileSize[1] - 1;
if (max_y >= size[1]) max_y = size[1]-1;
for (unsigned int min_x = 0; min_x < size[0] ; min_x += tileSize[0] )
{
unsigned int max_x = min_x + tileSize[0] - 1;
if (max_x >= size[0]) max_x = size[0]-1;
unsigned int index = 0;
for (unsigned int j = min_y; j <= max_y; j++ )
{
for (unsigned int i = min_x; i <= max_x; i++)
{
pixels[index] = evaluate(i, j);
index++;
}
}
{
displayError( "renderViewInteractiveRender: error occurred in updatePixels." );
delete [] pixels;
}
if (fullRefresh)
else
{
displayError( "renderViewInteractiveRender: error occurred in refresh." );
delete [] pixels;
}
if (verbose)
cerr << "Tile "<<min_x<<", "<<min_y<<
" (iteration "<<loopId<<")completed\n";
}
}
}
delete [] pixels;
}
MStatus renderViewInteractiveRender::updateRenderViewIpr()
{
unsigned int image_width = right - left;
unsigned int image_height = top - bottom;
{
displayError("renderViewInteractiveRender: error occurred in startRender.");
}
if(useRandomColors)
{
pixel.
r = (float)(rand() % 255);
pixel.
g = (float)(rand() % 255);
pixel.
b = (float)(rand() % 255);
}else
{
double currentTime = currentTimeObj.
value();
pixel.
r = (float)((sin(currentTime * 0.05) + 1.0) * 127.5);
pixel.
g = (float)((sin(currentTime * 0.10) + 1.0) * 127.5);
pixel.
b = (float)((sin(currentTime * 0.20) + 1.0) * 127.5);
}
for (unsigned int index = 0; index < image_height * image_width; ++index )
{
pixels[index] = pixel;
}
{
displayError( "renderViewInteractiveRender: error occurred in updatePixels." );
delete[] pixels;
}
delete[] pixels;
}
{
parseSyntax( argData );
if(!editMode)
{
renderViewInteractiveRender::updateRenderView();
}
return stat;
}