simpleImageFile.cpp
#include <maya/MPxImageFile.h>
#include <maya/MImageFileInfo.h>
#include <maya/MImage.h>
#include <maya/MFnPlugin.h>
#include <maya/MStringArray.h>
#include <maya/MIOStream.h>
#if defined(OSMac_MachO_)
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#else
#include <GL/gl.h>
#include <GL/glu.h>
#endif
MString kImagePluginName( "SimpleImageFile");
class SimpleImageFile : public MPxImageFile
{
public:
SimpleImageFile();
virtual ~SimpleImageFile();
static void* creator();
virtual MStatus open( MString pathname, MImageFileInfo* info);
virtual MStatus load( MImage& image, unsigned int idx);
virtual MStatus glLoad( const MImageFileInfo& info, unsigned int imageNumber);
private:
void populateTestImage( float* pixels, unsigned int w, unsigned int h);
};
SimpleImageFile::SimpleImageFile()
{
}
SimpleImageFile::~SimpleImageFile()
{
}
void * SimpleImageFile::creator()
{
return new SimpleImageFile();
}
MStatus SimpleImageFile::open( MString pathname, MImageFileInfo* info)
{
if( info)
{
info->width( 512);
info->height( 512);
info->channels( 3);
info->pixelType( MImage::kFloat);
info->hardwareType( MImageFileInfo::kHwTexture2D);
}
return MS::kSuccess;
}
void SimpleImageFile::populateTestImage( float* pixels, unsigned int w, unsigned int h)
{
#define RAINBOW_SCALE 4.0f
unsigned int x, y;
for( y = 0; y < h; y++)
{
float g = RAINBOW_SCALE * y / (float)h;
for( x = 0; x < w; x++)
{
float r = RAINBOW_SCALE * x / (float)w;
*pixels++ = r;
*pixels++ = g;
*pixels++ = RAINBOW_SCALE * 1.5f - r - g;
}
}
}
MStatus SimpleImageFile::load( MImage& image, unsigned int)
{
unsigned int w = 512;
unsigned int h = 512;
image.create( w, h, 3, MImage::kFloat);
populateTestImage( image.floatPixels(), w, h);
return MS::kSuccess;
}
MStatus SimpleImageFile::glLoad( const MImageFileInfo& info, unsigned int)
{
unsigned int w = info.width();
unsigned int h = info.height();
float* pixels = new float[ w * h * 3];
populateTestImage( pixels, w, h);
::glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_FLOAT, pixels);
delete[] pixels;
return MS::kSuccess;
}
MStatus initializePlugin( MObject obj )
{
MFnPlugin plugin( obj, PLUGIN_COMPANY, "8.0", "Any" );
MStringArray extensions;
extensions.append( "moo");
CHECK_MSTATUS( plugin.registerImageFile(
kImagePluginName,
SimpleImageFile::creator,
extensions));
return MS::kSuccess;
}
MStatus uninitializePlugin( MObject obj )
{
MFnPlugin plugin( obj );
CHECK_MSTATUS( plugin.deregisterImageFile( kImagePluginName ) );
return MS::kSuccess;
}