Multiple objects on map, how to achieve that?

Started by
1 comment, last by cyanflare 19 years, 7 months ago
Good day folks, I am writing a strategy game and I have a directx9 programming scenario here. I would like achieve 1)Put a tile on the map. The tiles arent rectangles in my game. There are part of my tile texture has black color which will become transparent in alpha blending so that tiles can join together nicely. 2)put a castle(or other map structure) on the tile. Again, the castle texture has black color which will become transparent in alpha blending so that only the castle shows and not the black part of texture. 3)put a unit on top of castle. Same requiement here. My code is like this p3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE); p3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE); p3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); DrawTile(...); // basically draw vertices DrawCastle(...); DrawUnit(...); The result is unit blends into castle then blends into tile. That isnt really what I want. What I would like to achieve is black becomes transparent and other keeps whatever color it is. A bit like: p3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE); p3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ZERO); Except the texture black color over writes the destination texture. I have tried a few combination of blendings here but still cant achieve what I want. Any help is appreciated Cheers
Advertisement
You must make black become transparent yourself, and for this you must give it an alpha of 0. There are several ways to do this:

- Load the image from a file that contains an alpha channel (such as Targa .tga), and set the alpha channel to 0 for the black areas.
- Load the image from a bitmap file, and use a loading function that takes a colorkey and makes all pixels of that color transparent (just choose black).
- Create your own loader, that sets an alpha of 255 for all pixels, except black pixels, which receive an alpha of 0, by creating a texture, getting its surface, and editing the raw byte data after reading them from a file.

Once your alpha has been loaded, you can use either:
SRC = ONE
DST = ZERO
if you only want your pixels to be fully transparent or fully opaque, or:
SRC = SRCALPHA
DST = INVSRCALPHA
if you want to have multiple levels of transparency for your pixels.

In any case, don't forget to activate *alpha testing*, which will discard pixels if their alpha is too small (they become transparent).
Thanks ToohrVyk.

I think you are right that my problem lies in the texture loading. My code is like this:

hResult = D3DXCreateTextureFromFileEx(m_pD3DDevice, "image\\grassland.bmp",
D3DX_DEFAULT, D3DX_DEFAULT, 1, 0, D3DFMT_A8B8G8R8,
D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0x00FFFFFF,
NULL, NULL, &m_pTextures[GX_TILE_TYPE_GRASS]);

I have tried using different color key eg 0xFF000000. What should I use in my case to make black transparent. I also suspect that the black in my bitmap may not be "real". Are there any free graphic tool that I can use to check the color value.
Sorry for newbish questions. I am an experience programmer but just starting on the graphic programming.

Cheers

This topic is closed to new replies.

Advertisement