🔒 Locked
Alpha Testing Question
Advertisement
Original Post
First time posting, but a long time viewer to GameDev.net, First of all i'd like to thank each and everyone of you from all the knowledge i was able to gain just from reading post responses and the articles here. Now for my question... I have tried to search over and over for this but i think im just wording it wrong so please excuse me if it has already been answered before and just point me in the right direction. But here's what im trying to do. I have a texture that i want to take chunks out of dynamically. I Figure id use Alpha testing and just alter the Alpha Channel of whatever pixel i wanna drop out. But im not sure how to do this on the fly dynamically. Is there a way i can lock the texture, retrieve an array of pixel bytes, and then alter their alpha channels? Lock the surface, and render with alpha testing and have unwanted pixels be transparent? If there is a way, how would i go about retrieving this alpha buffer from the texture so i can alter it? Thx again guys for all your help...
While this is possible, I would not suggest it. You need to LockRect the surface, then manipulate the data in the buffer it returns. The problem is that it's encoded to whatever format the texture was created as. By this I mean, RGB15/16, RGB24, RGB32, etc, you need to get one pixel of data (the size of the pixel dependant on the surface format), then use bit wise operations to extract the colors from those pixels. Then you can alter the alpha channel, then encode the colors back to the pixel format, and write it back into the texture.
This is all possible, but can get really slow depending on what you're trying to do. So what ARE you trying to do, because there may be easier ways? :D
This is all possible, but can get really slow depending on what you're trying to do. So what ARE you trying to do, because there may be easier ways? :D
I see what your saying, What im trying to do is create a sort of Deformable Terrain, i was just gonna set the pixels that i want to not appear alpha channel to 0 after that area of terrain has an inpact. That depending on the alpha channel being > 0 i was going to do collision testing based on that for vehicles on top of the terrain. This is going to be a 2D game btw, as you can see...
I was thinking, if i had to do this only when there was an explosion would it be that bad? if i kept a copy of those Alpha channels in a side buffer, for two reasons. One for collision checking and another for quick Manipulation by just creating a function that would Alter all the Alpha bytes of the texture based on this copied buffer?
You could definitely do that. Write a shader that does a tex2d lookup on your normal texture and one on this other texture and multiplies the alpha of the explosion texture with the alpha of the normal texture. In fact, the explosion texture would only need 1 number per pixel for the alpha.
First of all... You don't need a separate array in system memory. Just create the texture as D3DPOOL_MANAGED and when you lock it, you're locking the system memory copy, which gets copied into video memory as needed. You'll need to know what format the locked memory is in and ensure it's created with an alpha channel.
Secondly, locking and modifying a few textures every frame is not that big a hit.
For setting the alpha value, you can have two goals. If you just set the alpha to a lower value, the texture is transparent, but you can also set it so that it's not rendered at all (like using color keys for 2D surfaces/blitting, etc).
Ensure you're setting the render states.
Alpha blending requires:
m_pDirect3DDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
m_pDirect3DDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
m_pDirect3DDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );
Alpha testing requires:
m_pDirect3DDevice->SetRenderState( D3DRS_ALPHATESTENABLE, TRUE );
m_pDirect3DDevice->SetRenderState( D3DRS_ALPHAREF, 5 );
m_pDirect3DDevice->SetRenderState( D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL );
-Alamar
Secondly, locking and modifying a few textures every frame is not that big a hit.
For setting the alpha value, you can have two goals. If you just set the alpha to a lower value, the texture is transparent, but you can also set it so that it's not rendered at all (like using color keys for 2D surfaces/blitting, etc).
Ensure you're setting the render states.
Alpha blending requires:
m_pDirect3DDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
m_pDirect3DDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
m_pDirect3DDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );
Alpha testing requires:
m_pDirect3DDevice->SetRenderState( D3DRS_ALPHATESTENABLE, TRUE );
m_pDirect3DDevice->SetRenderState( D3DRS_ALPHAREF, 5 );
m_pDirect3DDevice->SetRenderState( D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL );
-Alamar
Thanks allot guys, The reason i was thinking of copying the alpha into a side buffer is because id' probably have to do collision checking every frame based on those alpha values. Wouldn't locking the tex every frame to check for that be a little Intensive and take a hit to framerate? If not then yea i wont even use an alpha buffer. Seccond of all i havent even touched writing my own Shaders yet, i have to read up on it, Can anyone suggest a good tutorial for being a complete Beginner to Shaders? and Alamar thank you man, i have all those things set, but thanks for the reminder.
Yes, you do NOT want to lock textures every frame for collision detection, so if you're doing pixel-perfect detection (2D I'm hoping), a secondary buffer (or bit list, as using bytes for an on/off state is a huge waste - unless you're not comfortable with bit shifting and what not) is a good idea.
Also, no idea on tutorials for shaders... Only really looked into it with a book I bought a while back (3D Game Engine Programming - not bad, but leaves a lot to be desired), and while they're the new and in thing, you really don't need that complexity for 99% of what you want to do... On the other hand... Learning is fun : )
-Alamar
Also, no idea on tutorials for shaders... Only really looked into it with a book I bought a while back (3D Game Engine Programming - not bad, but leaves a lot to be desired), and while they're the new and in thing, you really don't need that complexity for 99% of what you want to do... On the other hand... Learning is fun : )
-Alamar
Thx Alamar, Yea i went with a Bit List instead of Bytes, I starting programming in Assembly back in the day, and am now only getting into game programming so shifting bits is no biggie at all, it's perferred. The system works flawlessly, and yes the pixel by pixel detection is for 2D :) thanks allot guys. As for the Shader Programming i found a couple of introductory tut's and has some pretty neat things. I personally like the Vertex shader. Intricate Animation is pretty cool...
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement