Skip to content

Commit e0159ae

Browse files
author
Evan Wallace
committed
bounding sphere and aabb calculations for mesh, a few more math functions
1 parent 2222938 commit e0159ae

File tree

4 files changed

+49
-8
lines changed

4 files changed

+49
-8
lines changed

src/matrix.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,24 @@ Matrix.prototype.multiply = function(matrix) {
6464
);
6565
};
6666

67+
Matrix.prototype.transformPoint = function(v) {
68+
var m = this.m;
69+
return new Vector(
70+
m[0] * v.x + m[1] * v.y + m[2] * v.z + m[3],
71+
m[4] * v.x + m[5] * v.y + m[6] * v.z + m[7],
72+
m[8] * v.x + m[9] * v.y + m[10] * v.z + m[11]
73+
).divide(m[12] * v.x + m[13] * v.y + m[14] * v.z + m[15]);
74+
};
75+
76+
Matrix.prototype.transformVector = function(v) {
77+
var m = this.m;
78+
return new Vector(
79+
m[0] * v.x + m[1] * v.y + m[2] * v.z,
80+
m[4] * v.x + m[5] * v.y + m[6] * v.z,
81+
m[8] * v.x + m[9] * v.y + m[10] * v.z
82+
);
83+
};
84+
6785
Matrix.perspective = function(fov, aspect, near, far) {
6886
var y = Math.tan(fov * Math.PI / 360) * near;
6987
var x = y * aspect;

src/mesh.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,26 @@ Mesh.prototype.compile = function() {
5656
this.indexBuffer.compile();
5757
};
5858

59+
Mesh.prototype.computeAABB = function() {
60+
var aabb = { min: new Vector(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE) };
61+
aabb.max = aabb.min.negative();
62+
for (var i = 0; i < this.vertices.length; i++) {
63+
var v = this.vertices[i];
64+
aabb.min = aabb.min.min(v);
65+
aabb.max = aabb.max.max(v);
66+
}
67+
return aabb;
68+
};
69+
70+
Mesh.prototype.computeBoundingSphere = function() {
71+
var aabb = this.computeAABB();
72+
var sphere = { center: aabb.min.add(aabb.max).divide(2), radius: 0 };
73+
for (var i = 0; i < this.vertices.length; i++) {
74+
sphere.radius = Math.max(sphere.radius, this.vertices[i].subtract(sphere.center).length());
75+
}
76+
return sphere;
77+
};
78+
5979
Mesh.plane = function(sizeX, sizeY, countX, countY, options) {
6080
var mesh = new Mesh(options);
6181
for (var y = 0; y <= countY; y++) {
@@ -64,7 +84,7 @@ Mesh.plane = function(sizeX, sizeY, countX, countY, options) {
6484
var s = x / countX;
6585
mesh.vertices.push(new Vector((s - 0.5) * sizeX, (t - 0.5) * sizeY, 0));
6686
if (mesh.coords) mesh.coords.push(new Vector(s, t));
67-
if (mesh.normals) mesh.normals.push(new Vector(0, 1, 0));
87+
if (mesh.normals) mesh.normals.push(new Vector(0, 0, 1));
6888
}
6989
}
7090
for (var y = 0; y < countY; y++) {

src/texture.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ Texture = function(width, height, options) {
33
this.id = gl.createTexture();
44
this.width = width;
55
this.height = height;
6+
this.format = options.format || gl.RGBA;
67
gl.bindTexture(gl.TEXTURE_2D, this.id);
78
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1);
8-
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, options.magFilter || gl.LINEAR);
9-
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, options.minFilter || gl.LINEAR);
9+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, options.filter || options.magFilter || gl.LINEAR);
10+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, options.filter || options.minFilter || gl.LINEAR);
1011
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, options.wrap || options.wrapS || gl.CLAMP_TO_EDGE);
1112
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, options.wrap || options.wrapT || gl.CLAMP_TO_EDGE);
12-
var format = options.format || gl.RGBA;
13-
gl.texImage2D(gl.TEXTURE_2D, 0, format, width, height, 0, format, options.type || gl.UNSIGNED_BYTE, null);
13+
gl.texImage2D(gl.TEXTURE_2D, 0, this.format, width, height, 0, this.format, options.type || gl.UNSIGNED_BYTE, null);
1414
};
1515

1616
Texture.prototype.bind = function(unit) {
@@ -56,8 +56,8 @@ Texture.prototype.swapWith = function(other) {
5656
temp = other.height; other.height = this.height; this.height = temp;
5757
};
5858

59-
Texture.fromImage = function(image) {
60-
var texture = new Texture(image.width, image.height);
61-
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
59+
Texture.fromImage = function(image, options) {
60+
var texture = new Texture(image.width, image.height, options);
61+
gl.texImage2D(gl.TEXTURE_2D, 0, texture.format, texture.format, gl.UNSIGNED_BYTE, image);
6262
return texture;
6363
};

src/vector.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Vector = function(x, y, z) {
44
this.z = z || 0;
55
};
66

7+
Vector.prototype.negative = function() { return new Vector(-this.x, -this.y, -this.z); };
78
Vector.prototype.add = function(v) { return new Vector(this.x + v.x, this.y + v.y, this.z + v.z); };
89
Vector.prototype.subtract = function(v) { return new Vector(this.x - v.x, this.y - v.y, this.z - v.z); };
910
Vector.prototype.multiply = function(n) { return new Vector(this.x * n, this.y * n, this.z * n); };
@@ -12,3 +13,5 @@ Vector.prototype.dot = function(v) { return this.x * v.x + this.y * v.y + this.z
1213
Vector.prototype.cross = function(v) { return new Vector(this.y * v.z - this.z * v.y, this.z * v.x - this.x * v.z, this.x * v.y - this.y * v.x); };
1314
Vector.prototype.length = function() { return Math.sqrt(this.dot(this)); };
1415
Vector.prototype.unit = function() { return this.divide(this.length()); };
16+
Vector.prototype.min = function(v) { return new Vector(Math.min(this.x, v.x), Math.min(this.y, v.y), Math.min(this.z, v.z)); };
17+
Vector.prototype.max = function(v) { return new Vector(Math.max(this.x, v.x), Math.max(this.y, v.y), Math.max(this.z, v.z)); };

0 commit comments

Comments
 (0)