  '% BackGroundColor 0 , 0 , 0.2, 0.0

  $ title     "glo2 Transforms"
  % ExplicitMain
  % ColorCodedPick

  uses OpenglSceneFrame
  uses glo2\ShaderSupport



  sub initialize(sys hWnd)
  '=======================
  SetupExtFunctions 'ShaderSupport
  GDIplus 1
  GenTextures 32
  end sub

  sub Release(sys hwnd)
  '====================
  DeleteTextures 32
  GDIplus 0
  end sub

  %% BackGroundColor 0.5, 0.5, 0.7, 0.0

  sys gwnd
  ! main()

  sub scene(sys hwnd)
  ===================
  gwnd=hwnd
  BeginPick
  ActiveFrame
  if pick then
    'glLoadIdentity
    PickSetup
  else
    glClearColor BackGroundColor
  end if
  if ShaderError then exit sub
  main
  EndPick
  end sub

  '
  macro BuildShader
  =================

  static ShaderProg ShaderJP

  ShaderJP.vsrc=
  "
  void main() {	
  gl_TexCoord[0] = gl_MultiTexCoord0;
  gl_Position = ftransform();		
  }
  "
  ShaderJP.fsrc=
  "
  // Originally created for WebGL by aiekick @ www.shadertoy.com
  // Optimized for low GPU and OpenGL by ML @ www.oxygenbasic.org

  uniform vec2 iResolution;  // viewport resolution (in pixels)
  uniform float iGlobalTime; // shader playback time (in seconds)

  #define particles_count 5.
  #define k 0.05
  #define threshold 6.1

  #define zmul(a, b) vec2(a.x * b.x - a.y * b.y, a.x * b.y + b.x * a.y)
  #define zinv(a) vec2(a.x, -a.y) / dot(a, a)

  // ML: !!! uncomment one or none of these !!!
  #define POINTPARTICLES
  //#define TRIPARTICLES
  //#define RINGPARTICLES

  void main(void) {
  vec2 g = gl_FragCoord.xy;
  vec2 si = iResolution.xy;
  float t = iGlobalTime;

  vec3 stars = vec3(0);
  vec2 z = (g + g - si) / min(si.x, si.y) * 2.;
  vec2 c = vec2(0.66, 1.23);

  float r = 0.;
  float pi_over_count = 3.14159 / (particles_count * k);

  for (float i = 0.; i < 100.; i++) {
    if (r > threshold) break;
    r = dot(z, z);
    z = zinv((zmul(z, z) + c));
    
    vec3 col = mix(vec3(0.5, 0, 0.5), vec3(0, 1, 0.29), i / 18.);
    vec3 acc = vec3(0);
    
    for (float j = 0.; j < particles_count; j++) {
      float tt = t + j / pi_over_count;
      vec2 b = vec2(cos(tt), sin(tt)) * 2.5;
      vec2 df = z - b; // ML: 1-point particles if nothing else defined
      
      #ifdef POINTPARTICLES
        df = abs(df) - 0.05; // 4-point particles
      #elif defined(TRIPARTICLES)
        df = vec2(max(abs(df.x) + df.y, -df.y)) - 0.1; // tri-shaped particles
      #elif defined(RINGPARTICLES)
        df = vec2(length(df)) - 0.1; // ring-shaped particles
      #endif
      
      acc += col / r / 8. / dot(df, df);
    }
    stars += acc / particles_count / 0.75;
  }
  
  gl_FragColor = vec4(stars * 0.3, 1.0);
  }
  "

  ShaderJP.create
  % uniform       %1=glGetUniformLocation(ShaderJP.ph, %2)
  % iResolution   ShaderJP.v4
  % iGlobalTime   ShaderJP.v5
  if ShaderError=0 then
    uniform iResolution "iResolution"
    uniform iGlobalTime "iGlobalTime"
  end if
  end macro



  sub main
  ========

  if opening=0 then goto EachFrame

  BuildShader

  EachFrame:
  ==========
  '
  static float gtime
  shaderJP.use
  RECT r
  GetClientRect gwnd,r
  ShaderVector2 iResolution, 1, float {r.right,r.bottom} 'resolution in viewport pixels
  ShaderVector1 iGlobalTime, 1, float {gtime}            'time (seconds)
  gtime+=1/60
  glTranslatef 0,0,-2.4
  'QUAD
  glBegin GL_QUADS
  glVertex3f -1.0, -1.0, 0.0
  glVertex3f  1.0, -1.0, 0.0
  glVertex3f  1.0,  1.0, 0.0
  glVertex3f -1.0,  1.0, 0.0
  glEnd
  ShaderProgram -1

  if closing then
    ShaderJP.release
  end if
  '
  end sub


  MainWindow width,height,WS_OVERLAPPEDWINDOW
