How do you implement Fog into a scene?

Hi All,

I have finally got some 3D up and running so things are whizzing about (which is always fun to watch... for a while, yeah I know, small things...) and I was wondering if anyone had any examples of putting fog into a scene.

I tried last night and succeeded in turning all my meshes to white, not matter how far away they were from the camera. No matter what I did to the values, nothing changed.

As always, any help is greatly appreciated.

Cheers




Answer this question

How do you implement Fog into a scene?

  • TBeadle

     LeeC22 wrote:
    More than 8 bits per colour component in an image format used in games, which format is that


    One of the biggest developments in the latest generation of games is the use of high dynamic range lighting, which involves using colors brighter than "white" to give more realistic lighting and exposure effects. Tool support for this is still limited, but there are several HDR formats around already (OpenEXR, developed by ILM, is one of the most widely used). Photoshop has also been making a big push to get proper support for >8 bit formats in the last couple of versions, mostly driven by the needs of photographers who are increasingly wanting to work with 16 bit per channel digital images, but also extremely useful for the lastest generation of high end games.

    Admittedly, we're still a long way from the point where all games will care about this stuff, but one of the challenges in designing a programming platform is to try to build in some longevity. If we see things moving in a direction toward HDR rendering, it seems sensible to assume that trend will continue, and if we want our framework to still be relevant a few years down the road, we'd better make sure we have good support ready for this!

    Note also that our use of floating point color formats doesn't prevent you using the traditional 0-255 integer range if that's what you prefer. We provide the Color type for exactly that purpose. Color implements conversions to and from Vector3 and Vector4.




  • Eslucafe

    As far as I remember, most good artists are versed in quite a few color schemes, of which RGB is not entirely the most important. For instance, print artists work almost entirely in CMYK because they know the colors they see on screen will be more easily reproduced with real accuracy by their printers, granted the only difference between that and RGB is the additive/subtractive thing, and normalizing to use black ink it is still a different system to learn and be comfortable with. Using the 0-1 scale should be no different than learning any other color scheme.


  • MJC2006

    Shawn Hargreaves - MSFT wrote:
    LeeC22 wrote:
    More than 8 bits per colour component in an image format used in games, which format is that



    One of the biggest developments in the latest generation of games is the use of high dynamic range lighting, which involves using colors brighter than "white" to give more realistic lighting and exposure effects. Tool support for this is still limited, but there are several HDR formats around already (OpenEXR, developed by ILM, is one of the most widely used). Photoshop has also been making a big push to get proper support for >8 bit formats in the last couple of versions, mostly driven by the needs of photographers who are increasingly wanting to work with 16 bit per channel digital images, but also extremely useful for the lastest generation of high end games.

    Admittedly, we're still a long way from the point where all games will care about this stuff, but one of the challenges in designing a programming platform is to try to build in some longevity. If we see things moving in a direction toward HDR rendering, it seems sensible to assume that trend will continue, and if we want our framework to still be relevant a few years down the road, we'd better make sure we have good support ready for this!

    Note also that our use of floating point color formats doesn't prevent you using the traditional 0-255 integer range if that's what you prefer. We provide the Color type for exactly that purpose. Color implements conversions to and from Vector3 and Vector4.


    Sorry HDR, I always think of that as more a rendering thing more than ingame. I guess if I'm having trouble with simple fog, HDR is a way off.. but you're right.

    So can I do a Color(new Color(255,255,255)).ToVector3() or Color(myColour).ToVector3()

    I'd like to apologise if people think I go a bit overboard and defensive some times... it's a frustrating time for non-programmers (well, secondary programmers in my case) learning new languages. We've got all these ideas and struggle to get them out. I do appreciate the help and I hope I don't cause offense with any of my replies.

    Thanks Guys (and girls if there are any here)



  • BubbaHasty

    "As far as I remember, most good artists are versed in quite a few color schemes, of which RGB is not entirely the most important."

    On a phosphor based screen where the colours are composed of Red, Green and Blue elements and utilised by around 90% of all games systems, how can RGB not be the most important

    Versed in a few colour schemes Well I started on the ZX81 with a choise of 2 colours, then the Spectrum where some bright spark though Bright Black would be useful. Or how about the Amstrad CPC with a 27 colour palette composed of 50% orange or blue... CGA, EGA, Gameboy Colour, Mobile Phones, 12 bit, 14 bit... how many colour schemes should I cover

    CMYK is used in the printing process because you are dealing with ink or paint, which has entirely different cumulative properties than light. If printers were RGB and you wanted Black, you would need to print with no ink... which I suspect might give you white (on white paper). CMYK is typically use in separation printing, not games. Images are generally submitted in the 4 separate layers to enable more accurate representation of the colours. No game artist would ever need to use CMYK for games... ever. For the record, CMYK uses 0 to 100 as a percentage scale, not colour value.

    But we're not talking print artists, we're talking game artists which has nothing to do with Good or Bad. Unless you are implying that as I am complaining about this, that I qualify as a bad artist. If that's the case, then I would suggest that my 4 recent 10/10 projects may disagree.

    8 bit colour has been around for I'd say, 20 years if not more, long before 32 bit colour (with alpha components). Certainly as long as I have been making games (20 years).

    Photoshop is pretty much the accepted standard in professional art packages, how are the colours represented Pro-Motion is pretty much the industry standard for mobile and handheld development, what about the colours in that Microsoft Paint anyone Illustrator, Corel Painter.. 0 - 1 is a programmer decision, if it wasn't, art packages wouldn't be using 0 - 255.

    More than 8 bits per colour component in an image format used in games, which format is that

    Maya See this pic http://www.digital-essence.co.uk/misc/maya_colour_picker.png ... sorry, "shock horror" which one is the odd one out here Maybe that's why they are happy to work in Maya, you can still use 0 - 255. Perhaps they just tell you otherwise... so you can misquote them on message boards... I'd have words

    "giving programmers used to DirectX the power to do what they want and making life easier for others is quite hard." pretty much says it all... giving the programmers the power to do what they want. It's not hard, it's a case of "why should we make our system usable by those who are too stupid to learn how to programme ". You've only got to read through these forums to find the "if you're thick, try Flash, it's drag and drop for dummies" type threads.

    I guess it's a good job they didn't make art packages too difficult for those too stupid to be able to draw.

    The bottom line is, one man dictates, the rest have no choice but to follow. I'm sorry if this sounds a bit harsh but... if you're not an artist, how can you make a reasonable judgement on what is easy to work with for artists Can you imagine the fuss if an artist/programmer developed a new OS and said, "sorry guys, we've gone to 5 bits for code instructions. You better start learning to count differently"

    Anyway, this argument will go nowhere other than a "we think it's right... well we think it's wrong" scenario, so I'll leave my contribution at that... I'll go write my conversion functions instead...



  • Danny Tuppeny

    Its not like a bunch of artists got together and decided that 0..255 is the most artist friendly colour range around. 0..255 is only used because it is common to represent images in 24 bits (32 bits with alpha or padding) with 8 bits per colour component and 255 is the maximum number that can be represented with 8 bits. Hardly an artist friendly definition and also not so useful if you have a graphics format with more or less than 8 bits per colour component.

    Most of my game artist colleagues are more than happy to work with 0..1 (or shock, horror larger than 1.0 when dealing with HDR) as their artist applications like Maya, XSI and blender all use 0..1 colour ranges (Max is the odd one out here).

    Admittedly, XNA could be improved for people without their technical hats on but complaining about the colour formats is a bit peevish and I imagine that finding a balance in the API between giving programmers used to DirectX the power to do what they want and making life easier for others is quite hard.

    Cheers,
    Leaf.



  • Maheswar

    code please

  • dvh

    As well as setting those device renderstates, you also need to compute the amount of fog in your vertex shader. Typically that will just be the distance from the camera, which you output using the FOG shader semantic, but you could use some more sophisticated calculations to implement volumetric effects, layered fog, and so on.

    If you are using BasicEffect, that has fog support built in so you can just set the fog properties on your BasicEffect instance, and not need to mess with the device renderstates at all.


  • Tsayers

    The BasicEffect FogColor property is a Vector3, so it ranges from 0,0,0 = black to 1,1,1 = white, not all the way up to 255.

    For a mid-grey fog color, you can either set 0.5f, or use the conversion methods on the Color class, for instance you could pass Color.Gray.ToVector3().


  • Josip Orec

    vidalsasoon wrote:
    Vectors that range from 0,0,0 to 1,1,1 are called "Normalized" vectors. They're a good thing.

    The guys at XNA are thinking beyond hard coding for 0-255 (or 0-65535).
    I.e. 0 is always the minimum and 1 is always the maximum.

    I get the Vectors thing, but that's maths, this is graphics and colour. What's a good thing for those people who can't think in RGB isn't a good thing for those that can. 0 to 1 to me is Black to almost Black, not Black to White.

    Thank god these guys don't write art packages There's no reason to "think beyond hard coding" a perfectly acceptable and all too common set of numbers other than to make it programmer compatible.

    Another conversion method on the way I guess... who knows, I might get round to writing my games one day.

    I sometimes think there's no wonder we've seen nothing but space invaders and asteroids, stifling and obstructing the creative element is no way to encourage creative development. XBLA is going to be so much fun...

    Thanks for the help guys (and that is a sincere thank you, I do appreciate your help), I guess my art and design head gets left in the drawer again tonight



  • rWarrior

    LeeC22 wrote:

    Shawn Hargreaves - MSFT wrote:
    The BasicEffect FogColor property is a Vector3, so it ranges from 0,0,0 = black to 1,1,1 = white, not all the way up to 255.

    For a mid-grey fog color, you can either set 0.5f, or use the conversion methods on the Color class, for instance you could pass Color.Gray.ToVector3().

    Seriously Who comes up with this stuff Only a programmer could refer to something as a Colour, sorry "Color" (learn a language and the Americanisms that go with it eh You planning on a language pack ) and then change the range that people are familiar with, for setting colours.

    So, let me see if I understand this, the range 0 to 255 is now represented as the range 0 to 1 I have to wonder why the conversion isn't done internally, in order to leave the standard colour range exposed to the user.

    Please, before you release the final of this developers tool, give it to someone who is part of the market you are aiming this at... for the sakes of everyone who doesn't "just programme".

    Actually, just to jump back to the beginning of your post... "The BasicEffect FogColor property is a Vector3, so it ranges from 0,0,0 = black to 1,1,1 = white". Are you saying that Vector3's only cover the range 0,0,0 to 1,1,1

    Thanks Shawn... good fun this isn't it



    Vectors that range from 0,0,0 to 1,1,1 are called "Normalized" vectors. They're a good thing.

    The guys at XNA are thinking beyond hard coding for 0-255 (or 0-65535).
    I.e. 0 is always the minimum and 1 is always the maximum.

  • TwilightBrigade

    Shawn Hargreaves - MSFT wrote:
    The BasicEffect FogColor property is a Vector3, so it ranges from 0,0,0 = black to 1,1,1 = white, not all the way up to 255.

    For a mid-grey fog color, you can either set 0.5f, or use the conversion methods on the Color class, for instance you could pass Color.Gray.ToVector3().

    Seriously Who comes up with this stuff Only a programmer could refer to something as a Colour, sorry "Color" (learn a language and the Americanisms that go with it eh You planning on a language pack ) and then change the range that people are familiar with, for setting colours.

    So, let me see if I understand this, the range 0 to 255 is now represented as the range 0 to 1 I have to wonder why the conversion isn't done internally, in order to leave the standard colour range exposed to the user.

    Please, before you release the final of this developers tool, give it to someone who is part of the market you are aiming this at... for the sakes of everyone who doesn't "just programme".

    Actually, just to jump back to the beginning of your post... "The BasicEffect FogColor property is a Vector3, so it ranges from 0,0,0 = black to 1,1,1 = white". Are you saying that Vector3's only cover the range 0,0,0 to 1,1,1

    Thanks Shawn... good fun this isn't it



  • tkrasinger

    Okay, so I now have fog in the distance but have something strange with it. I set my FogColor to 128,128,128 and my background color to 128,128,128 but when the meshes appear in the distance, they are bright white.

    The only way I can get things to fade in, is to have the background as 255,255,255. Have I missed something totally obvious here or is this how the blending on fogged meshes works



  • Dhil

    Shawn Hargreaves - MSFT wrote:
    As well as setting those device renderstates, you also need to compute the amount of fog in your vertex shader. Typically that will just be the distance from the camera, which you output using the FOG shader semantic, but you could use some more sophisticated calculations to implement volumetric effects, layered fog, and so on.

    If you are using BasicEffect, that has fog support built in so you can just set the fog properties on your BasicEffect instance, and not need to mess with the device renderstates at all.

    Ah, now you see, if you were talking to a dedicated programmer then the use of sophisticated calculations would no doubt be second nature. Unfortunately, I am one of those primary artist/designer, secondary programmer types who got attracted to XNA, so I have to take the simple option. Unfortunately, I am battling against documentation and losing on a regular basis.

    For example..

    I think, okay, let's search for Fog... and we get all the properties, FogStart, FogEnd etc... So I click FogStart and get...

    Property Value:
    Fog start distance specified as a positive value."

    .... based on what Over what distance, what unit measurement, is there a min/max, is it based on the near/far clip plane of the FOV matrix If you look at Renderstate.FogEnd, the default property is 1.0f. Does that mean that the range of fog extends from 0.0f to 1.0f If so, where does 1.0f sit in the world, how far away is 1.0f. Is the BasicEffect.FogStart/FogEnd even the same as this

    You see why non-programmers are having such problems

    I'm getting concerned that the graphical polish has been put into the hands of the coders but if we rely on them to provide the basics to add the polish to, well... we're all familiar with programmer art.

    I've looked at pixel and vertex shaders... in between quaternions and the likes. If I stood on the Empire State building, it would still go way above my head. In my mind, special effects do not equate to maths calculations, they equate to variances and fluctuations in light and colour properties. Specularity and reflectivity... not SQRT and STRUCT etc...

    Or am I simply underestimating the power and potential of the BasicEffect I know it can handle multi-sub object materials, but it would be nice to translate even the basic falloff material into XNA or animated texture surfaces.

    Thanks for your help though Shawn, any help is greatly appreciated... I'll get there with this darned language, even if it kills me.



  • Ramanujam Sampath

     vidalsasoon wrote:
    code please

    Here is what I had as far as I can remember... I tend not to leave code that doesn't work in a project, it becomes a distraction...

    in my Draw function...

    graphics.GraphicsDevice.RenderState.FogEnable = true;
    graphics.GraphicsDevice.RenderState.FogColor =
    new Color(255, 255, 255);
    graphics.GraphicsDevice.RenderState.FogStart = 0.5f;
    graphics.GraphicsDevice.RenderState.FogEnd = 1.0f;
    graphics.GraphicsDevice.RenderState.FogVertexMode =
    FogMode.Linear;

    The FogStart and FogEnd seem like they go from 0.0f to 1.0f... I can't quite fathom out what that range is relative to wihch is kinda confusing.

    The result of this is all my meshes turn white from the front of the scene to as far back as I can see them.



  • How do you implement Fog into a scene?