Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (739)
Games in Android Showcase (224)
games submitted by our members
Games in WIP (820)
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: steg90 (posted 2013-04-19 18:27:00, viewed 450 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   275   276   277   278   279   280   281   282   283   284   285   286   287   288   289   290   291   292   293   294   295   296   297   298   299   300   301   302   303   304   305   306   307   308   309   310   311   312   313   314   315   316   317   318   319  
// Frustum class

package com.voxby.game;


/*
 * Author: Ron Sullivan (modified by Thomas Hourdel).
 * E-mail: thomas.hourdel@libertysurf.fr
 */

import java.nio.*;
import org.lwjgl.*;
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.util.vector.Vector3f;


/* Main class.
 */

public class Frustum
{

        // We create an enum of the sides so we don't have to call each side 0 or 1.
        // This way it makes it more understandable and readable when dealing with frustum sides.
        public static final int RIGHT   = 0;            // The RIGHT side of the frustum
        public static final int LEFT    = 1;            // The LEFT      side of the frustum
        public static final int BOTTOM  = 2;            // The BOTTOM side of the frustum
        public static final int TOP         = 3;                // The TOP side of the frustum
        public static final int BACK    = 4;            // The BACK     side of the frustum
        public static final int FRONT   = 5;            // The FRONT side of the frustum

        // Like above, instead of saying a number for the ABC and D of the plane, we
        // want to be more descriptive.
        public static final int A = 0;                          // The X value of the plane's normal
        public static final int B = 1;                          // The Y value of the plane's normal
        public static final int C = 2;                          // The Z value of the plane's normal
        public static final int D = 3;                          // The distance the plane is from the origin

        // This holds the A B C and D values for each side of our frustum.
        float[][] m_Frustum = new float[6][4];

          /** FloatBuffer to get ModelView matrix. **/
        FloatBuffer modl_b;
        
          /** FloatBuffer to get Projection matrix. **/
        FloatBuffer proj_b;

        ///////////////////////////////// NORMALIZE PLANE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
        /////
        /////   This normalizes a plane (A side) from a given frustum.
        /////
        ///////////////////////////////// NORMALIZE PLANE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

        public void normalizePlane(float[][] frustum, int side)
        {
                // Here we calculate the magnitude of the normal to the plane (point A B C)
                // Remember that (A, B, C) is that same thing as the normal's (X, Y, Z).
                // To calculate magnitude you use the equation:  magnitude = sqrt( x^2 + y^2 + z^2)
                float magnitude = (float)Math.sqrt( frustum[side][A] * frustum[side][A] + 
                                                frustum[side][B] * frustum[side][B] + frustum[side][C] * frustum[side][C] );

                // Then we divide the plane's values by it's magnitude.
                // This makes it easier to work with.
                frustum[side][A] /= magnitude;
                frustum[side][B] /= magnitude;
                frustum[side][C] /= magnitude;
                frustum[side][D] /= magnitude; 
        }



        ///////////////////////////////// CALCULATE FRUSTUM \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
        /////
        /////   This extracts our frustum from the projection and modelview matrix.
        /////
        ///////////////////////////////// CALCULATE FRUSTUM \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

        public void calculateFrustum()
        {    
                float[] proj = new float[16];           // This will hold our projection matrix
                float[] modl = new float[16];           // This will hold our modelview matrix
                float[] clip = new float[16];           // This will hold the clipping planes


                // glGetFloat() is used to extract information about our OpenGL world.
                // Below, we pass in GL_PROJECTION_MATRIX to abstract our projection matrix.
                // It then stores the matrix into an array of [16].
                proj_b.rewind();
                glGetFloat(GL_PROJECTION_MATRIX, proj_b);
                proj_b.rewind();
                proj_b.get(proj);

                // By passing in GL_MODELVIEW_MATRIX, we can abstract our model view matrix.
                // This also stores it in an array of [16].
                modl_b.rewind();
                glGetFloat(GL_MODELVIEW_MATRIX, modl_b);
                modl_b.rewind(); 
                modl_b.get(modl);

                // Now that we have our modelview and projection matrix, if we combine these 2 matrices,
                // it will give us our clipping planes.  To combine 2 matrices, we multiply them.
                
                clip[ 0] = modl[ 0] * proj[ 0] + modl[ 1] * proj[ 4] + modl[ 2] * proj[ 8] + modl[ 3] * proj[12];
                clip[ 1] = modl[ 0] * proj[ 1] + modl[ 1] * proj[ 5] + modl[ 2] * proj[ 9] + modl[ 3] * proj[13];
                clip[ 2] = modl[ 0] * proj[ 2] + modl[ 1] * proj[ 6] + modl[ 2] * proj[10] + modl[ 3] * proj[14];
                clip[ 3] = modl[ 0] * proj[ 3] + modl[ 1] * proj[ 7] + modl[ 2] * proj[11] + modl[ 3] * proj[15];

                clip[ 4] = modl[ 4] * proj[ 0] + modl[ 5] * proj[ 4] + modl[ 6] * proj[ 8] + modl[ 7] * proj[12];
                clip[ 5] = modl[ 4] * proj[ 1] + modl[ 5] * proj[ 5] + modl[ 6] * proj[ 9] + modl[ 7] * proj[13];
                clip[ 6] = modl[ 4] * proj[ 2] + modl[ 5] * proj[ 6] + modl[ 6] * proj[10] + modl[ 7] * proj[14];
                clip[ 7] = modl[ 4] * proj[ 3] + modl[ 5] * proj[ 7] + modl[ 6] * proj[11] + modl[ 7] * proj[15];

                clip[ 8] = modl[ 8] * proj[ 0] + modl[ 9] * proj[ 4] + modl[10] * proj[ 8] + modl[11] * proj[12];
                clip[ 9] = modl[ 8] * proj[ 1] + modl[ 9] * proj[ 5] + modl[10] * proj[ 9] + modl[11] * proj[13];
                clip[10] = modl[ 8] * proj[ 2] + modl[ 9] * proj[ 6] + modl[10] * proj[10] + modl[11] * proj[14];
                clip[11] = modl[ 8] * proj[ 3] + modl[ 9] * proj[ 7] + modl[10] * proj[11] + modl[11] * proj[15];

                clip[12] = modl[12] * proj[ 0] + modl[13] * proj[ 4] + modl[14] * proj[ 8] + modl[15] * proj[12];
                clip[13] = modl[12] * proj[ 1] + modl[13] * proj[ 5] + modl[14] * proj[ 9] + modl[15] * proj[13];
                clip[14] = modl[12] * proj[ 2] + modl[13] * proj[ 6] + modl[14] * proj[10] + modl[15] * proj[14];
                clip[15] = modl[12] * proj[ 3] + modl[13] * proj[ 7] + modl[14] * proj[11] + modl[15] * proj[15];
                
                // Now we actually want to get the sides of the frustum.  To do this we take
                // the clipping planes we received above and extract the sides from them.

                // This will extract the RIGHT side of the frustum
                m_Frustum[RIGHT][A] = clip[ 3] - clip[ 0];
                m_Frustum[RIGHT][B] = clip[ 7] - clip[ 4];
                m_Frustum[RIGHT][C] = clip[11] - clip[ 8];
                m_Frustum[RIGHT][D] = clip[15] - clip[12];

                // Now that we have a normal (A,B,C) and a distance (D) to the plane,
                // we want to normalize that normal and distance.

                // Normalize the RIGHT side
                normalizePlane(m_Frustum, RIGHT);

                // This will extract the LEFT side of the frustum
                m_Frustum[LEFT][A] = clip[ 3] + clip[ 0];
                m_Frustum[LEFT][B] = clip[ 7] + clip[ 4];
                m_Frustum[LEFT][C] = clip[11] + clip[ 8];
                m_Frustum[LEFT][D] = clip[15] + clip[12];

                // Normalize the LEFT side
                normalizePlane(m_Frustum, LEFT);

                // This will extract the BOTTOM side of the frustum
                m_Frustum[BOTTOM][A] = clip[ 3] + clip[ 1];
                m_Frustum[BOTTOM][B] = clip[ 7] + clip[ 5];
                m_Frustum[BOTTOM][C] = clip[11] + clip[ 9];
                m_Frustum[BOTTOM][D] = clip[15] + clip[13];

                // Normalize the BOTTOM side
                normalizePlane(m_Frustum, BOTTOM);

                // This will extract the TOP side of the frustum
                m_Frustum[TOP][A] = clip[ 3] - clip[ 1];
                m_Frustum[TOP][B] = clip[ 7] - clip[ 5];
                m_Frustum[TOP][C] = clip[11] - clip[ 9];
                m_Frustum[TOP][D] = clip[15] - clip[13];

                // Normalize the TOP side
                normalizePlane(m_Frustum, TOP);

                // This will extract the BACK side of the frustum
                m_Frustum[BACK][A] = clip[ 3] - clip[ 2];
                m_Frustum[BACK][B] = clip[ 7] - clip[ 6];
                m_Frustum[BACK][C] = clip[11] - clip[10];
                m_Frustum[BACK][D] = clip[15] - clip[14];

                // Normalize the BACK side
                normalizePlane(m_Frustum, BACK);

                // This will extract the FRONT side of the frustum
                m_Frustum[FRONT][A] = clip[ 3] + clip[ 2];
                m_Frustum[FRONT][B] = clip[ 7] + clip[ 6];
                m_Frustum[FRONT][C] = clip[11] + clip[10];
                m_Frustum[FRONT][D] = clip[15] + clip[14];

                // Normalize the FRONT side
                normalizePlane(m_Frustum, FRONT);
        }

        // The code below will allow us to make checks within the frustum.  For example,
        // if we want to see if a point, a sphere, or a cube lies inside of the frustum.
        // Because all of our planes point INWARDS (The normals are all pointing inside the frustum)
        // we then can assume that if a point is in FRONT of all of the planes, it's inside.

        ///////////////////////////////// POINT IN FRUSTUM \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
        /////
        /////   This determines if a point is inside of the frustum
        /////
        ///////////////////////////////// POINT IN FRUSTUM \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

        public boolean pointInFrustum( float x, float y, float z )
        {
                // Go through all the sides of the frustum
                for(int i = 0; i < 6; i++ )
                {
                        // Calculate the plane equation and check if the point is behind a side of the frustum
                        if(m_Frustum[i][A] * x + m_Frustum[i][B] * y + m_Frustum[i][C] * z + m_Frustum[i][D] <= 0)
                        {
                                // The point was behind a side, so it ISN'T in the frustum
                                return false;
                        }
                }

                // The point was inside of the frustum (In front of ALL the sides of the frustum)
                return true;
        }



        ///////////////////////////////// SPHERE IN FRUSTUM \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
        /////
        /////   This determines if a sphere is inside of our frustum by it's center and radius.
        /////
        ///////////////////////////////// SPHERE IN FRUSTUM \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

        public boolean sphereInFrustum( float x, float y, float z, float radius )
        {
                // Go through all the sides of the frustum
                for(int i = 0; i < 6; i++ )     
                {
                        // If the center of the sphere is farther away from the plane than the radius
                        if( m_Frustum[i][A] * x + m_Frustum[i][B] * y + m_Frustum[i][C] * z + m_Frustum[i][D] <= -radius )
                        {
                                // The distance was greater than the radius so the sphere is outside of the frustum
                                return false;
                        }
                }
                
                // The sphere was inside of the frustum!
                return true;
        }


        public boolean cubeInFrustum(Vector3f center, float size )
        {
                return cubeInFrustum( center.x, center.y, center.z, size );
        }
        
        ///////////////////////////////// CUBE IN FRUSTUM \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
        /////
        /////   This determines if a cube is in or around our frustum by it's center and 1/2 it's length
        /////
        ///////////////////////////////// CUBE IN FRUSTUM \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

        public boolean cubeInFrustum( float x, float y, float z, float size )
        {
                // This test is a bit more work, but not too much more complicated.
                // Basically, what is going on is, that we are given the center of the cube,
                // and half the length.  Think of it like a radius.  Then we checking each point
                // in the cube and seeing if it is inside the frustum.  If a point is found in front
                // of a side, then we skip to the next side.  If we get to a plane that does NOT have
                // a point in front of it, then it will return false.

                // *Note* - This will sometimes say that a cube is inside the frustum when it isn't.
                // This happens when all the corners of the bounding box are not behind any one plane.
                // This is rare and shouldn't effect the overall rendering speed.

                for(int i = 0; i < 6; i++ )
                {
                        if(m_Frustum[i][A] * (x - size) + m_Frustum[i][B] * (y - size) + m_Frustum[i][C] * (z - size) + m_Frustum[i][D] > 0)
                           continue;
                        if(m_Frustum[i][A] * (x + size) + m_Frustum[i][B] * (y - size) + m_Frustum[i][C] * (z - size) + m_Frustum[i][D] > 0)
                           continue;
                        if(m_Frustum[i][A] * (x - size) + m_Frustum[i][B] * (y + size) + m_Frustum[i][C] * (z - size) + m_Frustum[i][D] > 0)
                           continue;
                        if(m_Frustum[i][A] * (x + size) + m_Frustum[i][B] * (y + size) + m_Frustum[i][C] * (z - size) + m_Frustum[i][D] > 0)
                           continue;
                        if(m_Frustum[i][A] * (x - size) + m_Frustum[i][B] * (y - size) + m_Frustum[i][C] * (z + size) + m_Frustum[i][D] > 0)
                           continue;
                        if(m_Frustum[i][A] * (x + size) + m_Frustum[i][B] * (y - size) + m_Frustum[i][C] * (z + size) + m_Frustum[i][D] > 0)
                           continue;
                        if(m_Frustum[i][A] * (x - size) + m_Frustum[i][B] * (y + size) + m_Frustum[i][C] * (z + size) + m_Frustum[i][D] > 0)
                           continue;
                        if(m_Frustum[i][A] * (x + size) + m_Frustum[i][B] * (y + size) + m_Frustum[i][C] * (z + size) + m_Frustum[i][D] > 0)
                           continue;

                        // If we get here, it isn't in the frustum
                        return false;
                }

                return true;
        }


 
        /** Frustum constructor.
         */

        public Frustum()
        {
                modl_b = BufferUtils.createFloatBuffer(16);
                proj_b = BufferUtils.createFloatBuffer(16);
        }
}


// Chunk manager class render method:

// c.getX returns the chunks X position, c.getZ the chunks Z position
// These are multiplied by 8 to get centre of them (chunks are 16x16x16)
// Size is also multiplied by 8 to get half the length 

public void render()
   {
      frustum.calculateFrustum();
      
      for(Chunk c : ChunkList.values())
      {         
         
         if(frustum.cubeInFrustum(c.getX() * 8 , 0 * 8, c.getZ() * 8 , Chunk.CHUNK_SIZE * 8 ))
         {
            c.render();
          }
      }
   }





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
 
Ecumene (57 views)
2017-09-30 02:57:34

theagentd (79 views)
2017-09-26 18:23:31

cybrmynd (187 views)
2017-08-02 12:28:51

cybrmynd (185 views)
2017-08-02 12:19:43

cybrmynd (192 views)
2017-08-02 12:18:09

Sralse (203 views)
2017-07-25 17:13:48

Archive (767 views)
2017-04-27 17:45:51

buddyBro (901 views)
2017-04-05 03:38:00

CopyableCougar4 (1451 views)
2017-03-24 15:39:42

theagentd (1326 views)
2017-03-24 15:32:08
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
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!