Advertisement
BatBlaster

WolfieGL Frustum

Oct 23rd, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     WolfieGL_Frustum.js
  3.     by Richard McKenna
  4.     ©2016
  5.  
  6.     The WolfieGL_Frustum JavaScript object manages a 3D persepctive frustum such that we may perform frustum culling. Note that this object also stores and updates the projection matrix as needed.
  7. */
  8. function WolfieGL_Frustum(canvasWidth, canvasHeight) {
  9.     // THESE ARE THE DEFAULT VALUES FOR OUR PROJECTION MATRIX
  10.     this.fieldOfView = 45 * 180 / Math.PI;
  11.     this.nearClippingPlane = 0.1;
  12.     this.farClippingPlane = 1000.0;
  13.     this.aspectRatio = canvasWidth/canvasHeight;
  14.     this.fp_ftr = [this.nearClippingPlane,canvasHeight/2,canvasWidth/2];
  15.     this.fp_fbr = [this.nearClippingPlane,-1*(canvasHeight/2),canvasWidth/2];
  16.     this.fp_ftl = [this.nearClippingPlane,canvasHeight/2,-1*(canvasWidth/2)];
  17.     this.fp_fbl = [this.nearClippingPlane,-1*(canvasHeight/2),-1*(canvasWidth/2)];
  18.     this.fp_btr = [this.farClippingPlane,canvasHeight/2,canvasWidth/2];
  19.     this.fp_bbr = [this.farClippingPlane,-1*(canvasHeight/2),canvasWidth/2];
  20.     this.fp_btl = [this.farClippingPlane,canvasHeight/2,-1*(canvasWidth/2)];
  21.     this.fp_bbl = [this.farClippingPlane,-1*(canvasHeight/2),-1*(canvasWidth/2)];
  22.     var tempcordx = this.fp_ftr[0]-this.fp_ftl[0];
  23.     var tempcordy = this.fp_ftr[1]-this.fp_ftl[1];
  24.     var tempcordz = this.fp_ftr[2]-this.fp_ftl[2];
  25.     var npvec1 = loadVector(tempcordx,tempcordy,tempcordz);
  26.     tempcordx = this.fp_ftr[0]-this.fp_fbr[0];
  27.     tempcordy = this.fp_ftr[1]-this.fp_fbr[1];
  28.     tempcordz = this.fp_ftr[2]-this.fp_fbr[2];
  29.     var npvec2 = loadVector(tempcordx,tempcordy,tempcordz);
  30.     vec3.normalize(npvec1);
  31.     vec3.normalize(npvec2);
  32.     var nearNorm = vec3.cross(npvec2,npvec1);
  33.     console.log("near:"+nearNorm);
  34.     tempcordx = this.fp_btr[0]-this.fp_btl[0];
  35.     tempcordy = this.fp_btr[1]-this.fp_btl[1];
  36.     tempcordz = this.fp_btr[2]-this.fp_btl[2];
  37.     var fpvec1 = loadVector(tempcordx,tempcordy,tempcordz);
  38.     tempcordx = this.fp_btr[0]-this.fp_bbr[0];
  39.     tempcordy = this.fp_btr[1]-this.fp_bbr[1];
  40.     tempcordz = this.fp_btr[2]-this.fp_bbr[2];
  41.     var fpvec2 = loadVector(tempcordx,tempcordy,tempcordz);
  42.     vec3.normalize(fpvec1);
  43.     vec3.normalize(fpvec2);
  44.     var farNorm = vec3.cross(fpvec1,fpvec2);
  45.     console.log("far:"+farNorm);    
  46.     tempcordx = this.fp_btl[0]-this.fp_ftl[0];
  47.     tempcordy = this.fp_btl[1]-this.fp_ftl[1];
  48.     tempcordz = this.fp_btl[2]-this.fp_ftl[2];
  49.     var lpvec1 = loadVector(tempcordx,tempcordy,tempcordz);
  50.     tempcordx = this.fp_fbl[0]-this.fp_ftl[0];
  51.     tempcordy = this.fp_fbl[1]-this.fp_ftl[1];
  52.     tempcordz = this.fp_fbl[2]-this.fp_ftl[2];
  53.     var lpvec2 = loadVector(tempcordx,tempcordy,tempcordz);
  54.     vec3.normalize(lpvec1);
  55.     vec3.normalize(lpvec2);
  56.     var leftNorm = vec3.cross(lpvec2,lpvec1);
  57.     console.log("left:"+leftNorm);
  58.     tempcordx = this.fp_btr[0]-this.fp_ftr[0];
  59.     tempcordy = this.fp_btr[1]-this.fp_ftr[1];
  60.     tempcordz = this.fp_btr[2]-this.fp_ftr[2];
  61.     var rpvec1 = loadVector(tempcordx,tempcordy,tempcordz);
  62.     tempcordx = this.fp_fbr[0]-this.fp_ftr[0];
  63.     tempcordy = this.fp_fbr[1]-this.fp_ftr[1];
  64.     tempcordz = this.fp_fbr[2]-this.fp_ftr[2];
  65.     var rpvec2 = loadVector(tempcordx,tempcordy,tempcordz);
  66.     vec3.normalize(rpvec1);
  67.     vec3.normalize(rpvec2);
  68.     var rightNorm = vec3.cross(rpvec1,rpvec2);
  69.     console.log("right:"+rightNorm);
  70.     tempcordx = this.fp_btr[0]-this.fp_btl[0];
  71.     tempcordy = this.fp_btr[1]-this.fp_btl[1];
  72.     tempcordz = this.fp_btr[2]-this.fp_btl[2];
  73.     var tpvec1 = loadVector(tempcordx,tempcordy,tempcordz);
  74.     tempcordx = this.fp_ftl[0]-this.fp_btl[0];
  75.     tempcordy = this.fp_ftl[1]-this.fp_btl[1];
  76.     tempcordz = this.fp_ftl[2]-this.fp_btl[2];
  77.     var tpvec2 = loadVector(tempcordx,tempcordy,tempcordz);
  78.     vec3.normalize(tpvec1);
  79.     vec3.normalize(tpvec2);
  80.     var topNorm = vec3.cross(tpvec1,tpvec2);
  81.     console.log("top:"+topNorm);
  82.     tempcordx = this.fp_bbr[0]-this.fp_bbl[0];
  83.     tempcordy = this.fp_bbr[1]-this.fp_bbl[1];
  84.     tempcordz = this.fp_bbr[2]-this.fp_bbl[2];
  85.     var bpvec1 = loadVector(tempcordx,tempcordy,tempcordz);
  86.     tempcordx = this.fp_fbl[0]-this.fp_bbl[0];
  87.     tempcordy = this.fp_fbl[1]-this.fp_bbl[1];
  88.     tempcordz = this.fp_fbl[2]-this.fp_bbl[2];
  89.     var bpvec2 = loadVector(tempcordx,tempcordy,tempcordz);
  90.     vec3.normalize(bpvec1);
  91.     vec3.normalize(bpvec2);
  92.     var botNorm = vec3.cross(bpvec2,bpvec1);
  93.     console.log("bottom:"+botNorm);
  94.     // MAKE THE MATRIX
  95.     this.projectionMatrix = mat4.create();
  96.  
  97.     /*
  98.         This function rebuilds the projection matrix using the current frustum values. It should be called after ever time any of the fieldOfView, near or far clipping planes, or aspectRatio values change.
  99.      */
  100.     this.updateProjectionMatrix = function() {
  101.         mat4.perspective(this.fieldOfView, this.aspectRatio, this.nearClippingPlane, this.farClippingPlane, this.projectionMatrix);
  102.     }
  103.  
  104.     // THIS MATRIX WILL BE USED TO MOVE ALL MODELS FROM SCREEN SPACE TO CLIP SPACE
  105.     this.updateProjectionMatrix();
  106.  
  107.     // DURING FRUSTUM CULLING WE'LL LOAD THIS FULL OF ALL THE MODELS IN THE FRUSTUM
  108.     this.modelsInFrustum = new Array();
  109.  
  110.     /*
  111.         This function goes through all the models in the modelsToTest array and tests each one to see if it is inside the frustum. If it is, it adds it to the modelsInFrustum array.
  112.     */
  113.     this.cull = function(modelsToTest) {
  114.         for (var key in modelsToTest) {
  115.             var model = modelsToTest[key];
  116.             if (this.isModelInFrustum(model)) {
  117.                 this.modelsInFrustum[key] = model;
  118.             }
  119.         }
  120.         return this.modelsInFrustum;
  121.     }
  122.  
  123.     /*
  124.         This function tests if the testModel argument is inside the viewing frustum. If it is, it returns true, else it returns false.
  125.     */
  126.     this.isModelInFrustum = function(testModel) {
  127.         // @todo perform frustum culling here
  128.         var pos = loadVector(testModel.position[0],testModel.position[1],testModel.position[2]);
  129.         var dist = [checkDist(pos,farNorm,1000.0),checkDist(pos,nearNorm,0.1),checkDist(pos,leftNorm,0),checkDist(pos,rightNorm,0),checkDist(pos,topNorm,0),checkDist(pos,botNorm,0)];
  130.         if (dist[0]<0||dist[1]<0||dist[2]<0||dist[3]<0||dist[4]<0||dist[5]<0)
  131.         {
  132.             console.log(testModel);
  133.             console.log(dist);
  134.             return false;
  135.         }
  136.         else
  137.         {
  138.         return true;
  139.     }
  140.     }
  141.    
  142. }
  143. loadVector = function(tx,ty,tz)
  144.     {
  145.     var vect = vec3.create();
  146.     vect[0] = tx;
  147.     vect[1] = ty;
  148.     vect[2] = tz;
  149.     return vect;
  150.     }
  151. checkDist = function(p,norm,d)
  152. {
  153.     return vec3.dot(p,norm)+d;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement