1. -- Change log: 
  2.  
  3.  -- RK  18 - Oct - 2006 : removed initialisation by dynamic allocation, 
  4.  --                  to avoid memory leaks 
  5.  
  6. with GLU; 
  7.  
  8. package body GLOBE_3D.Software_Anti_Aliasing is 
  9.  
  10.   use REF; 
  11.  
  12.   type Jitter_matrix is 
  13.      array (Positive range <>, Positive range <>) of GL.Double; 
  14.  
  15.   type p_Jitter_matrix is access all Jitter_matrix; 
  16.  
  17.   -- Matrices for anti - aliasing (choice : matrix J at the end of Jitter) : 
  18.  
  19.   J3  : aliased Jitter_matrix := ( 
  20.     ((0.5, 0.5), 
  21.      (1.35899e-05, 0.230369), 
  22.      (0.000189185, 0.766878))); 
  23.  
  24.   J4 : aliased Jitter_matrix := ( 
  25.     (0.375, 0.23), (0.123, 0.77), 
  26.      (0.875, 0.27), (0.627, 0.73)); 
  27.  
  28.   J11  : aliased Jitter_matrix := ( 
  29.     ((0.5, 0.5), (0.406537, 0.135858), 
  30.      (0.860325, 0.968558), (0.680141, 0.232877), 
  31.      (0.775694, 0.584871), (0.963354, 0.309056), 
  32.      (0.593493, 0.864072), (0.224334, 0.415055), 
  33.      (0.0366643, 0.690884), (0.139685, 0.0313988), 
  34.      (0.319861, 0.767097))); 
  35.  
  36.   J16  : aliased Jitter_matrix := ( 
  37.     ((0.4375, 0.4375), (0.1875, 0.5625), 
  38.      (0.9375, 1.1875), (0.4375, -0.0625), 
  39.      (0.6875, 0.5625), (0.1875, 0.0625), 
  40.      (0.6875, 0.3125), (0.1875, 0.3125), 
  41.      (0.4375, 0.1875), (-0.0625, 0.4375), 
  42.      (0.6875, 0.8125), (0.4375, 0.6875), 
  43.      (0.6875, 0.0625), (0.9375, 0.9375), 
  44.      (1.1875, 0.8125), (0.9375, 0.6875))); 
  45.  
  46.   J29  : aliased Jitter_matrix := ( 
  47.     ((0.5, 0.5), (0.498126, 0.141363), 
  48.      (0.217276, 0.651732), (0.439503, 0.954859), 
  49.      (0.734171, 0.836294), (0.912454, 0.79952), 
  50.      (0.406153, 0.671156), (0.0163892, 0.631994), 
  51.      (0.298064, 0.843476), (0.312025, 0.0990405), 
  52.      (0.98135, 0.965697), (0.841999, 0.272378), 
  53.      (0.559348, 0.32727), (0.809331, 0.638901), 
  54.      (0.632583, 0.994471), (0.00588314, 0.146344), 
  55.      (0.713365, 0.437896), (0.185173, 0.246584), 
  56.      (0.901735, 0.474544), (0.366423, 0.296698), 
  57.      (0.687032, 0.188184), (0.313256, 0.472999), 
  58.      (0.543195, 0.800044), (0.629329, 0.631599), 
  59.      (0.818263, 0.0439354), (0.163978, 0.00621497), 
  60.      (0.109533, 0.812811), (0.131325, 0.471624), 
  61.      (0.0196755, 0.331813))); 
  62.  
  63.   J90  : aliased Jitter_matrix := ( 
  64.     ((0.5, 0.5), (0.784289, 0.417355), 
  65.      (0.608691, 0.678948), (0.546538, 0.976002), 
  66.      (0.972245, 0.270498), (0.765121, 0.189392), 
  67.      (0.513193, 0.743827), (0.123709, 0.874866), 
  68.      (0.991334, 0.745136), (0.56342, 0.0925047), 
  69.      (0.662226, 0.143317), (0.444563, 0.928535), 
  70.      (0.248017, 0.981655), (0.100115, 0.771923), 
  71.      (0.593937, 0.559383), (0.392095, 0.225932), 
  72.      (0.428776, 0.812094), (0.510615, 0.633584), 
  73.      (0.836431, 0.00343328), (0.494037, 0.391771), 
  74.      (0.617448, 0.792324), (0.688599, 0.48914), 
  75.      (0.530421, 0.859206), (0.0742278, 0.665344), 
  76.      (0.979388, 0.626835), (0.183806, 0.479216), 
  77.      (0.151222, 0.0803998), (0.476489, 0.157863), 
  78.      (0.792675, 0.653531), (0.0990416, 0.267284), 
  79.      (0.776667, 0.303894), (0.312904, 0.296018), 
  80.      (0.288777, 0.691008), (0.460097, 0.0436075), 
  81.      (0.594323, 0.440751), (0.876296, 0.472043), 
  82.      (0.0442623, 0.0693901), (0.355476, 0.00442787), 
  83.      (0.391763, 0.361327), (0.406994, 0.696053), 
  84.      (0.708393, 0.724992), (0.925807, 0.933103), 
  85.      (0.850618, 0.11774), (0.867486, 0.233677), 
  86.      (0.208805, 0.285484), (0.572129, 0.211505), 
  87.      (0.172931, 0.180455), (0.327574, 0.598031), 
  88.      (0.685187, 0.372379), (0.23375, 0.878555), 
  89.      (0.960657, 0.409561), (0.371005, 0.113866), 
  90.      (0.29471, 0.496941), (0.748611, 0.0735321), 
  91.      (0.878643, 0.34504), (0.210987, 0.778228), 
  92.      (0.692961, 0.606194), (0.82152, 0.8893), 
  93.      (0.0982095, 0.563104), (0.214514, 0.581197), 
  94.      (0.734262, 0.956545), (0.881377, 0.583548), 
  95.      (0.0560485, 0.174277), (0.0729515, 0.458003), 
  96.      (0.719604, 0.840564), (0.325388, 0.7883), 
  97.      (0.26136, 0.0848927), (0.393754, 0.467505), 
  98.      (0.425361, 0.577672), (0.648594, 0.0248658), 
  99.      (0.983843, 0.521048), (0.272936, 0.395127), 
  100.      (0.177695, 0.675733), (0.89175, 0.700901), 
  101.      (0.632301, 0.908259), (0.782859, 0.53611), 
  102.      (0.0141421, 0.855548), (0.0437116, 0.351866), 
  103.      (0.939604, 0.0450863), (0.0320883, 0.962943), 
  104.      (0.341155, 0.895317), (0.952087, 0.158387), 
  105.      (0.908415, 0.820054), (0.481435, 0.281195), 
  106.      (0.675525, 0.25699), (0.585273, 0.324454), 
  107.      (0.156488, 0.376783), (0.140434, 0.977416), 
  108.      (0.808155, 0.77305), (0.282973, 0.188937))); 
  109.  
  110.   J : p_Jitter_matrix; 
  111.  
  112.   function Anti_Alias_phases return Positive is 
  113.   begin 
  114.     if J = null then 
  115.       return 1; 
  116.     else 
  117.       return J'Length (1) + 2; 
  118.     end if; 
  119.   end Anti_Alias_phases; 
  120.  
  121.   procedure Display_with_Anti_Aliasing (phase : Positive) is 
  122.  
  123.     procedure Jitter is 
  124.       Dxy  : array (J'Range (2)) of GL.Double; 
  125.       weight : constant GL.C_Float := 1.0 / GL.C_Float (J'Length (1)); 
  126.  
  127.       procedure LoadDxDy (jt : Positive) is 
  128.         view : aliased GLU.Viewport_Rec; 
  129.         inv : array (1 .. 2) of GL.Double; 
  130.       begin 
  131.         -- GLU.Get (VIEWPORT, view'unrestricted_access); 
  132.         GLU.Get (view); 
  133.         inv (1) := 10.0 / GL.Double (view.Width); 
  134.         inv (2) := 10.0 / GL.Double (view.Height); 
  135.         for d in Dxy'Range loop 
  136.           Dxy (d) := (J.all (jt, d) - 0.25) * inv (d); 
  137.         end loop; 
  138.       end LoadDxDy; 
  139.  
  140.     begin 
  141.       if phase = 1 then 
  142.           Clear (COLOR_BUFFER_BIT or ACCUM_BUFFER_BIT); 
  143.       elsif phase in 2 .. Anti_Alias_phases - 1 then 
  144.           Clear (COLOR_BUFFER_BIT); 
  145.           LoadDxDy (phase - 1); 
  146.           MatrixMode (MODELVIEW); 
  147.           PushMatrix; 
  148.           Translate (Dxy (1), Dxy (2), 0.0); 
  149.           Display; 
  150.           MatrixMode (MODELVIEW); 
  151.           PopMatrix; 
  152.           Accum (ACCUM, weight); 
  153.       elsif phase = Anti_Alias_phases then 
  154.           Accum (GL_RETURN, 1.0); 
  155.           -- ^ Transfers accumulation buffer values to the color buffer or 
  156.           --   buffers currently selected for writing. 
  157.           Flush; 
  158.       else 
  159.           raise Constraint_Error; 
  160.       end if; 
  161.     end Jitter; 
  162.   begin 
  163.     if J = null then 
  164.       Clear (COLOR_BUFFER_BIT); 
  165.       Display; 
  166.       Flush; 
  167.     else 
  168.       Jitter; 
  169.     end if; 
  170.   end Display_with_Anti_Aliasing; 
  171.  
  172.   procedure Set_Quality (q : Quality) is 
  173.   begin 
  174.     case q is 
  175.       when Q1  => J := null; 
  176.       when Q3  => J := J3'Access; 
  177.       when Q4  => J := J4'Access; 
  178.       when Q11 => J := J11'Access; 
  179.       when Q16 => J := J16'Access; 
  180.       when Q29 => J := J29'Access; 
  181.       when Q90 => J := J90'Access; 
  182.     end case; 
  183.   end Set_Quality; 
  184.  
  185. begin 
  186.   Set_Quality (Q3); 
  187. end GLOBE_3D.Software_Anti_Aliasing;