![]() |
| ||
|
|
Новости |
Поиск |
FAQ |
Статьи |
Книги |
Новости VCL
| | Форумы ЧАТ |
Орешник |
|
| |||
| |||
« предыдущая ветвь | форум | следующая ветвь »
opengl и динамические массивы
zvb © (2005-06-28 07:42:00)требуется сваять нечто вроде 3d-редактора :)
как мне это видится: неограниченный массив точек в пространстве (x, y, z), массив треугольников, хранящий ссылки на эти точки (a1, b1, c1)
а теперь, внимание, вопрос(ы) :)
1) есть ли способ лучше?
2) читал о glnewlist(), можно/нужно ли привязать это?
3) можно пример грамотной работы с динамическими массивами? :)
zvb (2005-06-28 11:54:00) [1]спасибо, попробую ;)
Sapersky (2005-06-28 10:51:00) [2]SetLength(Vertex, Length(Vertex) + 1);
В принципе правильно, но лучше перевыделять блоками, например, так:
// Arr is Delphi dymamic array (array of ...)
// array elements MUST NOT contain elements with automatic memory management,
// i.e. long strings, variants etc.
// this proc may not work in future Delphi versions
// (if dymamic array format will change)
procedure GrowArray(Var Arr; NewCount, ElSize : Integer; GrowKoeff : Single = 2);
Type
TByteArr = array of Byte;
Var
Capacity : Integer;
PLength : PInteger;
begin
PLength := PInteger(Arr);
If (PLength <> nil) then begin
Dec(PLength);
If (NewCount <= PLength^) then Exit; // we fit to existing capacity, no need to grow
Capacity := Round(PLength^ * GrowKoeff);
If Capacity < NewCount then Capacity := NewCount;
PLength^ := PLength^ * ElSize;
// as we cast array to byte array, we need to temporarily convert
// its length in bytes for correct reallocation
end else
Capacity := NewCount; // was no array - just allocate NewCount elements
SetLength(TByteArr(Arr), Capacity * ElSize);
PLength := PInteger(Arr); Dec(PLength);
PLength^ := Capacity; // set length in elements
end;
Назвать это "грамотной" работой с массивами я бы не рискнул - сплошное хакерство :) Зато работает с любыми (почти - см. комментарий в начале!) массивами.
Length(Vertex) в данном случае играет роль Capacity, так что реально используемую длину нужно хранить в отдельной переменной:
function AddVertex(v: TVector): integer;
begin
Result := FVertexCount;
Inc(FVertexCount);
GrowArray(FVertices, FVertexCount, SizeOf(TVector));
FVertices[Result] := v;
end;
XProger (2005-06-28 09:08:00) [3]1) есть. Хранить списки вершин списком.
2) зачем?
3)
var
Vertex : array of TVector;
...
function AddVertex(v: TVector): integer;
begin
SetLength(Vertex, Length(Vertex) + 1);
Result := Length(Vertex) - 1;
Vertex[Result] := v;
end;
function DelVertex(idx: integer): boolean;
var
i : integer;
begin
Result := false;
if (idx < 0) or (idx > High(Vertex)) then
Exit;
for i := idx to High(Vertex) - 1 do
Vertex[i] := Vertex[i + 1];
SetLength(Vertex, Length(Vertex) - 1);
Result := true;
end;
P.S. код не проверял