1. -- 
  2. -- Jan & Uwe R. Zimmer, Australia, July 2011 
  3. -- 
  4.  
  5. with Ada.Numerics;                      use Ada.Numerics; 
  6. with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions; 
  7.  
  8. with Graphics_Configuration;             use Graphics_Configuration; 
  9. with Graphics_Data;                     use Graphics_Data; 
  10. with Vectors_2D_N;                      use Vectors_2D_N; 
  11.  
  12. with GLOBE_3D; 
  13. with GLOBE_3D.Textures; 
  14. with GL; 
  15. with GLU; 
  16. with GLUT;                              use GLUT; 
  17. with GLUT.Devices; 
  18.  
  19. package body Graphics_Setup is 
  20.  
  21.    Deg_2_Rad : constant := Pi / 180.0; 
  22.  
  23.    ------------------ 
  24.    -- Clear For 3D -- 
  25.    ------------------ 
  26.  
  27.    procedure Reset_for_3D is 
  28.  
  29.       use GL; 
  30.  
  31.       Aspect_Ratio       : constant GL.Double := GL.Double (Viewer_Size (x)) / GL.Double (Viewer_Size (y)); 
  32.       Half_Field_of_View : Float := 0.5 * Float (Eye.FOVy) * Deg_2_Rad; -- In Radians 
  33.  
  34.    begin 
  35.       if Aspect_Ratio > 1.0 then -- If Wider than High 
  36.          Half_Field_of_View := Arctan (Float (Aspect_Ratio) * Tan (Float (Eye.FOVy))); 
  37.       end if; 
  38.       Eye.Clipper.max_dot_product := GLOBE_3D.Real (Sin (Half_Field_of_View)); 
  39.       Eye.Clipper.main_clipping := (0, 0, Viewer_Size (x) - 1, Viewer_Size (y) - 1); 
  40.  
  41.       GL.Viewport (0, 0, GL.Sizei (Viewer_Size (x)), GL.Sizei (Viewer_Size (y))); 
  42.       GL.MatrixMode (GL.PROJECTION); 
  43.       GL.LoadIdentity; 
  44.  
  45.       GLU.Perspective (fovy   => Eye.FOVy, -- Field of View in the Y 
  46.                        aspect => Aspect_Ratio, 
  47.                        zNear  => Camera_Close_Clipping_Plane, 
  48.                        zFar   => Camera_Far_Clipping_Plane); 
  49.  
  50.       GL.MatrixMode (GL.MODELVIEW); 
  51.       GL.ShadeModel (GL.SMOOTH);           -- Smooth vs. Flat 
  52.       GL.ClearColor (0.0, 0.0, 0.07, 0.0); -- Clear Value for Colour Buffer 
  53.       GL.ClearAccum (0.0, 0.0, 0.0, 0.0);  -- Clear Value for Accumulation Buffer 
  54.    end Reset_for_3D; 
  55.  
  56.    -------------------- 
  57.    -- Window Resized -- 
  58.    -------------------- 
  59.  
  60.    procedure Window_Resize (Size_x, Size_y : Integer) is 
  61.  
  62.    begin 
  63.       Viewer_Size := ((x => Size_x, y => Size_y)); 
  64.       Reset_for_3D; 
  65.    end Window_Resize; 
  66.  
  67.    ---------------------- 
  68.    -- Initialize Video -- 
  69.    ----------------------- 
  70.  
  71. --     Actual_LINE_WIDTH : aliased GL.Float; 
  72.  
  73.    procedure Initialize_Graphics (Operations : access procedure) is 
  74.  
  75.       GLUT_Options : constant Unsigned := GLUT.DOUBLE or GLUT.RGB or GLUT.DEPTH; 
  76.       Error : Integer; 
  77.  
  78.    begin 
  79.       GLOBE_3D.Set_global_data_name ("Textures.zip"); 
  80.       GLOBE_3D.Textures.Register_textures_from_resources; 
  81.  
  82.       Eye.FOVy := Camera_Field_of_View; 
  83.  
  84.       -- GLUT Stuff -- 
  85.       -- Get a Window with the correct properties -- 
  86.       GLUT.Init; 
  87.       GLUT.InitDisplayMode (GLUT_Options); 
  88.       GLUT.InitWindowSize (Viewer_Size (x), Viewer_Size (y)); 
  89.       GLUT.InitWindowPosition (0, 50); 
  90.       Error := GLUT.CreateWindow (Viewer_Title); 
  91.       if Error = 0 then 
  92.          null; 
  93.       end if; 
  94.  
  95.       -- Feedback Procedures -- 
  96.       GLUT.ReshapeFunc (Window_Resize'Address); 
  97.       GLUT.DisplayFunc (Operations.all'Address); 
  98.       GLUT.IdleFunc (Operations.all'Address); 
  99.  
  100.       GLUT.Devices.Initialize; 
  101.  
  102.       -- GL Stuff -- 
  103.       -- Clear Modes -- 
  104.       GL.Disable (GL.BLEND); 
  105.       GL.Disable (GL.LIGHTING); 
  106.       GL.Disable (GL.AUTO_NORMAL); 
  107.       GL.Disable (GL.NORMALIZE); 
  108.       GL.Disable (GL.DEPTH_TEST); 
  109.  
  110.       Reset_for_3D; 
  111.  
  112.       -- Enable Lighting -- 
  113.       GL.Enable (GL.LIGHTING); 
  114.       for Source in Initial_Lights'Range loop 
  115.          GLOBE_3D.Define (Source, Initial_Lights (Source)); 
  116.          GLOBE_3D.Switch_light (Source, True); 
  117.       end loop; 
  118.  
  119.       -- Reset Eye 
  120.       Eye.Clipper.eye_position := Camera_Initial_Position; 
  121.       Eye.world_Rotation       := GLOBE_3D.Id_33; 
  122.       Eye.FOVy                 := Camera_Field_of_View; 
  123.  
  124. --        GL.GetFloatv (GL.LINE_WIDTH_RANGE, Actual_LINE_WIDTH'Access); 
  125. --        Put ("Actual_LINE_WIDTH: "); Put (Float (Actual_LINE_WIDTH), 3, 2, 0); 
  126.    end Initialize_Graphics; 
  127.  
  128.    -- 
  129.  
  130. end Graphics_Setup;