[Solved] Scene fog is ignored using ShadeEntity

BlitzMax Forums/MiniB3D Module/[Solved] Scene fog is ignored using ShadeEntity

RustyKristi(Posted 2016) [#1]
Hi, When I tried to set up some custom material shader on a mesh, the current fog setting does not affect that object when I apply ShadeEntity.

A good example is with per pixel lighting demo where the scene fog is ignored if you add fog in it.

' sl_pixellight.bmx
' per pixel lighting

Strict

Framework Openb3d.B3dglgraphics

Graphics3D 800,600,0,2


Local camera:TCamera=CreateCamera()
CameraClsColor camera,70,180,235

Local lighttype%=2 ' set lighttype 1 or 2
Local light:TLight=CreateLight(lighttype)
RotateEntity light,45,45,0
PositionEntity light,10,10,0
LightRange light , 10


CameraFogMode camera,1
CameraFogRange camera , 1 , 100
CameraFogColor camera , 94 , 95 , 110


Local teapot:TMesh=LoadMesh("media/teapot.b3d")
PositionEntity teapot,0,5,3

Local teapot2:TMesh=LoadMesh("media/teapot.b3d")
PositionEntity teapot2,0,5,3
HideEntity teapot2

Local shader:TShader=LoadShader("","shaders/pixellight.vert.glsl","shaders/pixellight.frag.glsl")
SetInteger(shader,"lighttype",lighttype)
ShadeEntity(teapot,shader)

Local pixellight%=1

Local MSY
Local MSX

While Not KeyDown(KEY_ESCAPE)

	' enable pixel lighting
	If KeyHit(KEY_P)
		pixellight=Not pixellight
		If pixellight
			HideEntity teapot2 ; ShowEntity teapot
		Else
			HideEntity teapot ; ShowEntity teapot2
		EndIf
	EndIf
	
	MSY = MouseY() - GraphicsHeight()/2
	MSX = MouseX() - GraphicsWidth()/2
	
	RotateEntity camera, EntityPitch(camera) + MSY/2, EntityYaw(camera) - MSX/2, 0
	If KeyDown(KEY_S) Then MoveEntity camera, 0, 0, -1
	If KeyDown(KEY_W) Then MoveEntity camera, 0, 0, 1
	If KeyDown(KEY_A)  Then MoveEntity camera, -1, 0, 0
	If KeyDown(KEY_D) Then MoveEntity camera , 1 , 0 , 0		
	
	
	MoveMouse GraphicsWidth()/2,GraphicsHeight()/2
		

	TurnEntity teapot,0,0.5,-0.1
	TurnEntity teapot2,0,0.5,-0.1
	
	RenderWorld
	
	Text 0,0,"lighttype = "+lighttype+", P: pixellight = "+pixellight
	
	Flip

Wend
End



Maybe this has something to do with passing some uniforms or values to the pixel/frag shader or something?


markcw(Posted 2016) [#2]
You need to code the fog in. I'll get an example of this when I've worked it out. For now try just this line in the vert (taken from here) although it probably won't be this easy.

gl_FogFragCoord = length(vec3(gl_ModelViewMatrix * gl_Vertex))



RustyKristi(Posted 2016) [#3]
Got it thanks! Looking forward to your example as well. :D


markcw(Posted 2016) [#4]
Ok, I have an example here which does per pixel fog, the link I gave before does per vertex fog and bumpmap which conflicted with per pixel lighting but this does both.

It's very simple, so should be easy to add to other shaders like bumpmap. All you do is in the frag add this, where lpixel is what you would normally be setting gl_FragColor to.
uniform float density;

main()
{

	// calculate 2nd order exponential fog factor based on fragment's Z distance
	const float e = 2.71828;
	float fogFactor = (density * gl_FragCoord.z);
	fogFactor *= fogFactor;
	fogFactor = clamp(pow(e, -fogFactor), 0.0, 1.0);
	
	gl_FragColor = mix(fogColor, lpixel, fogFactor);

Then in Bmx set up the fog like this - density calculation is something I came up with.
Local fogmode%=1,fogrange#=100
Local fogr#=94, fogg#=95, fogb#=110
CameraFogMode camera,fogmode
CameraFogRange camera, 1, fogrange
CameraFogColor camera, fogr, fogg, fogb

Local shader:TShader=LoadShader("","shaders/pixelfog.vert.glsl","shaders/pixelfog.frag.glsl")
SetInteger(shader,"lighttype",lighttype)
SetFloat4(shader,"fogColor", fogr/255, fogg/255, fogb/255, 1.0)
Local density#=1
UseFloat(shader,"density",density)

While Not KeyDown(KEY_ESCAPE)

	density=EntityDistance(camera,sphere2)/(fogrange*0.5) ' calculate fog density
	If Not fogmode Then density=0



RustyKristi(Posted 2016) [#5]
Awesome, just awesome munch. thanks!