<shader compiler="xmlshader" name="terrain_fixed_base">
  <technique priority="300">
    <tag>inverted_fog</tag>
    <tag>linear_fog</tag>
    <tag>terrain_splatting</tag>
    <pass>
      <mixmode><copy /></mixmode>
      <buffer source="texture coordinate 0" destination="texture coordinate 0" />
      <buffer source="color" destination="color" />
      <vp plugin="glfixed">
	<fixedvp>
	  <constantcolor layer="0" type="vector4">0,0,0,1</constantcolor>
	  <?Include snippets/fog-fvp.inc ?>
	</fixedvp>
      </vp>
      <texture name="tex diffuse" destination="unit 0" />
      <?Include snippets/fog-mappings.inc ?>
      <fp plugin="glfixed">
        <fixedfp>
	  <fog>
	    <mode>linear</mode>
	    <start type="shadervar">texture lod distance</start>
	    <end type="float">0</end>
	    <fogcolor type="vector4">0,0,0,0</fogcolor>	
	  </fog>
	  <layer>
	    <colorsource num="0" source="texture" modifier="color"/>
	    <colorsource num="1" source="primary color" modifier="color"/>
	    <coloroperation operation="modulate" scale="2" />
            <alphasource num="0" source="constant color" modifier="alpha"/>
	    <alphaoperation operation="replace"/>
	  </layer>
	  <?Include snippets/fog-ffp.inc ?>
        </fixedfp>
      </fp>
    </pass>
  </technique>
  <technique priority="250">
    <tag>terrain_splatting</tag>
    <pass>
      <mixmode><copy /></mixmode>
      <buffer source="texture coordinate 0" destination="texture coordinate 0" />
      <buffer source="color" destination="color" />
      <vp plugin="glarb">
	<!--
	  Certain HW does not like the "inverted fog" trick above and does
	  not display any fog at all. In this case, the driver DB should 
	  disable 'inverted_fog' tagged techniques, so the one below is used.
	 -->
	<arbvp>
	  <variablemap variable="texture lod distance" destination="register 0"/>
	  <program><![CDATA[
	  !!ARBvp1.0
	  OPTION ARB_position_invariant;

	  PARAM mvp[4]		= { state.matrix.mvp };
	  PARAM mv[4]	     	= { state.matrix.modelview };
	  PARAM mvi[4]		= { state.matrix.modelview.inverse };
	  PARAM one    		= { 1, 1, 1, 1 };
	  
	  PARAM loddist		= program.local[0];
	  
	  ATTRIB iPos		= vertex.position;
	  ATTRIB iCol		= vertex.color;
	  ATTRIB iTex		= vertex.texcoord[0];
	  
	  OUTPUT oTex		= result.texcoord[0];
	  OUTPUT oCol		= result.color;
	  
	  TEMP fogDense;
	  TEMP eyeVertex;
	  #TEMP outCol;
	  
	  # Note transforming the vertex to viewspace isn't needed -
	  # ARB_position_invariant does this

	  # Transform position to eye
	  DP4 eyeVertex.x, mv[0], iPos;
	  DP4 eyeVertex.y, mv[1], iPos;
	  DP4 eyeVertex.z, mv[2], iPos;
	  DP4 eyeVertex.w, mv[3], iPos;
	  
	  # Calculate what density the "inverted fog" has at this vertex
	  RCP fogDense, loddist.x;
	  MUL fogDense, fogDense, eyeVertex;
	  
	  # Clamp
	  MIN fogDense.z, fogDense.z, one.z;
	  
	  # Multiply that into color.
	  MUL oCol.xyz, iCol, fogDense.z;
	  MOV oCol.w, iCol.w;
	  
	  MOV oTex, iTex;
	  
	  END
	  ]]></program>	  
	</arbvp>
      </vp>
      <texture name="tex diffuse" destination="unit 0" />
      <fp plugin="glfixed">
        <fixedfp>
	  <layer>
	    <colorsource num="0" source="texture" modifier="color"/>
	    <colorsource num="1" source="primary color" modifier="color"/>
	    <coloroperation operation="modulate" scale="2" />
            <alphasource num="0" source="primary color" modifier="alpha"/>
	    <alphaoperation operation="replace"/>
	  </layer>
        </fixedfp>
      </fp>
    </pass>
    <?Include snippets/fog-pass.inc ?>    
  </technique>

  <!-- Some HW doesn't even handle the linear fog correctly at all. 
       Effectively disable splatting there. (controlled through linear_fog
       tag.) -->
  <technique priority="200">
    <pass>
      <mixmode><copy /></mixmode>
      <buffer source="texture coordinate 0" destination="texture coordinate 0" />
      <buffer source="color" destination="color" />
      <vp plugin="glfixed">
	<fixedvp>
	  <constantcolor layer="0" type="vector4">0,0,0,1</constantcolor>
	</fixedvp>
      </vp>
      <texture name="tex diffuse" destination="unit 0" />
      <fp plugin="glfixed">
        <fixedfp>
	  <layer>
	    <colorsource num="0" source="texture" modifier="color"/>
	    <colorsource num="1" source="primary color" modifier="color"/>
	    <coloroperation operation="modulate" scale="2" />
            <alphasource num="0" source="constant color" modifier="alpha"/>
	    <alphaoperation operation="replace"/>
	  </layer>
        </fixedfp>
      </fp>
    </pass>
  </technique>

  <!-- Yes. Software. -->
  <technique priority="75">
    <pass>
      <texture name="tex diffuse" destination="unit 0" />
      <buffer source="texture coordinate 0" destination="texture coordinate 0" />
      <buffer source="color" destination="color" />
      <vp plugin="software" />
      <fp plugin="software">
	<softfp>
	  <flatcolor type="shadervar">mat flatcolor</flatcolor>
	  <colorfactor type="float">2</colorfactor>
	</softfp>
      </fp>
    </pass>
  </technique>

    <technique priority="50">
      <pass>
        <?if !vars."tex diffuse".texture ?>
          <texture name="standardtex white" destination="unit 0" />
        <?else?>
          <texture name="tex diffuse" destination="unit 0" />
          <buffer source="texture coordinate 0" destination="texture coordinate 0"/>
        <?endif?>
      </pass>
      <!-- @@@ Flat materials -->
      <?if vars."tex lightmap".texture ?>
        <pass>
          <mixmode><multiply2 /></mixmode><!-- @@@ probably not always desired -->
          <zmode><zmesh2 /></zmode>
          <buffer source="texture coordinate lightmap" destination="texture coordinate 0" />
          <texture name="tex lightmap" destination="unit 0" />
        </pass>
      <?endif?>
      <?Include snippets/fog-pass.inc ?>
    </technique>
</shader>
