
  shader += "  lightvec = normalize(lightvec);\n";

  shader += "  vec3 reflecvec = reflect(lightvec, normal);\n";
  shader += "  float DiffMag = abs(dot(normal, lightvec));\n";
  shader += "  vec3 Diff = (diffuse*DiffMag)*gl_FragColor.rgb;\n";
  shader += "  float Spec = pow(abs(dot(normal, reflecvec)), speccoeff);\n";
  shader += "  Spec *= specular*gl_FragColor.a;\n";
  shader += "  vec3 Amb = ambient*gl_FragColor.rgb;\n";
  shader += "  float litfrac;\n";
  shader += "  litfrac = smoothstep(0.05, 0.1, grad);\n";
  shader += "  gl_FragColor.rgb = mix(gl_FragColor.rgb, Amb, litfrac);\n";
  shader += "  if (litfrac > 0.0)\n";
  shader += "   {\n";
  shader += "     vec3 frgb = gl_FragColor.rgb + litfrac*(Diff + Spec);\n";
  shader += "     if (any(greaterThan(frgb,vec3(1.0,1.0,1.0)))) \n";
  shader += "        frgb = vec3(1.0,1.0,1.0);\n";
  shader += "     if (any(greaterThan(frgb,gl_FragColor.aaa))) \n";  
  shader += "        frgb = gl_FragColor.aaa;\n";
  shader += "     gl_FragColor.rgb = frgb;\n";
  shader += "   }\n";
  shader += "  gl_FragColor.rgb *= lightcol;\n";

  return shader;
}


QString
ShaderFactory::genBlurShaderString(bool softShadows,
				   int blurSize,
				   float blurRadius)
{
  QString shader;

  shader  = "uniform sampler2DRect shadowTex;\n";
  shader += "\n";
  shader += "void main(void)\n";
  shader += "{\n";
  shader += "  vec4 shadow;\n";
  shader += "  vec4 spos = gl_TexCoord[0];\n";
  shader += "\n";
  shader += "  shadow = texture2DRect(shadowTex, spos.xy);\n";

  if (softShadows)
    {
      shader += "  vec4 vshadow;\n";
      shader += "\n";
      shader += "  vshadow = vec4(0.0,0.0,0.0,0.0);\n";
      shader += "\n";

      shader += "  ivec2 idel;\n";
      shader += "  idel = ivec2(spos.xy);\n";
      shader += "  idel -= 2*(idel/2);\n";
      shader += "  idel.y = int(idel.x == idel.y);\n";
      shader += "  idel.x = 1;\n";
      shader += "\n";

      shader += "  vec2 del, delprev, delstep;\n";
      shader += QString("  del = vec2(idel)*float(%1);\n").arg(blurRadius);
      shader += "  delstep = del;\n";
      shader += "\n";

      float frc = 1.0/(4*blurSize + 1);
      for (int i=0; i<blurSize; i++)
	{
	  if (i<blurSize-1)
	    shader += "  delprev = del;\n";

	  shader += "  vshadow += texture2DRect(shadowTex, spos.xy+del.xy);\n";
	  shader += "  vshadow += texture2DRect(shadowTex, spos.xy-del.xy);\n";
	  shader += "  del.xy = del.yx;\n";
	  shader += "  del.x = -del.x;\n";
	  shader += "  vshadow += texture2DRect(shadowTex, spos.xy+del.xy);\n";
	  shader += "  vshadow += texture2DRect(shadowTex, spos.xy-del.xy);\n";

	  shader += "\n";
	  
	  if (i<blurSize-1)
	    shader += "  del = delprev + delstep;\n";
	}

      shader += QString("  shadow = float(%1)*(shadow + vshadow);\n").arg(frc);
    }

  shader += "  gl_FragColor.rgba = shadow;\n";
  shader += "}\n";

  return shader;
}

QString
ShaderFactory::genBoxShaderString()
{
  QString shader;

  shader += "#extension GL_ARB_texture_rectangle : enable\n";
  shader += "uniform sampler2DRect blurTex;\n";
  shader += "uniform vec2 direc;\n";
  shader += "uniform int type;\n";
  shader += "\n";
  shader += "void main(void)\n";
  shader += "{\n";
  shader += "  vec4 color = vec4(0.0);\n";
  shader += "  vec2 uv = gl_TexCoord[0].xy;\n";
  shader += "\n";
  shader += "  if (type == 1)\n";
  shader += "   {\n";
  shader += "    gl_FragColor = texture2DRect(blurTex, uv+direc*0.5)*0.5;\n";
  shader += "    gl_FragColor+= texture2DRect(blurTex, uv-direc*0.5)*0.5;\n";
  shader += "   }\n";
  shader += "  else if (type == 2)\n";
  shader += "   {\n";
  shader += "    vec2 off1 = vec2(1.3333333333333333) * direc;\n";
  shader += "    gl_FragColor =  texture2DRect(blurTex, uv)*0.29411764705882354;\n";
  shader += "    gl_FragColor += texture2DRect(blurTex, uv+off1)*0.35294117647058826;\n";
  shader += "    gl_FragColor += texture2DRect(blurTex, uv-off1)*0.35294117647058826;\n";
  shader += "   }\n";
  shader += "  else if (type == 3)\n";
  shader += "   {\n";
  shader += "    vec2 off1 = vec2(1.3846153846) * direc;\n";
  shader += "    vec2 off2 = vec2(3.2307692308) * direc;\n";
  shader += "    gl_FragColor =  texture2DRect(blurTex, uv) * 0.2270270270;\n";
  shader += "    gl_FragColor += texture2DRect(blurTex, uv + off1) * 0.3162162162;\n";
  shader += "    gl_FragColor += texture2DRect(blurTex, uv - off1) * 0.3162162162;\n";
  shader += "    gl_FragColor += texture2DRect(blurTex, uv + off2) * 0.0702702703;\n";
  shader += "    gl_FragColor += texture2DRect(blurTex, uv - off2) * 0.0702702703;\n";
  shader += "   }\n";
  shader += "  else if (type == 4)\n";
  shader += "   {\n";
  shader += "    vec2 off1 = vec2(1.411764705882353) * direc;\n";
  shader += "    vec2 off2 = vec2(3.2941176470588234)* direc;\n";
  shader += "    vec2 off3 = vec2(5.176470588235294) * direc;\n";
  shader += "    gl_FragColor =  texture2DRect(blurTex, uv) * 0.1964825501511404;\n";
  