
  uses glo2\Shaders 'sky


  def MakeShaders
  ===============
  '
  ShaderMin.vsrc=
  "
  #version 440
  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 440

  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);
  
  }


  "


  ShaderPostPro.vsrc=
  "
  #version 440 core
  layout (location = 0) in vec3 aPos;
  layout (location = 2) in vec2 aTexCoords;

  out vec2 TexCoords;

  void main()
  {
      gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); 
    TexCoords = aTexCoords;
  }  
  "


  ShaderPostPro.fsrc=
  "
  #version 440 core
  out vec4 Color;
  
  in vec2 TexCoords;

  uniform sampler2D screenTexture;

  void main()
  { 
    Color = texture(screenTexture, TexCoords);
  }
  "
 





  '
  'VERTEX SHADER
  '
  ShaderMaj.vsrc="
  #version 440
  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 mm = modelView * v_matrix;
    vertex.position = projection * mm * vec4(v_vertex, 1.0);
    vertex.normal   = normalize ( transpose ( inverse ( mat3 ( mm ) ) ) * 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 440
  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 440
  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
  uniform vec4      fog0; // FOG COLOR RGB AND DISTANCE A

  in fData {
    vec4 position;
    vec3 normal;
    vec2 texcor;
  } frag;

  layout(location = 0) out vec4 color;

  //layout(location = 1) out vec3 normal_tangentspace; // additional output

  vec4 fadec;
  float fadef;
  void main(void) {
    if (tint1.a>0.0) {color=tint1;} // FLAT COLOR FOR PICK-LABELS
    else {
      vec4  ct=texture(texUnit0, frag.texcor);  //COLOR TEXTURE
      vec4  nt=texture(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
        fadef = -frag.position.z / fog0.a;
        fadec = vec4( fog0.r, fog0.g , fog0.b, color.a );
        color = ( color * vec4( 1.0 - fadef ) ) +  vec4( fadec * fadef );
      //TRANSPARENCY
        if (color.a > tint0.a) {color.a = tint0.a;};
    }
  }
  "

  'COMPILE AND LINK
  =================
  '
  $ GUL glGetUniformLocation

  CreateShaderProg ShaderMin : if ShaderError then exit sub
  'UNIFORM VARIABLES
  ShaderMin.t0  = GUL(ShaderMin.ph, "texUnit0"    )
  '
  CreateShaderProg ShaderPostPro : if ShaderError then exit sub
  'UNIFORM VARIABLES
  ShaderPostPro.t0  = GUL(ShaderPostPro.ph, "screenTexture" )
  '
  CreateShaderProg ShaderMaj : if ShaderError then exit sub
  'UNIFORM VARIABLES
  ShaderMaj.m0  = GUL(ShaderMaj.ph, "projection"  )
  ShaderMaj.m1  = GUL(ShaderMaj.ph, "modelView"   )
 'ShaderMaj.n0  = GUL(ShaderMaj.ph, "normalMatrix")
  ShaderMaj.t0  = GUL(ShaderMaj.ph, "texUnit0"    ) 'color map
  ShaderMaj.t1  = GUL(ShaderMaj.ph, "texUnit1"    ) 'normal map
 'ShaderMaj.t2  = GUL(ShaderMaj.ph, "texUnit2"    )
 'ShaderMaj.t3  = GUL(ShaderMaj.ph, "texUnit3"    )
  ShaderMaj.v0  = GUL(ShaderMaj.ph, "tint0"       ) 'color bias
  ShaderMaj.v1  = GUL(ShaderMaj.ph, "light0"      ) 'light color
  ShaderMaj.v2  = GUL(ShaderMaj.ph, "direction0"  ) 'light direction
  ShaderMaj.v3  = GUL(ShaderMaj.ph, "tint1"  )      'flat color (pick mode)
  ShaderMaj.v4  = GUL(ShaderMaj.ph, "fog0"  )       'fog color and dist limit
  '
  end def 'MakeShaders


  def ReleaseShaders
  ==================
  ReleaseShaderProg ShaderMin
  ReleaseShaderProg ShaderPostPro
  ReleaseShaderProg ShaderMaj
  end def

  def BuildSky
  ============
    static float  ang1,angi1=.5

    static int    SkyShader
    static int    SkyTexture
    static int    SkyUL[4]
    '
    'glClientActiveTexture(GL_TEXTURE0)
    '
    LoadCubicTextures (
    "..\..\images\SkyBox\left.jpg",
    "..\..\images\SkyBox\right.jpg",
    "..\..\images\SkyBox\bottom.jpg",
    "..\..\images\SkyBox\top.jpg",
    "..\..\images\SkyBox\front.jpg",
    "..\..\images\SkyBox\back.jpg",
    SkyTexture
    )
    '
    % skyboxi 13

    shaders()
    CreateShaderProg ps[skyboxi]
    SkyShader = ps[skyboxi].ph
    SkyUL[1] = glGetUniformLocation(SkyShader, "CubeMap")
    SkyUL[2] = glGetUniformLocation(SkyShader, "Projection")
    SkyUL[3] = glGetUniformLocation(SkyShader, "ModelView")
    BuildSkyBox() 'Cube buffer
    end Def


  def RenderSky
  =============

  ShaderProgram SkyShader

  'SET PROJECTION
  int ortho=0
  SetProjectionMatrix projection, 45, aspect, 1, 1e4, ortho
  'GENERATE TRANSFORM MATRICES
  ShaderMatrix4 ShaderMaj.m0, 1, GL_FALSE, projection  'PROJECTION

  'SET MODEL VIEW
  LoadIdentity
  if ortho
    scalef 0.2,0.2,0.2      'correction for ortho scale
  endif

  Rotatef 30,-1,0,0     'x tilt
  Rotatef Ang1*.4,0,1,0 'y spin
  Scalef 1000,1000,1000
  savematrix modelView
  '
  ShaderMatrix4 SkyUL[2], 1, GL_FALSE, projection  'PROJECTION
  ShaderMatrix4 SkyUL[3], 1, GL_FALSE, modelView   'MODELVIEW
  DrawSkyBox(SkyTexture,SkyShader)

  end def

