Site3D.java

Java source for Expanding blurred XOR pattern, 285 lines. Raw file

import java.awt.image.*;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Event;
import java.awt.Image;
import java.awt.Font;
import java.util.Vector;  //  http://java.sun.com/products/jdk/1.1/docs/api/java.util.Vector.html




//---- MAIN CLASS ------------------------------------------------------------//

public class Site3D extends tinyptc
{
  //---- DATA
  
  //- Output buffer

  int size;

  int[] buf;
  int[] tex;

  int mouse_x = 0, mouse_y = 0;
  boolean mouse_click = false;



  //---- rand
  
  //  Lewpen's random number algorithm - Creates random real numbers within a given range

  int rand_1 = 0x87361872, rand_2 = 0x12487, rand_3 = 0x8129743;

  public double rand(double min, double max)
  {
    rand_1 += rand_2^rand_3;  //  Lewpen's algo
    rand_2 ^= rand_3*rand_1;
    rand_3 *= rand_1+rand_2;
    
    rand_1 = (rand_1 << 2) | (rand_1 >> 30);  //  Rotate seeds by different ammounts
    rand_2 = (rand_2 << 7) | (rand_2 >> 25);
    rand_3 = (rand_3 << 9) | (rand_3 >> 23);

    rand_1 += 0x15214391;  //  Randomize the bits somewhat
    rand_2 += 0xF2248365;
    rand_3 += 0x42C427AA;

    //  Create random number 0..1234566;
    
    int r = ((rand_1+rand_2+rand_3)/3) % 1234567;

    //  Scale to the range [min..max)

    return min + (max-min)*(r+0.0) / 1234567.0;
  }



  //---- makeNoise
  
  public int[] makeNoise()
  {
    int[] tex = new int[256*256];
    
    int i, j;
    int x, y;
    
    for(j=0; j<256; j++)
      for(i=0; i<256; i++)
      {
        x = (int)
            (
              128 +
              93*Math.sin(-i*3.14159/ 64 +j*3.14159/128 + 0.2481) + 
              51*Math.sin( i*3.14159/128 -j*3.14159/ 64 + 2.6812)
            );

        y = (int)
            (
              128 +
              53*Math.sin( i*3.14159/256 -j*3.14159/ 32 + 1.6171) + 
              84*Math.sin( i*3.14159/ 64 +j*3.14159/128 + 0.1536)
            );
            
        x = i;
        y = j;

        tex[j*256+i] = (((x&255)^(y&255))>>5)*16;
      }

    int k;

   
    for(k=0; k<64; k++)
    {
      for(j=0; j<256; j++)
        for(i=0; i<256; i++)
          tex[j*256+i] = (
                           (tex[(j)*256+(i)]<<2) +
                           (
                             tex[((j-1)&255)*256+((i  )&255)] +
                             tex[((j+1)&255)*256+((i  )&255)] +
                             tex[((j  )&255)*256+((i-1)&255)] +
                             tex[((j  )&255)*256+((i+1)&255)]
                           ) + 4
                         )>>3;
      for(j=0; j<256; j++)
        for(i=0; i<256; i++)
          tex[j*256+i] = (
                           (tex[(j)*256+(i)]<<3) +
                           (
                             tex[((j-1)&255)*256+((i  )&255)] +
                             tex[((j+1)&255)*256+((i  )&255)] +
                             tex[((j  )&255)*256+((i-1)&255)] +
                             tex[((j  )&255)*256+((i+1)&255)] +
                             tex[((j-1)&255)*256+((i-1)&255)] +
                             tex[((j+1)&255)*256+((i-1)&255)] +
                             tex[((j+1)&255)*256+((i+1)&255)] +
                             tex[((j-1)&255)*256+((i+1)&255)]
                           ) + 8
                         )>>4;
    }

    
    return tex;
  }



  //---- main
  
  //  Main loop

  public void main(int width, int height)
  {
    size = width * height;
    buf = new int[size];

    int i, j, k;

    //- Make smoke texture
    
    tex = makeNoise();

    double px=0, py=0, pz=0, zz=0;

    double x, y, z, scale, oscale;

    int ox, oy;
    int sox, soy;
    int pox = 0, poy = 0;
        
    //- Anim loop

    while(true)
    {
      //- Move
      
      if(true || mouse_click)
      {
        zz += 0.1;
        pz = zz - (int)zz;
      }

/*
      px = (mouse_x-128);
      py = (mouse_y-128);
*/
      pox = (-mouse_x);
      poy = (-mouse_y);

      
      //- Render
     
      int mx = mouse_x, my = mouse_y;
      
      mx = 128;
      my = 128;
      
 
      double z1 = 0.237, z2 = 3.198;
      
      for(i=0; i<size; i++) buf[i] = 0;

      int maxk = 4;

      for(k=0; k<maxk; k++)
      {
        z = z1 + (z2-z1) * (1+k-pz) / (0.0+maxk);

        scale = 1.0 / z;

        oscale = 1.0 / scale;

        ox = (int)(-px*oscale);//(int)(-0.125*width*oscale);
        oy = (int)(-py*oscale);//(int)(-0.125*height*oscale);

        sox = (int)(pox*oscale);
        soy = (int)(poy*oscale);
        

        sox += (k+(int)zz)*637*(k+(int)zz) - 79*(k+(int)zz) + 2;
        soy += (k+(int)zz)*73*(k+(int)zz) + 181*(k+(int)zz) - 89;

        int fade = 255;

        fade = (int)(255*(0.5-0.5*Math.cos( (z-z1) * 2.0 * 3.1415926 / (z2-z1) )));

        if(fade < 0) fade = 0;
        if(fade > 255) fade = 255;

        for(j=0; j<height; j++)
        {
          for(i=0; i<width; i++)
          {
            int c = tex[((soy+(int)(oscale*(double)(j)))&255)*256+((sox+(int)(oscale*(double)(i)))&255)];
            
            c = (c*fade)>>8;
            
            buf[j*width+i] += c;
          }
        }
      
      }


//      for(i=0; i<size-width-1; i++) buf[i] = (buf[i]+buf[i]-buf[i+1]-buf[i+width] + 128);
      for(i=0; i<size; i++) buf[i] *= 0x01010101;

      update(buf);
    }
  }


  //---- mouseMove

  public boolean mouseMove(Event e, int x, int y)
  {
    mouse_x = x;
    mouse_y = y;
    return true;
  }



  //---- mouseDrag

  public boolean mouseDrag(Event e, int x, int y)
  {
    mouse_x = x;
    mouse_y = y;
    return true;
  }



  //---- mouseDown

  public boolean mouseDown(Event e, int x, int y)
  {
    mouse_click = true;
    return true;
  }



  //---- mouseUp

  public boolean mouseUp(Event e, int x, int y)
  {
    mouse_click = false;
    return true;
  }


}



//---- end of code -----------------------------------------------------------//