[SOLVED]I want something divine like

Edit the frag shader that you THINK you are using just to set every pixel to red at the end. gl_FragColor = vec4(1, 0, 0, 1)

…see if the whole screen is red or not. That will tell you if you are running the code you think you are.

1 Like

About the exception, I read this int J3MLoader.

// <TYPE> <LANG> : <SOURCE>
private void readShaderStatement(String statement) throws IOException {
    String[] split = statement.split(":");
    if (split.length != 2) {
        throw new IOException("Shader statement syntax incorrect" + statement);
    }
    String[] typeAndLang = split[0].split(whitespacePattern);
    if (typeAndLang.length != 2) {
        throw new IOException("Shader statement syntax incorrect: " + statement);
    }

    for (Shader.ShaderType shaderType : Shader.ShaderType.values()) {
        if (typeAndLang[0].equals(shaderType.toString() + "Shader")) {
            readShaderDefinition(shaderType, split[1].trim(), typeAndLang[1]);
        }
    }
}

If accept <TYPE> <LANG> : <SOURCE>, but got <TYPE> <LANG> <LANG> : <SOURCE> when parsing VertexShader GLSL100 GLSL150 : Commom/MatDefs/Light/SPLighting.vert.

1 Like

ok, give me a minute…

1 Like

I changed last statement of Materials/Fog/Lighting.frag.

void main(){
    // ...
    #ifdef USE_FOG
        //gl_FragColor = mix(m_FogColor, gl_FragColor, fogFactor);
        gl_FragColor = vec4(1, 0, 0, 1);
    #endif
}

It looks like this now.

1 Like

I thought you said it wasn’t working. I may have misread the thread and been trying to fix something that wasn’t broken.

1 Like

Note: you will be happier basing your fog on distance from the camera instead of distance from the screen surface… at least unless you like having the fog change as your turn your head. I always found that disconcerting.

1 Like

Thank you, sir. :smile:

Yes, that is the problem I am worrying about now. When I turn my “head” up and down, the floor color is so different… I don’t understand why.

Edit: What’s more, I don’t know how to implements the FogMaxDistanc, FogMinDistance and so on. I’m reading this article: oZone3D.Net Tutorials - Fog in GLSL - gl_FogFragCoord - gl_FragCoord - gl_FogCoord - gl_Fog - OpenGL Shading Language

1 Like

I’m really happier.

I change this

#ifdef USE_FOG
    vec4 viewSpacePos = g_WorldViewMatrix * modelPosition;
    fogFactor = exp2(-m_FogDensity * m_FogDensity * viewSpacePos.z *  viewSpacePos.z * LOG2 );
    fogFactor = clamp(fogFactor, 0.0, 1.0);
#endif

to this

#ifdef USE_FOG
    vec4 worldSpacePos = g_WorldMatrix * modelPosition;
    float dist = length(worldSpacePos.xyz-g_CameraPosition.xyz);
    fogFactor = exp2(-m_FogDensity * m_FogDensity * dist *  dist * LOG2 );
    fogFactor = clamp(fogFactor, 0.0, 1.0);
#endif

The fog don’t change when I turning my head now.

1 Like

After reading some book, I know there is a better way to calculate distance.

Here is the scene when distance calculated in this way:

float dist = length(worldSpacePos.xyz-g_CameraPosition.xyz);

I removed the VertexShader (Materials/Fog/Fog.vert), and calculate dist and fogFactor in FragmentShader (Materials/Fog/Fog.frag).

float dist = gl_FragCoord.z / gl_FragCoord.w

#import "Common/ShaderLib/GLSLCompat.glsllib"

uniform vec4 m_Color;
uniform vec4 m_FogColor;
uniform float m_FogDensity;

const float LOG2 = 1.442695;

void main() {

    gl_FragColor = m_Color;

#ifdef USE_FOG
    float dist = gl_FragCoord.z / gl_FragCoord.w;
    float fogFactor = exp2(-m_FogDensity * m_FogDensity * dist *  dist * LOG2 );
    gl_FragColor = mix(m_FogColor, gl_FragColor, fogFactor);
#endif

}

Now I feel more happier.

In the Lighting.j3md, I also changed the Lighting.frag, and reuse Common/MatDefs/Light/Lighting.vert.

    VertexShader GLSL100 :   Common/MatDefs/Light/Lighting.vert
    FragmentShader GLSL100 : Materials/Fog/Lighting.frag

Thus how I get what I want, thank you @nehon @pspeed @FrozenShade .

2 Likes

I think this bases the fog on distance from the screen, yes? In other words, you can see a little farther towards the edges of the screen.

This means that if you want to see that box in the far center a little better then you can just turn your head. That seems wrong to me so I don’t do it that way and opt for the “distance from camera in .vert” approach.

But then for a hundred other reasons, I would never have a giant two triangle quad floor like you do. There are many good reasons to subdivide such a surface. That also makes vertex-based fog a fine option.

1 Like

Agreed.

Just one thing. In my understanding, float dist = gl_FragCoord.z / gl_FragCoord.w is the distance from the camera, not the screen.

https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/gl_FragCoord.xhtml

First I look at the farest place in the scene.

Then I move the camera, the color of that place did not change.

1 Like

From that link:
" window-relative coordinates "

ie: fog depth will be parallel to the screen instead of slightly circular as would be for camera distance. Meaning the points to the left and the right of the screen are actually farther away from the camera than they are from the “screen”.

In your screen shots the effect is not very dramatic I guess so it’s hard to see… but if you look at the box behind the green box, you can see that it is less foggy in the second picture even though it is the same distance from you.

1 Like

Yes, sir. You are right.

I did a more complicated experiment today. It proves what you told me is right.

I wrote another shader using both vertex-based and frag-based fog. Then I made a test app with it.

Here is the code:

Materials/Fog/VertexBasedFog.j3md
Materials/Fog/VertexBasedFog.vert
Materials/Fog/VertexBasedFog.frag

TestVertexBasedFog.java

The result shows that I can see a little farther near the screen edge, when using frag-based fog.

The picture blow show difference of frag-based / linear-factor fog. I can see nothing after the 6th green box when it’s near screen center, will I can see something between the 6th and 7th green box when they are near screen edge.

So I disabled FragShader, and this is the result. No matter how I rotate the camera, I can see nothing after the 6th green box.


I also tried different algorithms to calculate distance. Not to prove anything, just for fun. :smile:

frag-based fog

linear distance

exp distance

exp2 distance

vert-based fog

linear distance

exp distance

exp2 distance

4 Likes