Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

UncleDarryl

D3D9 Single-pass multitexturing

This topic is 5274 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm new to D3D and I'm having trouble getting a couple of textures blended properly and applied to a mesh in a single pass. My mesh has a 32-bit texture, and I'd like to use its alpha channel to control how "strongly" the reflection map is applied in places. The reflection map is just a straightforward 24-bit texture. My approach has been to set up texture stage 0 with the mesh's texture and normal UV coords, and then stage 1 with the reflection map and spherical coordinate generation. However I can't seem to get them to blend properly. I've tried to switch the stages and blend modes around as far as my understanding of SetTextureStageState will go, but still haven't been able to come up with the desired effect. Can anyone help me out with how the texture stages/blending operations should be set up, or point me to a resource? Thank you [edited by - UncleDarryl on June 5, 2004 2:43:56 AM]

Share this post


Link to post
Share on other sites
Advertisement
Shader? We don''t need no stinkin shaders.

// Base texture

SetTSS(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
SetTSS(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
SetTSS(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
SetTSS(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
SetTSS(0, D3DTSS_TEXCOORDINDEX, 0);

// Sphere map mixed by previous alpha

SetTSS(1, D3DTSS_COLOROP, D3DTOP_BLENDCURRENTALPHA);
SetTSS(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
SetTSS(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
SetTSS(1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
SetTSS(1, D3DTSS_ALPHAARG1, D3DTA_CURRENT);
// Note we use CSN rather than spheremap since spheremap is

// new to DX9 an the docs never mention how it''s implemented.

// Does the driver translate this, or do you need a new card?

// Best to stick to the old way.

SetTSS(1, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACENORMAL);
// End texture stage chain

SetTSS(2, D3DTSS_COLOROP, D3DTOP_DISABLE);
SetTSS(2, D3DTSS_ALPHAOP, D3DTOP_DISABLE);

// Scale and offset X,Y of normal from -1..1 to 0..1

D3DXMatrixScaling(&texmat,0.5,0.5,1,1);
texmat._41 = 0.5;
texmat._42 = 0.5;
SetTransform(D3DTS_TEXTURE0, &texmat);
// Enable transform

SetTransform(1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTTF_COUNT3);

// Remember to turn off texture transform after rendering!


Share this post


Link to post
Share on other sites
Thanks for the replies.

Namethatnobodyelsetook, I tried your code segment (thank you) - it appears to blend the textures properly, but it loses (or overrides?) the actual diffuse shading on the mesh. It looks like it''s being lit with 100% ambient now (my scene light/material ambient values are well below this).

I''d run into this particular problem before - I was able to partially get the maps blended the way I wanted, but it "removed" the diffuse shading wherever the reflection map was visible. In the current instance, diffuse shading is gone entirely.

Any idea what I am doing wrong?

Thank you

Share this post


Link to post
Share on other sites
Oh, sorry. Yeah, I never blended in lighting. Depending on how you want lighting to work, do one of the following.

(base * light) blendto (spheremap)
Change the stage 0 colorop to modulate (or modulate2x), and add a stage 0 arg2 of D3DTA_DIFFUSE.

(base blendto spheremap) * light
Change the disables to stage 3.
Change stage 2 colorop modulate or modulate2x, with arg1 of current and arg2 of diffuse. Set stage2 alpha to something (selectarg1, current for example).

Share this post


Link to post
Share on other sites
Thank you, Namethatnobodyelsetook.

I wasn't sure what you meant by "Change the disables to stage 3.", but I added the stage 2 color states and it worked just fine. I found it wasn't necessary to add the stage 2 alpha states (unless I missed something), the three color state calls worked fine.

I'm going to try and see if I can get the lighting blended in with my original method. Putting the 32-bit tex in stage 0 is forcing me to disable alpha blending before rendering meshes with these kinds of textures applied, and then re-enabling it after. May not a big deal, but a couple less state changes I'd like to have to make.

Thanks

[edited by - UncleDarryl on June 7, 2004 3:41:10 PM]

Share this post


Link to post
Share on other sites
as a help, be sure and play with the MFCTex sample in the DX Docs. you can input your homemade textures and work wit them till you get the desired look, the MFC App will output the necessary state calls and the order to duplicate the effect in code.

Dredd
________________________________________

"To die with your sword still in its sheath is most regrettable" -- Miyomoto Musashi


Share this post


Link to post
Share on other sites
quote:
Original post by UncleDarryl
I wasn''t sure what you meant by "Change the disables to stage 3.", but I added the stage 2 color states and it worked just fine. I found it wasn''t necessary to add the stage 2 alpha states (unless I missed something), the three color state calls worked fine.


Two problems.... You need the disables, and you need an alphaop in stage2.

Problem 1:

I meant change the
SetTSS(2, D3DTSS_COLOROP, D3DTOP_DISABLE)
SetTSS(2, D3DTSS_ALPHAOP, D3DTOP_DISABLE)
to
SetTSS(3, D3DTSS_COLOROP, D3DTOP_DISABLE)
SetTSS(3, D3DTSS_ALPHAOP, D3DTOP_DISABLE)

This terminates your blending stages. To see why this is important keep reading. Lets make two effects, that we don''t terminate and see what happens. One will be your 3 stage effect above, and the other just a simple 1 stage diffuse texture mixed with lighting.

Setup effect 1 (colorops shown only):
0 - ARG1, TEXTURE
1 - BLENDCURRENTALPHA, CURRENT, TEXTURE
2 - MODULATE, CURRENT, DIFFUSE

Setup effect 2 (colorops shown only):
0 - MODULATE TEXTURE, DIFFUSE (from effect 2)
1 - BLENDCURRENTALPHA, CURRENT, TEXTURE (left over from effect 1)
2 - MODULATE, CURRENT, DIFFUSE (left over from effect 1)

Notice how we still have most of effect 1 around. This is why you need to ensure you end your blending list with a disable. If effect 2 was setup correctly, stage 1 would be set to disable, finishing the effect.


Problem 2:

The SDK says you must have an alphaop in any stage where the colorop is not disable, otherwise the results are not defined. So, even though you don''t care what the result is, you must ensure you set an alphaop in case it''s currently set to DISABLE. Ignoring this will cause your code to work on some machines, or some driver versions, but not work on others. You could spend months trying to figure out what''s wrong. Just follow the few simple rules set forth in the SDK and you won''t have as much trouble.

Share this post


Link to post
Share on other sites
I wasn't aware that you had to specify an alpha op for a stage if you specify a color op - thanks for pointing that out. I can see that would've been a nightmare to debug if I ran this on another machine and had to figure out why it suddenly wasn't rendering properly.

Since I am currently using only this one blend setup, I didn't see the need to explicitly terminate it since the SDK docs state that unused stages are disabled by default. But your point is well taken, I understand why that is necessary when you've got multiple effects (which I will eventually).

To that end, my next goal is to implement render state tracking. I've been leaning towards manually tracking state changes, but a friend mentioned that the ID3DXEffectStateManager class is made for this. I'm not familiar with the ID3DXEffectXXX framework - is this worth looking into?

Thanks

[edited by - UncleDarryl on June 8, 2004 12:24:46 AM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!