<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>
<chapter name="Appendix C: The JXS File Format">

<previous name="jitterchapter99_appendixb">The OpenGL Matrix Format</previous>
<next name="jitterchapter99_appendixd">Building Standalone Applications with Jitter</next>
<parent name="jitindex">Jitter Tutorials</parent>



<h1>Appendix C: The JXS File Format</h1>
<h2>The Jitter Shader Description File</h2>
<p>Jitter provides a means of encapsulating shader parameters and programs through an XML based shader description file (<i>JXS</i>). This <i>JXS</i> file offers an easy way of setting up any required OpenGL state to achieve a particular effect, without any additional patcher related work.</p>
<p>The <i>JXS</i> file may contain a list of parameters and textures, followed by a set of language dependent implementations for the shader's vertex and fragment programs.</p>
<p>Each language implementation must specify both a vertex and a fragment program. These programs may either reference external files, or be stored inline within a CDATA section or an XML comment. Programs for each language implementation will be loaded and bound in the order that they are listed in the <i>JXS</i> file. If a particular language implementation fails to compile, the next implementation will be used, until a successful candidate is found.</p>
<p>Inside of the language implementation declaration, any shader parameter previously specified must be identified with a bind tag that indicates which shader program should receive the parameter's values.</p>
<p>The following listing outlines the format of a typical <i>JXS</i> file:</p>
<code language="markup">&lt;jittershader name="myshader"&gt;
  &lt;!-- optional description --&gt;
  &lt;description&gt;This is my shader&lt;/description&gt;

  &lt;!-- optional list of texture objects to bind --&gt;
  &lt;texture file="mytexture.jpg"/&gt;

  &lt;!-- optional list of shader parameters --&gt;
  &lt;param name="myparam" type="vec3" default="3.0 4.0 5.0"&gt;
    &lt;description&gt;This is my parameter&lt;/description&gt;
  &lt;/param&gt;

  &lt;!-- list of language implementations --&gt;
  &lt;language type="glsl" version="1.0"&gt;

    &lt;!-- list of binding targets for shader parameters --&gt;
    &lt;bind param="myparam" program="vp"/&gt;

    &lt;!-- vertex and fragment programs --&gt;
    &lt;program name="vp" type="vertex" source="sh.passthru.xform.vp.glsl"/&gt;
    &lt;program name="fp" type="fragment"&gt;
      &lt;![CDATA[

        // entry point
        void main()
        {
          gl_FragColor=vec4(0.5, 0.5, 0.5, 1.0);
        }
      ]]&gt;
    &lt;/program&gt;
  &lt;/language&gt;
&lt;/jittershader&gt;</code>
<p>Note that the XML document must be well-formed with matching opening/closing tags.</p>
<h2>JXS Shader State Variables</h2>
<p>Jitter provides several built in uniform variables that expose OpenGL state to <i>JXS</i> shader programs. These can be accessed by specifying the variable name in the state attribute of the XML param tag</p> 
<p>For exmaple:</p>
<code language="markup">&lt;param name="itvmat" type="mat3" state="NORMAL_MATRIX" /&gt;</code>
<p>Available shader parameter state bindings are listed below.</p>
<h3>Model View and Projection Matrices</h3>
<ul>
  <li><b>WORLD_MATRIX</b> (mat4)</li>
  <li><b>VIEW_MATRIX</b> (mat4)</li>
  <li><b>MODELVIEW_MATRIX</b> (mat4)</li>
  <li><b>PROJECTION_MATRIX</b> (mat4)</li>
  <li><b>VIEW_PROJECTION_MATRIX</b> (mat4)</li>
  <li><b>MODELVIEW_PROJECTION_MATRIX</b> (mat4)</li>
  <li><b>PREV_MODELVIEW_PROJECTION_MATRIX</b> (mat4)</li>
  <li><b>NORMAL_MATRIX</b> (mat3)</li>
</ul>
<h3>Camera</h3>
<ul>
  <li><b>CAMERA_POSITION</b> (vec3)</li>
  <li><b>CAMERA_DIRECTION</b> (vec3)</li>
  <li><b>VIEWPORT</b> (vec2)</li>
  <li><b>INVERSE_VIEWPORT</b> (vec2)</li>
  <li><b>NEAR_CLIP</b> (float)</li>
  <li><b>FAR_CLIP</b> (float)</li>
  <li><b>FAR_CORNER</b> (vec3)</li>  
</ul>
<h3>Light</h3>
<ul>
  <li><b>LIGHT_VIEWPROJ_MATRIX<i>0-7</i></b> (mat4)</li>
  <li><b>LIGHT_RANGE<i>0-7</i></b> (float)</li>
  <li><b>LIGHT<i>0-7</i>_POSITION</b> (vec3)</li>
  <li><b>LIGHT<i>0-7</i>_DIRECTION</b> (vec3)</li>
  <li><b>LIGHT<i>0-7</i>_AMBIENT</b> (vec4)</li>
  <li><b>LIGHT<i>0-7</i>_DIFFUSE</b> (vec4)</li>
  <li><b>LIGHT<i>0-7</i>_SPECULAR</b> (vec4)</li>
  <li><b>LIGHT<i>0-7</i>_CUTOFF</b> (float)</li>
  <li><b>LIGHT<i>0-7</i>_EXPONENT</b> (float)</li>
</ul>
<h3>Material</h3>
<ul>
  <li><b>AMBIENT</b> (vec4)</li>
  <li><b>DIFFUSE</b> (vec4)</li>
  <li><b>SPECULAR</b> (vec4)</li>
  <li><b>EMISSION</b> (vec4)</li>
</ul>
<h3>Texture</h3>
<ul>
  <li><b>TEXTURE<i>0-7</i></b>_MATRIX (mat4)</li>
  <li><b>TEXDIM<i>0-7</i></b> (vec2)</li>
</ul>
<h2>Matrix Transformations</h2>
<p>Matrix variables (<i>mat3</i> and <i>mat4</i> type variables) can be transformed in specified ways using the <b>transform</b> attribute of the XML <i>param</i> tag:</p>
<code language="markup">&lt;param name="itvmat" type="mat4" state="VIEW_MATRIX" transform="INVERSE_TRANSPOSE" /&gt;</code>
<p>Available matrix transformations include:</p>
<ul>
  <li><b>IDENTITY</b></li>
  <li><b>TRANSPOSE</b></li>
  <li><b>INVERSE</b></li>
  <li><b>INVERSE_TRANSPOSE</b></li>
</ul>
<h2>Vertex Attributes</h2>
<p>Jitter <i>JXS</i> shader programs are able to access custom vertex attributes via the <b>VERTEX_ATTR</b> state tag:</p>
<code language="markup">&lt;param name="pvel" type="vec4" state="VERTEX_ATTR" /&gt;</code>
<p>and in the vertex program:</p>
<code language="markup">attribute vec4 pvel;</code>
<p>To set the values of custom vertex attributes from the patch, send <o>jit.gl.mesh</o> the <m>vertex_attr_matrix</m> message followed by the name of a <o>jit.matrix</o> containing the attribute values. The example patch <openfilelink filename="custom.vertex.attribute.maxpat">custom.vertex.attribute</openfilelink> demonstrates this.</p>
<p>Additionally there are several built in vertex attributes available via the following state tags:</p>
<ul>
  <li><b>POSITION</b> (vec3)</li>
  <li><b>NORMAL</b> (vec3)</li>
  <li><b>TANGENT</b> (vec3)</li>
  <li><b>BITANGENT</b> (vec3)</li>
</ul>
</chapter>
