// A POV-Ray interpretation of the Lua logo.
// by Philippe Lhoste <PhiLho at GMX dot net> http://phi.lho.free.fr
// v. 1.1 -- 2002/08/06 -- flatRendering switch, added finish.
// v. 1.0 -- 2002/08/01 -- Original release
// Graphic design by A. Nakonechny. Copyright © 1998. All rights reserved.

// +W200 +H200 +Fn
// Bugs of the scene: the blue is too bright, and if non hollow, the fill-ins are not pure white.

#version 3.5;

#declare flatRendering = true;

#if (flatRendering)
  // Try to look like the official logo as much as possible
  #declare logoThickness = 0.01;
  #declare nonHollowLogo = false;
#else
  // Change these to alter the appearance of the logo
  #declare logoThickness = 0.5;
  #declare nonHollowLogo = true;
#end

global_settings
{
  assumed_gamma 1
}

camera
{
  #if (flatRendering)
    location <0, 0, -3>
    look_at  <0, 0, 0>
  #else
//    location <3, 0.75, 0>
    location <3, 2, -4>
    look_at  <0.4, 0.2, 0>
    angle 30
  #end
  right x*image_width/image_height
}

light_source
{
  #if (flatRendering)
    <0, 0, -10>
    color 1
    shadowless
  #else
    <-10, 30, -10>
    color 1
  #end
}
background { color rgb <1, 1, 1> }

// Change only for special effects... Official colors.
#declare logoColor = rgb <0, 0, 123/255>; // Dark blue
#declare orbitColor = rgb <0.5, 0.5, 0.5>;  // Grey

#macro Finish()
  #if (flatRendering)
    finish { ambient 0.2 diffuse 0.7 brilliance 1 }
  #else
    finish { ambient 0.5 diffuse 0.5 brilliance 0.7 }
  #end
#end

// These should not be changed: geometry of the logo
#declare satelliteRadius = 0.015;
#declare cutterNb = 36;

#declare moonRadius = 1-sqrt(2)/2;
#declare orbitRadius = 1 + moonRadius;

#declare logoText =
  object
  {
    text
    {
      ttf "Arial.ttf" "Lua" logoThickness, 0
      scale 0.9*(x + y) + z
    }
  };

#macro GetCenterOfText(txt)
  #local minTxt = min_extent(txt);
  #local maxTxt = max_extent(txt);
  (minTxt + (maxTxt - minTxt) / 2)
#end

union
{
  difference
  {
    // Planet
    cylinder
    {
      0, -logoThickness*y, 1
      rotate -90*x
    }
    // Hole
    cylinder
    {
      logoThickness*y, -logoThickness*2*y, moonRadius
      rotate -90*x
      translate <1-2*moonRadius, 1-2*moonRadius, 0>
    }
    // Logo
    object
    {
      logoText
      scale z*3 + x+y
      translate <-GetCenterOfText(logoText).x, -0.5, -logoThickness>
    }
  }
  // Moon
  cylinder
  {
    0, -logoThickness*y, moonRadius
    rotate -90*x
    translate <1, 1, 0>
  }
  pigment { color logoColor }
  Finish()
}

#declare baseCutterAngle = 360 / cutterNb;
#declare cutterRadius = orbitRadius*2*pi / cutterNb / 4;

// Orbit
difference
{
  torus
  {
    orbitRadius, satelliteRadius
    rotate -90*x
  }
  // Cut off around the moon
  cylinder
  {
    0, satelliteRadius*3*y, moonRadius*1.2
    rotate 90*x
    translate <1, 1, -satelliteRadius>
  }
  // Cut off to get dash effect
  #while (cutterNb > 0)
    sphere
    {
      0, cutterRadius
      translate x*orbitRadius
      rotate z * cutterNb * baseCutterAngle
      #declare cutterNb = cutterNb - 1;
    }
  #end
  #if (flatRendering)
    // Orbit near the surface of the logo, otherwise with thick versions,
    // the apparent diameter of the orbit diminishes.
    translate z*satelliteRadius
  #else
    // Center the orbit on the thickness of the logo
    translate z*logoThickness/2
  #end
  pigment { color orbitColor }
//  no_shadow
}

#if (nonHollowLogo)
  // Hole
  cylinder
  {
    0, logoThickness*y, moonRadius
    rotate 90*x
    translate <1-2*moonRadius, 1-2*moonRadius, 0>
    pigment { rgb <1, 1, 1> }
    Finish()
  }
  // Logo
  object
  {
    logoText
    translate <-GetCenterOfText(logoText).x, -0.5, 0>
    pigment { rgb <1, 1, 1> }
    Finish()
  }
#end