# Everything is fogged...

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

## Recommended Posts

Hi all,

I'm having a problem with fog.  No matter what parameters I set, my entire scene renders in the fog color.  I am not sure, but I *think* this is because my matrix is not "w-fog" complaint.  But for my purposes, rendering with z-fog would be just fine-- I just want a little bit of fogginess around the edges.

I'm setting up my projection matrix like this:

	float aAspect = (float)gPageWidth/(float)gPageHeight;
float aNear = 1.0f;
float aFar = GetZDepth();

float aWidth=COS(theFOV / 2.0f);
float aHeight=COS(theFOV / 2.0f);
if (aAspect > 1.0) aWidth /= aAspect;
else aHeight *= aAspect;

//aHeight/=.75f;

float s  = SIN(theFOV / 2.0f);
float d  = 1.0f - aNear/aFar;

float aMatrix[4][4];
aMatrix[0][0]=aWidth;
aMatrix[1][0]=0;
aMatrix[2][0]=0;
aMatrix[3][0]=0;
aMatrix[0][1]=0;
aMatrix[1][1]=aHeight;
aMatrix[2][1]=0;
aMatrix[3][1]=0;
aMatrix[0][2]=0;
aMatrix[1][2]=0;
aMatrix[2][2]=s/d;
aMatrix[3][2]=-(s * aNear / d);
aMatrix[0][3]=0;
aMatrix[1][3]=0;
aMatrix[2][3]=s;
aMatrix[3][3]=0;


And I'm trying to render fog like this:

	gDevice->SetRenderState(D3DRS_FOGENABLE, TRUE);
gDevice->SetRenderState(D3DRS_FOGCOLOR,theColor);
gDevice->SetRenderState(D3DRS_FOGTABLEMODE,D3DFOG_LINEAR);

theStart=.5f;
theEnd=1.0f;
gDevice->SetRenderState(D3DRS_FOGSTART,*(DWORD *)(&theStart));
gDevice->SetRenderState(D3DRS_FOGEND,*(DWORD *)(&theEnd));



No matter what numbers I put in there, my entire scene is fogged.  Even if I make theStart=.99999f .... it's all pure fog!  I've tried a million things-- using the true z numbers instead of the fractions, reversing them... I've thrown it all at it.

Anyone have any idea what's going on here?  Is there a way to force z-fog?

Edited by Raptisoft

##### Share on other sites

Increase theEnd by a lot. The fog is applied in different amounts to whatever is between the start and end distances, so whatever is closer to the camera than .5 distance has no fog, halfway (.75 away) would have half fog, and whatever is farther than 1 unit of distance from the camera is completely in covered in fog.

##### Share on other sites

Thank you for your help.  Using your adviced, I tweaked the numbers-- I managed to get the effect I want with

theStart=10;

theEnd=15;

I have a question, though-- I can't seem to make any of these values relate to the actual size of my frustrum.

For example, if I set my far clip plane to 80 (my world is scaled small), then start at 15, end at 20 works.  If I cut that in half to 40, then I get similar results using 5 and 7???  How do these numbers relate?

It's linear fog.

I'm trying, basically, to make it so full 100% fog kicks in right at the clip plane (or a little less, I'll have to see how the effect goes), and starts about halfway, maybe three quarters of the way there.  But like I said, I can't seem to find a good relationship.

Edited by Raptisoft

##### Share on other sites

You shouldn't be getting results like that, it looks like your projection matrix is set up wrong. Try using this formula http://msdn.microsoft.com/en-us/library/windows/desktop/bb147302(v=vs.85).aspx or just use a directx method like D3DXMatrixPerspectiveFovLH to set it up. If it's working correctly you should be able to set the far clip for the fog at the same distance as your regular  far clip plane and full fog should kick in at exactly the maximum view distance, and it should be a linear relationship.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 9
• 15
• 9
• 11