Skip to content

Commit 717cf1a

Browse files
committed
add primitive counter; fix mat4orthoAspect bugs
1 parent 58b0d94 commit 717cf1a

File tree

6 files changed

+122
-77
lines changed

6 files changed

+122
-77
lines changed

frame.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,22 @@ FrameCounter.prototype.update=function(){
3333
FrameCounter.prototype.getFPS=function(){
3434
return this.fps;
3535
}
36+
3637
function FrameCounterDiv(scene){
3738
var canvas=scene.getContext().canvas
39+
this.div=FrameCounterDiv._makeDiv(canvas);
40+
this.count=0;
41+
this.scene=scene;
42+
this.fc=new FrameCounter();
43+
}
44+
FrameCounterDiv._makeDiv=function(element){
3845
var div=document.createElement("div")
3946
div.style.backgroundColor="white"
4047
div.style.position="absolute"
41-
div.style.left=canvas.offsetLeft+"px"
42-
div.style.top=canvas.offsetTop+"px"
48+
div.style.left=element.offsetLeft+"px"
49+
div.style.top=element.offsetTop+"px"
4350
document.body.appendChild(div)
44-
this.div=div;
45-
this.count=0;
46-
this.fc=new FrameCounter();
51+
return div
4752
}
4853
FrameCounterDiv.prototype.update=function(){
4954
this.fc.update();
@@ -58,3 +63,15 @@ FrameCounterDiv.prototype.update=function(){
5863
}
5964
}
6065
}
66+
67+
function PrimitiveCounter(scene){
68+
var canvas=scene.getContext().canvas
69+
this.div=FrameCounterDiv._makeDiv(canvas);
70+
this.count=0;
71+
this.scene=scene;
72+
}
73+
PrimitiveCounter.prototype.update=function(){
74+
var v=this.scene.vertexCount();
75+
var p=this.scene.primitiveCount();
76+
this.div.innerHTML=v+" vertices, "+p+" primitives"
77+
}

glmath.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,14 +1280,14 @@ mat4ortho2dAspect:function(l,r,b,t,aspect){
12801280
* @return {Array<number>} The resulting 4x4 matrix.
12811281
*/
12821282
mat4orthoAspect:function(l,r,b,t,n,f,aspect){
1283-
var xdist=Math.abs(right-left);
1284-
var ydist=Math.abs(top-bottom);
1283+
var xdist=Math.abs(r-l);
1284+
var ydist=Math.abs(t-b);
12851285
var boxAspect=xdist/ydist;
12861286
aspect/=boxAspect;
12871287
if(aspect<1){
1288-
return GLMath.mat4ortho(left,right,bottom/aspect,top/aspect,near,far);
1288+
return GLMath.mat4ortho(l,r,b/aspect,t/aspect,n,f);
12891289
} else {
1290-
return GLMath.mat4ortho(left*aspect,right*aspect,bottom,top,near,far);
1290+
return GLMath.mat4ortho(l*aspect,r*aspect,b,t,n,f);
12911291
}
12921292
},
12931293
/**

glutil-binders.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,24 +163,21 @@ TextureBinder.prototype.bind=function(program){
163163
// set magnification
164164
context.texParameteri(context.TEXTURE_2D,
165165
context.TEXTURE_MAG_FILTER, context.LINEAR);
166+
var wrapMode=context.CLAMP_TO_EDGE;
166167
if(GLUtil._isPowerOfTwo(texture.width) &&
167168
GLUtil._isPowerOfTwo(texture.height)){
168169
// Enable mipmaps if texture's dimensions are powers of two
170+
if(!texture.clamp)wrapMode=context.REPEAT;
169171
context.texParameteri(context.TEXTURE_2D,
170172
context.TEXTURE_MIN_FILTER, context.LINEAR_MIPMAP_LINEAR);
171-
context.texParameteri(context.TEXTURE_2D,
172-
context.TEXTURE_WRAP_S, context.REPEAT);
173-
context.texParameteri(context.TEXTURE_2D,
174-
context.TEXTURE_WRAP_T, context.REPEAT);
175173
} else {
176174
context.texParameteri(context.TEXTURE_2D,
177175
context.TEXTURE_MIN_FILTER, context.LINEAR);
178-
// Other textures require this wrap mode
179-
context.texParameteri(context.TEXTURE_2D,
180-
context.TEXTURE_WRAP_S, context.CLAMP_TO_EDGE);
181-
context.texParameteri(context.TEXTURE_2D,
182-
context.TEXTURE_WRAP_T, context.CLAMP_TO_EDGE);
183176
}
177+
context.texParameteri(context.TEXTURE_2D,
178+
context.TEXTURE_WRAP_S, wrapMode);
179+
context.texParameteri(context.TEXTURE_2D,
180+
context.TEXTURE_WRAP_T, wrapMode);
184181
}
185182
}
186183

glutil.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,9 +826,25 @@ var Texture=function(name){
826826
this.loadedTexture=null;
827827
this.name=name;
828828
this.width=0;
829+
this.clamp=false;
829830
this.height=0;
830831
}
831832

833+
/**
834+
* Sets the wrapping behavior of texture coordinates that
835+
* fall out of range when using this texture.
836+
* @param {boolean} clamp If true, the image's texture
837+
* coordinates will be clamped to the range [0, 1]. If false,
838+
* the image's texture coordinates' fractional parts will
839+
* be used as the coordinates (causing wraparound).
840+
* The default is false.
841+
* @return {glutil.Texture} This object.
842+
*/
843+
Texture.prototype.setClamp=function(clamp){
844+
this.clamp=clamp;
845+
return this;
846+
}
847+
832848
/**
833849
* Loads a texture by its URL.
834850
* @param {string} name URL of the texture data. Images with a TGA
@@ -861,6 +877,18 @@ Texture.loadTexture=function(name, textureCache){
861877
});
862878
}
863879

880+
Texture.fromUint8Array=function(array, width, height){
881+
if(width<0)throw new Error("width less than 0")
882+
if(height<0)throw new Error("height less than 0")
883+
if(array.length<width*height*4)throw new Error("array too short for texture")
884+
var texImage=new Texture("")
885+
texImage.image=array;
886+
texImage.width=Math.ceil(width);
887+
texImage.height=Math.ceil(height);
888+
texImage.loadStatus=2;
889+
return texImage;
890+
}
891+
864892
/** @private */
865893
Texture.loadTga=function(name){
866894
var tex=this;

0 commit comments

Comments
 (0)