Original Post
Hi! I am trying to render transparent texture to another texture, but alpha blending works strangely. In code below I draw two image. For drawing first image I just load texture from .png file having alfa channel and render this texture using alfa blending. For drawing second image I created texture having alpha channel, then I draw to this texture image loaded from .png file (same as in the first image). The first and second images are differ. I cannot understand what I did wrong. There are main steps I did: Create quad: Create textures: Turn on alpha blending: Draw to texture: Draw first image: Draw second image: Whole programm: Result images are:
Source image here:
Any help would be greatly appreciated, Ilja [Edited by - xdrt on July 25, 2008 1:33:15 PM]
LPDIRECT3DVERTEXBUFFER9 createQuad(int w, int h, D3DCOLOR color)
{
LPDIRECT3DVERTEXBUFFER9 buffer;
g_pd3dDevice->CreateVertexBuffer(4 * sizeof(CUSTOMVERTEX),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &buffer, NULL);
CUSTOMVERTEX* pVertices;
//const D3DCOLOR color = D3DCOLOR_COLORVALUE(1, 1, 1, 1);
buffer->Lock(0, 0, (void**)&pVertices, 0);
// left top vertex
pVertices[0].position = D3DXVECTOR3(0, 0, 0);
pVertices[0].color = color;
pVertices[0].tx = 0;
pVertices[0].ty = 0;
// left bottom vertex
pVertices[1].position = D3DXVECTOR3(0, h, 0);
pVertices[1].color = color;
pVertices[1].tx = 0;
pVertices[1].ty = 1;
// right bottom vertex
pVertices[2].position = D3DXVECTOR3(w, h, 0);
pVertices[2].color = color;
pVertices[2].tx = 1;
pVertices[2].ty = 1;
// right top vertex
pVertices[3].position = D3DXVECTOR3(w, 0, 0);
pVertices[3].color = color;
pVertices[3].tx = 1;
pVertices[3].ty = 0;
buffer->Unlock();
return buffer;
}
// load texture from file
D3DXCreateTextureFromFile(g_pd3dDevice, L"circle.png", &imageTexture);
// create texture for drawing to
g_pd3dDevice->CreateTexture(textureSize, textureSize, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &targetTexture, NULL);
//enable alpha blending
g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
//source blend factor
g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
//destination blend factor
g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
// draw to texture
// we have to set new render target
// save current render target
LPDIRECT3DSURFACE9 backBufferSurface;
g_pd3dDevice->GetRenderTarget(0, &backBufferSurface);
LPDIRECT3DSURFACE9 textureSurface;
targetTexture->GetSurfaceLevel(0, &textureSurface);
g_pd3dDevice->SetRenderTarget(0, textureSurface);
//// set projection matrix for texture
D3DXMATRIXA16 matrix;
g_pd3dDevice->SetTransform(D3DTS_PROJECTION, D3DXMatrixOrthoOffCenterLH(&matrix, 0, textureSize, textureSize, 0, 0, 1));
// set view matrix for texture
g_pd3dDevice->SetTransform(D3DTS_VIEW, D3DXMatrixIdentity(&matrix));
// set world matrix for texture
g_pd3dDevice->SetTransform(D3DTS_WORLD, &matrix);
g_pd3dDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
// clear target texture by transparent black color
g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_COLORVALUE(0, 0, 0, 0), 1.0f, 0);
// begin draw to texture
g_pd3dDevice->BeginScene();
// draw texture loaded from file
// set current texture
g_pd3dDevice->SetTexture(0, imageTexture);
// mix texture color and quad vertices color
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
// mix texture alpha and quad vertices alpha
g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
// draw quad
g_pd3dDevice->SetStreamSource(0, imageBuffer, 0, sizeof( CUSTOMVERTEX ));
g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
// end draw to texture
g_pd3dDevice->EndScene();
// restore render target
g_pd3dDevice->SetRenderTarget(0, backBufferSurface);
// draw texture loaded from file
g_pd3dDevice->SetTexture(0, imageTexture);
// mix texture color and quad vertices color
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
// mix texture alpha and quad vertices alpha
g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
// draw quad
g_pd3dDevice->SetStreamSource(0, imageBuffer, 0, sizeof( CUSTOMVERTEX ));
g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
// draw target texture
g_pd3dDevice->SetTexture(0, targetTexture);
// mix texture color and quad vertices color
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
// mix texture alpha and quad vertices alpha
g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
// draw quad
g_pd3dDevice->SetStreamSource(0, targetBuffer, 0, sizeof( CUSTOMVERTEX ));
g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <mmsystem.h>
#include <d3dx9.h>
#pragma warning( disable : 4996 ) // disable deprecated warning
#include <strsafe.h>
#pragma warning( default : 4996 )
//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device
LPDIRECT3DTEXTURE9 imageTexture = 0; // texture from file
LPDIRECT3DTEXTURE9 targetTexture = 0; // texture for drawing to
LPDIRECT3DVERTEXBUFFER9 imageBuffer = 0; // vertex buffer for drawing texture from file
LPDIRECT3DVERTEXBUFFER9 targetBuffer = 0; // vertex buffer for drawing target texture
// A structure for our custom vertex type.
struct CUSTOMVERTEX
{
D3DXVECTOR3 position; // The position
D3DCOLOR color; // The color
FLOAT tx, ty; // The texture coordinates
};
// Our custom FVF, which describes our custom vertex structure
const int D3DFVF_CUSTOMVERTEX = (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1);
const int screenWidth = 640;
const int screenHeight = 480;
const int textureSize = 64; // textures size
LPDIRECT3DVERTEXBUFFER9 createQuad(int w, int h, D3DCOLOR color)
{
LPDIRECT3DVERTEXBUFFER9 buffer;
g_pd3dDevice->CreateVertexBuffer(4 * sizeof(CUSTOMVERTEX),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &buffer, NULL);
CUSTOMVERTEX* pVertices;
//const D3DCOLOR color = D3DCOLOR_COLORVALUE(1, 1, 1, 1);
buffer->Lock(0, 0, (void**)&pVertices, 0);
// left top vertex
pVertices[0].position = D3DXVECTOR3(0, 0, 0);
pVertices[0].color = color;
pVertices[0].tx = 0;
pVertices[0].ty = 0;
// left bottom vertex
pVertices[1].position = D3DXVECTOR3(0, h, 0);
pVertices[1].color = color;
pVertices[1].tx = 0;
pVertices[1].ty = 1;
// right bottom vertex
pVertices[2].position = D3DXVECTOR3(w, h, 0);
pVertices[2].color = color;
pVertices[2].tx = 1;
pVertices[2].ty = 1;
// right top vertex
pVertices[3].position = D3DXVECTOR3(w, 0, 0);
pVertices[3].color = color;
pVertices[3].tx = 1;
pVertices[3].ty = 0;
buffer->Unlock();
return buffer;
}
//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D( HWND hWnd )
{
// Create the D3D object.
if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
return E_FAIL;
// Set up the structure used to create the D3DDevice
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof( d3dpp ) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
// Create the D3DDevice
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice ) ) )
{
return E_FAIL;
}
// Turn off culling, so we see the front and back of the triangle
g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
// Turn off D3D lighting, since we are providing our own vertex colors
g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
// load texture from file
D3DXCreateTextureFromFile(g_pd3dDevice, L"circle.png", &imageTexture);
// create texture for drawing to
g_pd3dDevice->CreateTexture(textureSize, textureSize, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &targetTexture, NULL);
// create quad for drawing texture from file
imageBuffer = createQuad(textureSize, textureSize, D3DCOLOR_COLORVALUE(1, 1, 1, 1));
// create quad for drawing created texture
targetBuffer = createQuad(textureSize, textureSize, D3DCOLOR_COLORVALUE(1, 1, 1, 1));
//enable alpha blending
g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
//source blend factor
g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
//destination blend factor
g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
// draw to texture
// we have to set new render target
// save current render target
LPDIRECT3DSURFACE9 backBufferSurface;
g_pd3dDevice->GetRenderTarget(0, &backBufferSurface);
LPDIRECT3DSURFACE9 textureSurface;
targetTexture->GetSurfaceLevel(0, &textureSurface);
g_pd3dDevice->SetRenderTarget(0, textureSurface);
//// set projection matrix for texture
D3DXMATRIXA16 matrix;
g_pd3dDevice->SetTransform(D3DTS_PROJECTION, D3DXMatrixOrthoOffCenterLH(&matrix, 0, textureSize, textureSize, 0, 0, 1));
// set view matrix for texture
g_pd3dDevice->SetTransform(D3DTS_VIEW, D3DXMatrixIdentity(&matrix));
// set world matrix for texture
g_pd3dDevice->SetTransform(D3DTS_WORLD, &matrix);
// clear target texture by transparent black color
g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_COLORVALUE(0, 0, 0, 0), 1.0f, 0);
// begin draw to texture
g_pd3dDevice->BeginScene();
// draw texture loaded from file
// set current texture
g_pd3dDevice->SetTexture(0, imageTexture);
// mix texture color and quad vertices color
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
// mix texture alpha and quad vertices alpha
g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
// draw quad
g_pd3dDevice->SetStreamSource(0, imageBuffer, 0, sizeof( CUSTOMVERTEX ));
g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
// end draw to texture
g_pd3dDevice->EndScene();
// restore render target
g_pd3dDevice->SetRenderTarget(0, backBufferSurface);
// set projection matrix for screen
g_pd3dDevice->SetTransform(D3DTS_PROJECTION, D3DXMatrixOrthoOffCenterLH(&matrix, 0, screenWidth, screenHeight, 0, 0, 1));
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID Cleanup()
{
if( g_pd3dDevice != NULL )
g_pd3dDevice->Release();
if( g_pD3D != NULL )
g_pD3D->Release();
}
//-----------------------------------------------------------------------------
// Name: RenderScene()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID RenderScene()
{
// Clear the backbuffer to a black color
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_COLORVALUE( 1, 1, 1, 1 ), 1.0f, 0 );
// Begin the scene
if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
{
// reset world matrix
D3DXMATRIX matrix;
// set world matrix for texture
g_pd3dDevice->SetTransform(D3DTS_WORLD, D3DXMatrixIdentity(&matrix));
// draw texture loaded from file
g_pd3dDevice->SetTexture(0, imageTexture);
// mix texture color and quad vertices color
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
// mix texture alpha and quad vertices alpha
g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
// draw quad
g_pd3dDevice->SetStreamSource(0, imageBuffer, 0, sizeof( CUSTOMVERTEX ));
g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
// move right
g_pd3dDevice->SetTransform(D3DTS_WORLD, D3DXMatrixTranslation(&matrix, 64, 0, 0));
// draw target texture
g_pd3dDevice->SetTexture(0, targetTexture);
// mix texture color and quad vertices color
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
// mix texture alpha and quad vertices alpha
g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
// draw quad
g_pd3dDevice->SetStreamSource(0, targetBuffer, 0, sizeof( CUSTOMVERTEX ));
g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
g_pd3dDevice->EndScene();
}
// Present the backbuffer contents to the display
g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}
//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
Cleanup();
PostQuitMessage( 0 );
return 0;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI wWinMain( HINSTANCE hInst, HINSTANCE, LPWSTR, INT )
{
// Register the window class
WNDCLASSEX wc =
{
sizeof( WNDCLASSEX ), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle( NULL ), NULL, NULL, NULL, NULL,
L"D3D Tutorial", NULL
};
RegisterClassEx( &wc );
RECT rc;
SetRect( &rc, 0, 0, screenWidth, screenHeight );
AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, FALSE );
// Create the application's window
HWND hWnd = CreateWindow( L"D3D Tutorial", L"D3D Tutorial 03: Matrices",
WS_OVERLAPPEDWINDOW, 100, 100, ( rc.right - rc.left ), ( rc.bottom - rc.top ),
NULL, NULL, wc.hInstance, NULL );
// Initialize Direct3D
if( SUCCEEDED( InitD3D( hWnd ) ) )
{
// Show the window
ShowWindow( hWnd, SW_SHOWDEFAULT );
UpdateWindow( hWnd );
// Enter the message loop
MSG msg;
ZeroMemory( &msg, sizeof( msg ) );
while( msg.message != WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
RenderScene();
}
}
}
UnregisterClass( L"D3D Tutorial", wc.hInstance );
return 0;
}
Source image here:
Any help would be greatly appreciated, Ilja [Edited by - xdrt on July 25, 2008 1:33:15 PM]