.fx fog && pixed piple fog

Started by
7 comments, last by VizOne 15 years, 8 months ago
hi: i disable fog state in the effect pass;and want to enable fog when i push 'F'. but, i found it didn't work. my .fx is:

technique Fog
{
    pass P0
    {
        pixelshader      = null;
        vertexshader     = null;
        FogVertexMode = LINEAR; 
        FogStart      = 50.0f; 
        FogEnd        = 300.0f; 
        FogColor      = 0x00CCCCCC; 
        FogEnable     = false;       
    }
}
and the c++ code is:

Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00CCCCCC, 1.0f, 0);
		Device->BeginScene();

		// set the technique to use
		FogEffect->SetTechnique( FogTechHandle );

		UINT numPasses = 0;
    	FogEffect->Begin(&numPasses, 0);

		D3DXMATRIX I;
		D3DXMatrixIdentity(&I);
		for(int i = 0; i < numPasses; i++)
		{
			FogEffect->BeginPass(i);

			if( TheTerrain )
				TheTerrain->draw(&I, false);
		}
		FogEffect->End();

		if( ::GetAsyncKeyState('F') & 0x8000f )
		{
			Device->SetRenderState(D3DRS_FOGENABLE, true);

			Device->SetRenderState( D3DRS_FOGSTART, 50.0f );
			Device->SetRenderState( D3DRS_FOGEND, 300.0f );
			Device->SetRenderState( D3DRS_FOGCOLOR, 0x00ffCCCC );
			Device->SetRenderState( D3DRS_FOGTABLEMODE ,  D3DFOG_LINEAR);
		}

		Device->EndScene();

		Device->Present(0, 0, 0, 0);
so, why it doesn't work. thanks a lot
Advertisement
When you call FogEffect->BeginPass(0), the ID3DXEffect interface calls IDirect3DDevice9::SetRenderState and sets the fog states according to what you have in the effect file. This means it will override the render states you're setting later, after you've drawn everything.

You'll have to either...

a) Set the render state after you call BeginPass and before you render your terrain. You'll also have to do it every frame, since every time you call BeginPass the effect will set things as they are in the technique

b) Make 2 techniques, one with fog and one without. Then pick the technique you want based on user input. This, IMO, is more ideal than a).
Quote:Original post by MJP
When you call FogEffect->BeginPass(0), the ID3DXEffect interface calls IDirect3DDevice9::SetRenderState and sets the fog states according to what you have in the effect file. This means it will override the render states you're setting later, after you've drawn everything.

You'll have to either...

a) Set the render state after you call BeginPass and before you render your terrain. You'll also have to do it every frame, since every time you call BeginPass the effect will set things as they are in the technique

b) Make 2 techniques, one with fog and one without. Then pick the technique you want based on user input. This, IMO, is more ideal than a).


if i beginpass at first, will it override the render states later? i found if i disable the fog in the pass, later i enable it by SetRenderState, the somethings i draw out shader is effected by fog.
Quote:Original post by huaner
Quote:Original post by MJP
When you call FogEffect->BeginPass(0), the ID3DXEffect interface calls IDirect3DDevice9::SetRenderState and sets the fog states according to what you have in the effect file. This means it will override the render states you're setting later, after you've drawn everything.

You'll have to either...

a) Set the render state after you call BeginPass and before you render your terrain. You'll also have to do it every frame, since every time you call BeginPass the effect will set things as they are in the technique

b) Make 2 techniques, one with fog and one without. Then pick the technique you want based on user input. This, IMO, is more ideal than a).


if i beginpass at first, will it override the render states later? i found if i disable the fog in the pass, later i enable it by SetRenderState, the somethings i draw out shader is effected by fog.


ID3DXEffect will always set render states in BeginPass. If you're confused about the order in which things are occurring, go ahead and capture a frame in PIX and look at the order in which SetRenderState calls occur.

Some people don't know, but you can use parameters to set render states in the fx file:

bool g_enableFog = false;technique MyTec {  pass P0  {        pixelshader      = null;        vertexshader     = null;        FogVertexMode = LINEAR;         FogStart      = 50.0f;         FogEnd        = 300.0f;         FogColor      = 0x00CCCCCC;         FogEnable     = g_enableFog; // assign value of parameter    }}


In your app you can now control the render state by setting the parameter:

// grab a handle to the parameterD3DXHANDLE enableFogHandle = fx->GetParameterByName("g_enableFog");// to enable fogfx->SetBool(enableFogHandle, true);// to disable fogfx->SetBool(enableFogHandle, false);


I really like this method of setting render states as it fits perfectly in the way I handle other parameters. No need to create a different technique or pass render states to the device directly!

You can control all render states and sampler state settings with parameters (makes it easy to e.g. control texture filtering from the app).

Regards,
Andre
Andre Loker | Personal blog on .NET
Quote:Original post by MJP
Quote:Original post by huaner
Quote:Original post by MJP
When you call FogEffect->BeginPass(0), the ID3DXEffect interface calls IDirect3DDevice9::SetRenderState and sets the fog states according to what you have in the effect file. This means it will override the render states you're setting later, after you've drawn everything.

You'll have to either...

a) Set the render state after you call BeginPass and before you render your terrain. You'll also have to do it every frame, since every time you call BeginPass the effect will set things as they are in the technique

b) Make 2 techniques, one with fog and one without. Then pick the technique you want based on user input. This, IMO, is more ideal than a).


if i beginpass at first, will it override the render states later? i found if i disable the fog in the pass, later i enable it by SetRenderState, the somethings i draw out shader is effected by fog.


ID3DXEffect will always set render states in BeginPass. If you're confused about the order in which things are occurring, go ahead and capture a frame in PIX and look at the order in which SetRenderState calls occur.


thank your for your reply. i also confused that i enable the fog in pass 0; after endpass(0), i just use SetRenderState to enable fog, my intention just next rendering is effected by fog and not rendering by pass(0).
the renderstate can be changed by different pass; so, i think, why i end a pass,
its also effected by later?
thanks a lot!
Quote:
You can control all render states and sampler state settings with parameters (makes it easy to e.g. control texture filtering from the app).

At first, thanks for your reply. i try this method befor, but failed. my pass is:
Quote:
pass P0
{
Sampler[0] = (LayerMap0Sampler);
Sampler[1] = (BlendMapSampler);
vertexShader = compile vs_2_0 Terrain_MultiTexVS();
pixelShader = compile ps_2_0 Terrain_MultiTexPS1();


FogEnable = bEnableFog;
FogStart = g_FogStart;
FogEnd = g_FogEnd;
FogColor = g_FogColor;
}

and transforming fog parameter by:
Quote:
//set fog start
D3DXHANDLE FogStartHandle = g_pEffect->GetParameterByName(0, "g_FogStart");
g_pEffect->SetFloat(FogStartHandle, 300.0f);

//set fog end
D3DXHANDLE FogEndHandle = g_pEffect->GetParameterByName(0, "g_FogEnd");
g_pEffect->SetFloat(FogEndHandle, 3000.0f);

//set fog color
D3DXVECTOR4 colorfog = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f);
D3DXHANDLE FogColorHandle = g_pEffect->GetParameterByName(0, "g_FogColor");
g_pEffect->SetVector(FogColorHandle, &colorfog);

it can't work:(
Quote:
// grab a handle to the parameter
D3DXHANDLE enableFogHandle = fx->GetParameterByName("g_enableFog");
// to enable fog
fx->SetBool(enableFogHandle, true);
// to disable fog
fx->SetBool(enableFogHandle, false);


i just hava another question that confused. i just used SetValue as follow:
bool fogenable = true;g_pEffect->SetValue("bEnableFog", &fogenable, sizeof(bool));

this also didn't work. why?
Quote:Original post by huaner
pass P0
{
Sampler[0] = (LayerMap0Sampler);
Sampler[1] = (BlendMapSampler);
vertexShader = compile vs_2_0 Terrain_MultiTexVS();
pixelShader = compile ps_2_0 Terrain_MultiTexPS1();


FogEnable = bEnableFog;
FogStart = g_FogStart;
FogEnd = g_FogEnd;
FogColor = g_FogColor;
}

Try:
FogEnable = (bEnableFog);
FogStart = (g_FogStart);
FogEnd = (g_FogEnd);
FogColor = (g_FogColor);
Andre Loker | Personal blog on .NET

This topic is closed to new replies.

Advertisement