Procedural Texture Material

From KitwarePublic
Revision as of 02:47, 29 November 2006 by Kmorel (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Back to VTKShaders

<?xml version="1.0" encoding="UTF-8"?>

<!-- This is a shader for a simple procedural texture example.  The texture
     is a 3D texture with concentric spheres of alternating colors.  The
     frequency of the sphere alternation is specified in a uniform variable
     of the fragment processor.

 Copyright 2006 Sandia Corporation.
 Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
 license for use of this work by or on behalf of the
 U.S. Government. Redistribution and use in source and binary forms, with
 or without modification, are permitted provided that this Notice and any
 statement of authorship are reproduced on all copies.
-->

<Material name="SimpleProceduralTexture"
          number_of_properties="0"
          number_of_vertex_shaders="1"
          number_of_fragment_shaders="1">

  <Shader scope="Vertex"
          name="ProceduralTextureVert"
          location="Inline"
          language="GLSL"
          entry="main">
    <LightUniform name="NumLights" value="NumberOfLights"/>
/* This is a vertex program for a simple procedural texture example.
 * The texture is a 3D texture with concentric spheres of alternating
 * colors.  The frequency of the sphere alternation is specified in a
 * uniform variable of the fragment processor.
 */

/*
 * Copyright 2006 Sandia Corporation.
 * Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
 * license for use of this work by or on behalf of the
 * U.S. Government. Redistribution and use in source and binary forms, with
 * or without modification, are permitted provided that this Notice and any
 * statement of authorship are reproduced on all copies.
 */

uniform int NumLights;

/* Compute the contribution from a particular light source.  This basically
 * comes straight out of the OpenGL orange book. */
void DirectionalLight(in int lightIndex,
                      in vec3 normal,
                      inout vec4 ambient,
                      inout vec4 diffuse,
                      inout vec4 specular)
{
  /**** Compute ambient term. ****/
  ambient += gl_LightSource[lightIndex].ambient;

  /**** Compute diffuse term. ****/
  /* normal dot light direction.  Assume the light direction vector is already
     normalized.*/
  float nDotL = max(0.0, dot(normal,
                             normalize(vec3(gl_LightSource[lightIndex].position))));
  diffuse += gl_LightSource[lightIndex].diffuse * nDotL;

  /**** Compute specular term. ****/
  /* normal dot halfway vector */
  float nDotH = max(0.0, dot(normal,
                             vec3(gl_LightSource[lightIndex].halfVector)));
  float pf;     /* Power factor. */
  if (nDotH <= 0.0)
    {
    pf = 0.0;
    }
  else
    {
    pf = pow(nDotH, gl_FrontMaterial.shininess);
    }
  specular += gl_LightSource[lightIndex].specular * pf;
}

void AllLights(in vec3 normal,
               inout vec4 ambient,
               inout vec4 diffuse,
               inout vec4 specular)
{
  DirectionalLight(0, normal, ambient, diffuse, specular);
  if (NumLights > 1)
    {
    DirectionalLight(1, normal, ambient, diffuse, specular);
    if (NumLights > 2)
      {
      DirectionalLight(2, normal, ambient, diffuse, specular);
      if (NumLights > 3)
        {
        DirectionalLight(3, normal, ambient, diffuse, specular);
        if (NumLights > 4)
          {
          DirectionalLight(4, normal, ambient, diffuse, specular);
          }
        }
      }
    }
}

void main()
{
  /* Transform the normal. */
  vec3 normal = normalize(gl_NormalMatrix*gl_Normal);

  /* Compute light contributions. */
  vec4 ambient = vec4(0.0);
  vec4 diffuse = vec4(0.0);
  vec4 specular = vec4(0.0);
  AllLights(normal, ambient, diffuse, specular);

  gl_FrontColor = gl_BackColor = (  ambient*gl_FrontMaterial.ambient
                                  + diffuse*gl_FrontMaterial.diffuse);
  gl_FrontSecondaryColor = gl_BackSecondaryColor
    = specular*gl_FrontMaterial.specular;

  gl_TexCoord[0].stp = gl_Vertex.xyz;

  gl_Position = ftransform();
}

  </Shader>

  <Shader scope="Fragment"
          name="ProceduralTextureVert"
          location="Inline"
          language="GLSL"
          entry="main">
    <Uniform type="float" name="Frequency"
	     number_of_elements="1" value="0.1"/>
    <Uniform type="float" name="Color1"
	     number_of_elements="4" value="1.0 0.0 0.0 1.0"/>
    <Uniform type="float" name="Color2"
	     number_of_elements="4" value="1.0 1.0 1.0 1.0"/>
/* This is a fragment program for a simple procedural texture example.
 * The texture is a 3D texture with concentric spheres of alternating
 * colors.  The frequency of the sphere alternation is specified in a
 * uniform variable of the fragment processor.
 */

/*
 * Copyright 2006 Sandia Corporation.
 * Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
 * license for use of this work by or on behalf of the
 * U.S. Government. Redistribution and use in source and binary forms, with
 * or without modification, are permitted provided that this Notice and any
 * statement of authorship are reproduced on all copies.
 */

uniform float Frequency;
uniform vec4 Color1;
uniform vec4 Color2;

void main()
{
  float radius = length(gl_TexCoord[0].stp);

  if (fract(radius/Frequency) < 0.5)
    {
    gl_FragColor = gl_Color*Color1 + gl_SecondaryColor;
    }
  else
    {
    gl_FragColor = gl_Color*Color2 + gl_SecondaryColor;
    }
}

  </Shader>

</Material>

Back to VTKShaders