Skip to content

Commit d2b4a09

Browse files
committed
edit docs; add math constants
1 parent 014f446 commit d2b4a09

File tree

9 files changed

+202
-159
lines changed

9 files changed

+202
-159
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ Examples
6464
rotation[0]+=.5; // Adjust x-rotation by .5 degree
6565
rotation[1]+=1.0; // Adjust y-rotation by 1 degree
6666
// Update the shape's rotation
67-
var q=GLMath.quatFromTaitBryan(rotation);
68-
shape.setOrientation(q);
67+
shape.setQuaternion(GLMath.quatFromTaitBryan(rotation));
6968
// Render the scene
7069
scene.render();
7170
});

glmath.js

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ var GLMath={
3636
* The cross product is the vector that is perpendicular to
3737
* each of two other vectors.<p>
3838
* If both vectors are unit length
39-
* (via {@link glmath.GLMath.vec3norm}), the sine of
40-
* the angle between them is equal to the length of their
39+
* (via {@link glmath.GLMath.vec3norm}), the absolute value
40+
* of the sine of the angle between them is equal to the length of their
4141
* cross product. <small>(More formally, the length of the cross
42-
* product equals |<b>a</b>| * |<b>b</b>| * sin &theta;
42+
* product equals |<b>a</b>| * |<b>b</b>| * |sin &theta|;
4343
* where |<b>x</b>| is the length of vector <b>x</b>.)</small><p>
4444
* The cross product (<b>c</b>) of vectors <b>a</b> and <b>b</b> is found as
4545
* follows:<pre>
@@ -424,16 +424,20 @@ return r;
424424
},
425425
/**
426426
* Inverts the rotation given in this quaternion without normalizing it;
427-
* returns a new quaternion.
427+
* returns a new quaternion. The conjugate represents the same
428+
* rotation as the original.
428429
* @param {Array<number>} quat A quaternion, containing four elements.
430+
* @return {Array<number>}
429431
*/
430432
quatConjugate:function(quat){
431433
return [-quat[0],-quat[1],-quat[2],quat[3]];
432434
},
433435
/**
434436
* Inverts the rotation given in this quaternion, then normalizes the result;
435-
* returns a new quaternion.
437+
* returns a new quaternion. The inverse represents the same
438+
* rotation as the original.
436439
* @param {Array<number>} quat A quaternion, containing four elements.
440+
* @return {Array<number>}
437441
*/
438442
quatInverse:function(quat){
439443
return GLMath.quatNormInPlace(
@@ -1085,9 +1089,7 @@ mat4translate:function(mat,v3,v3y,v3z){
10851089
* </ul>
10861090
* @param {number} fovY Y-axis field of view, in degrees. Should be less
10871091
* than 180 degrees. (The smaller
1088-
* this number, the bigger close objects appear to be. As a result,
1089-
* zoom can be implemented by multiplying field of view by an
1090-
* additional factor.)
1092+
* this number, the bigger close objects appear to be.)
10911093
* @param {number} aspectRatio The ratio of width to height of the viewport, usually
10921094
* the scene's aspect ratio.
10931095
* @param {number} near The distance from the camera to
@@ -1118,8 +1120,9 @@ mat4perspective:function(fovY,aspectRatio,near,far){
11181120
* the point in world space that the camera is looking at. May be null or omitted,
11191121
* in which case the default is the coordinates (0,0,0).
11201122
* @param {Array<number>} [up] A 3-element vector specifying
1121-
* the up-vector direction. May be null or omitted, in which case
1122-
* the default is a vector pointing positive on the Y axis. This
1123+
* the direction from the center of the camera to its top. This parameter may
1124+
* be null or omitted, in which case the default is the vector (0, 1, 0),
1125+
* the vector that results when the camera is held upright. This
11231126
* vector must not point in the same or opposite direction as
11241127
* the camera's view direction. (For best results, rotate the vector (0, 1, 0)
11251128
* so it points perpendicular to the camera's view direction.)
@@ -1299,7 +1302,12 @@ mat4multiply:function(a,b){
12991302
/**
13001303
* Multiplies two quaternions, creating a composite rotation.
13011304
* The quaternions are multiplied such that the second quaternion's
1302-
* rotation happens before the first quaternion's rotation.
1305+
* rotation happens before the first quaternion's rotation.<p>
1306+
* Multiplying two unit quaternions (each with a length of 1) will result
1307+
* in a unit quaternion. However, for best results, you should
1308+
* normalize the quaternions every few multiplications (using
1309+
* quatNormalize or quatNormInPlace, since successive
1310+
* multiplications can cause rounding errors.
13031311
* @param {Array<number>} a The first quaternion.
13041312
* @param {Array<number>} b The second quaternion.
13051313
* @return {Array<number>} The resulting quaternion.
@@ -1534,6 +1542,18 @@ GLMath.quatScaleInPlace=GLMath.vec4scaleInPlace;
15341542
* @return {Array<number>}
15351543
*/
15361544
GLMath.quatCopy=GLMath.vec4copy;
1545+
/**
1546+
Closest approximation to pi times 2.
1547+
@const
1548+
@default
1549+
*/
1550+
GLMath.PiTimes2 = 6.283185307179586476925286766559;
1551+
/**
1552+
Closest approximation to pi divided by 2.
1553+
@const
1554+
@default
1555+
*/
1556+
GLMath.HalfPi = 1.5707963267948966192313216916398;
15371557
/**
15381558
Closest approximation to pi divided by 180. Multiply by this number to convert degrees to radians.
15391559
@const

glutil-meshes.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ Meshes.createCylinder=function(baseRad, topRad, height, slices, stacks, flat, in
115115
var normDir=(inside) ? -1 : 1;
116116
var sc=[0,1]; // sin(0), cos(0)
117117
var tc=[0];
118-
var twopi=Math.PI*2;
118+
var twopi=GLMath.PiTimes2;
119119
for(var i=1;i<slices;i++){
120120
var t=i*1.0/slices;
121121
var angle=twopi*t;
@@ -294,7 +294,7 @@ Meshes.createPartialDisk=function(inner, outer, slices, loops, start, sweep, inw
294294
if(sweep==0)sweep=360;
295295
var sc=[];
296296
var tc=[];
297-
var twopi=Math.PI*2;
297+
var twopi=GLMath.PiTimes2;
298298
var arcLength=(sweep==360) ? twopi : sweep*GLMath.PiDividedBy180;
299299
start=start*GLMath.PiDividedBy180;
300300
if(sweepDir<0){
@@ -394,7 +394,7 @@ Meshes.createTorus=function(inner, outer, lengthwise, crosswise,flat,inward){
394394
if(inner==0)return mesh;
395395
var tubeRadius=inner;
396396
var circleRad=outer;
397-
var twopi=Math.PI*2.0;
397+
var twopi=GLMath.PiTimes2;
398398
var sci=[];
399399
var scj=[];
400400
for(var i = 0; i <= crosswise; i++){
@@ -530,8 +530,8 @@ Meshes.createSphere=function(radius, slices, stacks, flat, inside){
530530
var scStack=[];
531531
var texc=[];
532532
var tc=[0];
533-
var twopi=Math.PI*2;
534-
var pidiv2=Math.PI*0.5;
533+
var twopi=GLMath.PiTimes2;
534+
var pidiv2=GLMath.HalfPi;
535535
for(var i=1;i<slices;i++){
536536
var t=i*1.0/slices;
537537
var angle=twopi*t;

glutil-shaderprog.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ ShaderProgram.prototype.use=function(){
151151
this.savedUniforms={};
152152
return this;
153153
}
154+
/** @private */
154155
ShaderProgram.prototype._log=function(i,v){
155156
// console.log("setting "+i+": "+v);
156157
}
@@ -444,6 +445,25 @@ return ShaderProgram.makeEffect(context,
444445
"}}"
445446
].join("\n"));
446447
}
448+
449+
/** @private */
450+
ShaderProgram.getBasicVertex=function(){
451+
var shader=""+
452+
"attribute vec3 position;\n" +
453+
"attribute vec3 uv;\n" +
454+
"attribute vec3 colorAttr;\n" +
455+
"varying vec2 uvVar;\n"+
456+
"varying vec3 colorAttrVar;\n" +
457+
"void main(){\n"+
458+
"vec4 positionVec4;\n"+
459+
"positionVec4.w=1.0;\n"+
460+
"positionVec4.xyz=position;\n" +
461+
"gl_PointSize=1.0;\n" +
462+
"uvVar=uv;\n" +
463+
"colorAttrVar=colorAttr;\n" +
464+
"gl_Position=positionVec4;\n" +
465+
"}\n";
466+
}
447467
/**
448468
* Gets the text of the default vertex shader. Putting "#define SHADING\n"
449469
* at the start of the return value enables the lighting model.

glutil-transform.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ Transform.prototype.setOrientation=function(angle, v,vy,vz){
219219
*/
220220
Transform.prototype.multQuaternion=function(quat){
221221
if(this.complexMatrix)return this;
222-
this.rotation=GLMath.quatMultiply(this.rotation,quat);
223-
GLMath.quatNormInPlace(this.rotation);
222+
this.rotation=GLMath.quatNormInPlace(
223+
GLMath.quatMultiply(this.rotation,quat));
224224
this._isIdentity=false;
225225
this._matrixDirty=true;
226226
return this;

glutil.js

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -653,13 +653,13 @@ function Material(ambient, diffuse, specular,shininess,emission) {
653653
this.shininess=(shininess==null) ? 0 : Math.min(Math.max(0,shininess),128);
654654
/** Ambient reflection of this material.<p>
655655
* Ambient reflection indicates how much an object reflects
656-
* ambient lights (lights that color pixels
657-
* the same way regardless of direction or distance)
658-
* in the red, green, and blue components.
656+
* ambient colors, those that color pixels the same way regardless
657+
* of direction or distance.
659658
* Because every part of an object will be shaded the same way by ambient
660-
* light, an object with just ambient reflection will not look much like a 3D object.
661-
* Ambient reflection
662-
* depends on the color of ambient lights.<p>
659+
* light, an object with just ambient reflection will not look much like a 3D object.<p>
660+
* This value is a 3-element array giving the red, green, and blue
661+
* components of the ambient reflection; the final ambient color depends
662+
* on the ambient color of the scene.
663663
* (0,0,0) means no ambient reflection,
664664
* and (1,1,1) means total ambient reflection.<p>
665665
* Setting ambient and diffuse reflection to the same value usually defines an object's
@@ -669,14 +669,14 @@ function Material(ambient, diffuse, specular,shininess,emission) {
669669
*/
670670
this.ambient=ambient ? ambient.slice(0,3) : [0.2,0.2,0.2];
671671
/**
672-
* Diffuse reflection of this material.<p>
673-
* Diffuse reflection indicates how much an object reflects
674-
* diffuse lights (lights that point
675-
* directly on the object) in the red, green, and blue components.
676-
* Because different parts of an object are shaded differently depending
672+
* Diffuse reflection of this material. Diffuse reflection is the color that a material
673+
* reflects equally in all directions. Because different parts of an object are shaded
674+
* differently depending
677675
* on how directly they face diffuse lights, diffuse reflection can contribute
678-
* much of the 3D effect of that object. Diffuse reflection
679-
* depends on the color of diffuse lights.<p>
676+
* much of the 3D effect of that object.<p>
677+
* This value is a 4-element array giving the red, green, blue, and
678+
* alpha components of the diffuse reflection; the final diffuse color depends
679+
* on the reflected colors of lights that shine on the material.
680680
* (0,0,0,1) means no diffuse reflection,
681681
* and (1,1,1,1) means total diffuse reflection.<p>
682682
* Setting ambient and diffuse reflection to the same value usually defines an object's
@@ -688,12 +688,14 @@ function Material(ambient, diffuse, specular,shininess,emission) {
688688
*/
689689
this.diffuse=diffuse ? diffuse.slice(0,diffuse.length) : [0.8,0.8,0.8,1.0];
690690
/** Specular highlight reflection of this material.
691-
* Specular reflection makes an object shiny, and it depends
692-
* on lights that give off specular highlights as well as on the color
693-
* of those highlights. Similar to diffuse reflection, specular reflection
691+
* Similar to diffuse reflection, specular reflection
694692
* is affected by how directly each part of an object faces
695693
* a light that gives off specular highlights, but unlike diffuse light,
696694
* specular light doesn't scatter very much to other parts of an object.
695+
* Specular reflection can make an object shiny.
696+
* This value is a 3-element array giving the red, green, and blue
697+
* components of the ambient reflection; the final specular color depends
698+
* on the specular color of lights that shine on the material.
697699
* (0,0,0) means no specular reflection,
698700
* and (1,1,1) means total specular reflection.<p>
699701
*/
@@ -705,6 +707,8 @@ function Material(ambient, diffuse, specular,shininess,emission) {
705707
* same way regardless of lighting (this property won't be used in the
706708
* default shader if [Scene3D.disableLighting()]{@link glutil.Scene3D#disableLighting}
707709
* is called, disabling lighting calculations).<p>
710+
* This value is a 3-element array giving the red, green, and blue
711+
* components.
708712
* For each of the three color components, positive values add to that component,
709713
* while negative values subtract from it. (0,0,0) means no additive color.
710714
*/
@@ -1373,9 +1377,7 @@ Scene3D.prototype.createBuffer=function(){
13731377
* see {@link glmath.GLMath.mat4perspective}.
13741378
* @param {number} fov Y-axis field of view, in degrees. Should be less
13751379
* than 180 degrees. (The smaller
1376-
* this number, the bigger close objects appear to be. As a result,
1377-
* zoom can be implemented by multiplying field of view by an
1378-
* additional factor.)
1380+
* this number, the bigger close objects appear to be.)
13791381
* @param {number} aspect The ratio of width to height of the viewport, usually
13801382
* the scene's aspect ratio (getAspect()).
13811383
* @param {number} near The distance from the camera to
@@ -1401,7 +1403,7 @@ Scene3D.prototype.setPerspective=function(fov, aspect, near, far){
14011403
* plane and the top to the bottom.<p>
14021404
* If the view rectangle's aspect ratio doesn't match the desired aspect
14031405
* ratio, the view rectangle will be centered on the 3D scene's viewport
1404-
* or otherwise moved so as to keep the entire view rectangle visible without stretching
1406+
* or otherwise moved and scaled so as to keep the entire view rectangle visible without stretching
14051407
* or squishing it.
14061408
* <p>
14071409
* For considerations when choosing the "near" and "far" parameters,
@@ -1438,7 +1440,7 @@ Scene3D.prototype.setOrthoAspect=function(left, right, bottom, top, near, far, a
14381440
* The near and far clipping planes will be set to -1 and 1, respectively.<p>
14391441
* If the view rectangle's aspect ratio doesn't match the desired aspect
14401442
* ratio, the view rectangle will be centered on the 3D scene's viewport
1441-
* or otherwise moved so as to keep the entire view rectangle visible without stretching
1443+
* or otherwise moved and scaled so as to keep the entire view rectangle visible without stretching
14421444
* or squishing it.
14431445
* @param {number} left Leftmost coordinate of the view rectangle.
14441446
* @param {number} right Rightmost coordinate of the view rectangle.
@@ -1640,8 +1642,9 @@ Scene3D.prototype.setViewMatrix=function(matrix){
16401642
* the point in world space that the camera is looking at. May be null or omitted,
16411643
* in which case the default is the coordinates (0,0,0).
16421644
* @param {Array<number>} [up] A 3-element vector specifying
1643-
* the up-vector direction. May be null or omitted, in which case
1644-
* the default is a vector pointing positive on the Y axis. This
1645+
* the direction from the center of the camera to its top. This parameter may
1646+
* be null or omitted, in which case the default is the vector (0, 1, 0),
1647+
* the vector that results when the camera is held upright. This
16451648
* vector must not point in the same or opposite direction as
16461649
* the camera's view direction. (For best results, rotate the vector (0, 1, 0)
16471650
* so it points perpendicular to the camera's view direction.)

0 commit comments

Comments
 (0)