renderViewRenderCmd.cpp
#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>
class renderViewRender : public MPxCommand
{
public:
renderViewRender() {};
~renderViewRender() {};
virtual MStatus doIt ( const MArgList& );
static void* creator();
static MSyntax newSyntax();
MStatus parseSyntax (MArgDatabase &argData);
static const char * cmdName;
private:
bool doNotClearBackground;
};
static const char * kDoNotClearBackground = "-b";
static const char * kDoNotClearBackgroundLong = "-background";
const char * renderViewRender::cmdName = "renderViewRender";
void* renderViewRender::creator()
{
return new renderViewRender;
}
MSyntax renderViewRender::newSyntax()
{
MStatus status;
MSyntax syntax;
syntax.addFlag( kDoNotClearBackground, kDoNotClearBackgroundLong );
CHECK_MSTATUS_AND_RETURN(status, syntax);
return syntax;
}
MStatus renderViewRender::parseSyntax (MArgDatabase &argData)
{
doNotClearBackground = argData.isFlagSet( kDoNotClearBackground );
return MS::kSuccess;
}
MStatus initializePlugin( MObject obj )
{
MFnPlugin plugin( obj, PLUGIN_COMPANY, "4.5" );
MStatus stat;
stat = plugin.registerCommand( renderViewRender::cmdName,
renderViewRender::creator,
renderViewRender::newSyntax);
if ( !stat )
stat.perror( "registerCommand" );
return stat;
}
MStatus uninitializePlugin( MObject obj )
{
MFnPlugin plugin( obj );
MStatus stat;
stat = plugin.deregisterCommand( renderViewRender::cmdName );
if ( !stat )
stat.perror( "deregisterCommand" );
return stat;
}
RV_PIXEL evaluate(int x, int y)
{
unsigned int distance = (unsigned int) sqrt(double((x*x) + (y*y)));
RV_PIXEL pixel;
pixel.r = 255.0f;
pixel.g = pixel.b = 155.0f + (distance % 20) * 5;
pixel.a = 255.0f;
return pixel;
}
MStatus renderViewRender::doIt( const MArgList& args )
{
MStatus stat = MS::kSuccess;
if (!MRenderView::doesRenderEditorExist())
{
setResult( "Cannot renderViewRender in batch render mode. "
"Please run in interactive mode, "
"so that the render editor exists." );
return MS::kFailure;
}
else
{
cout<<"Past doesRenderEditorExist()"<<endl;
}
MArgDatabase argData( syntax(), args );
parseSyntax( argData );
M3dView curView = M3dView::active3dView();
MDagPath camDagPath;
curView.getCamera( camDagPath );
cout<<"Rendering camera"<<camDagPath.fullPathName().asChar()<<endl;
if( MRenderView::setCurrentCamera( camDagPath ) != MS::kSuccess )
{
setResult( "renderViewRender: error occurred in setCurrentCamera." );
return MS::kFailure;
}
unsigned int image_width = 640, image_height = 480;
if (MRenderView::startRender( image_width, image_height, doNotClearBackground) != MS::kSuccess)
{
setResult( "renderViewRender: error occured in startRender." );
return MS::kFailure;
}
unsigned int num_side_tiles = 8;
unsigned int average_tiles_width = image_width / num_side_tiles;
unsigned int average_tiles_height = image_height / num_side_tiles;
for (unsigned int tile_y = 0; tile_y < num_side_tiles; tile_y++)
{
for (unsigned int tile_x = 0; tile_x < num_side_tiles; tile_x++)
{
int min_x = tile_x * average_tiles_width;
int max_x;
if ((tile_x+1) == num_side_tiles)
max_x = image_width-1;
else
max_x = (tile_x + 1) * average_tiles_width - 1;
int min_y = tile_y * average_tiles_height;
int max_y;
if ((tile_y+1) == num_side_tiles)
max_y = image_height-1;
else
max_y = (tile_y + 1) * average_tiles_height - 1;
unsigned int tile_width = max_x - min_x + 1;
unsigned int tile_height = max_y - min_y + 1;
RV_PIXEL* pixels = new RV_PIXEL[tile_width * tile_height];
unsigned int index = 0;
for (unsigned int j = 0; j < tile_height; j++ )
{
for (unsigned int i = 0; i < tile_width; i++)
{
pixels[index] = evaluate(i - (tile_width / 2),
j - (tile_height / 2));
index++;
}
}
if (MRenderView::updatePixels(min_x, max_x, min_y, max_y, pixels) != MS::kSuccess)
{
setResult( "renderViewRender: error occured in updatePixels." );
delete [] pixels;
return MS::kFailure;
}
delete [] pixels;
if (MRenderView::refresh(min_x, max_x, min_y, max_y) != MS::kSuccess)
{
setResult( "renderViewRender: error occured in refresh." );
return MS::kFailure;
}
}
}
if (MRenderView::endRender() != MS::kSuccess)
{
setResult( "renderViewRender: error occured in endRender." );
return MS::kFailure;
}
setResult( "renderViewRender completed." );
return stat;
}