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 	This is an 'abstract' class which should not be instantiaed.  Doing so will result in a 
 10 	light which will not work and have no effect on the scene.  This class exists to serve as 
 11 	a base class for c3dl.PositionalLight and c3dl.DirectionalLight. To place a light into a 
 12 	scene use a class which derives from this one.
 13 */
 14 c3dl.Light = function()
 15 {
 16 	// Derived classes will overide this member so when scene calls getType() it will
 17 	// know its type.
 18 	this.type = c3dl.ABSTRACT_LIGHT;
 19 	
 20 	// assign names to lights as to make removing them easy.
 21 	this.name = "unnamed";
 22 
 23 	// OpenGL assigns default values to lights.  For example light0 already has some
 24 	// properties which will light the scene.  the other lights are off by default. It
 25 	// was decided to zero out all the components for all the lights 0-7 to prevent
 26 	// any confusion.
 27 	this.ambient = [0,0,0];
 28 	this.diffuse = [0,0,0];
 29 	this.specular = [0,0,0];
 30 	//this.on = 0;
 31 	this.on = false;
 32 	
 33 	/**
 34 		Get the name of this light.
 35 		
 36 		The name can be assigned with SetName() and should be unique.  When getting or 
 37 		removing a light from the scene, the name must be used as an identifier.
 38 		
 39 		@returns {string} the name of the light.
 40 	*/
 41 	this.getName = function()
 42 	{
 43 		return this.name;
 44 	}
 45 
 46 	/**
 47 		Get the ambient component of this light.
 48 
 49 		Ambient light does not have a direction or position, but seems to come from 
 50 		everywhere. If only using ambient light, all objects in the scene would be lit 
 51 		evenly. Assigning an ambient component to a light can seem strange since lights 
 52 		typically tend to attenuate. The functionality is simply provided because OpenGL 
 53 		supports it. Generally, you should use scene.setAmbientLight(array) to place 
 54 		ambient light in the scene.
 55 
 56 		Regardless of the position of the light in the scene, the ambient component 
 57 		will light the entire surface of all objects in the scene.  Also, if only using
 58 		ambient light, object will tend to appear flat, therefore set the diffuse color
 59 		component to give 'shape' to objects.
 60 		
 61 		@returns {Array} Array of three values in the order RGB.
 62 	*/
 63 	this.getAmbient = function()
 64 	{
 65 		return c3dl.copyObj(this.ambient);
 66 	}
 67 
 68 	/**
 69 		Get the difuse component of this light.
 70 		
 71 		Diffuse light is what most people associate with what light is. Diffuse gives 
 72 		shape to the object, making it appear 3D. Diffuse lighting is dependent on the 
 73 		light’s position relative to the object.  For example,  if you were looking 
 74 		at the object directly and the light was behind it, you would likely not see much of
 75 		the light's effect. If you were at the position of the light looking at the object,
 76 		you would see the object light and likely its contour would be less lit.
 77 		
 78 		@returns {Array} Array of three values in the order RGB.
 79 	*/
 80 	this.getDiffuse = function()
 81 	{
 82 		return c3dl.copyObj(this.diffuse);
 83 	}
 84 	
 85 	/**
 86 		Get the specular component of the light.  
 87 
 88 		Specular lighting is used to create shiny highlights. Highlights are typically 
 89 		seen on objects such as glass, metal or plastic and are usually white.
 90 		Unlike ambient and diffuse light, specular light takes not only the object's 
 91 		position into account, but also the viewer’s position. The highlights tend to
 92 		'follow' where the camera is looking.
 93 
 94 		@returns {Array} Array of three values in the order RGB.
 95 	*/
 96 	this.getSpecular = function()
 97 	{
 98 		return c3dl.copyObj(this.specular);
 99 	}
100 
101 	/**
102 		Get the type of light this is.
103 
104 		@returns {int} the type of light this is.
105 	*/
106 	this.getType = function()
107 	{
108 		return this.type;
109 	}
110 
111 	/**
112 		If the light is on, it will affect the colors of the object it hits.
113 		
114 		When creating a light, its initial state will be off.
115 		
116 		@returns {boolean} true if the light is on, otherwise false.
117 	*/
118 	this.isOn = function()
119 	{
120 		return this.on;
121 	}
122 	
123 	/**
124 		Turn the light on or off.
125 	
126 		@param {boolean} isOn true to turn the light on, false to turn it off.
127 	*/
128 	this.setOn = function(isOn)
129 	{
130 		this.on = isOn;
131 	}
132 
133 	/**
134 		Set the name of the light.
135 		
136 		The scene can later be queried for the light by this name so the light can be 
137 		updated or removed.  The default name is "unnamed".
138 	
139 		@param {String} name The new name of the light.
140 	*/	
141 	this.setName = function(name)
142 	{
143 		this.name = name;
144 	}
145 
146 	/**
147 		Set the ambient color component of this light.
148 
149 		Ambient light does not have a direction or position, but seems to come from 
150 		everywhere. If only using ambient light, all objects in the scene would be lit 
151 		evenly. Assigning an ambient component to a light can seem strange since lights 
152 		typically tend to attenuate. The functionality is simply provided because OpenGL 
153 		supports it. Generally, you should use scene.setAmbientLight(array) to place 
154 		ambient light in the scene.
155 		
156 		Regardless of the position of the light in the scene, the ambient component 
157 		will light the entire surface of all objects in the scene. Also, if only using
158 		ambient light, object will tend to appear flat, therefore set the diffuse color
159 		component to give 'shape' to objects.
160 
161 		@param {Array} color Array of three values in the order RGB.
162 	*/	
163 	this.setAmbient = function(color)
164 	{
165 		if(color.length >= 3)
166 		{
167 			this.ambient = color.slice(0,3);
168 		}
169 	}
170 
171 	/**
172 		Set the diffuse color component of this light.
173 
174 		Diffuse light is what most people associate with what light is. Diffuse gives 
175 		shape to the object, making it appear 3D. Diffuse lighting is dependent on the 
176 		light’s position relative to the object.  For example,  if you were looking 
177 		at the object directly and the light was behind it, you would likely not see much of
178 		the light's effect. If you were at the position of the light looking at the object,
179 		you would see the object light and likely its contour would be less lit.		
180 
181 		@param {Array} color Array of three values in the order RGB.
182 	*/	
183 	this.setDiffuse = function(color)
184 	{
185 		if(color.length >= 3)
186 		{
187 			this.diffuse = color.slice(0,3);
188 		}
189 	}
190 	
191 	/**
192 		Set the specular component of the light.  
193 
194 		Specular lighting is used to create shiny highlights. Highlights are typically 
195 		seen on objects such as glass, metal or plastic and are usually white.
196 		Unlike ambient and diffuse light, specular light takes not only the object's 
197 		position into account, but also the viewer’s position. The highlights tend to
198 		'follow' where the camera is looking.
199 
200 		@param {Array} color Array of three values in the order RGB.
201 	*/
202 	this.setSpecular = function(color)
203 	{
204 		if(color.length >= 3)
205 		{
206 			this.specular = color.slice(0,3);
207 		}
208 	}
209 }
210