Jump to content
  • Advertisement
Sign in to follow this  
Aragon

newbie question adding transparency using water example

This topic is 4849 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

hi all, i am using the old water demo from first dx9sdk my problem is, how can i add some alpha to it to make the ground under water a bit visible.. (without alpha tansparency it works perfect) when adding "diffuse" i get lot of errors in graphic original -> struct VERTEX { D3DXVECTOR3 p; FLOAT tu, tv; static const DWORD FVF; }; const DWORD VERTEX::FVF = D3DFVF_XYZ | D3DFVF_TEX1; my try -> struct VERTEX { D3DXVECTOR3 p; FLOAT tu, tv; DWORD color; static const DWORD FVF; }; const DWORD VERTEX::FVF = D3DFVF_XYZ | D3DFVF_TEX1 | D3DFVF_DIFFUSE; in the function HRESULT SetEMBMStates() { ... } i didnt found anything that helps maybe in that function: D3DVERTEXELEMENT9 decl[] = { { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 }, { 0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() }; but i dont know much about vertex/pixel shaders.. i hope that somebody understand my problem and/or know what to do with that can anybody help me please greeting aragon

Share this post


Link to post
Share on other sites
Advertisement
Two things that jump to mind.

One:

Quote:

struct VERTEX
{
D3DXVECTOR3 p;
FLOAT tu, tv;
static const DWORD FVF;
};
const DWORD VERTEX::FVF = D3DFVF_XYZ | D3DFVF_TEX1;


I'm no expert, so maybe you are doing it correctly, but my struct always matches my FVF. In your case you have an extra "static cons DWORD FVF"

I assume when you create your vertex buffer you are doing something like
.. sizeof(struct VERTEX)

but I think that would be wrong given the FVF you are using unless it's something specific to c# vs c++.


Second, if you do use a D3DFVF_DIFFUSE value, you'll also need to set your texture stages:

something like this might work (it's what I'm using for my water:


pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE );
pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP);
pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP);
pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);




BOL

Share this post


Link to post
Share on other sites
thx for answer

1.

the diffuse i was adding works at landscape and other vertex buffer things perfect, only at this water thing the problem appears

and 2.

the texture stages are set correctly, there is no problem

the problem is that without "diffus" i got
water mirror waves looking very very great, but cant watch
throu water to see the ground

when adding the diffuse ,the water waves turn into
long lines, so that something get in trouble

i think that the diffus value collides with something in
vertex shader maybe because of any static constant i didnt
understand, like that vertex shader except a fix size of
p ..and when adding extra value it overwrites other values
or something like that







Share this post


Link to post
Share on other sites
oh, I didn't realize you already had a vertex shader in use. When I use a vertex shader I put the alpha in the shader. In my case, I use a shader for my terrain which has 5 textures, texture 0 is a 4 color blend map (ARGB). Then texture 1-4 is the texture to render:

Shader=

ps_1_4

////////////////////////////////
// r0: alphamaps
// r1 - r4: textures
////////////////////////////////

// Sample textures
texld r0, t0
texld r1, t1
texld r2, t1
texld r3, t1
texld r4, t1

// Combine textures together based off of their alphamaps
mul r1, r1, r0.x
lrp r2, r0.y, r2, r1
lrp r3, r0.z, r3, r2
lrp r0, r0.w, r4, r3




I think without too much work you can rework it to use an alpha channel in the texture maybe?

Share this post


Link to post
Share on other sites
that is my water from dx9 sdk working very well
but without transparency




FILE: BUMPWAVES.VSH

vs.1.1

dcl_position v0
dcl_texcoord v1

m4x4 oPos, v0, c3 ; transform position to the projection space

; Compute vertex position in the camera space - this is our texture coordinates
dp4 r0.x, v0, c0
dp4 r0.y, v0, c1
dp4 r0.z, v0, c2

; Do the rest of texture transform (first part was combined with the camera matrix)
rcp r0.z, r0.z
mad oT1.x, r0.x, r0.z, c8.x
mad oT1.y, r0.y, r0.z, c8.y

mov oT0.xy, v1 ; Copy input texture coordinates for the stage 0;


----------------------------------------------------



struct VERTEX
{
D3DXVECTOR3 p;
FLOAT tu, tv;
static const DWORD FVF;
};
const DWORD VERTEX::FVF = D3DFVF_XYZ | D3DFVF_TEX1;


void render_water_bump(void)
{

if (water_run==0) return;

D3DXMATRIX matTrans;
D3DXMATRIX matRot;
D3DXMATRIX matObj;

D3DXMatrixTranslation( &matObj, unit_x[0], unit_y[0], unit_z[0] );
g_pd3dDevice->SetTransform( D3DTS_WORLD, &matObj );


// Render the water
SetEMBMStates();
if( m_bUseVertexShader )
{
g_pd3dDevice->SetVertexShader( m_pVertexShader );
g_pd3dDevice->SetVertexDeclaration( m_pVertexDeclaration );
}
else
{
g_pd3dDevice->SetFVF( VERTEX::FVF );
}

g_pd3dDevice->SetStreamSource( 0, m_pWaterVB, 0, sizeof(VERTEX) );


//test
g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, itest[1]);
g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, itest[2]);
g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);


g_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
g_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG1, D3DTA_TEXTURE );
g_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
g_pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
g_pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE );



g_pd3dDevice->SetTexture ( 1, g_pDynamicTexture );

g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, m_nTriangles );
g_pd3dDevice->SetTexture ( 1, g_pTexture[1] );

//reset
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
g_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );


g_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP );
g_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP );
g_pd3dDevice->SetSamplerState( 1, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP );
g_pd3dDevice->SetSamplerState( 1, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP );



}




LPDIRECT3DTEXTURE9 CreateBumpMap( DWORD dwWidth, DWORD dwHeight,D3DFORMAT d3dBumpFormat )
{
LPDIRECT3DTEXTURE9 psBumpMap;


// Create the bump map texture
if( FAILED( g_pd3dDevice->CreateTexture( dwWidth, dwHeight, 1, 0 /* Usage */,
d3dBumpFormat, D3DPOOL_MANAGED,
&psBumpMap, NULL ) ) )
return NULL;

D3DLOCKED_RECT d3dlr;
psBumpMap->LockRect( 0, &d3dlr, 0, 0 );
CHAR* pDst = (CHAR*)d3dlr.pBits;
CHAR iDu, iDv;

for( DWORD y=0; y<dwHeight; y++ )
{
CHAR* pPixel = pDst;

for( DWORD x=0; x<dwWidth; x++ )
{
FLOAT fx = x/(FLOAT)dwWidth - 0.5f;
FLOAT fy = y/(FLOAT)dwHeight - 0.2f;

FLOAT r = sqrtf( fx*fx + fy*fy );

iDu = (CHAR)( 200.2 * cosf( 450.0f * ( fx + fy ) ) );
iDu += (CHAR)( 100.6 * cosf( 440.0f * ( fx * 0.85f - fy ) ) );

iDv = (CHAR)( 200.2 * sinf( 450.0f * ( fx +fx + fy ) ) );
iDv += (CHAR)( 100.6 * sinf( 440.0f * ( fx * 0.85f - fy ) ) );

*pPixel++ = iDu;
*pPixel++ = iDv;
}
pDst += d3dlr.Pitch;
}
psBumpMap->UnlockRect(0);
return psBumpMap;
}

void initShader( void )
{
pixel_shader=1;
vertex_shader=1;
m_bUseVertexShader = TRUE;

//Prüfe Ob Vertex und Pixel Shader verfügbar sind

//if (water_run==0) return;

D3DCAPS9 caps;
g_pd3dDevice->GetDeviceCaps(&caps);

if ( caps.PixelShaderVersion < D3DPS_VERSION(1,1) ) pixel_shader=0;
if ( caps.VertexShaderVersion < D3DPS_VERSION(1,1) ) {vertex_shader=0;m_bUseVertexShader = FALSE;}


m_psBumpMap = CreateBumpMap( 512, 512, D3DFMT_V8U8 ); if( NULL == m_psBumpMap ) PostQuitMessage(0);

SetEMBMStates();

VERTEX* v;

if( caps.TextureCaps & D3DPTEXTURECAPS_PROJECTED && !m_bUseVertexShader)
{
m_n = 32; // Number of vertices in the ground grid along X
m_m = 32; // Number of vertices in the ground grid along Z
}
else
{
m_n = 32; // Number of vertices in the ground grid along X
m_m = 32; // Number of vertices in the ground grid along Z
}
m_nTriangles = (m_n-1)*(m_m-1)*2; // Number of triangles in the ground

// Create a square grid m_n*m_m for rendering the water
if( FAILED( g_pd3dDevice->CreateVertexBuffer( m_nTriangles*3*sizeof(VERTEX),
D3DUSAGE_WRITEONLY, VERTEX::FVF,
D3DPOOL_MANAGED, &m_pWaterVB, NULL ) ) )
return ;
m_pWaterVB->Lock( 0, 0, (void**)&v, 0 );
float dX = 8000.0f/(m_n-1);
float dZ = 8000.0f/(m_m-1);
float x0 = -4000;
float z0 = -4000;
float dU = (400.0f/(m_n-1));
float dV = (400.0f/(m_m-1));
float yh=0;

UINT k = 0;
for (UINT z=0; z < (m_m-1); z++)
{
for (UINT x=0; x < (m_n-1); x++)
{
v[k].p = D3DXVECTOR3(x0 + x*dX, yh, z0 + z*dZ );
v[k].tu = x*dU;
v[k].tv = z*dV;
k++;
v[k].p = D3DXVECTOR3(x0 + x*dX, yh, z0 + (z+1)*dZ );
v[k].tu = x*dU;
v[k].tv = (z+1)*dV;
k++;
v[k].p = D3DXVECTOR3(x0 + (x+1)*dX, yh, z0 + (z+1)*dZ );
v[k].tu = (x+1)*dU;
v[k].tv = (z+1)*dV;
k++;
v[k].p = D3DXVECTOR3(x0 + x*dX, yh, z0 + z*dZ );
v[k].tu = x*dU;
v[k].tv = z*dV;
k++;
v[k].p = D3DXVECTOR3(x0 + (x+1)*dX, yh, z0 + (z+1)*dZ );
v[k].tu = (x+1)*dU;
v[k].tv = (z+1)*dV;
k++;
v[k].p = D3DXVECTOR3(x0 + (x+1)*dX, yh, z0 + z*dZ );
v[k].tu = (x+1)*dU;
v[k].tv = z*dV;
k++;
}
}
m_pWaterVB->Unlock();

if ( m_bUseVertexShader )
{
D3DVERTEXELEMENT9 decl[] =
{
{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
{ 0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
D3DDECL_END()
};

if( FAILED( hr = g_pd3dDevice->CreateVertexDeclaration( decl, &m_pVertexDeclaration ) ) )
{
return ;
}

LPD3DXBUFFER pCode = NULL;
if( FAILED( hr = D3DXAssembleShaderFromFile( "BumpWaves.vsh", NULL, NULL, 0, &pCode, NULL ) ) )
{
PostQuitMessage(0);
return ;
}

if( FAILED( hr = g_pd3dDevice->CreateVertexShader( (DWORD*)pCode->GetBufferPointer(),
&m_pVertexShader ) ) )
{
PostQuitMessage(0);
return ;
}
pCode->Release();
}

D3DXMATRIXA16 matWorld, matView, matProj;

// Set the transform matrices
D3DXVECTOR3 vEyePt( 1600*sin(c360*3.141593/180), 400.0f, 1600*cos(c360*3.141593/180) );
D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );
D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );

D3DXMatrixIdentity( &matWorld );
D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
D3DXMatrixPerspectiveFovLH( &matProj, 1.00f, 1.0f, 0.01f, 10000.0f );
g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );

if ( m_bUseVertexShader )
{
D3DXMATRIXA16 matCamera, matFinal;
D3DXMatrixMultiply( &matCamera, &matWorld, &matView );
D3DXMatrixMultiply( &matFinal, &matCamera, &matProj );
D3DXMatrixTranspose(&matCamera, &matCamera);
D3DXMatrixTranspose(&matFinal, &matFinal);
matCamera(0, 0) *= 0.8f;
matCamera(0, 1) *= 0.8f;
matCamera(0, 2) *= 0.8f;
matCamera(0, 3) *= 0.8f;
matCamera(1, 0) *= 0.8f;
matCamera(1, 1) *= 0.8f;
matCamera(1, 2) *= 0.8f;
matCamera(1, 3) *= 0.8f;
if (FAILED( hr = g_pd3dDevice->SetVertexShaderConstantF(0, (float*)&matCamera, 3) ) )
{
return ;
}
if (FAILED( hr = g_pd3dDevice->SetVertexShaderConstantF(3, (float*)&matFinal, 4) ) )
{
return ;
}
FLOAT data[4] = {0.5f, -0.5f, 0, 0};
if (FAILED( hr = g_pd3dDevice->SetVertexShaderConstantF(8, (float*)data, 1) ) )
{
return ;
}
}


D3DXMatrixIdentity( &m_matBumpMat );

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


}





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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!