Layered Textures

This is a discussion on Layered Textures within the Game Programming forums, part of the General Programming Boards category; Hello, im not sure what the method is called, but basically, i want to put 2 textures on top of ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    288

    Layered Textures

    Hello,

    im not sure what the method is called, but basically, i want to put 2 textures on top of each other, while setting a specific color in the second texture to be transparent.

    ive tried almost every possibility imaginable, heres the code i used: (notice the sections commented out from failed attempts)

    Code:
    g_Device->SetTexture(0, woodtex); //wood.png
    g_Device->SetTexture(1, bloodtex); //blood.png
    
    
    /*g_Device->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, 0);
    g_Device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG2);
    g_Device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_DIFFUSE);
    g_Device->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_TEXTURE);
    g_Device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
    g_Device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE);*/
    
    //g_Device->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 0);
    //g_Device->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
    //g_Device->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
    
    //g_Device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
    //g_Device->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
    
    //g_Device->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
    
    /*g_Device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
    g_Device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
    g_Device->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);*/
    
    g_Device->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, 0);
    
    g_Device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG2);
    g_Device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_DIFFUSE);
    g_Device->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_TEXTURE);
    
    //g_Device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
    //g_Device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_CURRENT);
    
    g_Device->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 0);
    
    g_Device->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_SELECTARG2);
    g_Device->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_CURRENT);
    g_Device->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_TEXTURE);
    
    g_Device->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
    g_Device->SetTextureStageState(1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
    g_Device->SetTextureStageState(1, D3DTSS_ALPHAARG2, D3DTA_CURRENT);
    
    //g_Device->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_BLENDTEXTUREALPHAPM);
    //g_Device->SetTextureStageState(1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
    //g_Device->SetTextureStageState(1, D3DTSS_ALPHAARG2, D3DTA_CURRENT);
    
    g_Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
    g_Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
    
    g_Device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
    
    g_Device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, testquad);
    
    g_Device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
    the transparent color is the green in blood.png, i want that part to show through to the wood (so far all i could manage was making it show through to the Clear(..) color)

    here are the images im using:
    Attached Images Attached Images   

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    after i researched this a bit more, i found that some cards (most of the older cards) dont support many texture stages, so i thought that this might be a better method:

    i create another quad, which has the texture set to blood.png with green as the color key, and alpha blending enabled. i move this quad over (0.00025 higher) the position of the quad with the texture set to wood.png. this would eliminate the need for texture stages, but would this be more/less efficient?

    also, what should i set the height to be, currently im thinking that:

    bloodquad.y = woodquad.y + 0.00025;

    should be enough, but is that too low/too high?

    thanks in advance

  3. #3
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    Hmm.... I don't know if this applies to D3D, but at least in OpenGL this method may lead to some problems. That is, when two (or more) blood-quads intersect some flickering and other artifacts may occur. That will happen because they have exactly the same y-coordinate. So what you need to do is to check if there is such intersection before creating a new blood-quad. If there is, elivate it above the other quad. The gap between blood-quads (and also between the wood and these quads) mustn't be too large: a value like 0.000001 will do, I think (or just use the smallest float number D3D and C++ allows). When you have implemented this, you will be able to have many stains on top of eachother without the player noticing that they are higher then the floor.
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    is this really the best way? in some games ive notice that they use more than 1 texture stage with black as the transparent color (i know its black because on some older cards it doesnt show the black as transparent).

    besides, lets just say theres around 200 or 300 blood stains on the ground, thats an extra 1200 vertices/600 triangles, when it could be done without the triangles.

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    1,208
    I think what you want is multitexturing. With this you can use several textures and combine them to a single texture you can use.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    got any good places to start? im googling it now

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    1,208
    Sorry no, I dont program with DirectX, im an openGL guy.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  8. #8
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    is this really the best way?
    No, it's not. First of all you are right, it adds drastically to the number of triangles and, besides that, you have to test for interection each time.

    But: haven't you tried multitexturing in the first place and it didn't work? I just thought that you were looking for an alternative approach.

    Well, anyway, Shakti is right about multitexturing: it's much more efficient and easy. Stick to it if you can
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    i did try multitexturing but seeing as i never done it before, i kept getting really adverse effects, and i tried alot of options as you can see in the first post. anyway, im looking for tutorials on multitexturing, ill post again once i have a solution.

    thanks for the help guys

  10. #10
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    You may need to write a pixel shader to do this. Simply add the two textures together and output the result in the color register.

  11. #11
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    pixel shaders are beyond me.

    besides, i thought that pixel/vertex shaders were only supported on newer cards? and that they are a real performance hog?

  12. #12
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    You can get away with drawing the extra quads if you set up your D3DRS_ZFUNC render state correctly (set it to D3DCMP_ALWAYS when drawing the blood quads). You may also try drawing both the woodtex and the bloodtex to a writable texture surface, and then draw with that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Textures
    By fighter92 in forum Game Programming
    Replies: 1
    Last Post: 03-21-2009, 10:57 AM
  2. Textures not texturing the way I want
    By scwizzo in forum Game Programming
    Replies: 0
    Last Post: 12-16-2008, 11:12 AM
  3. loading textures from resources (dx8/9)
    By X PaYnE X in forum Game Programming
    Replies: 1
    Last Post: 12-25-2005, 03:44 PM
  4. the effects of textures on my frame rate
    By DavidP in forum Game Programming
    Replies: 37
    Last Post: 10-03-2003, 11:24 AM
  5. Q3A textures
    By glUser3f in forum Game Programming
    Replies: 11
    Last Post: 09-04-2003, 03:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21