Jump to content
  • Advertisement
Sign in to follow this  

OpenGL Volumetric Fog and CG Shaders

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

Note: This is kinda a cross-post from http://www.gamedev.net/community/forums/topic.asp?topic_id=444165 I've made a new thread since I think the topic was really more a Graphics issue than a GL issue. If a mod sees this and think's it's shameless cross posting, feel free to delete So I'm stuck writing a volumetric fog shader in CG for OpenGL based on theory I only half understand. Heres the fog segment of the shader so far:
	//Get distance to fragment
	float3 commonVec = eyePosition - position.xyz;
	float P = sqrt(pow(commonVec[0],2) + pow(commonVec[1],2) + pow(commonVec[2],2));

	//Find distance to plane along commonVec
		//Find equation of plane of fog
	float3 plNormal; plNormal[0] = 0.0f; plNormal[1] = 1.0f;   plNormal[2] = 0.0f;
	float3 plFog;	 plFog[0] = 0.0f;	 plFog[1] = fogHeight; plFog[2] = 0.0f;
		plNormal *= plFog;
		float4 planeEquation;
		planeEquation[0] = plNormal[0] * plFog[0];
		planeEquation[1] = plNormal[1] * plFog[1];
		planeEquation[2] = plNormal[2] * plFog[2];
		planeEquation[3] = planeEquation[0] + planeEquation[1] + planeEquation[2];
		//distance to plane
		//     |ax0 + by0 + cy0 + d |
		// D = ----------------------
		//     root(a^2 + b^2 + c^2)
		//where a + b + c + d = 0 is the equation of the plane
		//and  x y z is the position of the point
			//top line first
		//	float D = fabs(planeEquation[0] * eyePosition[0]);
		float D = abs((planeEquation[0] * eyePosition[0]) + (planeEquation[1] * eyePosition[1]) + (planeEquation[2] * eyePosition[2]) + planeEquation[3]);
		//bottom line
		D /= sqrt(pow(planeEquation[0],2) + pow(planeEquation[1],2) + pow(planeEquation[2],2));

		//ok so D is distance to plane, and P is distance to fragment point
	float3 F; F.g = F.b = 0.0f; F.r = 0.2f;
		    C = I * F + ( 1 - I ) * P

where C is the final color, I then fog intensity, 
F the fog color and P the polygon's color. 
	 color = fHeight * F + ( 1 - fHeight) * color;

it's (supposedly) based off the concepts presented in this article: http://www.gamedev.net/reference/articles/article677.asp however it renders everything in black. My thinking was: i) Get distance from camera to fragment, that's your Zfar value. ii) Get distance from camera to fog plane, that's your Znear value. iii) Distance of fragment under fog plane is the density. I'm not even sure if thats right and judging by the way the article talks about 'portals' (which i don't understand at all) I'm wrong. Another article/tutorial to reference would be great, or some pointers on what i've done wrong.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!