
  includepath "$\inc\"
  $ title     "glo2 Transforms"
  % ExplicitMain
  % ColorCodedPick
  include     "OpenglSceneFrame.inc"
  include     "glo2\ShaderSupport.inc"
  includepath ""


  float v[0x400]

  indexbase 0

  sys         vbo, mat, mathole,vbohole,ele
  sys         v_vertex, v_normal, v_texcor, v_color, v_matrix
  shaderProg  ShaderMaj
  shaderProg  ShaderMin
  '


  macro MakeShaders
  =================
  '
  ShaderMin.vsrc=
  "
  #version 330 compatibility
  out vec3 normal;
  out vec2 texcoord;
  void main()
  {
    normal = normalize(gl_NormalMatrix * gl_Normal);
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    //gl_TexCoord[0] = gl_MultiTexCoord0;
    texcoord = gl_MultiTexCoord0.st;
  }
  "
  /*
  'Pass multiple texture coordinates to my shader 
  'https://www.opengl.org/discussion_boards/showthread.php/176738-Pass-multiple-texture-coordinates-to-my-shader
  glMultiTexCoord2f( GL_TEXTURE0+0, s1, t0);
  glMultiTexCoord2f( GL_TEXTURE0+1, s2, t1);
  */
  '
  ShaderMin.fsrc=
  "
  #version 330 compatibility

  uniform sampler2D texUnit0;
  in      vec3  normal;
  in      vec2  texcoord;
  out     vec4 color;

  void main ()
  
{
     //color = vec4(0.75, 0.5, 0.0, 1.0);
     color = texture2D(texUnit0, texcoord)*vec4(1.0, 1.0, 1.0, 0.6);
  
}


  "




  '
  'VERTEX SHADER
  '
  ShaderMaj.vsrc="
  #version 400
  uniform mat4 modelView;
  uniform mat4 projection;
  layout (location = 0) in   vec3 v_vertex;
  layout (location = 1) in   vec3 v_normal;
  layout (location = 2) in   vec2 v_texcor;
  layout (location = 3) in   vec3 v_tan;
  layout (location = 4) in   vec3 v_bitan;
  layout (location = 5) in   mat4 v_matrix;

  out vData {
    vec4 position;
    vec3 normal;
    vec2 texcor;
  } vertex;

  void main(void) {
    mat4 m = modelView * v_matrix;
    vertex.position = projection * m * vec4(v_vertex, 1.0);
    vertex.normal   = normalize ( transpose ( inverse ( mat3 ( m ) ) ) * v_normal );
    vertex.texcor    = v_texcor;
  }
  "
  '
  'GEOMETRY SHADER
  '
  'http://www.informit.com/articles/article.aspx?p=2120983&seqNum=2
  'https://www.opengl.org/wiki/Geometry_Shader_Examples
  '
  ShaderMaj.gsrc="
  #version 400
  uniform sampler2D texUnit0;
  layout(triangles) in;
  layout(triangle_strip, max_vertices=6) out;
  int  i;
  in vData {
    vec4 position;
    vec3 normal;
    vec2 texcor;
  } vertex[];

  out fData {
    vec4 position;
    vec3 normal;
    vec2 texcor;
  } frag;
  
  
  void main()
  {
   for(i = 0; i < gl_VerticesIn; i++ )
   {
     frag.texcor = vertex[i].texcor;
     frag.position = vertex[i].position;
     frag.normal = vertex[i].normal;
     frag.position.x = frag.position.x - 5.0; // to left
     gl_Position = frag.position;
     EmitVertex();
   }
   EndPrimitive();

   //New piece of geometry!
   vec4 v;
   for(i = 0; i < gl_VerticesIn; i++)
   {
     v = vertex[i].position;
     // displace along the normal
     // v = v + 0.5 * vec4(vertex[i].normal,0.0);
     v.x *= 0.5; // shrink
     v.x +=  5.0; // to right
     gl_Position = v;
     frag.normal = vertex[i].normal;
     frag.texcor = vertex[i].texcor;
     EmitVertex();
   }
   EndPrimitive();

  }
  "
  'ShaderMaj.gsrc="" 'if no extra geometry!
  '
  'FRAGMENT SHADER
  '
  ShaderMaj.fsrc="
  #version 400
  uniform sampler2D texUnit0;   // TEXTURE MAP COLOR RGBA
  uniform sampler2D texUnit1;   // TEXTURE MAP NORMAL AND BUMP XYZB
  uniform vec4      tint0;      // TINT INCL ALPHA
  uniform vec4      tint1;      // FLAT COLOR RGBA
  uniform vec4[4]   light0;     // LIGHT RGBA
  uniform vec4      direction0; // LIGHT DIRETION XYZ
  in fData {
    vec4 position;
    vec3 normal;
    vec2 texcor;
  } frag;

  layout(location = 0) out vec4 color;
  //layout(location = 1) out vec3 normal_tangentspace; // additional output
  void main(void) {
    if (tint1.a>0.0) {color=tint1;} // FLAT COLOR FOR PICK-LABELS
    else {
      vec4  ct=texture2D(texUnit0, frag.texcor);              //COLOR TEXTURE
      vec4  nt=texture2D(texUnit1, frag.texcor);              //NORMAL TEXTURE
      float ni = max(0.0, dot(vec3(nt), vec3(direction0) ) );
      float di = max(0.0, dot(normalize(frag.normal), vec3(direction0)  ) )*ni*nt.a;
      float si = clamp(pow(di,0.3*light0[3].r), 0.0, 1.0); //specular with shiny term
      vec4 light=light0[0]+light0[1]*di+light0[2]*si; //combine ambient diffuse specular
      color = ct*light * tint0;
      //FOGGING
        float fadef = ( frag.position.z - 10.0 ) / 15.0;
        vec4  fadec = vec4 ( 0.7, 0.7 , 0.7, 0.7 ) * fadef;
        color = ( color * ( 1.0 - fadef ) ) +  ( fadec * fadef );
      //TRANSPARENCY
        if (color.a > tint0.a) {color.a = tint0.a;};
    }
  }
  "


  'COMPILE AND LINK
  =================
  '
  CreateShaderProg ShaderMin : if ShaderError then exit sub
  ShaderMin.t0  = glGetUniformLocation(ShaderMin.ph, "texUnit0"    )
  '
  CreateShaderProg ShaderMaj : if ShaderError then exit sub
  ShaderMaj.m0  = glGetUniformLocation(ShaderMaj.ph, "projection"  )
  '
  ShaderMaj.m1  = glGetUniformLocation(ShaderMaj.ph, "modelView"   )
 'ShaderMaj.n0  = glGetUniformLocation(ShaderMaj.ph, "normalMatrix")
  ShaderMaj.t0  = glGetUniformLocation(ShaderMaj.ph, "texUnit0"    )
  ShaderMaj.t1  = glGetUniformLocation(ShaderMaj.ph, "texUnit1"    )
 'ShaderMaj.t2  = glGetUniformLocation(ShaderMaj.ph, "texUnit2"    )
 'ShaderMaj.t3  = glGetUniformLocation(ShaderMaj.ph, "texUnit3"    )
  ShaderMaj.v0  = glGetUniformLocation(ShaderMaj.ph, "tint0"       )
  ShaderMaj.v1  = glGetUniformLocation(ShaderMaj.ph, "light0"      )
  ShaderMaj.v2  = glGetUniformLocation(ShaderMaj.ph, "direction0"  )
  ShaderMaj.v3  = glGetUniformLocation(ShaderMaj.ph, "tint1"  ) 'flat color
  '
  'UNIFORM DATA
  'glUniformMatrix4fv(ShaderMaj.m0, 1, GL_FALSE, Projection   )
  'glUniformMatrix4fv(ShaderMaj.m1, 1, GL_FALSE, MatStack     )
  'glUniformMatrix3fv(ShaderMaj.n0, 1, GL_FALSE, NormalMatrix )
  '
  end macro

  macro ReleaseShaders
  ==================
  ReleaseShaderProg ShaderMin
  ReleaseShaderProg ShaderMaj
  end macro


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

  sub Release(sys hwnd)
  '====================
  ReleaseShaders
  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
  glClearColor BackGroundColor
  main
  EndPick
  end sub

  sub PickLabel(int c) 'override
  ====================
  static float nc[4]
  if pick then
    float r,g,b,d=1/255
    int a
    a=c and 0xff : r=a*d : c=c>>8
    a=c and 0xff : g=a*d : c=c>>8
    a=c and 0xff : b=a*d
    ShaderVector4 ShaderMaj.v3, 1, float {r,g,b,1} 'sets flat color for label
  else
    ShaderVector4 ShaderMaj.v3, 1, nc 'no flat color
  end if
  end sub


  macro PickerBackGround()
  ========================
  float tm[16]
  if pick then
    picklabel 0xff00000
    loadidentity
    savematrix tm
    ShaderMatrix4 ShaderMaj.m1, 1, GL_FALSE, tm 'modelView
    translatef 0,0,-1000
    scalef 10000,10000,10000
    savematrix tm
    boa.clearmatrix
    boa.putmatrix tm,16
    boa.render 'using panel as background
  end if
  end macro
  '
