Simple GLSL fog,. also good for simple darkness lighting,.

Share your ZGE-development tips and techniques here!

Moderator: Moderators

Post Reply
User avatar
jph_wacheski
Posts: 1005
Joined: Sat Feb 16, 2008 8:10 pm
Location: Canada
Contact:

Simple GLSL fog,. also good for simple darkness lighting,.

Post by jph_wacheski » Wed Apr 15, 2009 7:32 pm

ok, i was looking for this befor but have not really needed it,. came accross some sample on the net and i just got it working in ZGE, so here is a basic implementation for Shader Fog,. this one works on the Z distance and I like how it can easily make simple darkness as objects seem to fade into the night,. . enjoy!
Attachments
GLSL_fog_simpleZ.zgeproj
the fogness
(2.02 KiB) Downloaded 512 times
iterationGAMES.com

User avatar
y offs et
Posts: 418
Joined: Wed Apr 22, 2009 4:26 pm
Location: BC, Canada

Post by y offs et » Thu May 14, 2009 5:56 am

Cool! Change vec4 to (0.5,0.5,0.5,1) and app clear color to (127,127,127) and FOG!
"great expectations"

User avatar
Ats
Posts: 257
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: Simple GLSL fog,. also good for simple darkness lighting,.

Post by Ats » Thu Jun 28, 2018 11:53 am

Hi. I'm currently adding this fog effect to my game, but I have a problem:
I don't use textures on my models, meshes are directly colored using MeshExpression. I'm a complete noob when it comes to OpenGL and I'm trying to replace :

Code: Select all

gl_TexCoord[0] = gl_MultiTexCoord0;
 ...
uniform sampler2D TheTexture;
 ...
vec4 color= texture2D(TheTexture, gl_TexCoord[0].st);
by something that retrieves the Vertex color instead of the texture color. Is that possible?

Edit:
I get something in the right direction with

Code: Select all

gl_TexCoord[0] = gl_Color;
...
vec4 color= gl_TexCoord[0];
I'm not sure that's how to do it correctly. But now I can remove the texture that overwrites my mesh Vertex colors.
Last edited by Ats on Thu Jun 28, 2018 3:53 pm, edited 1 time in total.

User avatar
Kjell
Posts: 1715
Joined: Sat Feb 23, 2008 11:15 pm

Re: Simple GLSL fog,. also good for simple darkness lighting,.

Post by Kjell » Thu Jun 28, 2018 1:36 pm

Hi Ats,

Quick question .. do you use / need shaders in your project? If not, you can enable fog using OpenGL calls without having to resort to shaders .. check out the following example.

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" ClearColor="0 0.502 1 1" CameraPosition="0 0 26.1314" FileVersion="2">
  <OnLoaded>
    <ZExternalLibrary ModuleName="opengl32">
      <Source>
<![CDATA[//

const int GL_FOG = 0x0B60;
const int GL_FOG_START = 0x0B63;
const int GL_FOG_END = 0x0B64;
const int GL_FOG_MODE = 0x0B65;
const int GL_FOG_COLOR = 0x0B66;

const int GL_LINEAR = 0x2601;

void glDisable(int cap){}
void glEnable(int cap){}
void glFogf(int pname, float param){}
void glFogfv(int pname, xptr params){}
void glFogi(int pname, int param){}]]>
      </Source>
    </ZExternalLibrary>
    <ZExpression>
      <Expression>
<![CDATA[//

glFogf(GL_FOG_START, 1);
glFogf(GL_FOG_END, 100);
glFogi(GL_FOG_MODE, GL_LINEAR);
glFogfv(GL_FOG_COLOR, App.ClearColor);
glEnable(GL_FOG);

//

for(int i=0; i<32; i++)
{
  createModel(Box);
}]]>
      </Expression>
    </ZExpression>
  </OnLoaded>
  <OnUpdate>
    <ZExpression>
      <Expression>
<![CDATA[//

App.CameraPosition.Z = sin(App.Time)*32;]]>
      </Expression>
    </ZExpression>
  </OnUpdate>
  <Content>
    <Model Name="Box">
      <OnSpawn>
        <ZExpression>
          <Expression>
<![CDATA[//

Box.Position.X = random(0, 8);
Box.Position.Y = random(0, 8);
Box.Position.Z = random(0, 64);

Box.Rotation.X = rnd();
Box.Rotation.Y = rnd();

Box.RotationVelocity.X = random(0, 0.125);
Box.RotationVelocity.Y = random(0, 0.125);]]>
          </Expression>
        </ZExpression>
      </OnSpawn>
      <OnRender>
        <RenderMesh Mesh="BoxMesh"/>
      </OnRender>
    </Model>
    <Mesh Name="BoxMesh">
      <Producers>
        <MeshBox/>
      </Producers>
    </Mesh>
  </Content>
</ZApplication>
K

User avatar
Ats
Posts: 257
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: Simple GLSL fog,. also good for simple darkness lighting,.

Post by Ats » Thu Jun 28, 2018 3:32 pm

Thanks Kjell. Your fog is simple and perfect for the ships, but it hides my starfield in the background as it is applied to everything.

User avatar
Kjell
Posts: 1715
Joined: Sat Feb 23, 2008 11:15 pm

Re: Simple GLSL fog,. also good for simple darkness lighting,.

Post by Kjell » Thu Jun 28, 2018 4:22 pm

Hi Ats,
Ats wrote:Your fog is simple and perfect for the ships, but it hides my starfield in the background as it is applied to everything.
You can simply disable the fog before rendering your starfield using "glDisable(GL_FOG)" and then enable it again afterwards.

K

User avatar
Ats
Posts: 257
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: Simple GLSL fog,. also good for simple darkness lighting,.

Post by Ats » Thu Jun 28, 2018 5:24 pm

Oh cool! Once again, many thanks.
Only a few little questions left and I'll be ready to release my game ! :D

User avatar
Ats
Posts: 257
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: Simple GLSL fog,. also good for simple darkness lighting,.

Post by Ats » Thu Jun 28, 2018 8:07 pm

Too bad: GL_FOG prevents the game from launching on Android...
I tried to change the OpenGL Constent values following https://developer.android.com/reference ... L10#GL_FOG

Code: Select all

const int GL_FOG = ANDROID ? 0x00000b60 : 0x0B60;
const int GL_FOG_START = ANDROID ? 0x00000b63 : 0x0B63;
const int GL_FOG_END = ANDROID ? 0x00000b64 : 0x0B64;
const int GL_FOG_MODE = ANDROID ? 0x00000b65 : 0x0B65;
const int GL_FOG_COLOR = ANDROID ? 0x00000b66 : 0x0B66;
const int GL_FOG_DENSITY = ANDROID ? 0x00000b62 : 0x0B62;
const int GL_LINEAR = ANDROID ? 0x00002601 : 0x2601;
const int GL_EXP = ANDROID ? 0x00000800 : 0x0800;
Without luck... Is this supposed to work on Android?

User avatar
Kjell
Posts: 1715
Joined: Sat Feb 23, 2008 11:15 pm

Re: Simple GLSL fog,. also good for simple darkness lighting,.

Post by Kjell » Thu Jun 28, 2018 8:23 pm

Hi Ats,

On Android you need to use glFogx instead of glFogi :wink:

K

User avatar
Ats
Posts: 257
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: Simple GLSL fog,. also good for simple darkness lighting,.

Post by Ats » Thu Jun 28, 2018 9:10 pm

Sorry, but I can't get it to work on Android. I replaced glFogi by glFogx: it's still working on PC, but it's also still crashing on Android...
So I shortened down everything to see what was crashing the game and it only comes to this:

Code: Select all

const int GL_FOG = ANDROID ? 0x00000b60 : 0x0B60; // I also tried 2912 and 0x0B60
void glEnable(int cap){}

...

glEnable(GL_FOG); // -> crash
If that can help, I have a Samsung Galaxy S6 :?
Edit: It also crashes on Shield TV.

User avatar
Kjell
Posts: 1715
Joined: Sat Feb 23, 2008 11:15 pm

Re: Simple GLSL fog,. also good for simple darkness lighting,.

Post by Kjell » Fri Jun 29, 2018 9:42 am

Hi Ats,

Not exactly sure what's wrong .. i suspect you're not loading the OpenGL ES library correctly, but who knows.

Anyway, can you try the following example? I tested it on a Android device and it works properly.

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" FileVersion="2">
  <OnLoaded>
    <ZExternalLibrary ModuleName="opengl32">
      <BeforeInitExp>
<![CDATA[//

if(ANDROID)
{
  this.ModuleName = "libGLESv1_CM.so";
}
else
{
  this.ModuleName = "opengl32";
}]]>
      </BeforeInitExp>
      <Source>
<![CDATA[//

const int GL_LINEAR = 0x2601;

const int GL_FOG = 0x0B60;
const int GL_FOG_START = 0x0B63;
const int GL_FOG_END = 0x0B64;
const int GL_FOG_MODE = 0x0B65;

void glEnable(int cap){}
void glFogf(int pname, float param){}
void glFogi(int pname, int param){}
void glFogx(int pname, int param){}]]>
      </Source>
    </ZExternalLibrary>
    <ZExpression>
      <Expression>
<![CDATA[//

for(int i=0; i<32; i++)
{
  float a = i/2f;
  float b = i/3f;

  Box.Position.X = sin(a)*4;
  Box.Position.Y = cos(a)*4;
  Box.Position.Z = i;

  Box.Rotation.X = b;
  Box.Rotation.Z = b;

  createModel(Box);
}]]>
      </Expression>
    </ZExpression>
  </OnLoaded>
  <OnUpdate>
    <ZExpression>
      <Expression>
<![CDATA[// Somehow this needs to be executed every frame ..
// for fog to not revert to the default values on Android :-?

if(ANDROID)
{
  glFogx(GL_FOG_MODE, GL_LINEAR);
}
else
{
  glFogi(GL_FOG_MODE, GL_LINEAR);
}

glFogf(GL_FOG_START, 0);
glFogf(GL_FOG_END, 32);
glEnable(GL_FOG);]]>
      </Expression>
    </ZExpression>
    <ZExpression>
      <Expression>
<![CDATA[//

App.CameraPosition.Z = sin(App.Time*2)*16+32;]]>
      </Expression>
    </ZExpression>
  </OnUpdate>
  <Content>
    <Model Name="Box">
      <OnRender>
        <RenderMesh Mesh="BoxMesh"/>
      </OnRender>
    </Model>
    <Mesh Name="BoxMesh">
      <Producers>
        <MeshBox/>
      </Producers>
    </Mesh>
  </Content>
</ZApplication>
However, something strange i noticed is that i need to set the fog parameters every frame or it'll revert back to the default values. No idea why this happens and not sure if this occurs on other Android devices either :|

K

User avatar
Ats
Posts: 257
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: Simple GLSL fog,. also good for simple darkness lighting,.

Post by Ats » Fri Jun 29, 2018 12:58 pm

Thanks for your test example. The problem was that I didn't have the opengl32 BeforeInitExp initialized.
Now everything is working fine. And yes, I have to set the fog in OnUpdate on all of my Android devices for it to work correctly. That's weird. I wouldn't have find that one...

Post Reply