<?xml version="1.0" encoding="utf-8" ?>
<shader compiler="xmlshader" name="std_lighting_detail_add">
  <?if !vars."tex lightmap".texture ?>
    <technique priority="250">
      <tag>detail texture</tag>
      <pass>
	<buffer source="texture coordinate 0" destination="texture coordinate detail" />
	<buffer source="texture coordinate 0" destination="texture coordinate diffuse" />
	<buffer source="color" destination="color" />
	<texture name="tex detail" destination="detail" />
	<texture name="tex diffuse" destination="diffuse" />
	<texture name="tex diffuse" destination="lighting" />
	<vp plugin="glfixed">
	  <fixedvp>
	    <texmatrix layer="detail">
	      <scale type="shadervar">tex detail scale</scale>
	    </texmatrix>
	  </fixedvp>
	</vp>
	<fp plugin="glfixed">
	  <fixedfp>
	    <layer name="detail">
	      <colorsource num="0" source="texture" modifier="color" />
	      <coloroperation operation="replace" />
	    </layer>
	    <layer name="diffuse">
	      <colorsource num="0" source="texture" modifier="color" />
	      <colorsource num="1" source="previous layer" modifier="color" />
	      <coloroperation operation="add" />
	      <alphasource num="0" source="texture" modifier="alpha" />
	      <alphaoperation operation="replace" />
	    </layer>
	    <layer name="lighting">
	      <colorsource num="0" source="primary color" modifier="color" />
	      <colorsource num="1" source="previous layer" modifier="color" />
	      <coloroperation operation="modulate" />
	      <alphasource num="0" source="previous layer" modifier="alpha" />
	      <alphaoperation operation="replace" />
	    </layer>
	  </fixedfp>
	</fp>
      </pass>
    <?Include snippets/fog-pass.inc ?>
    </technique>
  <?endif?>
  
  <technique priority="200">
    <tag>detail texture</tag>
    <tag>multipass detail texture</tag>
    <pass>
      <buffer source="texture coordinate 0" destination="texture coordinate detail" />
      <buffer source="texture coordinate 0" destination="texture coordinate diffuse" />
      <texture name="tex detail" destination="detail" />
      <texture name="tex diffuse" destination="diffuse" />
      <vp plugin="glfixed">
        <fixedvp>
          <texmatrix layer="detail">
            <scale type="shadervar">tex detail scale</scale>
          </texmatrix>
        </fixedvp>
      </vp>
      <fp plugin="glfixed">
        <fixedfp>
          <layer name="detail">
            <colorsource num="0" source="texture" modifier="color" />
            <coloroperation operation="replace" />
          </layer>
          <layer name="diffuse">
            <colorsource num="0" source="texture" modifier="color" />
            <colorsource num="1" source="previous layer" modifier="color" />
            <coloroperation operation="add" />
            <alphasource num="0" source="texture" modifier="alpha" />
            <alphaoperation operation="replace" />
          </layer>
        </fixedfp>
      </fp>
    </pass>
    <pass>
      <?if vars."tex lightmap".texture ?>
	<mixmode><multiply2 /></mixmode>
      <?else?>
	<mixmode><multiply /></mixmode>
      <?endif?>
      <zmode><zmesh2 /></zmode>
      <?if !vars."tex lightmap".texture ?>
	<texture name="tex diffuse" destination="lighting" />
	<buffer source="color" destination="color" />
      <?else?>
	<texture name="tex lightmap" destination="lighting" />
	<buffer source="texture coordinate lightmap" destination="texture coordinate lighting" />
      <?endif?>
      <vp plugin="glfixed">
        <fixedvp>
	  <?if vars."tex lightmap".texture ?>
	    <constantcolor layer="lighting" type="expression">
	      <sexp>(* "dynamic ambient" 0.5)</sexp>
	    </constantcolor>
	  <?endif?>
        </fixedvp>
      </vp>
      <fp plugin="glfixed">
        <fixedfp>
	  <layer name="lighting">
	    <?if vars."tex lightmap".texture ?>
	      <colorsource num="0" source="texture" modifier="color" />
	      <colorsource num="1" source="constant color" modifier="color" />
	      <coloroperation operation="add"/>
	      <alphasource num="0" source="texture" modifier="alpha" />
	      <alphaoperation operation="replace" />
	    <?else?>
	      <colorsource num="0" source="primary color" modifier="color" />
	      <coloroperation operation="replace" />
	      <alphasource num="0" source="previous layer" modifier="alpha" />
	      <alphaoperation operation="replace" />
	    <?endif?>
	  </layer>
        </fixedfp>
      </fp>
    </pass>
    <?Include snippets/fog-pass.inc ?>
  </technique>

  <technique priority="150">
    <tag>detail texture</tag>
    <tag>multipass detail texture</tag>
    <pass>
      <buffer source="texture coordinate 0" destination="texture coordinate 0" />
      <texture name="tex diffuse" destination="unit 0" />
    </pass>
    <pass>
      <mixmode><add /></mixmode>
      <zmode><zmesh2 /></zmode>
      <buffer source="texture coordinate 0" destination="texture coordinate 0" />
      <texture name="tex detail" destination="unit 0" />
      <vp plugin="glfixed">
        <fixedvp>
          <texmatrix layer="0">
            <scale type="shadervar">tex detail scale</scale>
          </texmatrix>
        </fixedvp>
      </vp>
    </pass>
    <pass>
      <?if !vars."tex lightmap".texture ?>
	<mixmode><multiply2 /></mixmode>
      <?else?>
	<mixmode><multiply /></mixmode>
      <?endif?>
      <zmode><zmesh2 /></zmode>
      <?if !vars."tex lightmap".texture ?>
	<!-- need to bind at least some texture -->
	<texture name="standardtex white" destination="unit 0" />
      <?else?>
	<texture name="tex lightmap" destination="unit 0" />
	<buffer source="texture coordinate lightmap" destination="texture coordinate 0" />
      <?endif?>
      <vp plugin="glfixed">
        <fixedvp>
	  <?if vars."tex lightmap".texture ?>
	    <constantcolor layer="lighting" type="expression">
	      <sexp>(* "dynamic ambient" 0.5)</sexp>
	    </constantcolor>
	  <?endif?>
        </fixedvp>
      </vp>
      <fp plugin="glfixed">
        <fixedfp>
          <layer>
	    <?if !vars."tex lightmap".texture ?>
	      <colorsource num="0" source="primary color" modifier="color" />
	      <coloroperation operation="replace" />
	    <?else?>
	      <colorsource num="0" source="texture" modifier="color" />
	      <colorsource num="1" source="constant color" modifier="color" />
	      <coloroperation operation="add" />
	    <?endif?>
          </layer>
        </fixedfp>
      </fp>
    </pass>
    <?Include snippets/fog-pass.inc ?>
  </technique>

  <fallbackshader name="std_lighting" />
</shader>
