lace_opengl_0.1.0_672a6415/source/lean/renderer/opengl-renderer-lean.ads

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
with
     openGL.Context,
     openGL.Surface,
     openGL.Geometry,
     openGL.Model,
     openGL.Visual,
     openGL.Impostor,
     openGL.Texture,
     openGL.Font,
     openGL.Light;

limited
with
     openGL.Camera;

private
with
     ada.Containers.hashed_Maps,
     ada.unchecked_Conversion;


package openGL.Renderer.lean
--
-- Provides a rendering engine for the 'lean' GL profile.
--
is
   type Item is limited new Renderer.item with private;
   type View is access all Item'Class;


   ---------
   --- Forge
   --

   procedure define  (Self : access Item);
   procedure destroy (Self : in out Item);

   procedure free    (Self : in out View);


   --------------
   --- Attributes
   --

   function  new_Light (Self : in out Item)           return Light.item;
   procedure set       (Self : in out Item;   the_Light : in Light.item);
   procedure rid       (Self : in out Item;   the_Light : in Light.item);
   function  Light     (Self : in out Item;   Id        : in light.Id_t) return openGL.Light.item;
   function  fetch     (Self : in out Item)                              return openGL.Light.items;

   type context_Setter is access procedure;
   type Swapper        is access procedure;

   procedure Context_is        (Self : in out Item;   Now : in Context.view);
   procedure Context_Setter_is (Self : in out Item;   Now : in context_Setter);
   procedure Swapper_is        (Self : in out Item;   Now : in Swapper);


   --------------
   --  Operations
   --

   type impostor_Update
   is
      record
         Impostor : openGL.Impostor.view;

         current_Width_pixels  : gl.GLsizei;
         current_Height_pixels : gl.GLsizei;

         current_copy_x_Offset : gl.GLsizei;
         current_copy_y_Offset : gl.GLsizei;
         current_copy_X        : gl.GLsizei;
         current_copy_Y        : gl.GLsizei;
         current_copy_Width    : gl.GLsizei;
         current_copy_Height   : gl.GLsizei;

         current_Camera_look_at_Rotation : Matrix_3x3;
      end record;

   type impostor_Updates is array (Positive range <>) of impostor_Update;



   procedure queue_Impostor_updates (Self : in out Item;    the_Updates   : in     impostor_Updates;
                                                            the_Camera    : access Camera.item'Class);

   procedure queue_Visuals          (Self : in out Item;    the_Visuals   : in     Visual.views;
                                                            the_Camera    : access Camera.item'Class);

   procedure start_Engine (Self : in out Item);
   procedure  stop_Engine (Self : in out Item);

   procedure render       (Self : in out Item;   to_Surface : in Surface.view := null);
   procedure add_Font     (Self : in out Item;   font_Id    : in Font.font_Id);
   procedure Screenshot   (Self : in out Item;   Filename   : in String;
                                                 with_Alpha : in Boolean := False);

   function  is_Busy      (Self : in Item) return Boolean;

   procedure draw         (Self : in out Item;   the_Visuals            : in Visual.views;
                                                 camera_world_Transform : in Matrix_4x4;
                                                 view_Transform         : in Matrix_4x4;
                                                 perspective_Transform  : in Matrix_4x4;
                                                 clear_Frame            : in Boolean;
                                                 to_Surface             : in Surface.view := null);
   --
   --  Raises buffer_Overflow if the renderer is unable to cope with the new 'draw'.


   procedure free (Self : in out Item;   the_Model    : in Model   .view);
   procedure free (Self : in out Item;   the_Impostor : in Impostor.view);

   buffer_Overflow   : exception;
   Texture_not_found : exception;



private

   type Camera_view is access all openGL.Camera.item'Class;

   max_Visuals : constant := 20_000;

   ----------
   -- Updates
   --

   type updates_for_Camera is
      record
         impostor_Updates      : lean.impostor_Updates (1 .. max_Visuals);
         impostor_updates_Last : Natural := 0;

         Visuals               : Visual.views (1 .. max_Visuals);
         visuals_Last          : Natural := 0;
      end record;

   type Updates_for_Camera_view is access Updates_for_Camera;

   function Hash                   is new ada.unchecked_Conversion   (Camera_view, ada.Containers.Hash_type);
   package  camera_Maps_of_updates is new ada.Containers.Hashed_Maps (Camera_view,
                                                                      updates_for_Camera_view,
                                                                      Hash,
                                                                      "=");
   type camera_updates_Couple is
      record
         Camera  : Camera_view;
         Updates : Updates_for_Camera_view;
      end record;

   type camera_updates_Couples is array (Positive range <>) of camera_updates_Couple;


   protected
   type safe_camera_Map_of_updates
   is
      procedure define;
      procedure destruct;

      procedure add (the_Updates : in impostor_Updates;
                     the_Camera  : in Camera_view);

      procedure add (the_Visuals : in Visual.views;
                     the_Camera  : in Camera_view);

      procedure fetch_all_Updates (the_Updates : out camera_updates_Couples;
                                   Length      : out Natural);

   private
      Map_1       : aliased camera_Maps_of_updates.Map;
      Map_2       : aliased camera_Maps_of_updates.Map;
      current_Map : access  camera_Maps_of_updates.Map;
   end safe_camera_Map_of_updates;


   -- visual_geometry_Couple
   --

   type visual_geometry_Couple is
      record
         Visual   : openGL.Visual  .view;
         Geometry : openGL.Geometry.view;
      end record;

   type visual_geometry_Couples      is array (math.Index range <>) of visual_geometry_Couple;
   type visual_geometry_Couples_view is access all visual_geometry_Couples;


   -- graphics_Models
   --

   type graphics_Models is array (1 .. max_Visuals) of Model.view;

   protected
   type safe_Models
   is
      procedure add   (the_Model  : in     Model.view);
      procedure fetch (the_Models :    out graphics_Models;
                       Count      :    out Natural);
   private
      my_Models : graphics_Models;
      my_Count  : Natural        := 0;
   end safe_Models;


   -- Impostors
   --

   type Impostor_Set is array (1 .. max_Visuals) of Impostor.view;

   protected
   type safe_Impostors
   is
      procedure add   (the_Impostor : in     Impostor.view);
      procedure fetch (Impostors    :    out Impostor_Set;
                       Count        :    out Natural);
   private
      the_Impostors : Impostor_Set;
      the_Count     : Natural := 0;
   end safe_Impostors;


   ----------
   --- Lights
   --

   function Hash (Id : in openGL.light.Id_t) return ada.Containers.Hash_type;
   use type openGL.Light.Id_t,
            openGL.Light.item;

   package  id_Maps_of_light is new ada.Containers.hashed_Maps (Key_type        => openGL.light.Id_t,
                                                                Element_type    => openGL.Light.item,
                                                                Hash            => Hash,
                                                                equivalent_Keys => "=");
   subtype  id_Map_of_light is id_Maps_of_light.Map;

   protected
   type safe_Lights
   is
      procedure add (Light : in openGL.Light.item);
      procedure set (Light : in openGL.Light.item);
      procedure rid (Light : in openGL.Light.item);

      function  get (Id    : in openGL.light.Id_t) return openGL.Light.item;
      function  fetch                              return openGL.Light.items;
   private
      the_Lights : id_Map_of_light;
   end safe_Lights;


   -- Engine
   --

   task type Engine (Self : access Item'Class)
   is
      entry start      (Context    : in openGL.Context.view);
      entry Stop;
      entry render;
      entry add_Font   (font_Id    : in Font.font_Id);
      entry Screenshot (Filename   : in String;
                        with_Alpha : in Boolean := False);

      pragma Storage_Size (100_000_000);
   end Engine;


   -- Renderer
   --

   type Item is limited new Renderer.item with
      record
         Lights             :         safe_Lights;
         prior_Light_Id     :         openGL.Light.Id_t := 0;

         Textures           : aliased Texture.name_Map_of_texture;
         Fonts              :         Font.font_id_Map_of_font;

         all_opaque_Couples :         visual_geometry_Couples_view := new visual_geometry_Couples (1 .. max_Visuals);
         all_lucid_Couples  :         visual_geometry_Couples_view := new visual_geometry_Couples (1 .. max_Visuals);

         obsolete_Models    :         safe_Models;
         obsolete_Impostors :         safe_Impostors;

         texture_Pool       : aliased Texture.Pool;

         safe_Camera_updates_Map
                            : aliased safe_camera_Map_of_updates;

         Engine             :         lean.Engine (Self => Item'Access);

         Context            :         openGL.Context.view;
         context_Setter     :         lean.context_Setter;
         Swapper            :         lean.Swapper;
         swap_Required      :         Boolean;
         is_Busy            :         Boolean := False;
      end record;


   procedure update_Impostors_and_draw_Visuals
                                (Self : in out Item;   all_Updates : in camera_updates_Couples);

   procedure update_Impostors   (Self : in out Item;   the_Updates            : in impostor_Updates;
                                                       camera_world_Transform : in Matrix_4x4;
                                                       view_Transform         : in Matrix_4x4;
                                                       perspective_Transform  : in Matrix_4x4);
   procedure free_old_Models    (Self : in out Item);
   procedure free_old_Impostors (Self : in out Item);


end openGL.Renderer.lean;