• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Shenjoku

DirectX 9 Image Masking Without Shaders

10 posts in this topic

I'm trying to figure out what the most efficient method is of applying an alpha transparency mask to a DirectX 9 texture in memory without using shaders. The current idea for implementing it is to do something like the following:

[CODE]
LPDIRECT3DTEXTURE9 texture, mask;

// Get the texture and the mask to apply from somewhere.

texture->Lock();
mask->Lock();

// Copy only the alpha channel from the mask into the texture, effectively applying the mask.
for (int32u i = 0; i < textureWIdth; ++i)
{
textureBits.alpha = maskBits.alpha;
}

texture->Unlock();
mask->Unlock();
[/CODE]

As you can see, this is a rather brute force method that would end up being extremely slow on large images, but I don't know of anything in DirectX 9 for doing this besides using shaders. So my question to all of you: Are there any DirectX helper function that can be used to obtain the same results?

I'm not sure if this changes anything, but the texture I'm trying to apply the mask to is being used in off-screen rendering, so it is a render target. Edited by Shenjoku
0

Share this post


Link to post
Share on other sites
This is possible in fixed function texture stages; sample the color texture in the first stage, and in the second stage, get the alpha from the second texture but take the color from the previous stage (D3DTA_CURRENT). The texture stage setup runs on the GPU hardware, unlike your proposed approach.

A pixel shader would be more simple here, though [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] Edited by Nik02
1

Share this post


Link to post
Share on other sites
[quote name='Nik02' timestamp='1340262057' post='4951237']
This is possible in fixed function texture stages; sample the color texture in the first stage, and in the second stage, get the alpha from the second texture but take the color from the previous stage (D3DTA_CURRENT). The texture stage setup runs on the GPU hardware, unlike your proposed approach.
[/quote]

That sounds interesting, but I have no idea how to do something like that. Does this have to be done every frame before rendering or something? If you have any articles or anything on it or could post some pseudo code that would be very appreciated [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] I'll try to do a bit of research on my own though.

[quote name='Nik02' timestamp='1340262057' post='4951237']
A pixel shader would be more simple here, though [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
[/quote]

I know it would be, but the engine I'm using has no support for shaders whatsoever and it would be rather difficult to add it, especially with the time constraints on the project.

EDIT: Well I looked into it and I sort of understand what to do, but I can't figure out the mixture of texture stage states to get it working. This is what I have currently, which is resulting in no change:

[CODE]
device->SetTexture(0, fTexture);
device->SetTexture(1, fMaskTexture);

// Only use the color from the first texture.
device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
device->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
device->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);

// Then use the color from the previous texture, and the alpha from the mask.
device->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
device->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_CURRENT);
device->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
device->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
device->SetTextureStageState(1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
device->SetTextureStageState(1, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
[/CODE] Edited by Shenjoku
0

Share this post


Link to post
Share on other sites
Just rendering it to a primitive right below those calls with:

[CODE]
device->SetSamplerState(0, D3DSAMP_MAGFILTER, fD3DTextureFilterType);
device->SetSamplerState(0, D3DSAMP_MINFILTER, fD3DTextureFilterType);

device->SetStreamSource(0, fVertexBuffer, 0, sizeof(DXUVert_TEXTURED));

device->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
[/CODE]
0

Share this post


Link to post
Share on other sites
Have you enabled the alpha blend renderstate and set the source + destination blend factor renderstates to meaningful values?
0

Share this post


Link to post
Share on other sites
Currently they are being set to:

[CODE]
device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
[/CODE]

Maybe there's something wrong with the mask I'm using? Here are the images I'm testing with:

The main image to apply the mask to.
[attachment=9596:test.png]

The image mask to apply. It has a green color just to make it more obvious if there was a problem with the color part. The empty areas have zero alpha, which needs to be applied to the first image somehow.
[attachment=9597:testMask.png]

This is what the result should be.
[attachment=9598:testResult.png]
0

Share this post


Link to post
Share on other sites
Well I sort of figured out what the problem is but I don't know how to fix it. After tinkering a bit I noticed that for some reason it's not picking up the alpha in the mask at all. If I set the states to:

[CODE]
// Combine the colors and use alpha from the mask only.
device->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE);
device->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_CURRENT);
device->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_TEXTURE);
device->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG2);
device->SetTextureStageState(1, D3DTSS_ALPHAARG1, D3DTA_CURRENT);
device->SetTextureStageState(1, D3DTSS_ALPHAARG2, D3DTA_TEXTURE);
[/CODE]

The texture gets tinted green as you would expect, except that the areas that are supposed to have zero alpha are also tinted green. Those areas should remain the same shouldn't they? The only logical reason why that would be happening would be because the alpha in the mask isn't getting applied or is getting ignored for some reason.

EDIT: After even more investigation I've narrowed it down to what the problem is. For some reason it's only using a single pixel, the top-left pixel at (0, 0), from the mask to apply to the entire texture. Anyone know why it would be doing this?

EDIT2: Think I figured it out! Took a lunch break to clear my head and realized something extremely important was mising; A second set of texture coordinates in the vertex data. I haven't tested it yet but I'm pretty sure this is why it isn't working. I'll post again once I confirm. Edited by Shenjoku
0

Share this post


Link to post
Share on other sites
Well that did it, now it's working perfectly. Thanks a lot for you help Nik02. In case anyone else needs help here's some pseudo code for how to get this working:

[CODE]
// The vertex struct to use has two sets of texture coordinates, one for the main texture and one for the mask.
struct tDXVertexTextureMask
{
FLOAT x, y, z;
D3DCOLOR color;
FLOAT u1, v1;
FLOAT u2, v2;
};

// Define the FVF to use.
#define D3DFVF_TEXTURE_MASK (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX2)

// Create your vertex buffer like usual using the above struct and FVF. When applying the texture coordinates,
// make sure to set u1 = u2 and v1 = v2, so the textures will line up exactly.

// Time to render! Setup the FVF and texture states.
device->SetFVF(D3DFVF_TEXTURE_MASK);

device->SetTexture(0, fTexture);
device->SetTexture(1, fMaskTexture);

device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
device->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
device->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);

// Use the color from the previous texture, and blend the alpha from the mask.
device->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
device->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_CURRENT);
device->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
device->SetTextureStageState(1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
device->SetTextureStageState(1, D3DTSS_ALPHAARG2, D3DTA_CURRENT);

// Render your primative and voila! Masked texture!
[/CODE] Edited by Shenjoku
0

Share this post


Link to post
Share on other sites
You can also direct the second texture stage to use the first texture coordinates, if you don't want to duplicate them. See D3DTSS_TEXCOORDINDEX :)
0

Share this post


Link to post
Share on other sites
Oh that's cool! That would be helpful if I needed them to be exactly the same, which I do for now, but eventually they are going to have to be calculated separately and will have different values potentially.
0

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  
Followers 0