Skip to content

Commit d747730

Browse files
committed
Add matrix methods to Scene3D; etc.
1 parent aa1c782 commit d747730

File tree

3 files changed

+82
-33
lines changed

3 files changed

+82
-33
lines changed

glutil-shaderprog.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,8 @@ return shader;
388388
* @return {glutil.ShaderProgram} The resulting shader program.
389389
*/
390390
ShaderProgram.makeEffect=function(context,functionCode){
391-
return new ShaderProgram(context, null,
391+
return new ShaderProgram(context,
392+
ShaderProgram.getBasicVertex(),
392393
ShaderProgram.makeEffectFragment(functionCode));
393394
}
394395
/**

glutil.js

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,7 @@ BufferedMesh.prototype.primitiveCount=function(){
11241124
function FrameBuffer(context, width, height){
11251125
if(width<0 || height<0)throw new Error("width or height negative");
11261126
this.context=context;
1127-
this.textureUnit=0;
1127+
this.textureUnit=1;
11281128
this.buffer=context.createFramebuffer();
11291129
// create color texture
11301130
this.colorTexture = context.createTexture();
@@ -1185,15 +1185,28 @@ FrameBuffer.prototype.bind=function(program){
11851185
if(program.getContext()!=this.context){
11861186
throw new Error("can't bind buffer: context mismatch");
11871187
}
1188-
this.context.bindFramebuffer(
1188+
this.context.activeTexture(this.context.TEXTURE0+this.textureUnit);
1189+
this.context.framebufferTexture2D(
1190+
this.context.FRAMEBUFFER,this.context.COLOR_ATTACHMENT0,
1191+
this.context.TEXTURE_2D,this.colorTexture,0);
1192+
this.context.framebufferRenderbuffer(
1193+
this.context.FRAMEBUFFER,this.context.DEPTH_ATTACHMENT,
1194+
this.context.RENDERBUFFER,this.depthbuffer);
1195+
this.context.bindFramebuffer(
11891196
this.context.FRAMEBUFFER,this.buffer);
11901197
}
11911198
/**
1192-
* Unbinds this frame buffer from its associated WebGL context.
1199+
* Unbinds this frame buffer from its associated WebGL this.context.
11931200
*/
11941201
FrameBuffer.prototype.unbind=function(){
1195-
this.context.bindFramebuffer(
1202+
this.context.bindFramebuffer(
11961203
this.context.FRAMEBUFFER,null);
1204+
this.context.framebufferTexture2D(
1205+
this.context.FRAMEBUFFER,this.context.COLOR_ATTACHMENT0,
1206+
this.context.TEXTURE_2D,0,0);
1207+
this.context.framebufferRenderbuffer(
1208+
this.context.FRAMEBUFFER,this.context.DEPTH_ATTACHMENT,
1209+
this.context.RENDERBUFFER,0);
11971210
}
11981211
/**
11991212
* Disposes all resources from this frame buffer object.
@@ -1370,7 +1383,18 @@ Scene3D.prototype.createBuffer=function(){
13701383
return new FrameBuffer(this.context,
13711384
this.getWidth(),this.getHeight());
13721385
}
1373-
1386+
/**
1387+
* Not documented yet.
1388+
*/
1389+
Scene3D.prototype.getProjectionMatrix=function(){
1390+
return this._projectionMatrix.slice(0,16);
1391+
}
1392+
/**
1393+
* Not documented yet.
1394+
*/
1395+
Scene3D.prototype.getViewMatrix=function(){
1396+
return this._viewMatrix.slice(0,16);
1397+
}
13741398
/**
13751399
* Sets this scene's projection matrix to a perspective projection.
13761400
* <p>
@@ -1661,7 +1685,7 @@ Scene3D.prototype.setLookAt=function(eye, center, up){
16611685
/**
16621686
* Adds a 3D shape to this scene. Its reference, not a copy,
16631687
* will be stored in the 3D scene's list of shapes.
1664-
* @param {Shape|MultiShape} shape A 3D shape.
1688+
* @param {Shape|ShapeGroup} shape A 3D shape.
16651689
* @return {glutil.Scene3D} This object.
16661690
*/
16671691
Scene3D.prototype.addShape=function(shape){
@@ -1683,7 +1707,7 @@ Scene3D.prototype.makeShape=function(mesh){
16831707

16841708
/**
16851709
* Removes all instances of a 3D shape from this scene.
1686-
* @param {Shape|MultiShape} shape The 3D shape to remove.
1710+
* @param {Shape|ShapeGroup} shape The 3D shape to remove.
16871711
* @return {glutil.Scene3D} This object.
16881712
*/
16891713
Scene3D.prototype.removeShape=function(shape){
@@ -1779,6 +1803,7 @@ Scene3D.prototype._setupMatrices=function(shape,program){
17791803
uniforms["normalMatrix"]=invTrans;
17801804
program.setUniforms(uniforms);
17811805
}
1806+
17821807
/**
17831808
* Renders all shapes added to this scene.
17841809
* This is usually called in a render loop, such
@@ -1797,8 +1822,8 @@ Scene3D.prototype.render=function(){
17971822
// Render to the framebuffer, then to the main buffer via
17981823
// a filter
17991824
var oldProgram=this.program;
1800-
var oldProj=this._projectionMatrix.slice(0,16);
1801-
var oldView=this._viewMatrix.slice(0,16);
1825+
var oldProj=this.getProjectionMatrix();
1826+
var oldView=this.getViewMatrix();
18021827
this.fbo.bind(this.program);
18031828
this._renderInner();
18041829
this.fbo.unbind();
@@ -1903,9 +1928,11 @@ function ShapeGroup(){
19031928
this.transform=new Transform();
19041929
}
19051930
/**
1906-
* Not documented yet.
1907-
* @param {*} shape
1908-
*/
1931+
* Adds a 3D shape to this shape group. Its reference, not a copy,
1932+
* will be stored in the list of shapes.
1933+
* @param {Shape|ShapeGroup} shape A 3D shape.
1934+
* @return {glutil.ShapeGroup} This object.
1935+
*/
19091936
ShapeGroup.prototype.addShape=function(shape){
19101937
shape.parent=this;
19111938
this.shapes.push(shape);
@@ -1916,6 +1943,9 @@ ShapeGroup.prototype.addShape=function(shape){
19161943
ShapeGroup.prototype.getTransform=function(){
19171944
return this.transform;
19181945
}
1946+
/**
1947+
* Not documented yet.
1948+
*/
19191949
ShapeGroup.prototype.getMatrix=function(){
19201950
return this.getTransform().getMatrix();
19211951
}
@@ -1927,14 +1957,30 @@ ShapeGroup.prototype.setTransform=function(transform){
19271957
this.transform=transform.copy();
19281958
return this;
19291959
}
1960+
/**
1961+
* Not documented yet.
1962+
* @param {*} x
1963+
* @param {*} y
1964+
* @param {*} z
1965+
*/
19301966
ShapeGroup.prototype.setPosition=function(x,y,z){
19311967
this.transform.setPosition(x,y,z)
19321968
return this;
19331969
}
1970+
/**
1971+
* Not documented yet.
1972+
* @param {*} quat
1973+
*/
19341974
ShapeGroup.prototype.setQuaternion=function(quat){
19351975
this.transform.setQuaternion(quat);
19361976
return this;
19371977
}
1978+
/**
1979+
* Not documented yet.
1980+
* @param {*} x
1981+
* @param {*} y
1982+
* @param {*} z
1983+
*/
19381984
ShapeGroup.prototype.setScale=function(x,y,z){
19391985
this.transform.setScale(x,y,z);
19401986
return this;

0 commit comments

Comments
 (0)