Sign in to follow this  
xdrt

Render transparent texture to another texture ***SOLVED***

Recommended Posts

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:


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;
}





Create textures:
    // 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);





Turn on alpha blending:
    //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:
    // 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 first image:
      // 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 second image:
      // 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);





Whole programm:
#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;
}






Result images are: Source image here: Any help would be greatly appreciated, Ilja [Edited by - xdrt on July 25, 2008 1:33:15 PM]

Share this post


Link to post
Share on other sites
The end result looks very much like the halo effect you get from texture filtering.

In this case the transparent black (D3DX tends to make completely transparent pixels 0xFF000000 in colour - lock the texture or use PIX to verify) pixels neighbouring opaque ones get bilinearly filtered together. This leads to a semi-transparent pixel that is a lot darker than the opaque one and the visual result is the dark outline you've posted.

Try setting texture filtering to POINT and seeing if it goes away. If that works then you'll need to look at either tweaking your texture coordinates (moving them in a 1/2 pixel can sometimes solve this) or use a texture gutter/border.

hth
Jack

Share this post


Link to post
Share on other sites
Thank you Jack,

I have tried use texture coordinates offset (shifting them in a 1/2 pixel)


// left top vertex
pVertices[0].position = D3DXVECTOR3(0, 0, 0);
pVertices[0].color = color;
pVertices[0].tx = .5f / w;
pVertices[0].ty = .5f / h;

// left bottom vertex
pVertices[1].position = D3DXVECTOR3(0, h, 0);
pVertices[1].color = color;
pVertices[1].tx = .5f / w;
pVertices[1].ty = 1.f - .5f / h;

// right bottom vertex
pVertices[2].position = D3DXVECTOR3(w, h, 0);
pVertices[2].color = color;
pVertices[2].tx = 1 - .5f / w;
pVertices[2].ty = 1 - .5f / h;

// right top vertex
pVertices[3].position = D3DXVECTOR3(w, 0, 0);
pVertices[3].color = color;
pVertices[3].tx = 1 - .5f / w;
pVertices[3].ty = .5f / h;



and I've tried setting texture filtering to POINT:

g_pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT);
g_pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT);



and tried to use texture border:

g_pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER);
g_pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER);



But result is the same... :(

Ilja

Share this post


Link to post
Share on other sites
The texture border isn't likely to affect anything here - that's for UV's outside of the [0,1] range.

Capture a single frame in PIX for Windows and try using the "pixel history" feature to drill down into the operations and associated inputs/outputs. That might well give you some insight into where the artifacts are coming from.

hth
Jack

Share this post


Link to post
Share on other sites
Jack,

Thank you for your contribution, I found the answer in Google :)
here

The code which gives the right result looks like below (when we drawing to texture alpha blending switched off, but turned on when rendering to backbuffer):


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));

// 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);

// !!!
// NO ALPHA BLENDING!!!
// !!!
// 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));

// !!!
// ALFA BLENDING turned on for rendering to backbuffer
// !!!
//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);

return S_OK;
}




Thanks a lot again,

Ilja

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