// Modified by Morgan from Staer9's shipset OXP 1.1.
uniform sampler2D    uColorMap;
uniform sampler2D    uNormalMap;
uniform sampler2D    uEffectsMap;

varying vec2         vTexCoord;
varying vec3         vEyeVector;   // These are all in tangent space
varying vec3         vLight0Vector;
varying vec3         vLight1Vector;

// Constants
const float KspecExponent = 5.0;

// Uniforms from Oolite
   uniform float  uTime;
   uniform bool   nearly_dead;
   uniform bool   isHostile;   
   uniform float  hull_heat_level;
   uniform float  laser_heat_level;   
   uniform float  engine_power;
   uniform vec4   PaintColor1; // used with paintmask map to tint diffuse texture

// Irregular flickering function
   #ifdef OO_REDUCED_COMPLEXITY 
   #define Pulse(v, ts) ((v) * 0.95) 
   #define Blink_on_off(value) (1.0)
   #else

     float Pulse(float value, float timeScale)
     {
        float t = uTime * timeScale;   

        float s0 = t;
        s0 -= floor(s0);
        float sum = abs( s0 - 0.5);
   
        float s1 = t * 0.7 - 0.05;
        s1 -= floor(s1);
        sum += abs(s1 - 0.5) - 0.25;
   
        float s2 = t * 1.3 - 0.3;
        s2 -= floor(s2);
        sum += abs(s2 - 0.5) - 0.25;
   
        float s3 = t * 5.09 - 0.6;
        s3 -= floor(s3);
        sum += abs(s3 - 0.5) - 0.25;

        return (sum * 0.1 + 0.9) * value;
      }

// Hull Temperate heat glow effect
     vec3 TemperatureGlow(float level) 
     { 
        vec3 result = vec3(0); 
    
        result.r = level; 
        result.g = level * level * level; 
        result.b = max(level - 0.7, 0.0) * 2.0;
        return result;    
     } 
     
  
   
#endif

void Light(in vec3 lightVector, in vec3 normal, in vec3 lightColor, in vec3 eyeVector, 
           in float KspecExponent, inout vec3 totalDiffuse, inout vec3 totalSpecular)
{
   lightVector = normalize(lightVector);
   vec3 reflection = normalize(-reflect(lightVector, normal));
   
   totalDiffuse += gl_FrontMaterial.diffuse.rgb * lightColor * max(dot(normal, lightVector), 0.0);
   totalSpecular += lightColor * pow(max(dot(reflection, eyeVector), 0.0), KspecExponent);
}


#define LIGHT(idx, vector) Light(vector, normal, gl_LightSource[idx].diffuse.rgb, eyeVector, KspecExponent, diffuse, specular)

#ifndef OO_REDUCED_COMPLEXITY 
// function to read in the colour map
vec3 diffuseColor() 
{ 
    
    
// Get texture values. 
vec3 baseTex = texture2D(uColorMap, vTexCoord).rgb; 
float PaintMap = texture2D(uNormalMap, vTexCoord).a; 
      baseTex += PaintMap * PaintColor1.rgb;
return baseTex;
}      
#endif  

void main()

	/*
		Sample our texture maps.
		
		uColorMap contains the base colour of the ship in the RGB channels, and
		a specular intensity (shininess) map in the A channel.
			
		uNormalMap contains a normal map in its RGB channels, and the tint map
		in the A channel. The tint map is used to determine which areas are
		repainted using uPaintColor (a random vector set up in shipdata.plist).
		The tinting adds the random vector, which may have negative components,
		to the value from the colur map. In the areas that are tinted, the
		colour map is roughty equal to the tint map modulated by the base colour.
		(The base colour is used as-is when shaders are disabled.)
		
		uEffectsMap is a lower-resolution texture containing four different
		glow effect maps.
			The R channel is the “exhaust metal glow map”, which controls a
			red heat glow on metal around the engines, proportional to engine
			power.
			The G channel is the “weapon glow map”, which controls a heat glow
			on metal around the main weapon, proportional to weapon temperature.
			The B channel is the “exhaust glow map”, which controls a cyan glow
			(representing reflected light from the exhaust plumes) on areas
			around the engines.
			The A channel is the “lamp glow map”, which represents running
			lights and lights from windows.
	*/
 	/*
		vEyeVector, provided by the vertex shader, is a vector from the point
		we’re shading to the camera in tangent space (a coordinate system
		relative to the part of the surface of the ship that we’re working on).
		While vEyeVector can be of any length, eyeVector is normalized to a
		length of 1 and represents the direction to the camera.
			
		The normal defines the direction straight outward from the surface,
		which is important in determining lighting. Without normal mapping,
		it is by definition vec3(0.0, 0.0, 1.0) in tangent space. By looking
		up a distorted normal from a texture, we can represent more surface
		detail without having to add large numbers of polygons.
	*/





{
   vec3 eyeVector = normalize(vEyeVector);
   vec3 normal = normalize(texture2D(uNormalMap, vTexCoord).rgb - 0.5);



   vec3 colorMap = texture2D(uColorMap, vTexCoord).rgb;
   float specIntensity = texture2D(uColorMap, vTexCoord).a * 4.0;

   vec4 effectsMap = texture2D(uEffectsMap, vTexCoord);
   vec3 diffuse = vec3(0.0), specular = vec3(0);

   vec3 LampColor = vec3(0.92, 0.62, 0.98);
#ifdef OO_LIGHT_0_FIX
   LIGHT(0, normalize(vLight0Vector));
#endif
   LIGHT(1, normalize(vLight1Vector)); // change the 0 to 1 when exporting back to oolite
   diffuse += gl_FrontMaterial.ambient.rgb * gl_LightModel.ambient.rgb;
   
// Calculate the lighting for full shader mode
   #ifndef OO_REDUCED_COMPLEXITY 
   vec3 color = diffuse * diffuseColor();
   color += diffuseColor() * specular * specIntensity;       
   #endif
// Calculate the lighting for simple shader mode
   #ifdef OO_REDUCED_COMPLEXITY    
   vec3 color = diffuse * colorMap;
   color += colorMap * specular * specIntensity;
   // Add in hull lights for simple shader mode
   color += LampColor * effectsMap.a;   
   #endif

// these next glow effects are only available in full shader mode   
   #ifndef OO_REDUCED_COMPLEXITY 
   
// check if ship is hostile, adjust lampColor accordingly
   if (isHostile)
      { 
       LampColor = vec3(1.5, -0.4, -0.4) * max(mod(uTime, 1.0) * 2.0, 0.5); 
      }  
/*
// Add the hull lights glow effects, check if ship is throwingSparks, if so flicker the effects
   if (nearly_dead)
      { 
      color += cyanGlow(effectsMap.b *  Pulse(min(engine_power, 1.0), 1.0)) * Blink_on_off(Pulse(1.0, 0.7)) * 2.0;
      color += LampColor * effectsMap.a * Blink_on_off(Pulse(1.0, 1.0));      
      }  
   if (!nearly_dead) 
      { 
      color += cyanGlow(effectsMap.b * Pulse(min(engine_power, 1.0), 1.0)) * 2.0;
      color += LampColor * effectsMap.a * Pulse(1.0, 0.3); 
      }

// Add in the red heated metal glow around the exhaust. 
   color += redGlow(effectsMap.r * Pulse(min(engine_power, 1.0), 1.0));
   
//Add in the laser firing gun barrel flash effect    
  color += WeaponGlow(effectsMap.g  * Pulse(min(laser_heat_level, 1.0), 1.0));*/
      
// Add the all over hull temperature glow. Full Shader mode only
   float hullHeat = max(hull_heat_level - 0.5, 0.0) * 2.0; 
   hullHeat = Pulse(hullHeat * hullHeat, 0.1); 
   color += TemperatureGlow(hullHeat); 
#endif   
    
   gl_FragColor = vec4(color.rgb, 1.0);
}
