[SOLVED][2.1] How to Fog

Discussion area about developing with Ogre-Next (2.1, 2.2 and beyond)


xrgo
OGRE Expert User
OGRE Expert User
Posts: 1148
Joined: Sat Jul 06, 2013 10:59 pm
Location: Chile
x 169

[SOLVED][2.1] How to Fog

Post by xrgo »

Hello! I need fog in my scene. I modified the pbs shader template to make it work.. I added this:

Code: Select all

float fogDistance = length( gl_FragCoord.z / gl_FragCoord.w );
float fogFactor = 1.0-clamp((fogParams.z - fogDistance) / (fogParams.z - fogParams.y),0.0,1.0);
outColour.xyz	= mix(finalColour, fogColor, fogFactor );
its linear fog... for "fogParams" and "fogColor" I just forced some values to test and it works

But obviously I need those params to come from Ogre, so I can set them in the SceneManager::setFog.

I read the porting manual and it says that you can use Hlms::setProperty( "key", value ) and @property... but its protected, and later on its chinese for me, too noob :roll:

I believe fog its a basic feature that must be out of the box, don't know what you guys think.
Last edited by xrgo on Wed Mar 04, 2015 3:35 pm, edited 2 times in total.
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5446
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1348

Re: [2.1] How to Fog

Post by dark_sylinc »

Wow! Interesting that you're going deeper into Ogre's src. Kudos.

The properties are not meant for passing runtime values that may change any time (otherwise it will generate another shader, which means more time spent compiling).

Since fogParams is a pass-wide parameter, you would insert into Structs_piece_vs_piece_ps.glsl the following snippet:

Code: Select all

layout(binding = 0) uniform PassBuffer
{
	//Vertex shader (common to both receiver and casters)
	mat4 viewProj;

@property( !hlms_shadowcaster )
	//Vertex shader
	mat4 view;
	vec4 fogParams; //<<--- SEE HERE THIS CHANGE.
	@property( hlms_num_shadow_maps )ShadowReceiverData shadowRcv[@value(hlms_num_shadow_maps)];@end

	//-------------------------------------------------------------------------

	//Pixel shader
	mat3 invViewMatCubemap;
@property( hlms_pssm_splits )@foreach( hlms_pssm_splits, n )
	float pssmSplitPoints@n;@end @end
	@property( hlms_lights_spot )Light lights[@value(hlms_lights_spot)];@end
@end @property( hlms_shadowcaster )
	//Vertex shader
	vec2 depthRange;
@end

} pass;
Note that the location in the struct is very important because when you're filling the buffer from C++; you must fill at the right offset.

Then on OgreHlmsPbs.cpp in HlmsPbs::preparePassHash; you'll create a bigger pass buffer accordingly:

Code: Select all

//mat4 viewProj;
size_t mapSize = 16 * 4;

if( !casterPass )
{
    //mat4 view + vec4 fogParams + mat4 shadowRcv[numShadowMaps].texViewProj +
    //              vec2 shadowRcv[numShadowMaps].shadowDepthRange +
    //              vec2 shadowRcv[numShadowMaps].invShadowMapSize +
    //mat3 invViewMatCubemap (upgraded to three vec4)
    mapSize += ( 16 + 4 + (16 + 2 + 2) * numShadowMaps + 4 * 3 ) * 4;
    mapSize += numPssmSplits * 4;
    mapSize = alignToNextMultiple( mapSize, 16 );
And finally fill this pass buffer with the right data at the right offset:

Code: Select all

if( !casterPass )
{
    //mat4 view;
    tmp = viewMatrix.transpose();
    for( size_t i=0; i<16; ++i )
        *passBufferPtr++ = (float)tmp[0][i];
    
    //Grab the fog parameters from wherever you like (e.g. sceneManager)
    *passBufferPtr++ = fogParam0;
    *passBufferPtr++ = fogParam1;
    *passBufferPtr++ = fogParam2;
    *passBufferPtr++ = fogParam3;

    for( int32 i=0; i<numShadowMaps; ++i )
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5446
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1348

Re: [2.1] How to Fog

Post by dark_sylinc »

xrgo wrote:I believe fog its a basic feature that must be out of the box, don't know what you guys think.
Yes, but linear fog is so 1999.

Once I'm done with architectural stuff (i.e. getting D3D11 stable; direct depth buffer access); I will be able to focus on pretty things like lights, fake/real GI and fog.
There are many kinds of fog now: Elevation based (it's like linear fog but accounts for height), volumetric fog, linear blending with background (I love this one btw).
xrgo
OGRE Expert User
OGRE Expert User
Posts: 1148
Joined: Sat Jul 06, 2013 10:59 pm
Location: Chile
x 169

Re: [2.1] How to Fog

Post by xrgo »

dark_sylinc wrote:Once I'm done with architectural stuff (i.e. getting D3D11 stable; direct depth buffer access); I will be able to focus on pretty things like lights, fake/real GI and fog.
There are many kinds of fog now: Elevation based (it's like linear fog but accounts for height), volumetric fog, linear blending with background (I love this one btw).
That's so great! I can't $&#@ wait!

In the meantime I still need fog to make things far fade a little =)

Using your snippets I am starting to understand how this works =). Thank you very much!!! but I still need a little help. this is what I did:

I want to use linear fog, so just 2 params are necessary... so I am just using a vec2.
And I need this params in the Pixel shader

in Structs_piece_vs_piece_ps.glsl I added this:

Code: Select all

//Uniforms that change per pass
layout(binding = 0) uniform PassBuffer
{
	//Vertex shader (common to both receiver and casters)
	mat4 viewProj;

@property( !hlms_shadowcaster )
	//Vertex shader
	mat4 view;
	@property( hlms_num_shadow_maps )ShadowReceiverData shadowRcv[@value(hlms_num_shadow_maps)];@end

	//-------------------------------------------------------------------------

	//Pixel shader
     vec2 fogParams;    //<<<<<<< I PUT IT HERE INSTEAD OF AVOBE
	mat3 invViewMatCubemap;
@property( hlms_pssm_splits )@foreach( hlms_pssm_splits, n )
	float pssmSplitPoints@n;@end @end
	@property( hlms_lights_spot )Light lights[@value(hlms_lights_spot)];@end
@end @property( hlms_shadowcaster )
	//Vertex shader
	vec2 depthRange;
@end

} pass;
and as you mentioned.. order is important, so I put it first in the pixel shader, before invViewMatCubemap... so in OgreHlmsPbs.cpp:

Code: Select all

        if( !casterPass )
        {
            //mat4 view + mat4 shadowRcv[numShadowMaps].texViewProj +
            //              vec2 shadowRcv[numShadowMaps].shadowDepthRange +
            //              vec2 shadowRcv[numShadowMaps].invShadowMapSize +
            //vec2 fogParams + mat3 invViewMatCubemap (upgraded to three vec4)
            mapSize += ( 16 + (16 + 2 + 2) * numShadowMaps + 2 + 4 * 3 ) * 4; //here I put a 2 before the three vec4 of invViewMatCubemap
            mapSize += numPssmSplits * 4;
            mapSize = alignToNextMultiple( mapSize, 16 );
and:

Code: Select all

            //---------------------------------------------------------------------------
            //                          ---- PIXEL SHADER ----
            //---------------------------------------------------------------------------

            //Linear Fog parameters, hardcoded for testing, Right before invViewMatCubemap
            *passBufferPtr++ = 50.0f;
            *passBufferPtr++ = 20.0f;

            Matrix3 viewMatrix3, invViewMatrix3;
            viewMatrix.extract3x3Matrix( viewMatrix3 );
            invViewMatrix3 = viewMatrix3.Inverse();

            //mat3 invViewMatCubemap
            for( size_t i=0; i<9; ++i )
            {
and then in the pixel shader:

Code: Select all

	float fogDistance = length( gl_FragCoord.z / gl_FragCoord.w );
	float fogFactor = 1.0-clamp((pass.fogParams.x - fogDistance) / (pass.fogParams.x - pass.fogParams.y),0.0,1.0);
	vec3  fogColor  = vec3(1.0,1.0,1.0);
	outColour.xyz	= mix(finalColour, fogColor, fogFactor );
I use it as "pass.fogParams"


And the thing is that fog is working!! but! material colors are all weird now, things look really funky :P
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5446
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1348

Re: [2.1] How to Fog

Post by dark_sylinc »

xrgo wrote:I want to use linear fog, so just 2 params are necessary... so I am just using a vec2.
And the thing is that fog is working!! but! material colors are all weird now, things look really funky :P
Two tips:
  • Many compilers fail to compile the offset correctly when using non-vec4 members because the layout rules from the spec are insanely hard to get right. Therefore, it's much easier if you just use vec4 and ignore the zw components you will not use (this is important when testing different GPU/driver combos!).
  • Even if the GLSL compiler correctly compiles using vec2, all variables have very strict alignment rules; in this context it means the next struct is aligned to a vec4; and that's why it gets funky. You're not considering the internal padding when filling the buffer from C++.
You can look at the std140 layout rules in the GL spec (they're 9) but they're nearly incomprehensible. Just use vec4 for all your variables (even GL driver engineers tell you this! :lol: ).

Cheers
xrgo
OGRE Expert User
OGRE Expert User
Posts: 1148
Joined: Sat Jul 06, 2013 10:59 pm
Location: Chile
x 169

Re: [2.1] How to Fog

Post by xrgo »

lol, yes, that did the trick =)
Thank you!!!
al2950
OGRE Expert User
OGRE Expert User
Posts: 1227
Joined: Thu Dec 11, 2008 7:56 pm
Location: Bristol, UK
x 157

Re: [2.1] How to Fog

Post by al2950 »

There are some very useful nugets of info in this thread!

**EDIT**
PS I have been playing more and more with HLMS, and I am beginning to really enjoy it :D
xrgo
OGRE Expert User
OGRE Expert User
Posts: 1148
Joined: Sat Jul 06, 2013 10:59 pm
Location: Chile
x 169

Re: [2.1] How to Fog

Post by xrgo »

EDIT: see this one, its using the hlms listener: http://www.ogre3d.org/forums/viewtopic. ... 81#p518819

If anyone is interested, this is the final working code for linear Fog:

on pixel shader, line ~347:

Code: Select all

	.....float fogDistance = length( gl_FragCoord.z / gl_FragCoord.w );  // or better "length( inPs.pos )" for a more accurate distance
	float fogFactor = 1.0-clamp((pass.fogParams.y - fogDistance) / (pass.fogParams.y - pass.fogParams.x),0.0,1.0);
	vec3  fogColor  = vec3(pass.fogColor.xyz);
	outColour.xyz	= mix(finalColour, fogColor, fogFactor );......
on Structs_piece_vs_piece_ps.glsl, line ~35:

Code: Select all

	.....//Pixel shader
  vec4 fogParams;
  vec4 fogColor;
	mat3 invViewMatCubemap;....
on OgreHlmsPbs.cpp, line ~526:

Code: Select all

       ...... if( !casterPass )
        {
            //mat4 view + mat4 shadowRcv[numShadowMaps].texViewProj +
            //              vec2 shadowRcv[numShadowMaps].shadowDepthRange +
            //              vec2 shadowRcv[numShadowMaps].invShadowMapSize +
            //vec4 fogParams + vec4 fogColor + mat3 invViewMatCubemap (upgraded to three vec4)
            mapSize += ( 16 + (16 + 2 + 2) * numShadowMaps + 4 + 4 + 4 * 3 ) * 4;
            mapSize += numPssmSplits * 4;
            mapSize = alignToNextMultiple( mapSize, 16 );

            if( shadowNode )
            {......
and line ~616:

Code: Select all

            .....//---------------------------------------------------------------------------
            //                          ---- PIXEL SHADER ----
            //---------------------------------------------------------------------------

            //Linear Fog parameters
            *passBufferPtr++ = sceneManager->getFogStart();
            *passBufferPtr++ = sceneManager->getFogEnd();
            *passBufferPtr++ = 0.0f;
            *passBufferPtr++ = 0.0f;

            *passBufferPtr++ = sceneManager->getFogColour().r;
            *passBufferPtr++ = sceneManager->getFogColour().g;
            *passBufferPtr++ = sceneManager->getFogColour().b;
            *passBufferPtr++ = 1.0f;

            Matrix3 viewMatrix3, invViewMatrix3;
            viewMatrix.extract3x3Matrix( viewMatrix3 );
            invViewMatrix3 = viewMatrix3.Inverse();

            //mat3 invViewMatCubemap
            for( size_t i=0; i<9; ++i )
            {......
Thank you very much Matias!
Last edited by xrgo on Sun Apr 17, 2016 5:54 pm, edited 2 times in total.
xrgo
OGRE Expert User
OGRE Expert User
Posts: 1148
Joined: Sat Jul 06, 2013 10:59 pm
Location: Chile
x 169

Re: [SOLVED][2.1] How to Fog

Post by xrgo »

Hello!!!!!! I have another related question:

I need some of my objects to "glow" with a certain frequency (to indicate a selected/target object).

on 1.X, in the fragment shader I had something like this:

glowColor = sin(time * freq)*0.5+0.5;
outColor += glowColor*enableGlow;

and in the fragment_program definition I had

param_named_auto time time_0_x 100.0
param_named freq float 1

and in code:

materialPass->getFragmentProgramParameters()->setNamedConstant("enableGlow", 1);


The question is:
How to do in my pbs shader something like "time_0_x 100.0" ?? for now I just want a fixed frequency but I do need to enable it and disabled it at my will by code.

Another option that I like more: create a clone of the datablock ( http://www.ogre3d.org/forums/viewtopic.php?f=25&t=82993 ) for this new datablock enable some @property( glow ) that does the glow math, and apply it to the object. I prefer this option because if I have many objects sharing the same datablock I can select/target/glow only the desired one(s)... I'll still need "time_0_x 100.0".

Thanks in advance!!!!!!
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5446
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1348

Re: [SOLVED][2.1] How to Fog

Post by dark_sylinc »

The first thing you need to decide is where this "enableGlow" parameter will live. I'll assume it will live in HlmsPbsDatablock. Let's call it bool mHasGlow.
Be careful not to put the variable in the middle of this declaration:

Code: Select all

float   mkDr, mkDg, mkDb;                   //kD
float   _padding0;
float   mkSr, mkSg, mkSb;                   //kS
float   mRoughness;
float   mFresnelR, mFresnelG, mFresnelB;    //F0
float   mNormalMapWeight;
float   mDetailNormalWeight[4];
float   mDetailWeight[4];
Vector4 mDetailsOffsetScale[8];
uint16  mTexIndices[NUM_PBSM_TEXTURE_TYPES];
Because we memcpy this data directly to the GPU in HlmsPbsDatablock::uploadToConstBuffer (unless you want the Vertex/Pixel shader to access this data, in which case you'll have to modify the template's material structure to keep the offsets correct).

Now, once you have the boolean flag, you can read it in HlmsPbs::calculateHashForPreCreate and call:

Code: Select all

setProperty( "glow", datablock->mHasGlow );
You will notice in the code that instead of writing "glow" directly, I use global constant variables. That is because I want to force the compiler to calculate the hash of string at compile time (or worst case at launch time) instead of hashing the string every time this code is executed (it will depend on how good the compiler optimizer is... let's say I don't trust it...). In other words it's just an optimization.
By doing this, @property( glow ) will now work.

Then the function HlmsPbs::calculateHashForPreCaster will delete this property since it's not needed for shadow casting. If you actually need it, you will have to modify its logic to spare the property variable from being deleted.

Deleting unused properties for the shadow casters is important because having the property "glow" set to 1, set to 0, and as non-existant property will be seen as 3 different materials by the RenderQueue (even if the shader template ignores the glow when shadow casting is set); which means less optimization opportunities when sorting ther RQ (batching) and possibly more memory used (which isn't much on its own, but it starts to add up with many datablocks and many different feature combinations).

Last but not least; when you toggle HlmsPbsDatablock::mHasGlow; the change won't be seen automatically by all the Renderables currently using your datablock. In order to do that, you need to call Hlms::flushRenderables.
See HlmsPbsDatablock::setDetailMapBlendMode for an example.
You need to call Hlms::flushRenderables whenever a new shader code is needed; because the actual code is modified.

Is flushRenderables cheap? Not really. But it's not super expensive either. It depends:
flushRenderables will iterate over all Renderables currently using that datablock. If this datablock is used by 1.000.000 renderables, it will make 1.000.000 iterations to reevaluate the Renderables again. Calling this frequently would be counterproductive.
Also the first few times you will end up probably generating new shader code, which needs to be compiled. Once it has been compiled for the first time, Ogre will save a cache; so toggling the setting back to its original value (ceteris paribus) won't trigger another recompilation.

If you need to toggle waaaay too often; then it may be best to use the glow code for all objects, and disable it via the pixel shader by reading the material variables in the GPU (i.e. multiply by 0 when glow is disabled).

Do not confuse flushRenderables with with HlmsPbsDatablock::scheduleConstBufferUpdate: This is only needed when you change a material variable that lives in the GPU buffer; like the diffuse colour (see HlmsPbsDatablock::setDiffuse). As the name implies this function schedules the datablock to later eventually uploadToConstBuffer call (and upload all variables, not just the ones that changed, in burst across the PCI-e bus).

Some functions may have to do both (flushRenderables and scheduleConstBufferUpdate); such as HlmsPbsDatablock::setFresnel: If the fresnel value changes, we need to update the GPU data. But if also the fresnel changes from being the same value for all 3 channels (RGB) to being different for each channel, different shader code is needed and thus we call flushRenderable.
darkheron
Kobold
Posts: 28
Joined: Fri Aug 29, 2014 9:05 pm

Re: [SOLVED][2.1] How to Fog

Post by darkheron »

This is kind of timely because I've just been poking around in the UnlitDatablock version of scheduleConstBufferUpdate and the HlmsUnlit->scheduleForUpdate methods.

I'm just trying to get a texture to animate with just a simple scrolling ( for testing )using this hlms material:

Code: Select all

hlms DebugCube unlit
{
	diffuse_map	Rocks_Diffuse.tga
	animate	0
}
and this little snippet of code:

Code: Select all

    Ogre::HlmsManager *hlmsManager = Ogre::Root::getSingleton().getHlmsManager();
    Ogre::HlmsUnlitDatablock *datablock = static_cast<Ogre::HlmsUnlitDatablock*>(
    	hlmsManager->getDatablockNoDefault( "DebugCube" ));
    if (datablock)
    {
    	Ogre::Matrix4 anim;
    	anim.setTrans(Ogre::Vector3(.1f,0,0));
    	datablock->setEnableAnimationMatrix(0,true);
    	datablock->setAnimationMatrix(0,anim);
    }
but I get an exception:

Code: Select all

terminate called after throwing an instance of 'Ogre::InvalidParametersException'
  what():  OGRE EXCEPTION(2:InvalidParametersException): Parameter called animationMatrixBuf does not exist. Known names are: worldMatBuf worldMatBuf[0]  in GpuProgramParameters::_findNamedConstantDefinition at /home/darkheron/Projects/ogre/v2-1/OgreMain/src/OgreGpuProgramParams.cpp (line 2214)
The text in the porting document pg 58 "There is also an “Unlit” implementation, specifically meant to deal with GUI and simple particle FXs (ignores normals & lighting, manages multiple UVs, can mix multiple texture with photoshop-like blend modes, can animate the UVs, etc)" Had me thinking that the animationMatrix was how I would accomplish this .. but maybe not?

Am I going about this the wrong way?
User avatar
Jayray
Greenskin
Posts: 115
Joined: Sun Sep 09, 2012 5:29 pm
Location: Strasbourg, France
x 9

Re: [SOLVED][2.1] How to Fog

Post by Jayray »

In the Ogre 2.1 trello, there is a task named "Unlit's texture animation should work" in the "TODO" list.
So I guess the animations are not finished yet :(
darkheron
Kobold
Posts: 28
Joined: Fri Aug 29, 2014 9:05 pm

Re: [SOLVED][2.1] How to Fog

Post by darkheron »

Jayray wrote:In the Ogre 2.1 trello, there is a task named "Unlit's texture animation should work" in the "TODO" list.
So I guess the animations are not finished yet :(
Oh snap. Thanks didn't know about that trello site. Guess I'll move on from this experiment for the time then :)
xrgo
OGRE Expert User
OGRE Expert User
Posts: 1148
Joined: Sat Jul 06, 2013 10:59 pm
Location: Chile
x 169

Re: [SOLVED][2.1] How to Fog

Post by xrgo »

Thank you very much Matias! i'll try that later =)
xrgo
OGRE Expert User
OGRE Expert User
Posts: 1148
Joined: Sat Jul 06, 2013 10:59 pm
Location: Chile
x 169

Re: [SOLVED][2.1] How to Fog

Post by xrgo »

Thank you matias!!!!!!! it works... now I need that "time_0_x 100.0" to animate my glow, can you help me with that please?

EDIT: I solved the animation the same way with the Fog:

in OgreHlmsPbs.cpp
I added:
#include "Ogre.h"
and later
*passBufferPtr++ = Ogre::Root::getSingletonPtr()->getTimer()->getMilliseconds();

and everything else the same way as the fog
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5446
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1348

Re: [SOLVED][2.1] How to Fog

Post by dark_sylinc »

There's a good talk about fog by Iñigo at http://iquilezles.org/www/articles/fog/fog.htm.

I'm leaving this link for later coming back.
User avatar
miki3d
Halfling
Posts: 56
Joined: Wed Jul 18, 2012 1:30 pm
Location: Italy
x 4

Re: [SOLVED][2.1] How to Fog

Post by miki3d »

Hi all!

Here's the way we added fog to the old build of our game running with ogre 1.9
Duno if it could be helpfull to you :D but we're also interested in the new version and I want to share our method with you.

Constant coloured fog did not fit good with envs so we applied the ambient cubemap to the fog function and the result was not bad

Code: Select all

//fog exp
float3 fogExp(float3 ret, samplerCUBE diffuseCubeSampler, float3 eyePos, float3 pos, float fogDensity)
{	
	float3 eyeDir = normalize(eyePos-pos);
	float3 fogAmbient = texCUBE(diffuseCubeSampler, eyeDir*float3(-1.0, -1.0, 1.0)).rgb;
	float3 tmp = (pos - eyePos);
	float f = exp(-dot(tmp, tmp)*fogDensity);
	return ret * f + fogAmbient*(1 - f);
}
Image
- Screenshots from the same env with different view directions



Anyway fog by Iñigo is also good!

For ogre 2.1 we also thinked to implement fog with a compositor, what do you think about it?

Thanks :)

Michele
Image
Co-Founder and Design Director at VaeVictis
Current project Racecraft
Feanor16
Halfling
Posts: 46
Joined: Tue Feb 18, 2014 10:49 pm

Re: [SOLVED][2.1] How to Fog

Post by Feanor16 »

Hi, is there at this time, any way to use offically the Fog using the 2.1 Version without edit the source Code Himself?
al2950
OGRE Expert User
OGRE Expert User
Posts: 1227
Joined: Thu Dec 11, 2008 7:56 pm
Location: Bristol, UK
x 157

Re: [SOLVED][2.1] How to Fog

Post by al2950 »

Feanor16 wrote:Hi, is there at this time, any way to use offically the Fog using the 2.1 Version without edit the source Code Himself?
No, but it is extremely easy! Although I guess thats not the answer you want to hear!

There is no standard fixed function way of doing 'fog' in modern engines, so it does not make a huge amount of sense having something out of the box, but perhaps a sample or tutorial of 'basic fog' would be useful.
123iamking
Gremlin
Posts: 152
Joined: Sat Aug 12, 2017 4:16 pm
x 4

Re: [SOLVED][2.1] How to Fog

Post by 123iamking »

So after skimming through this topic, I see that we has to tweak a little to add fog to the scene, but I really want to use the function setFog to create fog, but the code below just doesn't work, of course :cry:

Code: Select all

Ogre::ColourValue fadeColour(0.9, 0.9, 0.9);
sceneManager->setFog(Ogre::FOG_EXP2, fadeColour, 0.002);
I really want to make fog like this scene from Price of Persia: warrior within - mystic caves
Image
Is there a quick simple way I can archive this?
Thanks
User avatar
Kohedlo
Orc
Posts: 435
Joined: Fri Nov 27, 2009 3:34 pm
Location: Ukraine, Sumy
x 32

Re: [SOLVED][2.1] How to Fog

Post by Kohedlo »

If anyone is interested, this is the final working code for linear Fog:
this second implementstion have no visible working.


code before sets colout of ambience but with bug.

according forward code- possible need calling for type or other additions. :?:
c++ game developer.
current project: Imperial Game Engine 2.5
Image
User avatar
Crystal Hammer
Orc
Posts: 402
Joined: Sat Jun 23, 2007 5:16 pm
x 112

Re: [SOLVED][2.1] How to Fog

Post by Crystal Hammer »

Okay, it's been like 5 to 7 years, no matter. :roll:
Is this still the way to add fog?
I mean do I need to edit Ogre files? :shock: Or can I just maybe inherit from Pbs, override some methods and add what's needed for fog?

zxz
Gremlin
Posts: 184
Joined: Sat Apr 16, 2016 9:25 pm
x 19

Re: [SOLVED][2.1] How to Fog

Post by zxz »

As far as I know, this is still the way.

It doesn't require modifying Ogre though. You can set a custom HlmsListener that lives in your project, and you can keep your own shader snippets in your own project too, by adding a custom archive path when initializing HlmsPbs.

User avatar
Crystal Hammer
Orc
Posts: 402
Joined: Sat Jun 23, 2007 5:16 pm
x 112

Re: [SOLVED][2.1] How to Fog

Post by Crystal Hammer »

So I partially added fog to tutorial terrain demo from 2.3 here, in a new file Media/Hlms/Common/Any/Fog_piece_ps.any.
Just constant values, nothing passed yet.
And I already have some issues. Scren here:
Image
I think it's either that weird issue with glow around edges or some issue with alpha (even looks like additive). Or issue with wrong positions, because I'm using just inPs.pos for fog distance in all shaders. I don't even know how to get world pos passed into pixel shader in HLMS now. :? Or should I even follow Tutorial_ReconstructPosFromDepth for that? This is probably also the same problem I have with terrain. It has less fog than trees.
I also don't get why we have Any, GLSL, HLSL and such folders if all I do is edit .any shaders while using GL3+. Also what do those numbers on front do e.g. 200.Textures_piece_ps, is it order of including? Is there maybe already some documentation for this?
Any hints?

User avatar
Crystal Hammer
Orc
Posts: 402
Joined: Sat Jun 23, 2007 5:16 pm
x 112

Re: [SOLVED][2.1] How to Fog

Post by Crystal Hammer »

Okay I fixed it.
inPs.pos is enough, no need to pass, this is local pos mul by worldView, so z is depth.
That "additve" glow around trees is gone after I mul'ed fogColor by outPs_colour0.w (pixel alpha).
Details in my commit.