Creating Bitmaps
 
 
 

To create a bitmap from scratch, we need to declare a pointer to point to it:

Bitmap *bmap;

Remember that the Bitmap class represents the bitmap itself. Next, we need to declare an instance of the BitmapInfo class to describe the properties of the bitmap to create:

BitmapInfo bi;

Then we initialize the BitmapInfo with the properties of the bitmap we wish to create. This is done using the methods of BitmapInfo such as SetType(), SetWidth(), etc.:

 // Initialize the BitmapInfo instance
 bi.SetType(BMM_TRUE_64);
 bi.SetWidth(320);
 bi.SetHeight(200);
 bi.SetFlags(MAP_HAS_ALPHA);
 bi.SetCustomFlag(0);

Note in the above code how the type is set. The type describes the number of bits per pixel used to describe the image, whether the images is paletted (has a color map), and if it has an alpha channel. To see a list of the types of bitmaps that may be created see List of Bitmap Types . In this example, we use BMM_TRUE_64. This format has 16-bits for each color (RGB) and alpha component. This is the format that 3ds Max uses internally in the renderer.

Once the BitmapInfo is initialized, we can call a method of the bitmap manager to create the bitmap. A global instance of the BitmapManager class exists called TheManager. This is what we use to call methods of BitmapManager as shown below:

 // Create a new bitmap
 bmap = TheManager->Create(&bi);

Note that we pass it our BitmapInfo instance. This is where the bitmap manager gets the information on the bitmap to create. If the pointer returned is NULL, an error has occurred in creating the bitmap. If the pointer is non-NULL, its valid and we can use it to call methods of the Bitmap class to work with the bitmap. The code below shows how the PutPixels() method is used to write data to the bitmap. This code sets every pixel of the image to the color and opacity specified by r, g, b and alpha.

//...
BMM_Color_64 *line, *lp, color = {r, g, b, alpha};
int bmapWidth = bmap->Width(), bmapHeight = bmap->Height();
if ((line = ( BMM_Color_64 *)calloc(bmapWidth, sizeof( BMM_Color_64))))
{
   intix, iy;
   for(ix = 0, lp = line; ix < bmapWidth; ix++, lp++)
     *lp = color;
   for(iy = 0; iy < bmapHeight; iy++)
     int res = bmap->PutPixels(0, iy, bmapWidth, line);
   free(line);
}

When we are done using the bitmap, we need to delete it. This is done by simply using the delete operator:

if (bmap) bmap->DeleteThis();