Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (805)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (868)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: theagentd (posted 2015-07-10 17:42:51, viewed 509 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231   232   233   234   235   236   237   238   239   240   241   242   243   244   245   246   247   248   249   250   251   252   253   254   255   256   257   258   259   260   261   262   263   264   265   266   267   268   269   270   271   272   273   274  
package engine.gl.culling;

import static com.badlogic.gdx.math.Matrix4.*;

import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Vector3;

import engine.gl.Camera;
import engine.math.ScalarUtils;


public class FrustumCuller {
   
   public static final int NUM_PLANES = 6;
   
   public static final int TESTED_PLANES = 6;

   public static final int RIGHT_PLANE = 0;
   public static final int LEFT_PLANE = 1;
   public static final int TOP_PLANE = 2;
   public static final int BOTTOM_PLANE = 3;
   public static final int FAR_PLANE = 4;
   public static final int NEAR_PLANE = 5;

   private Matrix4 temp;
   private Plane[] planes;
   
   private float x, y, z, farPlane, farPlaneSqrd;

   public FrustumCuller(){
      temp = new Matrix4();
      planes = new Plane[NUM_PLANES];
      for(int i = 0; i < NUM_PLANES; i++){
         planes[i] = new Plane();
      }
   }
   
   public void update(Camera camera){
      update(camera.getX(), camera.getY(), camera.getZ(), ScalarUtils.max(camera.getNearPlane(), camera.getFarPlane()), camera.getProjectionMatrix(), camera.getViewMatrix());
   }

   public void update(float x, float y, float z, float farPlane, Matrix4... matrices){
      
      this.x = x;
      this.y = y;
      this.z = z;
      this.farPlane = farPlane;
      farPlaneSqrd = farPlane*farPlane;
      
      temp.set(matrices[0]);
      for(int i = 1; i < matrices.length; i++){
         temp.mul(matrices[i]);
      }
      
      
      float[] v = temp.getValues();
      
      //System.out.println("(" + v[M00] + ", " + v[M01] + ", " + v[M02] + ", " + v[M03] + ")");
      
      planes[0].set(
            v[M30] - v[M00], 
            v[M31] - v[M01],
            v[M32] - v[M02],
            v[M33] - v[M03]
      );
      
      planes[1].set(
            v[M30] + v[M00], 
            v[M31] + v[M01], 
            v[M32] + v[M02], 
            v[M33] + v[M03]
      );

       planes[2].set(
            v[M30] - v[M10],
            v[M31] - v[M11],
            v[M32] - v[M12],
            v[M33] - v[M13]
      );

      planes[3].set(
            v[M30] + v[M10],
            v[M31] + v[M11],
            v[M32] + v[M12],
            v[M33] + v[M13]
      );

      //Far plane
      planes[4].set(
            v[M30] - v[M20],
            v[M31] - v[M21],
            v[M32] - v[M22],
            v[M33] - v[M23]
      );

      //Near plane
      planes[5].set(
            v[M30] + v[M20],
            v[M31] + v[M21],
            v[M32] + v[M22],
            v[M33] + v[M23]
      );
   }
   
   public void pushPlane(int plane, float distance) {
      planes[plane].d += distance;
   }
   
   

   public boolean pointVisible(Vector3 position){
      return sphereVisible(position.x, position.y, position.z, 0, farPlane);
   }
   public boolean pointVisible(Vector3 position, float maxDistance){
      return sphereVisible(position.x, position.y, position.z, 0, ScalarUtils.min(farPlane, maxDistance));
   }

   public boolean pointVisible(float x, float y, float z){
      return sphereVisible(x, y, z, 0, farPlane);
   }

   public boolean pointVisible(float x, float y, float z, float maxDistance){
      return sphereVisible(x, y, z, 0, ScalarUtils.min(farPlane, maxDistance));
   }

   public boolean sphereVisible(Vector3 position, float radius){
      return sphereVisible(position.x, position.y, position.z, radius, farPlane);
   }

   public boolean sphereVisible(Vector3 position, float radius, float maxDistance){
      return sphereVisible(position.x, position.y, position.z, radius, ScalarUtils.min(farPlane, maxDistance));
   }

   public boolean sphereVisible(Bounds3D bounds) {
      return sphereVisible(bounds.centerX, bounds.centerY, bounds.centerZ, bounds.radius, farPlane);
   }

   public boolean sphereVisible(Bounds3D bounds, float maxDistance) {
      return sphereVisible(bounds.centerX, bounds.centerY, bounds.centerZ, bounds.radius, ScalarUtils.min(farPlane, maxDistance));
   }

   public boolean sphereVisible(float x, float y, float z, float radius){
      return sphereVisible(x, y, z, radius, farPlane);
   }

   public boolean sphereVisible(float x, float y, float z, float radius, float maxDistance){
      
      float dx = x - this.x, dy = y - this.y, dz = z - this.z;
      float distSqrd = dx*dx + dy*dy + dz*dz;
      
      float r = ScalarUtils.min(farPlane, maxDistance) + radius;
      if(distSqrd > r*r){
         return false;
      }
      
      float negativeRadius = -radius;
      for(int i = 0; i < TESTED_PLANES; i++){
         if(planes[i].isOutside(x, y, z, negativeRadius)){
            return false;
         }
      }
      
      return true;
   }
   
   public boolean boxVisible(Bounds3D bounds){
      return boxVisibleInternal(bounds.minX, bounds.minY, bounds.minZ, bounds.maxX, bounds.maxY, bounds.maxZ, farPlaneSqrd);
   }
   
   public boolean boxVisible(Bounds3D bounds, float maxDistance){
      return boxVisibleInternal(bounds.minX, bounds.minY, bounds.minZ, bounds.maxX, bounds.maxY, bounds.maxZ, ScalarUtils.min(farPlaneSqrd, maxDistance*maxDistance));
   }

   public boolean boxVisible(float x0, float y0, float z0, float x1, float y1, float z1){
      return boxVisibleInternal(x0, y0, z0, x1, y1, z1, farPlaneSqrd);
   }
   
   public boolean boxVisible(float x0, float y0, float z0, float x1, float y1, float z1, float maxDistance){
      return boxVisibleInternal(x0, y0, z0, x1, y1, z1, ScalarUtils.min(farPlaneSqrd, maxDistance*maxDistance));
   }
   
   private boolean boxVisibleInternal(float x0, float y0, float z0, float x1, float y1, float z1, float maxDistanceSqrd){

      if(calculateDistSqrd(clamp(x, x0, x1), clamp(y, y0, y1), clamp(z, z0, z1)) >= ScalarUtils.min(farPlaneSqrd, maxDistanceSqrd)){
         return false;
      }
      
      for(int i = 0; i < TESTED_PLANES; i++){
         Plane p = planes[i];
         if(
               p.isOutside(x0, y0, z0, 0) &&
               p.isOutside(x0, y0, z1, 0) &&
               p.isOutside(x0, y1, z0, 0) &&
               p.isOutside(x0, y1, z1, 0) &&
               p.isOutside(x1, y0, z0, 0) &&
               p.isOutside(x1, y0, z1, 0) &&
               p.isOutside(x1, y1, z0, 0) &&
               p.isOutside(x1, y1, z1, 0)
         ){
            return false;
            
         }
      }
      
      return true;
   }
   
   public boolean objectVisible(TransformedBounds3D transformedBounds){
      Bounds3D aabb = transformedBounds.bounds;
      return objectVisible(transformedBounds.corners, aabb.minX, aabb.minY, aabb.minZ, aabb.maxX, aabb.maxY, aabb.maxZ);
   }
   
   public boolean objectVisible(Vector3[] points, float x0, float y0, float z0, float x1, float y1, float z1){
      
      if(points.length == 0){
         return false;
      }
      
      if(calculateDistSqrd(clamp(x, x0, x1), clamp(y, y0, y1), clamp(z, z0, z1)) >= farPlaneSqrd){
         return false;
      }
      
      planeLoop:
      for(int i = 0; i < TESTED_PLANES; i++){
         Plane plane = planes[i];
         for(int j = 0; j < points.length; j++){
            Vector3 point = points[j];
            if(!plane.isOutside(point.x, point.y, point.z, 0)){
               continue planeLoop; //Object is inside the frustum according to this plane, skip to the next plane.
            }
         }
         return false;
      }

      return true;
   }
   
   private float clamp(float f, float min, float max){
      if (f > max)
            return max;
        if (f > min)
            return f;
        return min;
   }
   
   private float calculateDistSqrd(float x, float y, float z){
      float dx = x - this.x, dy = y - this.y, dz = z - this.z;
      
      return dx*dx + dy*dy + dz*dz;
   }
   
   private class Plane{
      
      private float a, b, c, d;
      
      public void set(float a, float b, float c, float d){

         float length = (float)Math.sqrt(a*a + b*b + c*c);
         this.a = a/length;
         this.b = b/length;
         this.c = c/length;
         this.d = d/length;
      }
      
      public boolean isOutside(float x, float y, float z, float negativeRadius){
         return a*x + b*y + c*z + d <= negativeRadius;
      }
      
      @Override
      public String toString() {
         return "(" + a + ", " + b + ", " + c + ", " + d + ")";
      }
   }
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 
Riven (585 views)
2019-09-04 15:33:17

hadezbladez (5526 views)
2018-11-16 13:46:03

hadezbladez (2407 views)
2018-11-16 13:41:33

hadezbladez (5788 views)
2018-11-16 13:35:35

hadezbladez (1228 views)
2018-11-16 13:32:03

EgonOlsen (4667 views)
2018-06-10 19:43:48

EgonOlsen (5685 views)
2018-06-10 19:43:44

EgonOlsen (3200 views)
2018-06-10 19:43:20

DesertCoockie (4102 views)
2018-05-13 18:23:11

nelsongames (5118 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!