Sign in to follow this  
BEngKohn

Masking for textures, help please.

Recommended Posts

While working off of the "Isometric 'n Hexagonal Maps" article I ran into a problem that I can't find any info on. Perhaps I'm not searching on the right terms, and I apologize in advance if the answer is right in front of me. I have a routine that puts a base terrain texture on a hex, using DirectX3D. In some cases I would like to modify that texture before I put it on the hex. So, for example, I have a texture that represents grass. I have a big white dot on a black background. I'd like to do something that represents a bitwise OR so that I end up with a texture with a big white dot on a grass background. so basically, I want the results of hexTexure = grassyTexture & whiteDotTexture What are some ways to do this? I use C#, if that matters. The only way I know will work would be to store the textures as bitmaps, go through each pixel and OR them, and then make a texture from that bitmap. I can't imagine this is (1) the right way to do this, or (2) sufficiently fast. Thank you in advance.

Share this post


Link to post
Share on other sites
The term you're looking for is "multitexturing". This is where you give D3D 2 textures, and let it combine them in some way. The way you describe, you want to do additive blending.

There's a bit in the DirectX SDK about it, and there's a bunch of articles and tutorials on the Internet. Most will be in C++, but the difference between C++ and C# DirectX is minimal.

I'll post some code samples when I get home (if nobody beats me to it), in around an hour.

Share this post


Link to post
Share on other sites
Ok, a bit longer than an hour...

Anyway, assuming you have your grass texture in pGrass, and your white dot in pDot (Code is C++, but like I said, it should be similar to C#, pGrass and pDot are IDirect3DTexture9*'s):

// Assign the textures to stages 0 and 1
pDevice->SetTexture(0, pGrass);
pDevice->SetTexture(1, pGrass);

// Set stage 1 to additive blend to stage 0 (add current (stage 0) to texture in this stage)
pDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_ADD);
pDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_CURRENT);
pDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);

// Disable stage 2 (just for good practice)
pDevice->SetTextureStageState(2, D3DTSS_COLOROP, D3DTOP_DISABLE);



That should work. My method requires that each vertex has 2 sets of texture coordinates - the first set is for the grass texture, and the second set is for the dot texture. You can call:
pDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 0);
if you want to share one set of texture coordinates (If the coordinates are the same, of course).

Share this post


Link to post
Share on other sites
First of all, thank you!

After some digging, I came up with this translation:


device.SetTexture(0, hexTexture);
device.SetTexture(1, textureWhite);

device.SetTextureStageState(1, TextureStageStates.ColorOperation, (int)TextureOperation.Add);
device.SetTextureStageState(1, TextureStageStates.ColorArgument1, (int)TextureArgument.Current);
device.SetTextureStageState(1, TextureStageStates.ColorArgument1, (int)TextureArgument.TextureColor);
device.SetTextureStageState(2, TextureStageStates.ColorOperation, (int)TextureOperation.Disable);

// since the textures do have the same u,v coordinates
device.SetTextureStageState(1, TextureStageStates.TextureCoordinateIndex, false);



which seemed to do something like what I want.

No idea why it made me cast to int on the last argument.

Now I have to figure out how to turn it back to normal!

Is there a good reference for this somewhere? The MSDN pages are less than helpful if I'm in the right place.

Again, thank you very much.

Share this post


Link to post
Share on other sites
Quote:
Original post by BEngKohn
No idea why it made me cast to int on the last argument.

Now I have to figure out how to turn it back to normal!

Is there a good reference for this somewhere? The MSDN pages are less than helpful if I'm in the right place.

Again, thank you very much.
What happens when you remove the int cast? What error do you get? SetTextureStageState() can take a bunch of different types for the last parameter, depending on the value of the 2nd parameter, which may be a slight problem in C# which the int cast fixes.

There's a few links in the Forum FAQ to various sites with D3D tutorials in them, and several will cover multitexturing - although I don't know which ones offhand.

Share this post


Link to post
Share on other sites
The message I get when I remove the int cast is that it can't convert to float, and that it can't find an overload for the function that matches that signature.

It just seemed odd that what I believed was basically an enumeration of ints triggered this, but it is just a curiosity, not a big deal (I think, at least).

Thanks for all the help. I'm still trying to find some details on this stuff. The forum FAQs haven't proven fruitful so far, but I'm still searching.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this