
  #case capital
  #compact
  $ FileName "t.exe"
  'uses rtl32
  'uses rtl64

  string title  = "Julia Fractal Rings  (space-bar to pause, Ctrl-P snapshot)"
  int width  = 512
  int height = 512
  '
  uses OpenglSceneFrame

  int texn[1]

  '
  '
  sub MakePatternA(int n)
  =======================
  '
  %size 1024
  static long pixels[size*size]
  static long tx=size, ty=size, x, y, p, c
  '
  '
  type   rgba
    byte r,g,b,a
  end type
  '
  double range,x,y,r,sc,stp
  long   px,py,p
  rgba   tex at (@pixels)
  '
  range=28 : sc=127 : stp=range*2/size
  y=-range
  p=@pixels
  for py=1 to size
    x=-range
    for px=1 to size
      r = Cos(x) + Sin(y)      
      Tex.r = 128+sc*Cos(y*r)
      Tex.g = 128+sc*Cos(x*y*r)
      Tex.b = 128+sc*Sin(r*x)
      Tex.a = 255
      @tex+=sizeof tex 'ADVANCE TEXTURE ARRAY POINTER
      x+=stp
    Next
    y+=stp
  Next
  '
  MakeTexture @Pixels,size,size,texn[1]
  '
  end sub
  '
  sub MakeJulia(int n)
  ====================
  '
  % MAXITER    = 5 ' 20
  % MAXSIZE    = 4
  % SCR_WIDTH  = 0x400
  % SCR_HEIGHT = 0x400
  % BUF_SIZE   = 0x100000
  '
  '
  static int   vpage[BUF_SIZE]
  static float lx[SCR_WIDTH]
  static float ly[SCR_HEIGHT]
  '
  indexbase 0
  '
  int px , py 
  float p , q
  float xmin , xmax , ymin , ymax 
  float theta
  float deltax , deltay 
  float x , y
  float xsquare , ysquare
  float ytemp
  float temp1 , temp2
  int i , pixel
  sys p_buffer  
  sys p_bufferl
  static int t, frame
  float ty
  int red , grn , blu
  int tmp , i_last

  float cmag
  float cmagsq
  float zmag
  float drad
  float drad_L
  float drad_H
  float ztot
    
  xmin = -2.0
  xmax =  2.0
  ymin = -1.5
  ymax =  1.5
  '
  deltax = (xmax - xmin) / (SCR_WIDTH - 1)
  deltay = (ymax - ymin) / (SCR_HEIGHT - 1)
  '
  for i = 0 to SCR_WIDTH-1
    lx[i] = xmin + i * deltax
  next
  '
  for i = 0 to SCR_HEIGHT-1
    ly[i] = ymax - i * deltay
  next
  '
  p_buffer  = &vpage
  p_bufferl = &vpage[BUF_SIZE-1]
  '
  'ITERATION
  '
  frame = (frame + 1) and 0x7fffffff
  theta = rad(frame*.5) ' * pi() / 180.0f
  p = cos(theta) * sin(theta * .7)
  q = sin(theta) + sin(theta)
  p = p * .6
  q = q * .6
  '
  cmag = sqrt(p *p + q* q)
  cmagsq = p *p + q* q
  drad = 0.04
  drad_L = cmag - drad
  drad_L = drad_L * drad_L
  drad_H = cmag + drad
  drad_H = drad_H * drad_H
  '
  int ey=SCR_HEIGHT>>1
  for py = 0 to ey-1
    ty = ly[py]
    for px = 0 to px SCR_WIDTH-1
      '
      'JULIA ALGORITHM
      '
      x = lx[px]
      y = ty
      xsquare = 0
      ysquare = 0
      ztot = 0
      i = 0
      while (i < MAXITER)and(xsquare + ysquare < MAXSIZE)
        xsquare = x * x
        ysquare = y * y
        ytemp = x * y * 2.0
        x = xsquare - ysquare + p
        y = ytemp + q
        zmag = x * x + y * y
        if (zmag < drad_H)and(zmag > drad_L)and(i > 0)
          ztot = ztot + 1 - abs(zmag - cmagsq) / drad
          i_last = i
        endif
        i++
        if zmag > 4.0
          exit while
        endif
      wend
      if ztot > 0
        i = sqrt(ztot) * 500
      else
        i = 0
      endif
      '
      'COLOR MODULATION
      '
      if i < 256
        red = i
      else
        red = 255
      end if
      if i < 512 and i > 255
        grn = i - 256
      else
        if i >= 512
          grn = 255
        else
          grn = 0
        end if
      end if
      if i <= 768 and i > 511
        blu = i - 512
      else
        if i >= 768
          blu = 255
        else
          blu = 0
        endif
      endif
      '
      'float sca=0.33
      '>>2 == 0.25 instead
      tmp = (red+grn+blu) >>2
      tmp = tmp and 0xFF
      red = (red+grn+tmp) >>2
      red = red and 0xFF
      grn = (grn+blu+tmp) >>2
      grn = grn and 0xFF
      blu = (blu+red+tmp) >>2
      blu = blu and 0xFF
      '
      'int a=mod(i_last,3.0)
      int a=i_last and 1
      select a 
      case 0
        tmp = red
        red = grn
        grn = blu
        blu = tmp
      case 1
        tmp = red
        blu = grn
        red = blu
        grn = tmp
      end select
      '
      pixel = (blu << 16)or(grn << 8)or red
      *p_buffer  = pixel
      *p_bufferl = pixel
      p_buffer  += 4
      p_bufferl -= 4
    next
  next        
  '
  indexbase 1
  MakeTexture &vPage,SCR_WIDTH,SCR_HEIGHT,texn[1]
  '
  end sub
  '
  '
  sub Initialize(sys hWnd)
  ========================
  glGenTextures 1, texn
  'MakePatternA 1
  'MakeJulia 1
  end sub 'initialize


  sub Release(sys hWnd)
  =====================
  glDeleteTextures 1, texn
  end sub 


  sub Scene(sys hWnd)
  ===================
  '
  static single ang1, angi1=.1
  static single sx,sy,sz
  '
  SnapShots(hwnd) 'Ctrl-P take snapshot
  '
  if pause
    act=0 : sleep 50 : exit sub
  endif
  '
  glClear GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT
  glClearColor 0.5, 0, 0, 0
  glLoadIdentity
  '
  '
  'ACTIVATE TEXTURE
  '----------------
  '
  glEnable GL_TEXTURE_2D
  'glDeleteTextures 1, texn
  'glGenTextures 1, texn
  'MakePatternA 1
  MakeJulia 1
  glBindTexture GL_TEXTURE_2D,texn[1]
  '
  sx=.4 : sy=.4 : sz=-1
  '
  '
  'MOVEMENT
  '--------
  '
  'glrotatef ang1, 0,0,1
  'single x=cos(rad(ang1))*.50
  'single y=sin(rad(ang1))*.50
  'single z=0
  
  'gltranslatef x,y,z
  '
  'DRAW SHAPE
  '----------
  '
  'this quad is drawn clockwise
  '
  glbegin GL_QUADS
  '
  glTexCoord2f 0,0 : glvertex3f -sx, -sy,  sZ 'BOTTOM LEFT
  glTexCoord2f 0,1 : glvertex3f -sx,  sy,  sZ 'TOP LEFT
  glTexCoord2f 1,1 : glvertex3f  sx,  sy,  sZ 'TOP RIGHT
  glTexCoord2f 1,0 : glvertex3f  sx, -sy,  sZ 'BOTTOM RIGHT
  '
  glend
  '
  glDisable GL_TEXTURE_2D
  '
  '
  'UPDATE ROTATION ANGLES
  '----------------------
  '
  ang1+=angi1 : if ang1>=360 then ang1-=360
  '
  sleep 10
  '
  end sub


