Skip to content

Commit 1b1ef18

Browse files
committed
Add convenience methods to Lights class
1 parent 842a9bf commit 1b1ef18

File tree

1 file changed

+48
-12
lines changed

1 file changed

+48
-12
lines changed

glutil.js

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,13 @@ function LightSource(position, ambient, diffuse, specular) {
479479
* @param {object} params An object whose keys have
480480
* the possibilities given below, and whose values are those
481481
* allowed for each key.<ul>
482-
* <li><code>position</code> - Light position.
482+
* <li><code>position</code> - Light position. An array of four numbers,
483+
* where the first three numbers are the X, Y, and Z components and the fourth
484+
* number is the W component.<ul>
485+
* <li>If W is 0, then X, Y, and Z specify a 3-element vector giving the direction
486+
* of the light; the light will shine everywhere in the given direction.
487+
* <li>If W is 0, then X, Y, and Z specify the position of the light in world
488+
* space; the light will shine brightest at the given position.</p>
483489
* <li><code>ambient</code> - Not used in the default shader program.
484490
* <li><code>diffuse</code> - Diffuse color.
485491
* <li><code>specular</code> - Specular highlight color.
@@ -545,34 +551,49 @@ Lights._createNewLight=function(index){
545551
}
546552
return ret;
547553
}
554+
/**
555+
* Not documented yet.
556+
* @param {*} index
557+
* @return {LightSource} The corresponding light source object.
558+
*/
559+
Lights.prototype.getLight=function(index){
560+
if(!this.lights[index])this.lights[index]=Lights._createNewLight(index);
561+
return this.lights[index];
562+
}
563+
/**
564+
* Not documented yet.
565+
* @param {*} index
566+
* @param {object} params An object as described in {@link glutil.LightSource.setParams}.
567+
* @return {Lights} This object.
568+
*/
569+
Lights.prototype.setParams=function(index,params){
570+
this.getLight(index).setParams(params);
571+
return this;
572+
}
548573

549574
/**
550575
* Sets a directional light.
551576
* @param {number} index Zero-based index of the light to set. The first
552577
* light has index 0, the second has index 1, and so on.
578+
* If the light doesn't exist at that index, it will be created.
553579
* @param {Array<number>} position A 3-element vector giving the direction of the light, along the X, Y, and Z
554-
* axes, respectively. May be null, in which case the default
555-
* is (0, 0, 1).
580+
* axes, respectively.
556581
* @return {Lights} This object.
557582
*/
558583
Lights.prototype.setDirectionalLight=function(index,direction){
559-
if(!this.lights[index])this.lights[index]=Lights._createNewLight(index);
560-
this.lights[index].setParams({"position":[direction[0],direction[1],direction[2],0]});
561-
return this;
584+
return this.setParams({"position":[direction[0],direction[1],direction[2],0]});
562585
}
563586
/**
564587
* Sets a point light.
565588
* @param {number} index Zero-based index of the light to set. The first
566589
* light has index 0, the second has index 1, and so on.
590+
* If the light doesn't exist at that index, it will be created.
567591
* @param {Array<number>} position A 3-element vector giving the X, Y, and Z
568-
* coordinates, respectively, of the light, in world coordinates. May be null, in which case the default
569-
* is (0, 0, 0).
592+
* coordinates, respectively, of the light, in world coordinates.
570593
* @return {Lights} This object.
571594
*/
572595
Lights.prototype.setPointLight=function(index,position){
573-
if(!this.lights[index])this.lights[index]=Lights._createNewLight(index);
574-
this.lights[index].setParams({"position":[position[0],position[1],position[2],1]});
575-
return this;
596+
return this.setParams(index,{"position":[position[0],position[1],position[2],1]});
576597
}
577598

578599
/**
@@ -1616,7 +1637,8 @@ Scene3D.prototype.setLookAt=function(eye, center, up){
16161637
return this;
16171638
}
16181639
/**
1619-
* Adds a 3D shape to this scene.
1640+
* Adds a 3D shape to this scene. Its reference, not a copy,
1641+
* will be stored in the 3D scene's list of shapes.
16201642
* @param {Shape|MultiShape} shape A 3D shape.
16211643
* @return {glutil.Scene3D} This object.
16221644
*/
@@ -1672,6 +1694,16 @@ Scene3D.prototype.setDirectionalLight=function(index,position,diffuse,specular){
16721694
new LightsBinder(this.lightSource).bind(this.program);
16731695
return this;
16741696
}
1697+
/**
1698+
* Not documented yet.
1699+
* @param {*} index
1700+
* @param {*} params
1701+
*/
1702+
Scene3D.prototype.setLightParams=function(index,params){
1703+
this.lightSource.setParams(index,params);
1704+
new LightsBinder(this.lightSource).bind(this.program);
1705+
return this;
1706+
}
16751707
/**
16761708
* Sets the color of the scene's ambient light.
16771709
* @param {Array<number>|number|string} r Array of three or
@@ -2132,12 +2164,16 @@ Shape.prototype.multRotation=function(angle, v,vy,vz){
21322164
Shape.prototype.getMatrix=function(){
21332165
if(this._matrixDirty){
21342166
this._matrixDirty=false;
2167+
// for best results, multiply in this order:
2168+
// 1. translation
21352169
this.matrix=GLMath.mat4translated(this.position[0],
21362170
this.position[1],this.position[2]);
2171+
// 2. rotation
21372172
if(!GLMath.quatIsIdentity(this.rotation)){
21382173
this.matrix=GLMath.mat4multiply(this.matrix,
21392174
GLMath.quatToMat4(this.rotation));
21402175
}
2176+
// 3. scaling
21412177
GLMath.mat4scaleInPlace(this.matrix,this.scale);
21422178
}
21432179
return this.matrix.slice(0,16);

0 commit comments

Comments
 (0)