# How to add fog in a 2D ortho scene?

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

## Recommended Posts

I used to make fog in a 3D scene with glEnable(GL_FOG).

Now, I'm working on a 2D scene. I have no idea if this would work. If not, are there some alternatives? I'd like fog turbulence in a 2D game like the screenshot below.

glOrtho(0.0, SCREEN_WIDTH, 0.0, SCREEN_HEIGHT, 0.0, 1.0);


    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GLfloat fLowLight[4] = { 0.7f, 0.7f, 0.7f, 0.1f };

glEnable(GL_FOG); // Turn Fog on
glFogfv(GL_FOG_COLOR, fLowLight); // Set fog color to match background
glFogf(GL_FOG_START, 0.0f); // How far away does the fog start
glFogf(GL_FOG_END, 0.0f); // How far away does the fog stop
glFogi(GL_FOG_MODE, GL_LINEAR); // Which fog equation do I use?



I'm just getting a gray screen, and my texture objects aren't showing up. Changing GL_FOG_END to 1.0f does show my textures, but there is no fog present.

The style I'm trying to go for is attached. Overall, I'm wondering how I can do something similar in a 2D environment possibly with GL_FOG.

##### Share on other sites

Looks like I got it, more or less. I used glFogCoordf().

##### Share on other sites

Adding this just so that you understand what happened here.

Fixed pipeline fog depends on the distance of the objects being drawn from the viewpoint, so in a typical 2D scene - with all objects being drawn at the same distance - it's natural to expect all objects to recieve the same amount of fogging.

In your first case (mode is linear, end is 0, start is 0) the formula is (end - dist) / (end - start).  Plugging in your numbers we see that you actually have a division by zero error here.  We can assume that your driver or hardware catches this and does some form of substitution rather than crashing and burning horribly.  Either way there's no expected behaviour, so we can't predict if you'll get fully fogged, no fog, partial fog, or a grand-unified theory of everything as a result.

In your second case you've set fog end to 1, but your objects are still all being drawn at the same constant distance - I'm assuming that's with a z of 0, which is specified for a 2-component position attribute.  So it's natural to expect no fogging, since the objects are right at your fog start position.

Using glFogCoord means that you get to specify the position used by each vertex for it's fog calculation rather than have it automatically come from the vertex position itself, and now things happen and you start to get results.

1. 1
2. 2
3. 3
Rutin
22
4. 4
frob
18
5. 5

• 33
• 13
• 10
• 10
• 12
• ### Forum Statistics

• Total Topics
632569
• Total Posts
3007121

×
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.20854467153549194, "perplexity": 2481.9634151645414}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2018-39/segments/1537267156224.9/warc/CC-MAIN-20180919122227-20180919142227-00178.warc.gz"}