Skip to content

Commit 34970a4

Browse files
committed
add ShapeGroup class; move reset method to Transform class; deprecate some obsolete methods
1 parent d3ac38c commit 34970a4

File tree

7 files changed

+185
-176
lines changed

7 files changed

+185
-176
lines changed

glmath.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1581,6 +1581,9 @@ GLMath.RollPitchYaw = 4;
15811581
* @const
15821582
*/
15831583
GLMath.RollYawPitch = 5;
1584-
1584+
/** @deprecated Renamed to quatToTaitBryan. */
1585+
GLMath.quatToEuler=GLMath.quatToTaitBryan;
1586+
/** @deprecated Renamed to quatFromTaitBryan. */
1587+
GLMath.quatFromEuler=GLMath.quatFromTaitBryan;
15851588
exports["GLMath"]=GLMath;
15861589
}));

glutil-transform.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,23 @@ function Transform(){
3939
this.rotation=GLMath.quatIdentity();
4040
this.complexMatrix=false;
4141
this._matrixDirty=false;
42+
this._isIdentity=true;
4243
/** @private */
4344
this.matrix=GLMath.mat4identity();
4445
}
45-
46+
/**
47+
* Resets this shape to the untransformed state.
48+
* @return {glutil.Shape} This object.
49+
*/
50+
Transform.prototype.reset=function(){
51+
this.matrix=GLMath.mat4identity();
52+
this.position=[0,0,0];
53+
this.scale=[1,1,1];
54+
this.rotation=GLMath.quatIdentity();
55+
this.complexMatrix=false;
56+
this._matrixDirty=false;
57+
return this;
58+
}
4659
/**
4760
* Sets this shape's transformation matrix. This method
4861
* will set the position, rotation, and scale properties
@@ -53,6 +66,7 @@ function Transform(){
5366
*/
5467
Transform.prototype.setMatrix=function(value){
5568
this._matrixDirty=false;
69+
this._isIdentity=false;
5670
this.complexMatrix=true;
5771
this.matrix=value.slice(0,16);
5872
this.position=[this.matrix[12],this.matrix[13],this.matrix[14]];
@@ -70,6 +84,7 @@ Transform.prototype.resetTransform=function(){
7084
this.matrix=GLMath.mat4identity();
7185
this.position=[0,0,0];
7286
this.scale=[1,1,1];
87+
this._isIdentity=true;
7388
this.rotation=GLMath.quatIdentity();
7489
this.complexMatrix=false;
7590
this._matrixDirty=false;
@@ -97,6 +112,10 @@ Transform.prototype.setScale=function(x,y,z){
97112
} else {
98113
this.scale=[x,y,z];
99114
}
115+
this._isIdentity=(this._isIdentity &&
116+
this.scale[0]==1 &&
117+
this.scale[1]==1 &&
118+
this.scale[2]==1);
100119
this._matrixDirty=true;
101120
return this;
102121
}
@@ -122,6 +141,10 @@ Transform.prototype.setPosition=function(x,y,z){
122141
} else {
123142
this.position=[x,y,z];
124143
}
144+
this._isIdentity=(this._isIdentity &&
145+
this.position[0]==0 &&
146+
this.position[1]==0 &&
147+
this.position[2]==0);
125148
this._matrixDirty=true;
126149
return this;
127150
}
@@ -146,6 +169,7 @@ Transform.prototype.setQuaternion=function(quat){
146169
if(this.complexMatrix)return this;
147170
this.rotation=quat.slice(0,4);
148171
GLMath.quatNormInPlace(this.rotation);
172+
this._isIdentity=false;
149173
this._matrixDirty=true;
150174
return this;
151175
}
@@ -197,6 +221,7 @@ Transform.prototype.multQuaternion=function(quat){
197221
if(this.complexMatrix)return this;
198222
this.rotation=GLMath.quatMultiply(this.rotation,quat);
199223
GLMath.quatNormInPlace(this.rotation);
224+
this._isIdentity=false;
200225
this._matrixDirty=true;
201226
return this;
202227
}
@@ -230,6 +255,9 @@ Transform.prototype.multOrientation=function(angle, v,vy,vz){
230255
* @return {Array<number>}
231256
*/
232257
Transform.prototype.getMatrix=function(){
258+
if(this._isIdentity){
259+
return GLMath.mat4identity();
260+
}
233261
if(this._matrixDirty){
234262
this._matrixDirty=false;
235263
// for best results, multiply in this order:

glutil.js

Lines changed: 61 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1802,9 +1802,15 @@ Scene3D.prototype.render=function(){
18021802

18031803
/** @private */
18041804
Scene3D.prototype._renderShape=function(shape,program){
1805+
if(shape.constructor==ShapeGroup){
1806+
for(var i=0;i<shape.shapes.length;i++){
1807+
this._renderShape(shape.shapes[i],program);
1808+
}
1809+
} else {
18051810
this._setupMatrices(shape,program);
18061811
Binders.getMaterialBinder(shape.material).bind(program);
18071812
shape.bufferedMesh.draw(program);
1813+
}
18081814
}
18091815
/**
18101816
* Uses a shader program to apply a texture filter after the
@@ -1865,6 +1871,20 @@ Scene3D.prototype._renderInner=function(){
18651871
return this;
18661872
}
18671873

1874+
function ShapeGroup(){
1875+
this.shapes=[];
1876+
this.transform=new Transform();
1877+
}
1878+
ShapeGroup.prototype.addShape=function(shape){
1879+
this.shapes.add(shape);
1880+
}
1881+
ShapeGroup.prototype.getTransform=function(){
1882+
return this.getTransform();
1883+
}
1884+
ShapeGroup.prototype.setTransform=function(transform){
1885+
this.transform=transform.copy();
1886+
return this;
1887+
}
18681888
/**
18691889
* An object that associates a geometric mesh (the shape of the object) with
18701890
* material data (which defines what is seen on the object's surface)
@@ -1881,6 +1901,7 @@ function Shape(mesh){
18811901
this.bufferedMesh=mesh;
18821902
this.transform=new Transform();
18831903
this.material=new Material();
1904+
this.parent=null;
18841905
}
18851906
/**
18861907
* Not documented yet.
@@ -1895,34 +1916,6 @@ Shape.prototype.primitiveCount=function(){
18951916
return (this.bufferedMesh) ? this.bufferedMesh.primitiveCount() : 0;
18961917
}
18971918

1898-
/**
1899-
* Makes a copy of this object. The copied object
1900-
* will have its own version of the transform and
1901-
* material data, but any texture
1902-
* image data and vertex buffers will not be duplicated,
1903-
* but rather just references to them will be used.
1904-
* @return {glutil.Shape} A copy of this object.
1905-
*/
1906-
Shape.prototype.copy=function(){
1907-
var ret=new Shape(this.bufferedMesh);
1908-
ret.material=this.material.copy();
1909-
ret.transform=this.transform.copy();
1910-
return ret;
1911-
}
1912-
1913-
1914-
/**
1915-
* Sets this shape's transformation matrix. This method
1916-
* will set the position, rotation, and scale properties
1917-
* accordingly to the matrix given.
1918-
* @param {Array<number>} value A 4x4 matrix.
1919-
* This method will copy the value of this parameter.
1920-
* @return {glutil.Shape} This object.
1921-
*/
1922-
Shape.prototype.setMatrix=function(value){
1923-
this.transform.setMatrix(value);
1924-
return this;
1925-
}
19261919
/**
19271920
* Sets material parameters that give the shape a certain color.
19281921
* However, if the mesh defines its own colors, those colors will take
@@ -1961,16 +1954,24 @@ Shape.prototype.setMaterial=function(material){
19611954
return this;
19621955
}
19631956
/**
1964-
* Resets this shape to the untransformed state.
1965-
* @return {glutil.Shape} This object.
1957+
* Makes a copy of this object. The copied object
1958+
* will have its own version of the transform and
1959+
* material data, but any texture
1960+
* image data and vertex buffers will not be duplicated,
1961+
* but rather just references to them will be used.
1962+
* @return {glutil.Shape} A copy of this object.
19661963
*/
1967-
Shape.prototype.resetTransform=function(){
1968-
this.matrix=GLMath.mat4identity();
1969-
this.position=[0,0,0];
1970-
this.scale=[1,1,1];
1971-
this.rotation=GLMath.quatIdentity();
1972-
this.complexMatrix=false;
1973-
this._matrixDirty=false;
1964+
Shape.prototype.copy=function(){
1965+
var ret=new Shape(this.bufferedMesh);
1966+
ret.material=this.material.copy();
1967+
ret.transform=this.getTransform().copy();
1968+
return ret;
1969+
}
1970+
Shape.prototype.getTransform=function(){
1971+
return this.transform;
1972+
}
1973+
Shape.prototype.setTransform=function(transform){
1974+
this.transform=transform.copy();
19741975
return this;
19751976
}
19761977
/**
@@ -1983,7 +1984,7 @@ Shape.prototype.resetTransform=function(){
19831984
* @return {glutil.Scene3D} This object.
19841985
*/
19851986
Shape.prototype.setScale=function(x,y,z){
1986-
this.transform.setScale(x,y,z);
1987+
this.getTransform().setScale(x,y,z);
19871988
return this;
19881989
}
19891990
/**
@@ -1996,7 +1997,7 @@ Shape.prototype.setScale=function(x,y,z){
19961997
* @return {glutil.Scene3D} This object.
19971998
*/
19981999
Shape.prototype.setPosition=function(x,y,z){
1999-
this.transform.setPosition(x,y,z);
2000+
this.getTransform().setPosition(x,y,z);
20002001
return this;
20012002
}
20022003
/**
@@ -2006,57 +2007,7 @@ Shape.prototype.setPosition=function(x,y,z){
20062007
* @return {glutil.Shape} This object.
20072008
*/
20082009
Shape.prototype.setQuaternion=function(quat){
2009-
this.transform.setQuaternion(quat);
2010-
return this;
2011-
}
2012-
/**
2013-
* Sets this object's orientation in the form of an angle and an axis of
2014-
* rotation.
2015-
* See {@link glutil.Transform.setOrientation}.
2016-
* @param {Array<number>|number} angle The desired angle
2017-
* to rotate in degrees, or a 4-element array as described in {@link glutil.Transform.setOrientation}.
2018-
* @param {Array<number>|number} v X-component of the axis
2019-
* of rotation or a 3-element array giving the axis
2020-
* of rotation in x, y, and z, respectively.
2021-
* @param {number} vy Y-component of the axis
2022-
* of rotation.
2023-
* @param {number} vz Z-component of the axis
2024-
* of rotation.
2025-
* @return {glutil.Shape} This object.
2026-
*/
2027-
Shape.prototype.setOrientation=function(angle, v,vy,vz){
2028-
this.transform.setOrientation(angle,v,vy,vz);
2029-
return this;
2030-
}
2031-
/**
2032-
* Combines this shape's current rotation with another rotation
2033-
* described by a [quaternion]{@link glmath.GLMath}.
2034-
* See {@link glutil.Transform.multQuaternion}.
2035-
* @param {Array<number>} quat A four-element array describing the rotation.
2036-
* @return {glutil.Shape} This object.
2037-
*/
2038-
Shape.prototype.multQuaternion=function(quat){
2039-
this.transform.multQuaternion(angle,v,vy,vz);
2040-
return this;
2041-
}
2042-
/**
2043-
* Combines this shape's current orientation with another orientation
2044-
* in the form of an angle and an axis of
2045-
* rotation.
2046-
* See {@link glutil.Transform.multOrientation}.
2047-
* @param {Array<number>|number} angle The desired angle
2048-
* to rotate in degrees, or a 4-element array as described in {@link glutil.Transform.setOrientation}.
2049-
* @param {Array<number>|number} v X-component of the axis
2050-
* of rotation or a 3-element array giving the axis
2051-
* of rotation in x, y, and z, respectively.
2052-
* @param {number} vy Y-component of the axis
2053-
* of rotation.
2054-
* @param {number} vz Z-component of the axis
2055-
* of rotation.
2056-
* @return {glutil.Shape} This object.
2057-
*/
2058-
Shape.prototype.multOrientation=function(angle, v,vy,vz){
2059-
this.transform.multOrientation(angle,v,vy,vz);
2010+
this.getTransform().setQuaternion(quat);
20602011
return this;
20612012
}
20622013
/**
@@ -2065,10 +2016,29 @@ Shape.prototype.multOrientation=function(angle, v,vy,vz){
20652016
* @return {Array<number>} The current transformation matrix.
20662017
*/
20672018
Shape.prototype.getMatrix=function(){
2068-
return this.transform.getMatrix();
2019+
return this.getTransform().getMatrix();
20692020
}
2021+
/////////////
2022+
// Deprecated methods
2023+
/** @deprecated Use Shape.getTransform().multQuaternion(...) instead. */
2024+
Shape.prototype.multQuaternion=function(x){
2025+
this.getTransform().multQuaternion(x);
2026+
return this;
2027+
}
2028+
/** @deprecated Use Shape.getTransform().multOrientation(...) instead. */
2029+
Shape.prototype.multRotation=function(a,b,c,d){
2030+
this.getTransform().multRotation(a,b,c,d);
2031+
return this;
2032+
}
2033+
/** @deprecated Use Shape.getTransform().setOrientation(...) instead. */
2034+
Shape.prototype.setRotation=function(a,b,c,d){
2035+
this.getTransform().setOrientation(a,b,c,d);
2036+
return this;
2037+
}
2038+
20702039
/////////////
20712040
exports["BufferedMesh"]=BufferedMesh;
2041+
exports["ShapeGroup"]=ShapeGroup;
20722042
exports["Lights"]=Lights;
20732043
exports["FrameBuffer"]=FrameBuffer;
20742044
exports["LightSource"]=LightSource;

0 commit comments

Comments
 (0)