Jump to content
  • Advertisement
Sign in to follow this  

how to transparent one pixel?

This topic is 4724 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

so far as i know , ican transparent the vertex ... but now i wanna make a transition effect ... now i use texture on one quad ... i could make pixel change color but i can't change transparence ... actually, you could see i tried the for ( UINT y = 0 ; y < 600 ; y ++ ) { for ( UINT x = 0 ; x < 800 ; x ++ ) { iBufferPosition = y * lockrect.Pitch + x * dwBytesPerPixel ; gray =(FLOAT ) ( ( pbyBuffer [iBufferPosition] * 0.11 + // r pbyBuffer [iBufferPosition+1] * 0.59 + // g pbyBuffer [iBufferPosition+2] * 0.30 ) / 255 ) ;// b if (gray > g_Gray) { // pbyBuffer [iBufferPosition] = 0 ; // pbyBuffer [iBufferPosition+1] = 0 ; // pbyBuffer [iBufferPosition+2] = 0 ; pbyBuffer [iBufferPosition+3] = 24 ; // here you see } } } pbyBuffer [iBufferPosition+3] = 24 ; // here you see the alpha value wasnt work maybe i should set some value ?? any one knows can tell me ?

Share this post


Link to post
Share on other sites
Advertisement
the following was the source code :

//
#include <d3d8.h>
#include <d3dx8.h>

#define SCREENWIDTH 800
#define SCREENHEIGHT 600

//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
LPDIRECT3D8 g_pD3D = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE8 g_pd3dDevice = NULL; // Our rendering device
LPDIRECT3DVERTEXBUFFER8 g_pVB = NULL; // Buffer to hold vertices
LPDIRECT3DVERTEXBUFFER8 g_pVB2 = NULL; // Buffer to hold vertices

LPDIRECT3DTEXTURE8 g_pTex = NULL;
FLOAT g_Gray = 1.00f ;
D3DDISPLAYMODE d3ddm ;
HWND g_hWnd ;


// A structure for our custom vertex type
struct CUSTOMVERTEX
{
FLOAT x, y, z, rhw; // The transformed position for the vertex
DWORD color; // The vertex color
FLOAT u, v; // u v
};

// Our custom FVF, which describes our custom vertex structure
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1)

// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------

VOID CheckGray()
{
BYTE* pbyBuffer = NULL ;
D3DLOCKED_RECT lockrect ;

g_pTex->LockRect (0 , &lockrect , NULL , D3DLOCK_NOSYSLOCK ) ;

DWORD dwBytesPerPixel = 4 ;
FLOAT gray ;

g_Gray -= 0.01f ;

pbyBuffer = (BYTE *) lockrect.pBits ;
int iBufferPosition ;

for ( UINT y = 0 ; y < 600 ; y ++ )
{
for ( UINT x = 0 ; x < 800 ; x ++ )
{
iBufferPosition = y * lockrect.Pitch + x * dwBytesPerPixel ;

gray =(FLOAT ) ( ( pbyBuffer [iBufferPosition] * 0.11 + // r
pbyBuffer [iBufferPosition+1] * 0.59 + // g
pbyBuffer [iBufferPosition+2] * 0.30 ) / 255 ) ; // b

if (gray > g_Gray)
{
pbyBuffer [iBufferPosition] = 0 ;
pbyBuffer [iBufferPosition+1] = 0 ;
pbyBuffer [iBufferPosition+2] = 0 ;
pbyBuffer [iBufferPosition+3] = 24 ;
}
}
}

g_pTex->UnlockRect( 0 ) ;
}

//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D( HWND hWnd )
{
// Create the D3D object.
if( NULL == ( g_pD3D = Direct3DCreate8( D3D_SDK_VERSION ) ) )
return E_FAIL;

// Get the current desktop display mode, so we can set up a back
// buffer of the same format
if( FAILED( g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm ) ) )
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 = d3ddm.Format;

// Create the D3DDevice
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice ) ) )
{
return E_FAIL;
}

// Device state would normally be set here

g_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
g_pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
g_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );
g_pd3dDevice->SetRenderState( D3DRS_BLENDOP , D3DBLENDOP_ADD ) ;
/*
g_pd3dDevice->SetRenderState ( D3DRS_ALPHATESTENABLE, TRUE) ;
g_pd3dDevice->SetRenderState ( D3DRS_ALPHAREF, 0xFE) ;
g_pd3dDevice->SetRenderState ( D3DRS_ALPHAFUNC, D3DCMP_GREATER) ;
*/


D3DXCreateTextureFromFile ( g_pd3dDevice, "effect.jpg", &g_pTex ) ;

return S_OK;
}

HRESULT InitVB()
{
// Initialize three vertices for rendering a triangle
CUSTOMVERTEX g_Vertices[] =
{
{ 0.0f, 0.0f, 0.5f, 1.0f, 0xfeff0000, 0.0f, 0.0f, },
{ 800.0f, 0.0f, 0.5f, 1.0f, 0xaf00ff00, 1.0f, 0.0f, },
{ 0.0f, 600.0f, 0.5f, 1.0f, 0xcf00ffff, 0.0f, 1.0f, },
{ 800.0f, 600.0f, 0.5f, 1.0f, 0xf100ffff, 1.0f, 1.0f, },
};

// Create the vertex buffer. Here we are allocating enough memory
// (from the default pool) to hold all our 3 custom vertices. We also
// specify the FVF, so the vertex buffer knows what data it contains.
if( FAILED( g_pd3dDevice->CreateVertexBuffer( 4*sizeof(CUSTOMVERTEX),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &g_pVB ) ) )
{
return E_FAIL;
}

// Now we fill the vertex buffer. To do this, we need to Lock() the VB to
// gain access to the vertices. This mechanism is required becuase vertex
// buffers may be in device memory.
VOID* pVertices;
if( FAILED( g_pVB->Lock( 0, sizeof(g_Vertices), (BYTE**)&pVertices, 0 ) ) )
return E_FAIL;
memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );
g_pVB->Unlock();

return S_OK;
}




//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID Cleanup()
{
if( g_pVB != NULL )
g_pVB->Release();

if( g_pTex != NULL )
g_pTex->Release();

if( g_pd3dDevice != NULL )
g_pd3dDevice->Release();

if( g_pD3D != NULL )
g_pD3D->Release();
}




//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render()
{
// Clear the backbuffer to a blue color
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );

// Begin the scene
g_pd3dDevice->BeginScene();

g_pd3dDevice->SetTexture( 0, g_pTex );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);

g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG2); // multiply
g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE); // texturealpha
// g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE); // by vertexalpha

/*

g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE); // multiply
g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE); // texturealpha
g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE); // by vertexalpha
*/


// Draw the triangles in the vertex buffer. This is broken into a few
// steps. We are passing the vertices down a "stream", so first we need
// to specify the source of that stream, which is our vertex buffer. Then
// we need to let D3D know what vertex shader to use. Full, custom vertex
// shaders are an advanced topic, but in most cases the vertex shader is
// just the FVF, so that D3D knows what type of vertices we are dealing
// with. Finally, we call DrawPrimitive() which does the actual rendering
// of our geometry (in this case, just one triangle).
g_pd3dDevice->SetStreamSource( 0, g_pVB, sizeof(CUSTOMVERTEX) );
g_pd3dDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );

// End the scene
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:
PostQuitMessage( 0 );
return 0;
}

return DefWindowProc( hWnd, msg, wParam, lParam );
}




//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
// Register the window class
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
"D3D Tutorial", NULL };
RegisterClassEx( &wc );

// Create the application's window
HWND hWnd = CreateWindow( "D3D Tutorial", "D3D Tutorial 02: Vertices",
WS_OVERLAPPEDWINDOW, 100, 100, 800, 600,
GetDesktopWindow(), NULL, wc.hInstance, NULL );
g_hWnd = hWnd ;

// Initialize Direct3D
if( SUCCEEDED( InitD3D( hWnd ) ) )
{
// Create the vertex buffer
if( SUCCEEDED( InitVB() ) )
{
// 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
{
CheckGray() ;
Render();
}
}
}
}

// Clean up everything and exit the app
Cleanup();
UnregisterClass( "D3D Tutorial", wc.hInstance );
return 0;
}



[Edited by - Coder on June 14, 2005 7:33:16 AM]

Share this post


Link to post
Share on other sites
i want to make the transition effect ,

i.e. tho one picture to another , the transition effect .

i.e. i dont want to change picture directly, but use some effect to do the change...

and here i wanna use the " gray-mask " -- a picture with gray infomation and if the correspond point is less than the definit value (a float value) then i want it to be transparent .

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!