-
Notifications
You must be signed in to change notification settings - Fork 2
/
i3d_model.pas
823 lines (722 loc) · 20.3 KB
/
i3d_model.pas
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
//------------------------------------------------------------------------------
//
// I3D_Viewer - Model Viewer for Speed Haste models
// Copyright (C) 2020-2022 by Jim Valavanis
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
// DESCRIPTION:
// I3D Models
//
//------------------------------------------------------------------------------
// Site : https://sourceforge.net/projects/i3dviewer/
//------------------------------------------------------------------------------
unit i3d_model;
interface
uses
SysUtils,
Classes,
i3d_structs;
type
TI3dModelCorrection = record
face: integer;
vertex: integer;
visible: boolean;
x, y, z: integer;
tx, ty: integer;
color: byte;
end;
PI3dModelCorrection = ^TI3dModelCorrection;
TI3dModelCorrectionArray = array[0..$FFF] of TI3dModelCorrection;
PI3dModelCorrectionArray = ^TI3dModelCorrectionArray;
type
TI3DModel = class(TObject)
private
obj: O3DM_TObject_p;
objfaces: PO3DM_TFaceArray;
objsize: integer;
textures: array[0..$7F] of LongWord; // texid is shortint (-128..127)
numtextures: integer;
headers: PO3DM_TFaceHeaderArray;
materials: PO3DM_TMaterialArray;
facevertexes: PO3DM_TFaceVertexArray;
numfacevertexes: integer;
vertexes: PO3DM_TVertexArray;
numvertexes: integer;
fbitmaps: TStringList;
fselected: integer;
corrections: PI3dModelCorrectionArray;
numcorrections: integer;
defPos: R3D_TPosVector;
defRot: R3D_TAngles;
protected
function GetNumFaces: integer; virtual;
function GetFace(Index: Integer): O3DM_TFace_p; virtual;
procedure ApplyCorrection(const cor: PI3dModelCorrection); virtual;
public
constructor Create; virtual;
destructor Destroy; override;
function LoadFromStream(const strm: TStream): boolean;
function LoadFromFile(const fname: string): boolean;
procedure Clear;
function CreateTexture(const m: O3DM_TMaterial_p): integer;
function RenderGL(const scale: single): integer;
procedure RenderSelectionCubeGL(const scale: single);
function AddCorrection(const face: integer; const vertex: integer; const visible: boolean;
const x, y, z: integer; const du, dv: single; const c: LongWord): boolean;
procedure SaveCorrectionsToStream(const strm: TStream);
procedure SaveCorrectionsToFile(const fname: string);
procedure LoadCorrectionsFromStream(const strm: TStream);
procedure LoadCorrectionsFromFile(const fname: string);
procedure UVtoGL(const tx, tv: integer; var du, dv: single);
procedure GLtoUV(const du, dv: single; var tx, tv: integer);
function dcx: integer;
function dcy: integer;
function dcz: integer;
property faces[Index: integer]: O3DM_TFace_p read GetFace;
property numfaces: integer read GetNumFaces;
property Bitmaps: TStringList read fbitmaps;
property selected: integer read fselected write fselected;
end;
implementation
uses
dglOpenGL,
Graphics,
i3d_palette,
i3d_utils,
i3d_scriptengine;
constructor TI3DModel.Create;
begin
Inherited Create;
defPos[0] := 0;
defPos[1] := 0;
defPos[2] := 0;
defRot[0] := 0;
defRot[1] := 0;
defRot[2] := 0;
obj := nil;
objsize := 0;
numtextures := 0;
fselected := -1;
fbitmaps := TStringList.Create;
corrections := nil;
numcorrections := 0;
end;
destructor TI3DModel.Destroy;
begin
Clear;
fbitmaps.Free;
Inherited Destroy;
end;
function TI3DModel.GetNumFaces: integer;
begin
if obj = nil then
Result := 0
else
Result := obj.nFaces;
end;
function TI3DModel.GetFace(Index: Integer): O3DM_TFace_p;
begin
if obj = nil then
Result := nil
else if (Index >= 0) and (Index < obj.nFaces) then
Result := @objfaces[Index]
else
Result := nil;
end;
function TI3DModel.LoadFromStream(const strm: TStream): boolean;
var
magic: LongWord;
base: LongWord;
i, j, l: integer;
facecachepos: integer;
function _OF(const p: pointer): pointer;
begin
Result := pointer(LongWord(p) + base);
end;
function _CacheRead(size: integer): pointer;
begin
result := @obj.facecache[facecachepos];
facecachepos := facecachepos + size;
end;
begin
Clear;
strm.Read(magic, SizeOf(LongWord));
if magic <> ID3_MAGIC then
begin
Result := False;
Exit;
end;
strm.Read(objsize, SizeOf(integer));
GetMem(obj, objsize);
strm.Read(obj^, objsize);
base := LongWord(obj);
obj.verts := _OF(obj.verts);
obj.normals := _OF(obj.normals);
obj.facecache := _OF(obj.facecache);
obj.materials := _OF(obj.materials);
if obj.pos = nil then
obj.pos := @defPos
else
obj.pos := _OF(obj.pos);
if obj.rot = nil then
obj.rot := @defRot
else
obj.rot := _OF(obj.rot);
if obj.scx = 0 then
obj.scx := DEF_I3D_SCALE;
if obj.scy = 0 then
obj.scy := DEF_I3D_SCALE;
if obj.scz = 0 then
obj.scz := DEF_I3D_SCALE;
GetMem(objfaces, obj.nFaces * SizeOf(O3DM_TFace));
GetMem(headers, obj.nFaces * SizeOf(O3DM_TFaceHeader));
GetMem(materials, obj.nFaces * SizeOf(O3DM_TMaterial));
numfacevertexes := 0;
numvertexes := 0;
facecachepos := 0;
for i := 0 to obj.nFaces - 1 do
begin
objfaces[i].h := _CacheRead(SizeOf(O3DM_TFaceHeader));
headers[i] := objfaces[i].h^;
objfaces[i].h := @headers[i];
numfacevertexes := numfacevertexes + headers[i].nVerts;
numvertexes := numvertexes + headers[i].nVerts;
objfaces[i].verts := _CacheRead(objfaces[i].h.nVerts * SizeOf(O3DM_TFaceVertex));
for j := 0 to objfaces[i].h.nVerts - 1 do
begin
objfaces[i].verts[j].vert := _OF(objfaces[i].verts[j].vert);
if objfaces[i].verts[j].normal <> nil then
objfaces[i].verts[j].normal := _OF(objfaces[i].verts[j].normal);
end;
if objfaces[i].h.material <> nil then
objfaces[i].h.material := _OF(objfaces[i].h.material);
objfaces[i].h.visible := True;
end;
for i := 0 to obj.nMaterials - 1 do
if obj.materials[i].texture <> nil then
begin
if obj.materials[i].flags and O3DMF_256 <> 0 then
l := 256 * 64
else
l := 64 * 64;
GetMem(obj.materials[i].texture, l);
if obj.materials[i].texture <> nil then
strm.read(obj.materials[i].texture^, l)
else
strm.Position := strm.Position + 1;
CreateTexture(@obj.materials[i]);
end
else
obj.materials[i].texid := -1;
GetMem(facevertexes, numfacevertexes * SizeOf(O3DM_TFaceVertex));
l := 0;
for i := 0 to obj.nFaces - 1 do
begin
for j := 0 to objfaces[i].h.nVerts - 1 do
begin
facevertexes[l] := objfaces[i].verts[j];
inc(l);
end;
objfaces[i].verts := @facevertexes[l - objfaces[i].h.nVerts];
end;
GetMem(vertexes, numvertexes * SizeOf(O3DM_TVertex));
l := 0;
for i := 0 to obj.nFaces - 1 do
for j := 0 to objfaces[i].h.nVerts - 1 do
begin
vertexes[l] := objfaces[i].verts[j].vert^;
objfaces[i].verts[j].vert := @vertexes[l];
inc(l);
end;
for i := 0 to obj.nFaces - 1 do
begin
materials[i] := objfaces[i].h.material^;
objfaces[i].h.material := @materials[i];
end;
Result := True;
end;
function TI3DModel.CreateTexture(const m: O3DM_TMaterial_p): integer;
type
TLongWordArrayBuffer = array[0..$3FFF] of LongWord;
PLongWordArrayBuffer = ^TLongWordArrayBuffer;
var
buffer: PLongWordArrayBuffer;
i: integer;
dest: PLongWord;
TEXDIMX, TEXDIMY: integer;
gltex: LongWord;
bm: TBitmap;
begin
if m.flags and O3DMF_256 <> 0 then
begin
TEXDIMX := 256;
TEXDIMY := 64;
end
else
begin
TEXDIMX := 64;
TEXDIMY := 64;
end;
bm := TBitmap.create;
bm.Width := TEXDIMX;
bm.height := TEXDIMY;
GetMem(buffer, TEXDIMX * TEXDIMY * SizeOf(LongWord));
dest := @buffer[0];
for i := 0 to TEXDIMX * TEXDIMY - 1 do
begin
dest^ := I3DPalColorL(m.texture[i]);
bm.Canvas.Pixels[i mod TEXDIMX, i div TEXDIMX] := dest^;
inc(dest);
end;
fbitmaps.AddObject(m.texname, bm);
glGenTextures(1, @gltex);
glBindTexture(GL_TEXTURE_2D, gltex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8,
TEXDIMX, TEXDIMY,
0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
FreeMem(buffer, TEXDIMX * TEXDIMY * SizeOf(LongWord));
Result := numtextures;
m.texid := Result;
textures[numtextures] := gltex;
inc(numtextures);
end;
function TI3DModel.LoadFromFile(const fname: string): boolean;
var
fs: TFileStream;
begin
fs := TFileStream.Create(fname, fmOpenRead or fmShareDenyWrite);
try
result := LoadFromStream(fs);
finally
fs.Free;
end;
end;
procedure TI3DModel.Clear;
var
i, l: integer;
begin
if obj <> nil then
begin
FreeMem(headers, obj.nFaces * SizeOf(O3DM_TFaceHeader));
FreeMem(materials, obj.nFaces * SizeOf(O3DM_TMaterial));
FreeMem(facevertexes, numfacevertexes * SizeOf(O3DM_TFaceVertex));
FreeMem(vertexes, numvertexes * SizeOf(O3DM_TVertex));
FreeMem(objfaces, obj.nFaces * SizeOf(O3DM_TFace));
for i := 0 to obj.nMaterials - 1 do
if obj.materials[i].texture <> nil then
begin
if obj.materials[i].flags and O3DMF_256 <> 0 then
l := 256 * 64
else
l := 64 * 64;
FreeMem(obj.materials[i].texture, l);
end;
FreeMem(obj, objsize);
obj := nil;
objsize := 0;
for i := 0 to numtextures - 1 do
glDeleteTextures(1, @textures[i]);
numtextures := 0;
end;
for i := 0 to fbitmaps.Count - 1 do
fbitmaps.Objects[i].Free;
fbitmaps.Clear;
fselected := -1;
if numcorrections <> 0 then
begin
FreeMem(corrections, numcorrections * SizeOf(TI3dModelCorrection));
corrections := nil;
numcorrections := 0;
end;
end;
procedure TI3DModel.ApplyCorrection(const cor: PI3dModelCorrection);
var
face: O3DM_TFace_p;
begin
if obj = nil then
Exit;
if not IsIntInRange(cor.face, 0, obj.nFaces - 1) then
Exit;
face := @objfaces[cor.face];
if not IsIntInRange(cor.vertex, 0, face.h.nVerts - 1) then
Exit;
face.h.visible := cor.visible;
face.verts[cor.vertex].vert.x := cor.x;
face.verts[cor.vertex].vert.y := cor.y;
face.verts[cor.vertex].vert.z := cor.z;
face.verts[cor.vertex].tx := cor.tx;
face.verts[cor.vertex].ty := cor.ty;
if face.h.material <> nil then
face.h.material.color := cor.color;
end;
function TI3DModel.AddCorrection(const face: integer; const vertex: integer; const visible: boolean;
const x, y, z: integer; const du, dv: single; const c: LongWord): boolean;
var
i, idx: integer;
cor: PI3dModelCorrection;
tx, ty: integer;
begin
Result := False;
if not IsIntInRange(face, 0, obj.nFaces - 1) then
Exit;
if not IsIntInRange(vertex, 0, objfaces[face].h.nVerts - 1) then
Exit;
idx := -1;
for i := 0 to numcorrections - 1 do
if (corrections[i].face = face) and (corrections[i].vertex = vertex) then
begin
idx := i;
Break;
end;
if idx < 0 then
begin
ReAllocMem(corrections, (numcorrections + 1) * SizeOf(TI3dModelCorrection));
idx := numcorrections;
inc(numcorrections);
end;
cor := @corrections[idx];
cor.face := face;
cor.vertex := vertex;
for i := 0 to numcorrections - 1 do
if corrections[i].face = face then
begin
corrections[i].visible := visible;
ApplyCorrection(@corrections[i]);
end;
cor.x := x;
cor.y := y;
cor.z := z;
GLtoUV(du, dv, tx, ty);
cor.tx := tx;
cor.ty := ty;
cor.color := I3DPalColorIndex(c);
ApplyCorrection(cor);
Result := True;
end;
procedure TI3DModel.SaveCorrectionsToStream(const strm: TStream);
var
i: integer;
s: TStringList;
cor: PI3dModelCorrection;
vis: integer;
begin
if obj = nil then
Exit;
s := TStringList.Create;
try
for i := 0 to numcorrections - 1 do
begin
cor := @corrections[i];
if IsIntInRange(cor.face, 0, obj.nFaces - 1) then
if IsIntInRange(cor.vertex, 0, objfaces[cor.face].h.nVerts - 1) then
begin
if cor.visible then
vis := 1
else
vis := 0;
s.Add(
Format('face %d vertex %d visible %d x %d y %d z %d tx %d ty %d color %d',
[cor.face, cor.vertex, vis, cor.x, cor.y, cor.z, cor.tx, cor.ty, cor.color])
);
end;
end;
s.SaveToStream(strm);
finally
s.Free;
end;
end;
procedure TI3DModel.SaveCorrectionsToFile(const fname: string);
var
fs: TFileStream;
begin
fs := TFileStream.Create(fname, fmCreate);
try
SaveCorrectionsToStream(fs);
finally
fs.Free;
end;
end;
procedure TI3DModel.LoadCorrectionsFromStream(const strm: TStream);
var
sc: TScriptEngine;
s: TStringList;
cor: PI3dModelCorrection;
begin
ReallocMem(corrections, 0);
numcorrections := 0;
s := TStringList.Create;
try
s.LoadFromStream(strm);
sc := TScriptEngine.Create(s.Text);
while sc.GetString do
begin
if sc.MatchString('face') then
begin
ReallocMem(corrections, (numcorrections + 1) * SizeOf(TI3dModelCorrection));
cor := @corrections[numcorrections];
inc(numcorrections);
sc.MustGetInteger;
cor.face := sc._Integer;
sc.MustGetStringName('vertex');
sc.MustGetInteger;
cor.vertex := sc._Integer;
sc.MustGetStringName('visible');
sc.MustGetInteger;
if sc._Integer = 0 then
cor.visible := False
else
cor.visible := True;
sc.MustGetStringName('x');
sc.MustGetInteger;
cor.x := sc._Integer;
sc.MustGetStringName('y');
sc.MustGetInteger;
cor.y := sc._Integer;
sc.MustGetStringName('z');
sc.MustGetInteger;
cor.z := sc._Integer;
sc.MustGetStringName('tx');
sc.MustGetInteger;
cor.tx := sc._Integer;
sc.MustGetStringName('ty');
sc.MustGetInteger;
cor.ty := sc._Integer;
sc.MustGetStringName('color');
sc.MustGetInteger;
cor.color := sc._Integer;
ApplyCorrection(cor);
end;
end;
sc.Free;
finally
s.Free;
end;
end;
procedure TI3DModel.LoadCorrectionsFromFile(const fname: string);
var
fs: TFileStream;
begin
fs := TFileStream.Create(fname, fmOpenRead or fmShareDenyWrite);
try
LoadCorrectionsFromStream(fs);
finally
fs.Free;
end;
end;
const
UVGLCONST = 262144 * 64;
procedure TI3DModel.UVtoGL(const tx, tv: integer; var du, dv: single);
begin
du := -tx / UVGLCONST;
dv := tv / UVGLCONST;
end;
procedure TI3DModel.GLtoUV(const du, dv: single; var tx, tv: integer);
begin
tx := -Round(du * UVGLCONST);
tv := Round(dv * UVGLCONST);
end;
function TI3DModel.dcx: integer;
begin
if obj = nil then
Result := 0
else
Result := obj.dcx;
end;
function TI3DModel.dcy: integer;
begin
if obj = nil then
Result := 0
else
Result := obj.dcy;
end;
function TI3DModel.dcz: integer;
begin
if obj = nil then
Result := 0
else
Result := obj.dcz;
end;
function TI3DModel.RenderGL(const scale: single): integer;
var
i, j: integer;
lasttex, newtex: LongWord;
procedure _glcolor(const m: O3DM_TMaterial_p);
var
cl: i3dcolor3f_t;
begin
cl := I3DPalColor3f(m.color);
if m.flags and O3DMF_TRANS <> 0 then
glColor4f(cl.r, cl.g, cl.b, 0.5)
else
glColor4f(cl.r, cl.g, cl.b, 1.0);
end;
procedure _gltexcoord(const tx, ty: integer);
var
ax, ay: single;
begin
UVtoGL(tx, ty, ax, ay);
glTexCoord2f(ax, ay);
end;
procedure _glvertex(const x, y, z: integer);
begin
glVertex3f(
(1.0 * x - obj.dcx) * obj.scx / DEF_I3D_SCALE * scale,
(1.0 * y - obj.dcy) * obj.scy / DEF_I3D_SCALE * scale,
(1.0 * z - obj.dcz) * obj.scz / DEF_I3D_SCALE * scale
);
end;
begin
Result := 0;
if obj = nil then
Exit;
lasttex := $FFFFFFFF;
for i := 0 to obj.nFaces - 1 do
begin
if not objfaces[i].h.visible then
Continue;
if fselected = i then
Continue;
if objfaces[i].h.material <> nil then
begin
newtex := 0;
if objfaces[i].h.material.texid >= 0 then
newtex := textures[objfaces[i].h.material.texid];
if newtex > 0 then
begin
glEnable(GL_TEXTURE_2D);
if newtex <> lasttex then
begin
glColor4f(1.0, 1.0, 1.0, 1.0);
glBindTexture(GL_TEXTURE_2D, newtex);
lasttex := newtex;
end;
end
else
begin
glDisable(GL_TEXTURE_2D);
lasttex := 0;
_glcolor(objfaces[i].h.material);
end;
end
else
begin
glDisable(GL_TEXTURE_2D);
lasttex := 0;
glColor4f(1.0, 1.0, 1.0, 1.0);
end;
glBegin(GL_TRIANGLE_FAN);
for j := 0 to objfaces[i].h.nVerts - 1 do
begin
_gltexcoord(objfaces[i].verts[j].tx, objfaces[i].verts[j].ty);
_glvertex(objfaces[i].verts[j].vert.x, objfaces[i].verts[j].vert.y, objfaces[i].verts[j].vert.z);
end;
glEnd;
Result := Result + objfaces[i].h.nVerts - 2;
end;
end;
procedure TI3DModel.RenderSelectionCubeGL(const scale: single);
const
BOUNDSIZE = 384;
var
minx, maxx: integer;
miny, maxy: integer;
minz, maxz: integer;
face: O3DM_TFace_p;
i: integer;
procedure _glvertex(const x, y, z: integer);
begin
glVertex3f(
(1.0 * x - obj.dcx) * obj.scx / DEF_I3D_SCALE * scale,
(1.0 * y - obj.dcy) * obj.scy / DEF_I3D_SCALE * scale,
(1.0 * z - obj.dcz) * obj.scz / DEF_I3D_SCALE * scale
);
end;
begin
if obj = nil then
Exit;
if not IsIntInRange(fselected, 0, obj.nFaces - 1) then
Exit;
face := @objfaces[fselected];
minx := MAXINT;
maxx := -MAXINT;
miny := MAXINT;
maxy := -MAXINT;
minz := MAXINT;
maxz := -MAXINT;
for i := 0 to face.h.nVerts - 1 do
begin
if face.verts[i].vert.x < minx then
minx := face.verts[i].vert.x;
if face.verts[i].vert.x > maxx then
maxx := face.verts[i].vert.x;
if face.verts[i].vert.y < miny then
miny := face.verts[i].vert.y;
if face.verts[i].vert.y > maxy then
maxy := face.verts[i].vert.y;
if face.verts[i].vert.z < minz then
minz := face.verts[i].vert.z;
if face.verts[i].vert.z > maxz then
maxz := face.verts[i].vert.z;
end;
minx := minx - BOUNDSIZE;
maxx := maxx + BOUNDSIZE;
miny := miny - BOUNDSIZE;
maxy := maxy + BOUNDSIZE;
minz := minz - BOUNDSIZE;
maxz := maxz + BOUNDSIZE;
glBegin(GL_LINES);
// Left rect
_glvertex(minx, miny, minz);
_glvertex(minx, miny, maxz);
_glvertex(minx, miny, maxz);
_glvertex(minx, maxy, maxz);
_glvertex(minx, maxy, maxz);
_glvertex(minx, maxy, minz);
_glvertex(minx, maxy, minz);
_glvertex(minx, miny, minz);
// Right rect
_glvertex(maxx, miny, minz);
_glvertex(maxx, miny, maxz);
_glvertex(maxx, miny, maxz);
_glvertex(maxx, maxy, maxz);
_glvertex(maxx, maxy, maxz);
_glvertex(maxx, maxy, minz);
_glvertex(maxx, maxy, minz);
_glvertex(maxx, miny, minz);
// Connect left & right rect
_glvertex(minx, miny, minz);
_glvertex(maxx, miny, minz);
_glvertex(minx, maxy, minz);
_glvertex(maxx, maxy, minz);
_glvertex(minx, maxy, maxz);
_glvertex(maxx, maxy, maxz);
_glvertex(minx, miny, maxz);
_glvertex(maxx, miny, maxz);
// Axes
_glvertex(minx - 2 * BOUNDSIZE, (maxy + miny) div 2, (maxz + minz) div 2);
_glvertex(maxx + 2 * BOUNDSIZE, (maxy + miny) div 2, (maxz + minz) div 2);
_glvertex((maxx + minx) div 2, miny - 2 * BOUNDSIZE, (maxz + minz) div 2);
_glvertex((maxx + minx) div 2, maxy + 2 * BOUNDSIZE, (maxz + minz) div 2);
_glvertex((maxx + minx) div 2, (maxy + miny) div 2, minz - 2 * BOUNDSIZE);
_glvertex((maxx + minx) div 2, (maxy + miny) div 2, maxz + 2 * BOUNDSIZE);
glEnd;
end;
end.