Trouble with ARB Vertex+Fragment Program and JME fogstate

I've added a wind vertex shader to my game for the grass and trees:



!!ARBvp1.0
PARAM mv[4] = { state.matrix.modelview };
PARAM mv2[4] = { state.matrix.projection };
TEMP pos,projpos;
TEMP a;
################################
# Apply modelview transformation
DP4 pos.x, mv[0], vertex.position;
DP4 pos.y, mv[1], vertex.position;
DP4 pos.z, mv[2], vertex.position;
DP4 pos.w, mv[3], vertex.position;
################################
# Add wind offset proportional to the texture height
MUL a, program.local[0], vertex.texcoord[0].yyyy;
ADD pos.xz, pos, a;
################################
# Apply projection matrix
DP4 projpos.x, mv2[0], pos;
DP4 projpos.y, mv2[1], pos;
DP4 projpos.z, mv2[2], pos;
DP4 projpos.w, mv2[3], pos;
MOV result.position, projpos;
MOV result.texcoord[0], vertex.texcoord[0];
MOV result.color, vertex.color;
MOV result.fogcoord, vertex.fogcoord;

END



This works fine, I've a sinusoid local parameter, grass and tree foliage quad moves cool..
I've also added fragment program to enable OPTION ARB_fog_linear too, because adding a vertex program makes the fogstate switched off by default:


!!ARBfp1.0
OPTION ARB_fog_linear;
TEMP finalColor;
TEMP diffuse;
TEX diffuse, fragment.texcoord[0], texture[0], 2D;
MUL finalColor, fragment.color, diffuse;
MUL result.color, finalColor, fragment.color.primary;



The problem is that I've set fog state on the mesh but with all these vertex/fragment program state put on the mesh it doesn't do anything at all.

AFAIK the fog calculated by the hardware will be calculated based on the FogState parameters set to that mesh...but it doesn't seem to work!

Without the VP / FP it works....

Anyone with advice, experience? Is this some JME/GL bug, the fog state values are not correctly set for use with the shaders? Any response welcome and appreciated! :)

No expert here, but I think if you use a fragment shader Fog state (and some other fixed function pipeline stuff) just won't work. That's not a jME limitation but due to the way graphics hardware works.

"    (27) How can conventional GL fog application be achieved within a

    fragment program?



      RESOLVED: Program options have been introduced that allow a

      program to request fog to be applied to the final clamped fragment

      color before being passed along to the antialiasing application

      stage.  This makes it easy for:

        1. developers to request conventional fog behavior

        2. implementations with dedicated fog hardware to use it

        3. implementations without dedicated fog hardware, so they need

          not track fog state after compilation, and constantly

          recompile when fog state changes.



      The three mandatory options are ARB_fog_exp, ARB_fog_exp2, and

      ARB_fog_linear.  As these options are mutually exclusive by

      nature, specifying more than one is not useful.  If more than one

      is specified, the last one encountered in the <optionSequence>

      will be the one to actually modify the execution environment.



"



This is by Nvidia.

There is also a state.fog available. Also if I don't set fogstate to the trimesh, all things become gray when the ARB_fog option is included…if I set the FogState in JME, things become normal color, but fog is not seen in the distance…



      fragment.fogcoord          (f,0,0,1)  fog distance/coordinate



This already exists…question is it set up correctly by GL part of LJWGL…



Other useful info:



    Fog Property Bindings

      Binding                      Components  Underlying State
     


 
 
      state.fog.color              (r,g,b,a)   RGB fog color (section 3.11)
      state.fog.params             (d,s,e,r)   fog density, linear start
                                               and end, and 1/(end-start)
                                               (section 3.11)

      Table X.2.5:  Fog Property Bindings

    If a program parameter binding matches "state.fog.color", the "x",
    "y", "z", and "w" components of the program parameter variable are
    filled with the "r", "g", "b", and "a" components, respectively, of
    the fog color (section 3.11).

    If a program parameter binding matches "state.fog.params", the "x",
    "y", and "z" components of the program parameter variable are filled
    with the fog density, linear fog start, and linear fog end
    parameters (section 3.11), respectively.  The "w" component is
    filled with 1/(end-start), where end and start are the linear fog
    end and start parameters, respectively.



I hope someone run into this kinda problem too...

My solution was to write my own fog program :slight_smile:


         if (core.extRootNode.equals(parent.getParent()) || core.extRootNode.equals(parent.getParent().getParent())|| parent.getParent().getParent()!=null && core.extRootNode.equals(parent.getParent().getParent().getParent())) {
            fp.setParameter(new float[]{core.fs_external.getColor().r,core.fs_external.getColor().g,core.fs_external.getColor().b,core.fs_external.getColor().a}, 0);
         } else
         {
            fp.setParameter(new float[]{core.fs_internal.getColor().r,core.fs_internal.getColor().g,core.fs_internal.getColor().b,core.fs_internal.getColor().a}, 0);
         }
         float dist = this.getWorldTranslation().add(avarageTranslation).distance(core.getCamera().getLocation());
         fp.setParameter(new float[]{1.2f-dist/(J3DCore.VIEW_DISTANCE*1.14f),0,0,0}, 1);



Fragment shader:


!!ARBfp1.0
TEMP foggedColor;
TEMP finalColor;
TEMP diffuse;
TEX diffuse, fragment.texcoord[0], texture[0], 2D;
MUL diffuse.xyz, diffuse, 1;
MUL finalColor, fragment.color, diffuse;
PARAM fogColor = program.local[0];
PARAM fogFactor = program.local[1];
LRP foggedColor, fogFactor.x, finalColor, fogColor;
MUL foggedColor.xyz,foggedColor,1;
MOV result.color, foggedColor;
END