MStatus RadiosityRenderer::prepareRenderView() {
	
	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
	{
		printf("Past doesRenderEditorExist()");
	}
	
	// get optional flags
	MArgDatabase	argData( syntax(), args );
	parseSyntax( argData );
	
	M3dView curView = M3dView::active3dView();
	MDagPath camDagPath;
	curView.getCamera( camDagPath );
	printf("Rendering camera: %s", camDagPath.fullPathName().asChar());
	
	if( MRenderView::setCurrentCamera( camDagPath ) != MS::kSuccess )
	{
		setResult( "renderViewRender: error occurred in setCurrentCamera." );
		return MS::kFailure;
	}
	
	if (MRenderView::startRender( windowWidth, windowHeight, doNotClearBackground) != MS::kSuccess)
	{
		setResult( "renderViewRender: error occured in startRender." );
		return MS::kFailure;
	}
	
	return MS::kSuccess;
}
MStatus renderViewRenderRegion::doIt( const MArgList& args )
//
//	Description:
//		Implements the MEL renderViewRenderRegion command.  This command
//		fills the currently selected Render Region with a circular blue 
//		and white pattern.  It assumes that the Render View is currently
//		displaying a 640x480 image (if it isn't, then this command will
//		resize the Render View to 640x480).
//
//	Arguments:
//		args - the argument list that was passes to the command from MEL.
//				-background/-b renders the pattern without clearing the region
//
//	Return Value:
//		MS::kSuccess - command succeeded
//		MS::kFailure - command failed (returning this value will cause the 
//                     MEL script that is being run to terminate unless the
//                     error is caught using a "catch" statement.
//
{
	MStatus stat = MS::kSuccess;

	unsigned int resX = 640;
	unsigned int resY = 480;

	// Check if the render view exists. It should always exist, unless
	// Maya is running in batch mode.
	//
	if (!MRenderView::doesRenderEditorExist())
	{
		setResult( "Cannot renderViewRenderRegion in batch render mode. "
				   "Please run in interactive mode, "
				   "so that the render editor exists." );
		return MS::kFailure;
	}

	// get optional flags
	MArgDatabase	argData( syntax(), args );
	parseSyntax( argData );

	// Pick a camera, and tell the Render View that we will be rendering
	// from its point of view.  Just use the camera for the active 
	// modelling view.
	//
	M3dView curView = M3dView::active3dView();
	MDagPath camDagPath;
	curView.getCamera( camDagPath );
	cout<<"Region rendering camera"<<camDagPath.fullPathName().asChar()<<endl;

	if( MRenderView::setCurrentCamera( camDagPath ) != MS::kSuccess )
	{
		setResult( "renderViewRenderRegion: error occurred in setCurrentCamera." );
		return MS::kFailure;
	}

	// Retrieve the dimensions of the currently selected Render Region.
	//
	unsigned int regionLeft, regionRight, regionBottom, regionTop;
	stat = MRenderView::getRenderRegion( regionLeft, regionRight, 
										 regionBottom, regionTop );
	if( stat != MS::kSuccess )
	{
		setResult( "renderViewRenderRegion: error occurred in getRenderRegion." );
		return MS::kFailure;
	}


	// Assume that the full rendered image is 640x480, and tell the 
	// Render View that we're about to start rendering the given region.
	//
	stat = MRenderView::startRegionRender( resX, resY, 
										   regionLeft, regionRight, 
										   regionBottom, regionTop,
										   doNotClearBackground );
	if( stat == MS::kSuccess )
	{
		cout<<"Rendering Region ("<<regionLeft<<","<<regionBottom
			<<") -> ("<<regionRight<<","<<regionTop<<")"<<endl;

		unsigned int width = regionRight - regionLeft + 1;
		unsigned int height = regionTop - regionBottom + 1;
		unsigned int numPixels = width * height;
		unsigned int middleX = width / 2;
		unsigned int middleY = height / 2;

		// Allocate buffer to store the region
		//
		RV_PIXEL* pixels = new RV_PIXEL[numPixels];

		// Fill the region buffer with a circular blue/white pattern centred on 
		// the middle of the region
		//
		for( unsigned int x = 0; x < width; x++ )
		{
			for( unsigned int y = 0; y < height; y++ )
			{
				int index = y*width + x;
				int xCoord = x - middleX;
				int yCoord = y - middleY;

				pixels[index] = evaluate( xCoord, yCoord );
			}
		}
	
		// Send the pixel data to the Render View.
		//
		stat = MRenderView::updatePixels( regionLeft, regionRight, 
										  regionBottom, regionTop, 
										  pixels );

		if( stat != MS::kSuccess )
		{
			setResult( "renderViewRenderRegion: error occurred in updatePixels." );
			return MS::kFailure;
		}

		// Force a refresh of the region in the Render View window.
		//
		stat = MRenderView::refresh( regionLeft, regionRight, 
									 regionBottom, regionTop );
		if( stat != MS::kSuccess )
		{
			setResult( "renderViewRenderRegion: error occurred in refresh." );
			return MS::kFailure;
		}
	}
	else
	{
		setResult( "renderViewRenderRegion: error occurred in startRegionRender." );
		return MS::kFailure;
	}

	// Notify the Render View that we are done rendering the region.
	//
	stat = MRenderView::endRender();
	if( stat != MS::kSuccess )
	{
		setResult( "renderViewRenderRegion: error occurred in endRender." );
		return MS::kFailure;
	}

	setResult( "renderViewRenderRegion completed." );
	return stat;
}
MStatus renderViewInteractiveRender::doIt( const MArgList& args )
//
//	Description:
//		Implements the MEL renderViewInteractiveRender command. This command
//		Draws a 640x480 tiled pattern of circles into Maya's Render
//		View window.
//
//	Arguments:
//		args - The argument list that was passed to the command from MEL.
//
//	Return Value:
//		MS::kSuccess - command succeeded
//		MS::kFailure - command failed (returning this value will cause the 
//                     MEL script that is being run to terminate unless the
//                     error is caught using a "catch" statement.
//
{
	MStatus stat = MS::kSuccess;

	// Check if the render view exists. It should always exist, unless
	// Maya is running in batch mode.
	//
	if (!MRenderView::doesRenderEditorExist())
	{
		displayError( 
			"Cannot renderViewInteractiveRender in batch render mode.\n"
			"Run in interactive mode, so that the render editor exists." );
		return MS::kFailure;
	}
	
	// get optional flags
	MArgDatabase argData( syntax(), args );
	parseSyntax( argData );

	// We'll render a 640x480 image.  Tell the Render View to get ready
	// to receive 640x480 pixels of data.
	//
	unsigned int image_width = size[0], image_height = size[1];
	if (MRenderView::startRender( image_width, image_height, 
								  doNotClearBackground, 
								  immediateRefresh) != MS::kSuccess)
	{
		displayError("renderViewInteractiveRender: error occured in startRender.");
		return MS::kFailure;
	}

	// The image will be composed of tiles consisting of circular patterns.
	//

	// Draw each tile
	//
	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;

	RV_PIXEL* pixels = new RV_PIXEL[tileSize[0] * tileSize[1]];
	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;

				// Fill up the pixel array with some the pattern, which is 
				// generated by the 'evaluate' function.  The Render View
				// accepts floating point pixel values only.
				//
				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++;
					}
				}

				// Send the data to the render view.
				//
				if (MRenderView::updatePixels(min_x, max_x, min_y, max_y, pixels) 
					!= MS::kSuccess)
				{
					displayError( "renderViewInteractiveRender: error occured in updatePixels." );
					delete [] pixels;
					return MS::kFailure;
				}

				// Force the Render View to refresh the display of the
				// affected region.
				//
				MStatus st;
				if (fullRefresh)
					st =MRenderView::refresh(0, image_width-1, 0, image_height-1);
				else
					st = MRenderView::refresh(min_x, max_x, min_y, max_y);
				if (st != MS::kSuccess)
				{
					displayError( "renderViewInteractiveRender: error occured in refresh." );
					delete [] pixels;
					return MS::kFailure;
				}

				if (verbose)
					cerr << "Tile "<<min_x<<", "<<min_y<<
						" (iteration "<<loopId<<")completed\n";
			}
		}
	}

	delete [] pixels;

	// Inform the Render View that we have completed rendering the entire image.
	//
	if (MRenderView::endRender() != MS::kSuccess)
	{
		displayError( "renderViewInteractiveRender: error occured in endRender." );
		return MS::kFailure;
	}

	displayError( "renderViewInteractiveRender completed." );
	return stat;
}