Tricky Texturing problem

Started by
3 comments, last by NotTaxes 22 years, 5 months ago
I am trying to find a really quick way to blend two textures together using DirectX. The blending would effectively add the rgba components together to create a single texture for output. For example, on a single pixel in the texture, S1 + S2 = D R 100 20 = 120 G 0 + 10 = 10 B 10+ 10 = 20 A 0 + 100 = 100 The main reason I would like to do this is because I would like to have a primary texture and then a mask texture and be able to blend the two together, such that the alpha from the mask becomes the alpha on the primary texture. Currently I get around this by having a primary texture and then about 30 other textures with different alpha masks. This works fine, but managing the ptexture and its 30 differently masked sub-textures is a real pain in the neck. If I could blend the two textures together on the fly, it would save a lot of memory since I would just store a whole bunch of primary textures and a whole bunch of mask textures, and then just blend texture x with mask y as I render along. AND, the reason I am doing this is because I want to apply a base texture to a terrain (e.g. grass) and then smoothly apply textures to some areas without getting a ''squared-off'' look (e.g. a rocky outcrop, or a mossy hollow or whatever). I have taken a look at using texture stage states, but this doesn''t seem to solve the problem because it applies the background texture, then it alpha-blends the mask on top of it and then it blends the final texture on top of all of it.
'Doing the impossible is kind of fun' - Walt Disney
Advertisement
[Sorry, I forgot to finish that last paragraph]

The problem with the texture stage state method is that the results are only blended within their own stage. Whereas, I would like two textures, in the same stage, to have their rgba blended.

If someone can tell me how to post pictures to this forum, I would be glad to give a graphical example of what I am trying to do.
'Doing the impossible is kind of fun' - Walt Disney
1. Are you sure you want to ADD the textures together ?! - adding colours makes the results look brighter (grey+grey=white), for that reason it''s usually used for things which glow such as explosions. Rocky outcrops and mossy hollows tend not to glow (at least not where I live).


2. The texture stage states form a "pipeline", the result of the operation in stage 0 is one of the inputs to stage 1, the output of the operation in stage 1 is one of the inputs to stage 2 and so on. There are 2 channels in the pipeline. One for alpha and one for colours. Each channel can have a different set of operations so what you can do is carry the result of the previous stage to the stage after the current one without performing any operations. So you''re wrong if you think "that the results are only blended within their own stage" (If I interpret your post correctly).


3. I assume (from your description) what you have is:

BaseTextureRGB = Base texture (e.g. grass)
BaseTextureAlpha = Nothing
MaskTextureRGB = Texture to blend on top (e.g. rocks)
MaskTextureAlpha = The blending mask

I assume what you want to do is something like:

PixelRGB = (MaskA * MaskRGB) + ((1-MaskA) * BaseRGB)

To do that you''d do something like:

Stage 0 Texture = BaseTexture
Stage 0, COLORARG1, TEXTURE // base texture RGB
Stage 0, COLOROP, SELECTARG1 // change to MODULATE for lighting
Stage 0, COLORARG2, DIFFUSE

Stage 0, ALPHAARG1, TEXTURE // dummy operation, we''ll discard
Stage 0, ALPHAOP, SELECTARG2 // this later, but we do need the
Stage 0, ALPHAARG2, DIFFUSE // alpha enabled in this stage


Stage 1, Texture = MaskTexture
Stage 1, COLORARG1, TEXTURE // mask texture RGB
Stage 1, COLOROP, BLENDTEXTUREALPHA
Stage 1, COLORARG2, CURRENT // RGB from stage 0

Stage 1, ALPHAARG1, TEXTURE // mask texture A
Stage 1, ALPHAOP, SELECTARG1 // A comes from texture only
Stage 1, ALPHAARG2, CURRENT


Try it in the MFCTexture sample which comes with the SDK - it does the kind of blend you''re asking for.

--
Simon O''''Connor
Creative Asylum Ltd
www.creative-asylum.com

Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site

OK, I''ve tried playing with the MFC texture blender, but it just doesn''t quite give the results I''m looking for. I''ve been able to produce these kinds of results already, but the problem is that the result of the first two texture stages blend correctly, but they contain no alpha in themselves, so when they get blended with the third stage, they just over-write the destination completely.

Let me see if I can explain this a bit better.

Assume that you have a completely black texture, with no alpha, and a completely white texture with no alpha. You also have a third texture that contains a chequered(sp?) alpha pattern.

I would like to be able to apply this chequered pattern to the white texture to CREATE a white texture that has a chequered alpha. I could then use this new texture and apply it over the black texture to get a black and white chess-board. Similarly, I could have a gold-laced marble texture and a blue-laced marble texture and apply the same chequered alpha texture to the blue-lace texture and combine the result with the gold-laced texture to create a gold- and blue- laced marble chess board.

But, instead of actually "creating" the merged texture (which I currently do with the DirectX Texture Tool, saving all the possible permutations of masks and base textures), I would like to be able to just supply two textures and a mask and get the end result on the fly.

What''s happening with the TSS is that I am getting the alpha texture blended onto the white texture, but the white texture doesn''t "contain" any alpha, so when I blend it with the black texture I just end up with a completely white result... or a completely gold-laced texture in the second case.



'Doing the impossible is kind of fun' - Walt Disney
G''day!

You would probably want to avoid using 3 textures since very few cards support them, but you could multi-pass this. Say you have tex 1 (grass), tex2 (sand) and tex 3 (your alpha transition mask).

Draw tex1 using the mask (2 stages required, Simon posted the stage set up recently), then draw tex2 using the mask but use the inverse alpha.

Actually, I guess you could draw tex1, then do another pass with tex2 & mask. Using the mask twice isn''t really needed. For the second pass I think the stages listed above would work if you set the second stage:
Stage 1, COLOROP, SELECTARG1
Stage 1, COLORARG1, CURRENT // RGB from stage 0

I think that''s right. Anyway, Simon posted them recently (a few times IIRC).


Stay Casual,

Ken
Drunken Hyena
Stay Casual,KenDrunken Hyena

This topic is closed to new replies.

Advertisement