HowToC:TransTextures

From S2PLOT

(Difference between revisions)
Jump to: navigation, search
Revision as of 05:16, 23 November 2007
Dbarnes (Talk | contribs)
(How to make textures transparent)
← Previous diff
Current revision
Dbarnes (Talk | contribs)
(How to make textures transparent)
Line 18: Line 18:
// get pointer to texture bitmap, and width and height // get pointer to texture bitmap, and width and height
int sbw, sbh; int sbw, sbh;
- char *starbits = ss2gt(texid, &sbw, &sbh);+ unsigned char *starbits = ss2gt(texid, &sbw, &sbh);
unsigned char *idx = (unsigned char *)starbits; unsigned char *idx = (unsigned char *)starbits;
float val; float val;

Current revision

How to make textures transparent

It's very common to want to read a texture from an existing bitmap image. It's also pretty common to want to modify that texture so that parts of it are transparent. One particular occasion this is useful is when you wish to create nice soft blobs for billboarding.

Here's an example bit of code that uses the S2PLOT texture functions to:

  • load an image as a texture,
  • get access to the bitmap of the texture,
  • set the transparency of each pixel based on the colour of each pixel, and
  • make the image completely white.

The resultant texture can then be drawn in different colours and at different overall transparency using S2PLOT's facet and billboard functions.

  // load texture from TGA file
  unsigned int texid = ss2lt("./star.xpm.32x32.tga");

  // get pointer to texture bitmap, and width and height
  int sbw, sbh;
  unsigned char *starbits = ss2gt(texid, &sbw, &sbh);
  unsigned char *idx = (unsigned char *)starbits;
  float val;

  // loop over pixels making necessary changes
  for (i = 0; i < sbw * sbh; i++) {
    val = (idx[0] + idx[1] + idx[2]) * 0.33333;
    if (val > 255.) {
      val = 255.;
    } else if (val < 0.) {
      val = 0.;
    }
    idx[3] =  (char)(val);
    idx[0] = idx[1] = idx[2] = 255;    
    idx += 4;
  }

  // update the texture with the new pixels
  ss2pt(texid);
Personal tools