lace_opengl_0.1.0_672a6415/source/lean/model/opengl-model.adb

  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
with
     ada.unchecked_Deallocation;


package body openGL.Model
is
   ---------
   --- Forge
   --

   procedure define (Self : out Item) is null;


   procedure deallocate is new ada.unchecked_Deallocation (Geometry.views,
                                                           access_Geometry_views);


   procedure destroy (Self : in out Item)
   is
   begin
      if Self.opaque_Geometries /= null
      then
         for i in Self.opaque_Geometries'Range
         loop
            Geometry.free (Self.opaque_Geometries (i));
         end loop;

         deallocate (Self.opaque_Geometries);
      end if;

      if Self.lucid_Geometries /= null
      then
         for i in Self.lucid_Geometries'Range
         loop
            Geometry.free (Self.lucid_Geometries (i));
         end loop;

         deallocate (Self.lucid_Geometries);
      end if;
   end destroy;



   procedure free (Self : in out View)
   is
      procedure deallocate is new ada.unchecked_Deallocation (Model.item'Class,
                                                              Model.view);
   begin
      Self.destroy;
      deallocate (Self);
   end free;



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

   function Id (Self : in Item'Class) return Model_Id
   is
   begin
      return Self.Id;
   end Id;


   procedure Id_is (Self : in out Item'Class;   Now : in Model_Id)
   is
   begin
      Self.Id := Now;
   end Id_is;



   procedure set_Bounds (Self : in out Item)
   is
   begin
      Self.Bounds := null_Bounds;

      if Self.opaque_Geometries /= null
      then
         for Each of Self.opaque_Geometries.all
         loop
            Self.Bounds.Box  :=    Self.Bounds.Box
                                or Each.Bounds.Box;

            Self.Bounds.Ball := Real'Max (Self.Bounds.Ball,
                                          Each.Bounds.Ball);
         end loop;
      end if;

      if Self.lucid_Geometries /= null
      then
         for Each of Self.lucid_Geometries.all
         loop
            Self.Bounds.Box  :=    Self.Bounds.Box
                                or Each.Bounds.Box;

            Self.Bounds.Ball := Real'Max (Self.Bounds.Ball,
                                          Each.Bounds.Ball);
         end loop;
      end if;
   end set_Bounds;



   procedure create_GL_Geometries (Self : in out Item'Class;   Textures : access Texture.name_Map_of_texture'Class;
                                                               Fonts    : in     Font.font_id_Map_of_font)
   is
      all_Geometries : constant Geometry.views := Self.to_GL_Geometries (Textures, Fonts);

      opaque_Faces   : Geometry.views (1 .. all_Geometries'Length);
      opaque_Count   : Index_t := 0;

      lucid_Faces    : Geometry.views (1 .. all_Geometries'Length);
      lucid_Count    : Index_t := 0;

   begin
      Self.Bounds := null_Bounds;

      -- Separate lucid and opaque geometries.
      --
      for i in all_Geometries'Range
      loop
         if all_Geometries (i).is_Transparent
         then
            lucid_Count               := lucid_Count + 1;
            lucid_Faces (lucid_Count) := all_Geometries (i);
         else
            opaque_Count                := opaque_Count + 1;
            opaque_Faces (opaque_Count) := all_Geometries (i);
         end if;

         Self.Bounds.Box :=    Self.Bounds.Box
                            or all_Geometries (i).Bounds.Box;

         Self.Bounds.Ball:= Real'Max (Self.Bounds.Ball,
                                      all_Geometries (i).Bounds.Ball);
      end loop;


      -- Free any existing geometries.
      --
      if Self.opaque_Geometries /= null
      then
         for i in Self.opaque_Geometries'Range
         loop
            Geometry.free (Self.opaque_Geometries (i));
         end loop;

         deallocate (Self.opaque_Geometries);
      end if;

      if Self.lucid_Geometries /= null
      then
         for i in Self.lucid_Geometries'Range
         loop
            Geometry.free (Self.lucid_Geometries (i));
         end loop;

         deallocate (Self.lucid_Geometries);
      end if;

      -- Create new gemometries.
      --
      Self.opaque_Geometries := new Geometry.views' (opaque_Faces (1 .. opaque_Count));
      Self. lucid_Geometries := new Geometry.views' ( lucid_Faces (1 ..  lucid_Count));
      Self.needs_Rebuild     := False;
   end create_GL_Geometries;



   function is_Modified (Self : in Item) return Boolean
   is
      pragma unreferenced (Self);
   begin
      return False;
   end is_Modified;


   function Bounds (Self : in Item) return openGL.Bounds
   is
   begin
      return Self.Bounds;
   end Bounds;


   function opaque_Geometries (Self : in Item) return access_Geometry_views
   is
   begin
      return Self.opaque_Geometries;
   end opaque_Geometries;


   function lucid_Geometries (Self : in Item) return access_Geometry_views
   is
   begin
      return Self.lucid_Geometries;
   end lucid_Geometries;


   function needs_Rebuild (Self : in Item) return Boolean
   is
   begin
      return Boolean (Self.needs_Rebuild);
   end needs_Rebuild;


   procedure needs_Rebuild (Self : in out Item)
   is
   begin
      Self.needs_Rebuild := True;
   end needs_Rebuild;

end openGL.Model;