Permalink
Browse files

Fix unnecessary matrix creation and improper frustum class name

  • Loading branch information...
1 parent 6217c88 commit a5c36c93e4dd066a886f7b9936d47b1111465e32 unknown committed Mar 14, 2011
Showing with 52 additions and 49 deletions.
  1. +2 −2 c3dl-dev/tests/frustum culling/fc.js
  2. +40 −40 c3dl/frustum_culling/frustum.js
  3. +1 −1 c3dl/frustum_culling/plane.js
  4. +1 −1 c3dl/math/matrix.js
  5. +8 −5 c3dl/scene.js
@@ -23,7 +23,7 @@ cam[5].setPosition([-50, 0, 0]);
cam[5].setLookAtPoint([0.0, 0.0, 0.0]);
var start = +new Date();
var currentCam = 0;
-
+var frustumCulling = new c3dl.Frustum();
function canvasMain(canvasName) {
scn = new c3dl.Scene();
scn.setCanvasTag(canvasName);
@@ -53,7 +53,7 @@ function test() {
var projMatrix = camera.getProjectionMatrix();
var viewMatrix = camera.getViewMatrix();
var frustumMatrix = c3dl.multiplyMatrixByMatrix(projMatrix, viewMatrix);
- var frustumCulling = new Frustum(frustumMatrix);
+ frustumCulling.init(frustumMatrix);
//Culling using spheres
var boundingSpheres = obj.getBoundingVolumes();
for (var j = 0; j < boundingSpheres.length; j++) {
@@ -1,44 +1,44 @@
-function Frustum(frustumMatrix) {
+c3dl.Frustum = function () {
this.frustumPlane = [];
- //right
- this.frustumPlane[0] = new Plane();
- this.frustumPlane[0].normal[0]=frustumMatrix[3]-frustumMatrix[0];
- this.frustumPlane[0].normal[1]=frustumMatrix[7]-frustumMatrix[4];
- this.frustumPlane[0].normal[2]=frustumMatrix[11]-frustumMatrix[8];
- this.frustumPlane[0].offset=frustumMatrix[15]-frustumMatrix[12];
- //left
- this.frustumPlane[1] = new Plane();
- this.frustumPlane[1].normal[0]=frustumMatrix[3]+frustumMatrix[0];
- this.frustumPlane[1].normal[1]=frustumMatrix[7]+frustumMatrix[4];
- this.frustumPlane[1].normal[2]=frustumMatrix[11]+frustumMatrix[8];
- this.frustumPlane[1].offset=frustumMatrix[15]+frustumMatrix[12];
- //bottom
- this.frustumPlane[2] = new Plane();
- this.frustumPlane[2].normal[0]=frustumMatrix[3]+frustumMatrix[1];
- this.frustumPlane[2].normal[1]=frustumMatrix[7]+frustumMatrix[5];
- this.frustumPlane[2].normal[2]=frustumMatrix[11]+frustumMatrix[9];
- this.frustumPlane[2].offset=frustumMatrix[15]+frustumMatrix[13];
- //top
- this.frustumPlane[3] = new Plane();
- this.frustumPlane[3].normal[0]=frustumMatrix[3]-frustumMatrix[1];
- this.frustumPlane[3].normal[1]=frustumMatrix[7]-frustumMatrix[5];
- this.frustumPlane[3].normal[2]=frustumMatrix[11]-frustumMatrix[9];
- this.frustumPlane[3].offset=frustumMatrix[15]-frustumMatrix[13];
- //far
- this.frustumPlane[4] = new Plane();
- this.frustumPlane[4].normal[0]=frustumMatrix[3]-frustumMatrix[2];
- this.frustumPlane[4].normal[1]=frustumMatrix[7]-frustumMatrix[6];
- this.frustumPlane[4].normal[2]=frustumMatrix[11]-frustumMatrix[10];
- this.frustumPlane[4].offset=frustumMatrix[15]-frustumMatrix[14] ;
- //near
- this.frustumPlane[5] = new Plane();
- this.frustumPlane[5].normal[0]=frustumMatrix[3]+frustumMatrix[2];
- this.frustumPlane[5].normal[1]=frustumMatrix[7]+frustumMatrix[6];
- this.frustumPlane[5].normal[2]=frustumMatrix[11]+frustumMatrix[10];
- this.frustumPlane[5].offset=frustumMatrix[15]+frustumMatrix[14];
- for(var j=0; j<6; j++) {
- this.frustumPlane[j].normalize();
- }
+ for (var i = 0; i < 6; i++) {
+ this.frustumPlane[i] = new c3dl.Plane();
+ }
+
+ this.init = function(frustumMatrix) {
+ //right
+ this.frustumPlane[0].normal[0]=frustumMatrix[3]-frustumMatrix[0];
+ this.frustumPlane[0].normal[1]=frustumMatrix[7]-frustumMatrix[4];
+ this.frustumPlane[0].normal[2]=frustumMatrix[11]-frustumMatrix[8];
+ this.frustumPlane[0].offset=frustumMatrix[15]-frustumMatrix[12];
+ //left
+ this.frustumPlane[1].normal[0]=frustumMatrix[3]+frustumMatrix[0];
+ this.frustumPlane[1].normal[1]=frustumMatrix[7]+frustumMatrix[4];
+ this.frustumPlane[1].normal[2]=frustumMatrix[11]+frustumMatrix[8];
+ this.frustumPlane[1].offset=frustumMatrix[15]+frustumMatrix[12];
+ //bottom
+ this.frustumPlane[2].normal[0]=frustumMatrix[3]+frustumMatrix[1];
+ this.frustumPlane[2].normal[1]=frustumMatrix[7]+frustumMatrix[5];
+ this.frustumPlane[2].normal[2]=frustumMatrix[11]+frustumMatrix[9];
+ this.frustumPlane[2].offset=frustumMatrix[15]+frustumMatrix[13];
+ //top
+ this.frustumPlane[3].normal[0]=frustumMatrix[3]-frustumMatrix[1];
+ this.frustumPlane[3].normal[1]=frustumMatrix[7]-frustumMatrix[5];
+ this.frustumPlane[3].normal[2]=frustumMatrix[11]-frustumMatrix[9];
+ this.frustumPlane[3].offset=frustumMatrix[15]-frustumMatrix[13];
+ //far
+ this.frustumPlane[4].normal[0]=frustumMatrix[3]-frustumMatrix[2];
+ this.frustumPlane[4].normal[1]=frustumMatrix[7]-frustumMatrix[6];
+ this.frustumPlane[4].normal[2]=frustumMatrix[11]-frustumMatrix[10];
+ this.frustumPlane[4].offset=frustumMatrix[15]-frustumMatrix[14] ;
+ //near
+ this.frustumPlane[5].normal[0]=frustumMatrix[3]+frustumMatrix[2];
+ this.frustumPlane[5].normal[1]=frustumMatrix[7]+frustumMatrix[6];
+ this.frustumPlane[5].normal[2]=frustumMatrix[11]+frustumMatrix[10];
+ this.frustumPlane[5].offset=frustumMatrix[15]+frustumMatrix[14];
+ for(var j=0; j<6; j++) {
+ this.frustumPlane[j].normalize();
+ }
+ }
this.sphereInFrustum = function(boundingSphere) {
for(var i = 0; i < 6; i++) {
var pos = boundingSphere.getPosition();
@@ -1,4 +1,4 @@
-function Plane() {
+c3dl.Plane = function () {
this.normal = new C3DL_FLOAT_ARRAY(3);
this.offset = null;
this.init = function(normal, offset) {
View
@@ -310,7 +310,7 @@ c3dl.inverseMatrix = function (mat) {
if (!mat) {
return
}
- var kInv = new C3DL_FLOAT_ARRAY(16);
+ var kInv = c3dl.mat1;
var fA0 = mat[ 0] * mat[ 5] - mat[ 1] * mat[ 4];
var fA1 = mat[ 0] * mat[ 6] - mat[ 2] * mat[ 4];
var fA2 = mat[ 0] * mat[ 7] - mat[ 3] * mat[ 4];
View
@@ -79,6 +79,7 @@ c3dl.Scene = function ()
var textureQueue = [];
var pointPositions = null;
//type of culling
+ var frustumCulling = new c3dl.Frustum();
var culling = "BoundingSphere"
//Collision
var collision = false;
@@ -956,12 +957,15 @@ c3dl.Scene = function ()
{
// calculate FPS.
// we update the FPS after a second or more has elapsed.
- if (Date.now() - FPS_LastTimeTaken >= 1000)
+ var sec = (new Date().getTime() - FPS_LastTimeTaken) / 1000;
+ FPS_Counter++;
+ var fps = FPS_Counter / sec;
+ if (sec > 0.5)
{
// frames / seconds
- FPS = FPS_Counter / (Date.now() - FPS_LastTimeTaken) * 1000;
+ FPS = fps;
FPS_Counter = 0;
- FPS_LastTimeTaken = Date.now();
+ FPS_LastTimeTaken = new Date().getTime();
}
// If a user wants to stop rendering, this is where it happens
@@ -1023,7 +1027,6 @@ c3dl.Scene = function ()
// the front and back buffers.
//renderer.swapBuffers();
numFramesSinceSceneStart++;
- FPS_Counter++;
}
this.refresh = function() {
@@ -1188,7 +1191,7 @@ c3dl.Scene = function ()
var projMatrix = cam.projMatrix;
var viewMatrix = cam.viewMatrix;
c3dl.multiplyMatrixByMatrix(projMatrix,viewMatrix, c3dl.mat1);
- var frustumCulling = new Frustum(c3dl.mat1);
+ frustumCulling.init(c3dl.mat1);
var boundingVolume = objList[i].getBoundingVolume();
//Culling using spheres
if (culling === "BoundingSphere") {

0 comments on commit a5c36c9

Please sign in to comment.