HowToC:TransTextures
From S2PLOT
(Difference between revisions)
Revision as of 05:11, 23 November 2007 Dbarnes (Talk | contribs) (→How to make textures transparent) ← Previous diff |
Revision as of 05:16, 23 November 2007 Dbarnes (Talk | contribs) (→How to make textures transparent) Next diff → |
||
Line 1: | Line 1: | ||
- | ==How to make textures transparent== | + | ===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. Here's an example bit of code that uses the S2PLOT texture functions to: | + | 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, | * load an image as a texture, | ||
- | * get access to the bitmap of the texture, and | + | * get access to the bitmap of the texture, |
- | * make faint pixels in the texture transparent. | + | * 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. | ||
+ | |||
+ | <code><pre> | ||
+ | // load texture from TGA file | ||
+ | texid = ss2lt("./star.xpm.32x32.tga"); | ||
+ | |||
+ | // get pointer to texture bitmap, and width and height | ||
+ | int sbw, sbh; | ||
+ | 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); | ||
+ | </pre></code> |
Revision as of 05:16, 23 November 2007
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
texid = ss2lt("./star.xpm.32x32.tga");
// get pointer to texture bitmap, and width and height
int sbw, sbh;
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);