1 /*
  2   Copyright (c) 2008 Seneca College
  3   Licenced under the MIT License (http://www.c3dl.org/index.php/mit-license/)
  4 */
  5 
  6 
  7 /**
  8 	@class
  9 	
 10 	A spotlight is a PositionalLight which can have a 'cone' of light used to 
 11 	restrict what objects or parts of an object get lit.  All vertices which 
 12 	fall inside the code are lit.
 13 	
 14 	One application of using a spotlight is making a headlight effect.
 15 	@see c3dl.PositionalLight
 16 	@augments c3dl.Light
 17 	@augments c3dl.PositionalLight	
 18 */
 19 c3dl.SpotLight = function()
 20 {
 21 	// this will be multiplied by 2 within opengl. 180 is the default opengl 
 22 	// value for cutoff.  So we are starting off which a Positional light 
 23 	// since there is no light 'cone'.
 24 	this.cutoff = 180;
 25 		
 26 	// override the type
 27 	this.type = c3dl.SPOT_LIGHT;
 28 
 29 	// the direction where the spot light is pointing.
 30 	this.direction = [0,0,-1];
 31 	
 32 	// how concentrated is the light?
 33 	// use the opengl default value of zero to indicate uniform 
 34 	// light distribution.
 35 	this.exponent = 0;
 36 
 37 
 38 	/**
 39 		The cutoff value defines the spread of the cone of the spotlight. The
 40 		value will either be 180 degrees or will range from 0 to 90 degrees. If 
 41 		the value is 180, the spotlight will not have a cone.
 42 		
 43 		@returns {float} the cutoff value.
 44 	*/
 45 	this.getCutoff = function()
 46 	{
 47 		return this.cutoff;
 48 	}
 49 
 50 	/**
 51 		Get the direction the spotlight is pointing which will be unit
 52 		length.
 53 	
 54 		@returns {Array} the direction where the spotlight is
 55 		pointing which will be unit length.
 56 	*/
 57 	this.getDirection = function()
 58 	{
 59 		return [this.direction[0],this.direction[1],this.direction[2]];
 60 	}
 61 	
 62 	/**
 63 		Get the intensity distribution of light within the cone of light the
 64 		spotlight creates. Higher exponents result in a more focused light.
 65 
 66 		@returns {float} Exponent will range from 0 to 128 inclusive.
 67 	*/
 68 	this.getExponent = function()
 69 	{
 70 		return this.exponent;
 71 	}
 72 	
 73 	/**
 74 		The cutoff defines the spread (angle) of the cone of the spotlight. 
 75 		If the angle between the direction of the light and direction of spotlight
 76 		to the vertex being lit is less than the cutoff, the vertex will be lit.
 77 		
 78 		@param {float} cutoff Measured in degrees. Must either be equal 
 79 		to 180 or range between 0 and 90.
 80 	*/
 81 	this.setCutoff = function(cutoff)
 82 	{
 83 		if((cutoff >= 0 && cutoff <= 90) || cutoff == 180)
 84 		{
 85 			this.cutoff = cutoff;
 86 		}
 87 	}
 88 	
 89 	/**
 90 		Set the direction of the spotlight. The 'dir' argument will be scaled to
 91 		a unit vector before being assigned if not already unit length.
 92 
 93 		@param {Array} dir Direction the spotlight is pointing. Will be scaled 
 94 		to a unit vector before being assigned if not already unit length.
 95 	*/
 96 	this.setDirection = function(dir)
 97 	{
 98 		if(c3dl.isValidVector(dir))
 99 		{
100 			this.direction = c3dl.normalizeVector(dir);
101 		}
102 	}
103 	
104 	/**
105 		Set the intensity distribution of the light within the cone the spotlight
106 		creates. Higher exponent values will result in a more focused light.
107 
108 		@param {float} exponent Must range from 0 to 128 inclusive.
109 	*/
110 	this.setExponent = function(exponent)
111 	{
112 		if(exponent >= 0 && exponent <= 128)
113 		{
114 			this.exponent = exponent;
115 		}
116 	}
117 }
118 
119 c3dl.SpotLight.prototype = new c3dl.PositionalLight;
120