Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (762)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (847)
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: Wave Propagation (posted 2016-09-20 13:29:55, viewed 933 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   320   321   322   323   324   325   326   327   328   329   330   331   332   333   334   335   336   337   338   339   340   341   342   343   344   345   346   347   348   349   350   351   352   353   354   355   356   357   358   359   360   361   362   363   364   365   366   367   368   369   370   371   372   373   374   375   376   377   378   379   380   381   382   383   384   385   386   387   388   389   390   391   392   393   394   395   396   397   398   399   400   401   402   403   404   405   406   407   408   409   410   411   412   413   414   415   416   417   418   419   420   421   422   423   424   425   426   427   428   429   430   431   432   433   434   435   436   437   438   439   440   441   442   443   444   445   446   447   448   449   450   451   452   453   454   455   456   457   458   459   460   461   462   463   464   465   466   467   468   469   470   471   472   473   474   475   476   477   478   479   480   481   482   483   484   485   486   487   488   489   490   491   492   493   494   495   496   497   498   499   500   501   502   503   504   505   506   507   508   509   510   511   512   513   514   515   516   517   518   519   520   521   522   523   524   525   526   527   528   529   530   531  
package org.dayflower.rm;

import java.util.Objects;
import java.util.Optional;

import com.amd.aparapi.Kernel.Constant;

public final class RendererKernel extends AbstractKernel {
   private static final float EPSILON = 0.0001F;
   private static final float GAMMA = 2.2F;
   private static final float GAMMA_RECIPROCAL = 1.0F / GAMMA;
   private static final int RELATIVE_OFFSET_RAY_DIRECTION = 3;
   private static final int RELATIVE_OFFSET_RAY_ORIGIN = 0;
   private static final int SIZE_PIXEL = 4;
   private static final int SIZE_RAY = 6;
   private static final int TONE_MAPPING_AND_GAMMA_CORRECTION_FILMIC_CURVE = 1;
   private static final int TONE_MAPPING_AND_GAMMA_CORRECTION_LINEAR = 2;
   private static final int TONE_MAPPING_AND_GAMMA_CORRECTION_REINHARD_1 = 3;
   private static final int TONE_MAPPING_AND_GAMMA_CORRECTION_REINHARD_2 = 4;
   
   ////////////////////////////////////////////////////////////////////////////////////////////////////
   
   private byte[] pixels;
   private final Camera camera;
   private float amplitude;
   private float frequency;
   private float gain;
   private float lacunarity;
   private final float[] accumulatedPixelColors;
   private final float[] cameraArray;
   private final float[] rays;
   private final int height;
   private int octaves;
   private int toneMappingAndGammaCorrection = TONE_MAPPING_AND_GAMMA_CORRECTION_FILMIC_CURVE;
   private final int width;
   @Constant
   private final int[] permutations0 = {151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23, 190, 6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117, 35, 11, 32, 57, 177, 33, 88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175, 74, 165, 71, 134, 139, 48, 27, 166, 77, 146, 158, 231, 83, 111, 229, 122, 60, 211, 133, 230, 220, 105, 92, 41, 55, 46, 245, 40, 244, 102, 143, 54, 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 18, 169, 200, 196, 135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64, 52, 217, 226, 250, 124, 123, 5, 202, 38, 147, 118, 126, 255, 82, 85, 212, 207, 206, 59, 227, 47, 16, 58, 17, 182, 189, 28, 42, 23, 183, 170, 213, 119, 248, 152, 2, 44, 154, 163, 70, 221, 153, 101, 155, 167, 43, 172, 9, 129, 22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, 218, 246, 97, 228, 251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51, 145, 235, 249, 14, 239, 107, 49, 192, 214, 31, 181, 199, 106, 157, 184, 84, 204, 176, 115, 121, 50, 45, 127, 4, 150, 254, 138, 236, 205, 93, 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180};
   @Constant
   private final int[] permutations1 = new int[512];
   
   ////////////////////////////////////////////////////////////////////////////////////////////////////
   
   public RendererKernel(final int width, final int height, final Camera camera) {
      this.width = width;
      this.height = height;
      this.camera = camera;
      this.amplitude = 0.5F;
      this.frequency = 0.2F;
      this.lacunarity = 2.0F;
      this.gain = 1.0F / this.lacunarity;
      this.accumulatedPixelColors = new float[width * height * 3];
      this.cameraArray = camera.getArray();
      this.rays = new float[width * height * SIZE_RAY];
      this.octaves = 2;
      
      for(int i = 0; i < this.permutations0.length; i++) {
         this.permutations1[i] = this.permutations0[i];
         this.permutations1[i + this.permutations0.length] = this.permutations0[i];
      }
   }
   
   ////////////////////////////////////////////////////////////////////////////////////////////////////
   
   public RendererKernel compile(final byte[] pixels, final int width, final int height) {
      this.pixels = Objects.requireNonNull(pixels, "pixels == null");
      
//      setExecutionMode(EXECUTION_MODE.JTP);
      setExplicit(true);
      setSeed(System.nanoTime(), width * height);
      
      updateTables();
      
      put(this.pixels);
      put(this.accumulatedPixelColors);
      put(this.rays);
      put(this.permutations0);
      put(this.permutations1);
      
      return this;
   }
   
   @Override
   public void run() {
      final int pixelIndex = getGlobalId();
      
      if(doCreatePrimaryRay(pixelIndex)) {
         doRayMarching(pixelIndex);
      } else {
         final int pixelIndex0 = pixelIndex * 3;
         
         this.accumulatedPixelColors[pixelIndex0] = 0.0F;
         this.accumulatedPixelColors[pixelIndex0 + 1] = 0.0F;
         this.accumulatedPixelColors[pixelIndex0 + 2] = 0.0F;
      }
      
      doCalculateColor(pixelIndex);
   }
   
   ////////////////////////////////////////////////////////////////////////////////////////////////////
   
   private boolean doCreatePrimaryRay(final int pixelIndex) {
//      Calculate the X- and Y-coordinates on the screen:
      final int y = pixelIndex / this.width;
      final int x = pixelIndex - y * this.width;
      
//      Retrieve the current X-, Y- and Z-coordinates of the camera lens (eye) in the scene:
      final float eyeX = this.cameraArray[Camera.ABSOLUTE_OFFSET_OF_EYE_X];
      final float eyeY = this.cameraArray[Camera.ABSOLUTE_OFFSET_OF_EYE_Y];
      final float eyeZ = this.cameraArray[Camera.ABSOLUTE_OFFSET_OF_EYE_Z];
      
//      Retrieve the current U-vector for the orthonormal basis frame of the camera lens (eye) in the scene:
      final float uX = this.cameraArray[Camera.ABSOLUTE_OFFSET_OF_ORTHONORMAL_BASIS_U_X];
      final float uY = this.cameraArray[Camera.ABSOLUTE_OFFSET_OF_ORTHONORMAL_BASIS_U_Y];
      final float uZ = this.cameraArray[Camera.ABSOLUTE_OFFSET_OF_ORTHONORMAL_BASIS_U_Z];
      
//      Retrieve the current V-vector for the orthonormal basis frame of the camera lens (eye) in the scene:
      final float vX = this.cameraArray[Camera.ABSOLUTE_OFFSET_OF_ORTHONORMAL_BASIS_V_X];
      final float vY = this.cameraArray[Camera.ABSOLUTE_OFFSET_OF_ORTHONORMAL_BASIS_V_Y];
      final float vZ = this.cameraArray[Camera.ABSOLUTE_OFFSET_OF_ORTHONORMAL_BASIS_V_Z];
      
//      Retrieve the current W-vector for the orthonormal basis frame of the camera lens (eye) in the scene:
      final float wX = this.cameraArray[Camera.ABSOLUTE_OFFSET_OF_ORTHONORMAL_BASIS_W_X];
      final float wY = this.cameraArray[Camera.ABSOLUTE_OFFSET_OF_ORTHONORMAL_BASIS_W_Y];
      final float wZ = this.cameraArray[Camera.ABSOLUTE_OFFSET_OF_ORTHONORMAL_BASIS_W_Z];
      
//      Calculate the Field of View:
      final float fieldOfViewX0 = this.cameraArray[Camera.ABSOLUTE_OFFSET_OF_FIELD_OF_VIEW_X];
      final float fieldOfViewX1 = tan(fieldOfViewX0 * PI_DIVIDED_BY_360);
      final float fieldOfViewY0 = this.cameraArray[Camera.ABSOLUTE_OFFSET_OF_FIELD_OF_VIEW_Y];
      final float fieldOfViewY1 = tan(-fieldOfViewY0 * PI_DIVIDED_BY_360);
      
//      Calculate the horizontal direction:
      final float horizontalX = uX * fieldOfViewX1;
      final float horizontalY = uY * fieldOfViewX1;
      final float horizontalZ = uZ * fieldOfViewX1;
      
//      Calculate the vertical direction:
      final float verticalX = vX * fieldOfViewY1;
      final float verticalY = vY * fieldOfViewY1;
      final float verticalZ = vZ * fieldOfViewY1;
      
//      Calculate the pixel jitter:
      final float jitterX = 0.5F;
      final float jitterY = 0.5F;
      
//      Calculate the pixel sample point:
      final float sx = (jitterX + x) / (this.cameraArray[Camera.ABSOLUTE_OFFSET_OF_RESOLUTION_X] - 1.0F);
      final float sy = (jitterY + y) / (this.cameraArray[Camera.ABSOLUTE_OFFSET_OF_RESOLUTION_Y] - 1.0F);
      final float sx0 = 2.0F * sx - 1.0F;
      final float sy0 = 2.0F * sy - 1.0F;
      
//      Initialize w to 1.0F:
      float w = 1.0F;
      
//      Retrieve whether or not this camera uses a Fisheye camera lens:
      final boolean isFisheyeCameraLens = this.cameraArray[Camera.ABSOLUTE_OFFSET_OF_CAMERA_LENS] == Camera.CAMERA_LENS_FISHEYE;
      
      if(isFisheyeCameraLens) {
//         Calculate the dot product that will be used in determining if the current pixel is outside the camera lens:
         final float dotProduct = sx0 * sx0 + sy0 * sy0;
         
         if(dotProduct > 1.0F) {
            return false;
         }
         
//         Update the w variable:
         w = sqrt(1.0F - dotProduct);
      }
      
//      Calculate the middle point:
      final float middleX = eyeX + wX * w;
      final float middleY = eyeY + wY * w;
      final float middleZ = eyeZ + wZ * w;
      
//      Calculate the point on the plane one unit away from the eye:
      final float pointOnPlaneOneUnitAwayFromEyeX = middleX + (horizontalX * sx0) + (verticalX * sy0);
      final float pointOnPlaneOneUnitAwayFromEyeY = middleY + (horizontalY * sx0) + (verticalY * sy0);
      final float pointOnPlaneOneUnitAwayFromEyeZ = middleZ + (horizontalZ * sx0) + (verticalZ * sy0);
      
//      Retrieve the focal distance:
      final float focalDistance = this.cameraArray[Camera.ABSOLUTE_OFFSET_OF_FOCAL_DISTANCE];
      
//      Calculate the point on the image plane:
      final float pointOnImagePlaneX = eyeX + (pointOnPlaneOneUnitAwayFromEyeX - eyeX) * focalDistance;
      final float pointOnImagePlaneY = eyeY + (pointOnPlaneOneUnitAwayFromEyeY - eyeY) * focalDistance;
      final float pointOnImagePlaneZ = eyeZ + (pointOnPlaneOneUnitAwayFromEyeZ - eyeZ) * focalDistance;
      
//      Retrieve the aperture radius:
      final float apertureRadius = this.cameraArray[Camera.ABSOLUTE_OFFSET_OF_APERTURE_RADIUS];
      
//      Initialize the X-, Y- and Z-coordinates of the aperture point:
      float aperturePointX = eyeX;
      float aperturePointY = eyeY;
      float aperturePointZ = eyeZ;
      
//      Check if Depth of Field (DoF) is enabled:
      if(apertureRadius > 0.00001F) {
//         Calculate two random values:
         final float random1 = nextFloat();
         final float random2 = nextFloat();
         
//         Calculate the angle:
         final float angle = PI_MULTIPLIED_BY_TWO * random1;
         
//         Calculate the distance:
         final float distance = apertureRadius * sqrt(random2);
         
//         Calculate the aperture:
         final float apertureX = cos(angle) * distance;
         final float apertureY = sin(angle) * distance;
         
//         Update the aperture point:
         aperturePointX = eyeX + uX * apertureX + vX * apertureY;
         aperturePointY = eyeY + uY * apertureX + vY * apertureY;
         aperturePointZ = eyeZ + uZ * apertureX + vZ * apertureY;
      }
      
//      Calculate the aperture to image plane:
      final float apertureToImagePlane0X = pointOnImagePlaneX - aperturePointX;
      final float apertureToImagePlane0Y = pointOnImagePlaneY - aperturePointY;
      final float apertureToImagePlane0Z = pointOnImagePlaneZ - aperturePointZ;
      final float apertureToImagePlane0LengthReciprocal = rsqrt(apertureToImagePlane0X * apertureToImagePlane0X + apertureToImagePlane0Y * apertureToImagePlane0Y + apertureToImagePlane0Z * apertureToImagePlane0Z);
      final float apertureToImagePlane1X = apertureToImagePlane0X * apertureToImagePlane0LengthReciprocal;
      final float apertureToImagePlane1Y = apertureToImagePlane0Y * apertureToImagePlane0LengthReciprocal;
      final float apertureToImagePlane1Z = apertureToImagePlane0Z * apertureToImagePlane0LengthReciprocal;
      
//      Calculate the offset in the rays array:
      final int raysOffset = pixelIndex * SIZE_RAY;
      
//      Update the rays array with information:
      this.rays[raysOffset + 0] = aperturePointX;
      this.rays[raysOffset + 1] = aperturePointY;
      this.rays[raysOffset + 2] = aperturePointZ;
      this.rays[raysOffset + 3] = apertureToImagePlane1X;
      this.rays[raysOffset + 4] = apertureToImagePlane1Y;
      this.rays[raysOffset + 5] = apertureToImagePlane1Z;
      
      return true;
   }
   
   private int doHash(final int index) {
      return this.permutations1[index % this.permutations1.length];
   }
   
   private float doSimplexFractalX(final int octaves, final float x) {
      float result = 0.0F;
      
      float amplitude = this.amplitude;
      float frequency = this.frequency;
      
      for(int i = 0; i < octaves; i++) {
         result += amplitude * doSimplexNoiseX(x * frequency);
         
         amplitude *= this.gain;
         frequency *= this.lacunarity;
      }
      
      return result;
   }
   
   private float doSimplexFractalXY(final int octaves, final float x, final float y) {
      float result = 0.0F;
      
      float amplitude = this.amplitude;
      float frequency = this.frequency;
      
      for(int i = 0; i < octaves; i++) {
         result += amplitude * doSimplexNoiseXY(x * frequency, y * frequency);
         
         amplitude *= this.gain;
         frequency *= this.lacunarity;
      }
      
      return result;
   }
   
   private float doSimplexNoiseX(final float x) {
      final int i0 = doFastFloor(x);
      final int i1 = i0 + 1;
      
      final float x0 = x - i0;
      final float x1 = x0 - 1.0F;
      
      final float t00 = 1.0F - x0 * x0;
      final float t01 = t00 * t00;
      
      final float t10 = 1.0F - x1 * x1;
      final float t11 = t10 * t10;
      
      final float n0 = t01 * t01 * doGradientX(doHash(i0), x0);
      final float n1 = t11 * t11 * doGradientX(doHash(i1), x1);
      
      return 0.395F * (n0 + n1);
   }
   
   private float doSimplexNoiseXY(final float x, final float y) {
      final float a = 0.366025403F;
      final float b = 0.211324865F;
      
      final float s = (x + y) * a;
      final float sx = s + x;
      final float sy = s + y;
      
      final int i0 = doFastFloor(sx);
      final int j0 = doFastFloor(sy);
      
      final float t = (i0 + j0) * b;
      
      final float x00 = i0 - t;
      final float y00 = j0 - t;
      final float x01 = x - x00;
      final float y01 = y - y00;
      
      final int i1 = x01 > y01 ? 1 : 0;
      final int j1 = x01 > y01 ? 0 : 1;
      
      final float x1 = x01 - i1 + b;
      final float y1 = y01 - j1 + b;
      final float x2 = x01 - 1.0F + 2.0F * b;
      final float y2 = y01 - 1.0F + 2.0F * b;
      
      final float t00 = 0.5F - x01 * x01 - y01 * y01;
      final float t01 = t00 < 0.0F ? t00 : t00 * t00;
      
      final float n0 = t00 < 0.0F ? 0.0F : t01 * t01 * doGradientXY(doHash(i0 + doHash(j0)), x01, y01);
      
      final float t10 = 0.5F - x1 * x1 - y1 * y1;
      final float t11 = t10 < 0.0F ? t10 : t10 * t10;
      
      final float n1 = t10 < 0.0F ? 0.0F : t11 * t11 * doGradientXY(doHash(i0 + i1 + doHash(j0 + j1)), x1, y1);
      
      final float t20 = 0.5F - x2 * x2 - y2 * y2;
      final float t21 = t20 < 0.0F ? t20 : t20 * t20;
      
      final float n2 = t20 < 0.0F ? 0.0F : t21 * t21 * doGradientXY(doHash(i0 + 1 + doHash(j0 + 1)), x2, y2);
      
      return 45.23065F * (n0 + n1 + n2);
   }
   
   private void doCalculateColor(final int pixelIndex) {
//      Retrieve the offset to the pixels array:
      final int pixelsOffset = pixelIndex * SIZE_PIXEL;
      
//      Calculate the pixel index:
      final int pixelIndex0 = pixelIndex * 3;
      
//      Retrieve the 'normalized' accumulated pixel color component values again:
      float r = this.accumulatedPixelColors[pixelIndex0];
      float g = this.accumulatedPixelColors[pixelIndex0 + 1];
      float b = this.accumulatedPixelColors[pixelIndex0 + 2];
      
      if(this.toneMappingAndGammaCorrection == TONE_MAPPING_AND_GAMMA_CORRECTION_REINHARD_1) {
//         Perform Tone Mapping on the 'normalized' accumulated pixel color components:
         r = r / (r + 1.0F);
         g = g / (g + 1.0F);
         b = b / (b + 1.0F);
         
//         Perform Gamma Correction on the 'normalized' accumulated pixel color components:
         r = pow(r, GAMMA_RECIPROCAL);
         g = pow(g, GAMMA_RECIPROCAL);
         b = pow(b, GAMMA_RECIPROCAL);
      } else if(this.toneMappingAndGammaCorrection == TONE_MAPPING_AND_GAMMA_CORRECTION_REINHARD_2) {
//         Set the exposure:
         final float exposure = 1.5F;
         
//         Perform Tone Mapping on the 'normalized' accumulated pixel color components:
         r *= exposure / (1.0F + r / exposure);
         g *= exposure / (1.0F + g / exposure);
         b *= exposure / (1.0F + b / exposure);
         
//         Perform Gamma Correction on the 'normalized' accumulated pixel color components:
         r = pow(r, GAMMA_RECIPROCAL);
         g = pow(g, GAMMA_RECIPROCAL);
         b = pow(b, GAMMA_RECIPROCAL);
      } else if(this.toneMappingAndGammaCorrection == TONE_MAPPING_AND_GAMMA_CORRECTION_FILMIC_CURVE) {
//         Calculate the maximum pixel color component values:
         final float rMaximum = max(r - 0.004F, 0.0F);
         final float gMaximum = max(g - 0.004F, 0.0F);
         final float bMaximum = max(b - 0.004F, 0.0F);
         
//         Perform Tone Mapping and Gamma Correction:
         r = (rMaximum * (6.2F * rMaximum + 0.5F)) / (rMaximum * (6.2F * rMaximum + 1.7F) + 0.06F);
         g = (gMaximum * (6.2F * gMaximum + 0.5F)) / (gMaximum * (6.2F * gMaximum + 1.7F) + 0.06F);
         b = (bMaximum * (6.2F * bMaximum + 0.5F)) / (bMaximum * (6.2F * bMaximum + 1.7F) + 0.06F);
      } else if(this.toneMappingAndGammaCorrection == TONE_MAPPING_AND_GAMMA_CORRECTION_LINEAR) {
//         Calculate the maximum component value of the 'normalized' accumulated pixel color component values:
         final float maximumComponentValue = max(r, max(g, b));
         
//         Check if the maximum component value is greater than 1.0:
         if(maximumComponentValue > 1.0F) {
//            Calculate the reciprocal of the maximum component value, such that no division is needed further on:
            final float maximumComponentValueReciprocal = 1.0F / maximumComponentValue;
            
//            Multiply the 'normalized' accumulated pixel color component values with the reciprocal of the maximum component value for Tone Mapping:
            r *= maximumComponentValueReciprocal;
            g *= maximumComponentValueReciprocal;
            b *= maximumComponentValueReciprocal;
         }
         
//         Perform Gamma Correction on the 'normalized' accumulated pixel color components:
         r = pow(r, GAMMA_RECIPROCAL);
         g = pow(g, GAMMA_RECIPROCAL);
         b = pow(b, GAMMA_RECIPROCAL);
      }
      
//      Clamp the 'normalized' accumulated pixel color components to the range [0.0, 1.0]:
      r = min(max(r, 0.0F), 1.0F);
      g = min(max(g, 0.0F), 1.0F);
      b = min(max(b, 0.0F), 1.0F);
      
//      Multiply the 'normalized' accumulated pixel color components with 255.0, to lie in the range [0.0, 255.0], so they can be displayed:
      r *= 255.0F;
      g *= 255.0F;
      b *= 255.0F;
      
//      Update the pixels array with the actual color to display it:
      this.pixels[pixelsOffset + 0] = (byte)(b);
      this.pixels[pixelsOffset + 1] = (byte)(g);
      this.pixels[pixelsOffset + 2] = (byte)(r);
      this.pixels[pixelsOffset + 3] = (byte)(255);
   }
   
   private void doRayMarching(final int pixelIndex) {
      final float[] rays = this.rays;
      
      final int pixelIndex0 = pixelIndex * 3;
      final int raysOffset = pixelIndex * SIZE_RAY;
      final int offsetOrigin = raysOffset + RELATIVE_OFFSET_RAY_ORIGIN;
      final int offsetDirection = raysOffset + RELATIVE_OFFSET_RAY_DIRECTION;
      
      final int octaves = 2;//this.octaves;
      
      float originX = rays[offsetOrigin];
      float originY = rays[offsetOrigin + 1];
      float originZ = rays[offsetOrigin + 2];
      
      float directionX = rays[offsetDirection];
      float directionY = rays[offsetDirection + 1];
      float directionZ = rays[offsetDirection + 2];
      
      float delT = 0.01F;
      
      final float minT = 0.001F;
      final float maxT = max(originY, 1.0F) * 20.0F;
      
      float lh = 0.0F;
      float ly = 0.0F;
      
      float hitT = -1.0F;
      
      float pixelColorR = 0.25F;
      float pixelColorG = 0.5F;
      float pixelColorB = 1.0F;
      
      for(float t = minT; t < maxT; t += delT) {
         final float pX = originX + directionX * t;
         final float pY = originY + directionY * t;
         final float pZ = originZ + directionZ * t;
         
         final float h = doSimplexFractalXY(octaves, pX, pZ);
         
         if(pY < h) {
            hitT = t - delT + delT * (lh - ly) / (pY - ly - h + lh);
            
//            final float surfaceNormal0X = doSimplexFractalXY(1, pX - EPSILON, pZ) - doSimplexFractalXY(1, pX + EPSILON, pZ);
//            final float surfaceNormal0Y = 2.0F * EPSILON;
//            final float surfaceNormal0Z = doSimplexFractalXY(1, pX, pZ - EPSILON) - doSimplexFractalXY(1, pX, pZ + EPSILON);
//            final float surfaceNormal0LengthReciprocal = 1.0F / sqrt(surfaceNormal0X * surfaceNormal0X + surfaceNormal0Y * surfaceNormal0Y + surfaceNormal0Z * surfaceNormal0Z);
//            final float surfaceNormal1X = surfaceNormal0X * surfaceNormal0LengthReciprocal;
//            final float surfaceNormal1Y = surfaceNormal0Y * surfaceNormal0LengthReciprocal;
//            final float surfaceNormal1Z = surfaceNormal0Z * surfaceNormal0LengthReciprocal;
            
            final float gray = max(doSimplexFractalXY(1, pX, pZ) + doSimplexFractalX(1, pY), 0.1F);
            
            if(h > 0.1F) {
               pixelColorR = 0.0F;
               pixelColorG = gray;
               pixelColorB = 0.0F;
            } else if(h > 0.02F) {
               pixelColorR = gray;
               pixelColorG = gray;
               pixelColorB = 0.0F;
            } else if(h <= 0.02F) {
               pixelColorR = 0.0F;
               pixelColorG = 0.0F;
               pixelColorB = gray;
            }
            
            t = maxT;
         }
         
         delT = 0.01F * t;
         
         lh = h;
         ly = pY;
      }
      
      this.accumulatedPixelColors[pixelIndex0] = pixelColorR;
      this.accumulatedPixelColors[pixelIndex0 + 1] = pixelColorG;
      this.accumulatedPixelColors[pixelIndex0 + 2] = pixelColorB;
   }
   
   ////////////////////////////////////////////////////////////////////////////////////////////////////
   
   private static float doGradientX(final int hash, final float x) {
      final int hash0 = hash & 0x0F;
      
      float gradient = 1.0F + (hash0 & 7);
      
      if((hash0 & 8) != 0) {
         gradient = -gradient;
      }
      
      return gradient * x;
   }
   
   private static float doGradientXY(final int hash, final float x, final float y) {
      final int hash0 = hash & 0x3F;
      
      final float u = hash0 < 4 ? x : y;
      final float v = hash0 < 4 ? y : x;
      
      return ((hash0 & 1) == 1 ? -u : u) + ((hash0 & 2) == 1 ? -2.0F * v : 2.0F * v);
   }
   
   private static int doFastFloor(final float value) {
      final int i = (int)(value);
      
      return value < i ? i - 1 : i;
   }
}





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
 
EgonOlsen (364 views)
2018-06-10 19:43:48

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

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

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

nelsongames (871 views)
2018-04-24 18:15:36

nelsongames (854 views)
2018-04-24 18:14:32

ivj94 (1306 views)
2018-03-24 14:47:39

ivj94 (426 views)
2018-03-24 14:46:31

ivj94 (1089 views)
2018-03-24 14:43:53

Solater (445 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

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
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!