-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDaggerTool.cpp
2703 lines (2203 loc) · 64.4 KB
/
DaggerTool.cpp
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
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*************************************************************************************************\
*
* Filename: DaggerTool.cpp
* Purpose: Implement all Daggerfall tool classes
* Version: 0.90
* Author: Gavin Clayton
*
* Last Updated: 29/08/2002
*
* Copyright 2002. Gavin Clayton. All Rights Reserved.
*
*
* NOTE:
* This information is derived from Dave Humphrey's DF hacking articles (http://www.m0use.net/~uesp).
* I have occasionally used different naming conventions and expanded based on my own investigations.
* I am deeply grateful to Dave Humphrey for his work. This would not be possible without him.
*
* If changes are made by you to this code, please log them below:
*
*
\*************************************************************************************************/
#include "stdafx.h"
#include "DaggerTool.h"
#include "Other/DH/DFFaceTex.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// BSABlocks Construction / Destruction
//////////////////////////////////////////////////////////////////////
BSABlocks::BSABlocks()
{
// Initialise
m_pRecordDirectory = NULL;
m_pFLD = NULL;
m_nBlockSubRecordCount = m_nOutsideBlockSubRecordCount = m_nInsideBlockSubRecordCount = 0;
}/* BSABlocks */
BSABlocks::~BSABlocks()
{
Close();
}/* ~BSABlocks */
//////////////////////////////////////////////////////////////////////
// BSABlocks Member Functions
//////////////////////////////////////////////////////////////////////
bool BSABlocks::Open( LPCSTR pszPath, BOOL bReadOnly/*=TRUE*/ )
{
// Open the blocm
if ( !BSAArchive::Open( pszPath, "BLOCKS.BSA", bReadOnly ) )
return false;
m_pRecordDirectory = m_pRecordDirectoryLong;
return true;
}/* Open */
void BSABlocks::Close()
{
if ( !IsOpen() )
return;
if ( IsBlockOpen() )
CloseBlock();
BSAArchive::Close();
// Initialise
m_pRecordDirectory = NULL;
}/* Close */
bool BSABlocks::OpenBlock( long nBlock )
{
// Archive must be open
if ( !IsOpen() )
return false;
// nBlock must be within limits
_ASSERT( nBlock >= 0 && nBlock <= GetRecordCount() );
// Close any existing object
if ( IsBlockOpen() )
CloseBlock();
// Page the record into RAM
char *pData;
long nLength = GetRecordLength( nBlock );
try {
pData = new char[nLength];
}
catch( ... ) {
return false;
}
if ( !pData ) return false;
if ( !GetRecord( nBlock, pData, nLength ) ) {
delete[] pData;
return false;
}
// Check filename, as only RMB objects are known at this time
char sz[64];
GetRecordDesc( nBlock, sz, 64 );
CString strFilename = sz;
if ( strFilename.Right(4) != ".RMB" ) {
delete[] pData;
return false;
}
// Set values
m_bIsBlockOpen = true;
m_pData = pData;
m_nDataLength = nLength;
m_pFLD = (LPRMBFLD)pData;
m_nCurBlock = nBlock;
m_nBlockSubRecordCount = m_pFLD->nSubRecords1;
return true;
}/* OpenBlock */
void BSABlocks::CloseBlock()
{
if ( !IsBlockOpen() )
return;
// Initialise
if ( m_pData ) {
delete[] m_pData;
m_pData = NULL;
}
m_nDataLength = 0;
m_bIsBlockOpen = false;
m_pFLD = NULL;
m_nBlockSubRecordCount = m_nOutsideBlockSubRecordCount = m_nInsideBlockSubRecordCount = 0;
}/* CloseObject */
LPRMB_BLOCKHEADER BSABlocks::GetOutsideBlockSubRecord( long nRecord )
{
_ASSERT( nRecord <= m_nBlockSubRecordCount );
if ( !IsBlockOpen() )
return NULL;
// Exit NULL if this block has no subrecords
if ( 0 == m_nBlockSubRecordCount ) {
return NULL;
}
// Compute offset to outside header
DWORD dwOffset = 0;
for ( long r = 0; r < nRecord; r++ ) {
dwOffset += m_pFLD->Offsets[r];
}
// Find start of specified outside header
char *pData = m_pData + sizeof(RMBFLD) + dwOffset;
return (LPRMB_BLOCKHEADER)pData;
}/* GetOutsideBlockSubRecord */
LPRMB_BLOCKHEADER BSABlocks::GetInsideBlockSubRecord( long nRecord )
{
// Get outside header for this subrecord
LPRMB_BLOCKHEADER pOutside = GetOutsideBlockSubRecord( nRecord );
if ( !pOutside )
return NULL;
// Compute offset to inside header
char *pData = (char*)pOutside;
pData += sizeof(RMB_BLOCKHEADER);
pData += sizeof(RMB_BLOCK3DOBJECTS) * pOutside->n3DObjectRecords;
pData += sizeof(RMB_BLOCKFLATOBJECTS) * pOutside->nFlatObjectsRecords;
pData += sizeof(RMB_BLOCKDATA3) * pOutside->nSection3Records;
pData += sizeof(RMB_BLOCKPEOPLEOBJECTS) * pOutside->nPeopleRecords;
pData += sizeof(RMB_BLOCKDOOROBJECTS) * pOutside->nDoorRecords;
return (LPRMB_BLOCKHEADER)pData;
}/* GetInsideBlockSubRecord */
LPRMB_BLOCK3DOBJECTS BSABlocks::GetBlockSubRecord3DObjects( LPRMB_BLOCKHEADER pBlockHeader )
{
_ASSERT( pBlockHeader );
if ( !IsBlockOpen() )
return NULL;
// Calculate offset to 3D objects
char *pData = (char*)pBlockHeader;
pData += sizeof(RMB_BLOCKHEADER);
return (LPRMB_BLOCK3DOBJECTS)pData;
}/* GetBlockSubRecord3DObjects */
LPRMB_BLOCK3DOBJECTS BSABlocks::GetBlock3DObjects()
{
if ( !IsBlockOpen() )
return NULL;
// Get start of data
char* pData = (char*)GetOutsideBlockSubRecord( m_nBlockSubRecordCount );
return (LPRMB_BLOCK3DOBJECTS)pData;
}/* GetBlock3DObjects */
bool BSABlocks::GetBlockAutomap( LPBLKIMG_AUTOMAP pAutomapOut )
{
// Validate
_ASSERT( pAutomapOut );
if ( !IsBlockOpen() )
return false;
// Locate automap pixel data
char *pImageRaw = m_pFLD->Automap;
// Set colors of image buffer in R8G8B8 format
int cx = 64;
int cy = 64;
for ( int y = 0; y < cy; y++ )
{
for ( int x = 0; x < cx; x++ )
{
// Determine palette index
unsigned char nIndex = pImageRaw[ y * 64 + x ];
// Extract the RGB colours for this pixel
unsigned char r = 200, g = 200, b = 200;
switch ( nIndex )
{
case 0x00:
r = g = b = 0;
break;
case 0x03:
r = 240;
g = 120;
b = 0;
break;
case 0x10:
r = 0;
g = 200;
b = 0;
break;
case 0x12:
case 0x13:
case 0x14:
r = 100;
g = 100;
b = 50;
break;
case 0x0e:
r = 1;
g = 1;
b = 1;
break;
case 0xfa:
case 0xfb:
r = 0;
g = 0;
b = 200;
break;
}
// Set the pixel colour
unsigned char *pOut = (unsigned char*)&pAutomapOut->pBuffer[(y*BLKSIZE_IMAGE_PITCH)+(x*3)];
pOut[0] = b;
pOut[1] = g;
pOut[2] = r;
}// end for ( x = 0; x < cx; x++ )
}// end for ( y = 0; y < cy; y++ )
return true;
}/* GetBlockAutomap */
//////////////////////////////////////////////////////////////////////
// BSAMaps Construction / Destruction
//////////////////////////////////////////////////////////////////////
BSAMaps::BSAMaps()
{
// Initialise
m_pRecordDirectory = NULL;
}/* BSAMaps */
BSAMaps::~BSAMaps()
{
Close();
}/* ~BSAMaps */
//////////////////////////////////////////////////////////////////////
// BSAMaps Member Functions
//////////////////////////////////////////////////////////////////////
bool BSAMaps::Open( LPCSTR pszPath, BOOL bReadOnly/*=TRUE*/ )
{
if ( !BSAArchive::Open( pszPath, "MAPS.BSA", bReadOnly ) )
return false;
m_pRecordDirectory = m_pRecordDirectoryLong;
return true;
}/* Open */
void BSAMaps::Close()
{
if ( !IsOpen() )
return;
BSAArchive::Close();
// Initialise
m_pRecordDirectory = NULL;
}/* Close */
//////////////////////////////////////////////////////////////////////
// BSAArch3D Construction / Destruction
//////////////////////////////////////////////////////////////////////
BSAArch3D::BSAArch3D()
{
// Initialise
m_bIsObjectOpen = false;
m_pData = NULL;
m_nDataLength = 0;
m_pHeader = NULL;
m_pPointList = NULL;
m_pNormalList = NULL;
m_lVersion = 0;
m_pRecordDirectory = NULL;
}/* BSAArch3D */
BSAArch3D::~BSAArch3D()
{
Close();
}/* ~BSAArch3D */
//////////////////////////////////////////////////////////////////////
// BSAArch3D Member Functions
//////////////////////////////////////////////////////////////////////
bool BSAArch3D::Open( LPCSTR pszPath, BOOL bReadOnly/*=TRUE*/ )
{
if ( !BSAArchive::Open( pszPath, "ARCH3D.BSA", bReadOnly ) )
return false;
m_pRecordDirectory = m_pRecordDirectoryShort;
// Index all objects by ID
char sz[64];
UINT* pn;
char r = 'a';
for ( long n = 0; n < NUM_UPPER_ARCH3D_RECORD; n++ ) {
GetRecordDesc( n, sz, 64 );
pn = m_haObjectID.New( sz );
if ( !pn ) {
// Resolve conflict
strncat( sz, &r, 1 );
r++;
pn = m_haObjectID.New( sz );
_ASSERT( pn );
if ( pn ) {
*pn = n;
}
}
else {
// Set number
*pn = n;
}
}
return true;
}/* Open */
void BSAArch3D::Close()
{
if ( !IsOpen() )
return;
if ( IsObjectOpen() )
CloseObject();
BSAArchive::Close();
// Initialise
m_pRecordDirectory = NULL;
m_haObjectID.Destroy();
}/* Close */
bool BSAArch3D::OpenObject( long nObject )
{
// Archive must be open
if ( !IsOpen() )
return false;
// nObject must be within limits
_ASSERT( nObject >= 0 && nObject <= GetRecordCount() );
// Close any existing object
if ( IsObjectOpen() )
CloseObject();
// Page the record into RAM
char *pData;
long nLength = GetRecordLength( nObject );
try {
pData = new char[nLength];
}
catch( ... ) {
return false;
}
if ( !pData ) return false;
if ( !GetRecord( nObject, pData, nLength ) ) {
delete[] pData;
return false;
}
// Set values
m_nCurObject = nObject;
m_bIsObjectOpen = true;
m_pData = pData;
m_nDataLength = nLength;
m_pHeader = (LPARCH3D_HEADER)pData;
m_pPointList = (LPARCH3D_POINT)(pData + m_pHeader->nPointOffset);
m_pNormalList = (LPARCH3D_POINT)(pData + m_pHeader->nNormalOffset);
if ( 0 == strncmp( m_pHeader->szVersion, "v2.7", 4 ) )
m_lVersion = ARCH3DVERSION_27;
else if ( 0 == strncmp( m_pHeader->szVersion, "v2.6", 4 ) )
m_lVersion = ARCH3DVERSION_26;
else if ( 0 == strncmp( m_pHeader->szVersion, "v2.5", 4 ) )
m_lVersion = ARCH3DVERSION_25;
else
m_lVersion = ARCH3DVERSION_UNKNOWN;
return true;
}/* OpenObject */
void BSAArch3D::CloseObject()
{
if ( !IsObjectOpen() )
return;
// Initialise
if ( m_pData ) {
delete[] m_pData;
m_pData = NULL;
}
m_nDataLength = 0;
m_bIsObjectOpen = false;
m_pHeader = NULL;
m_pPointList = NULL;
m_pNormalList = NULL;
m_lVersion = 0;
}/* CloseObject */
float BSAArch3D::GetVersionAsFloat()
{
float fv = 0.0f;
switch ( m_lVersion )
{
case ARCH3DVERSION_25:
fv = 2.5f;
break;
case ARCH3DVERSION_26:
fv = 2.6f;
break;
case ARCH3DVERSION_27:
fv = 2.7f;
break;
default:
fv = 0.0f;
}
return fv;
}/* GetVersionAsFloat */
long BSAArch3D::GetFaceCount()
{
// Object must be open
if ( !IsObjectOpen() )
return 0;
return m_pHeader->nFaceCount;
}/* GetFaceCount */
LPARCH3D_FACE BSAArch3D::GetFace( int nFace )
{
// Object must be open
_ASSERT( IsObjectOpen() );
if ( !IsObjectOpen() )
return NULL;
// nFace must be within limits
_ASSERT( nFace < m_pHeader->nFaceCount );
// Locate face
int nFaceDataOffset = m_pHeader->nPointOffset + (m_pHeader->nPointCount * 12);
if ( nFaceDataOffset == m_pHeader->nNormalOffset ) {
return NULL;
}
LPARCH3D_FACE pFace = (LPARCH3D_FACE)(m_pData + nFaceDataOffset);
for ( int i = 0; i < nFace; i++ ) {
char *pWork = (char*)pFace;
pWork += (8 + (pFace->nPointCount * 8));
pFace = (LPARCH3D_FACE)pWork;
}
// Some faces on various objects seem to have malformed texture coordinates
// These faces are patched in memory here until I find out what's going on
// NOTE: Confirmed the game displays most of these malformed faces incorrectly also
// Possible it uses an in-memory patch similar to this one.
switch ( m_nCurObject )
{
case 5563:
if ( nFace == 45 ) PatchPointUV( pFace, 1, 1024, DFTOOL_NOPATCH );
break;
case 5571:
if ( nFace == 18 ) {
PatchPointUV( pFace, 0, 0, DFTOOL_NOPATCH );
PatchPointUV( pFace, 1, 0, DFTOOL_NOPATCH );
PatchPointUV( pFace, 3, 0, DFTOOL_NOPATCH );
}
break;
}
return pFace;
}/* GetFace */
bool BSAArch3D::GetFaceTexture( LPARCH3D_FACE pFace, long* pnArchiveOut, long* pnRecordOut )
{
// Object must be open
if ( !IsObjectOpen() )
return false;
// Out values cannot be NULL
_ASSERT( pnArchiveOut );
_ASSERT( pnRecordOut );
// pFace cannot be NULL
_ASSERT( pFace );
*pnRecordOut = pFace->nTexture & 0x7f;
*pnArchiveOut = (pFace->nTexture / 0x80);
return true;
}/* GetFaceTexture */
bool BSAArch3D::GetPureFaceUV( LPARCH3D_FACE pFace, LPSIZE uvOut )
{
// Object must be open
if ( !IsObjectOpen() )
return false;
// Out values cannot be NULL
_ASSERT( uvOut );
// pFace cannot be NULL
_ASSERT( pFace );
// pFace must have 3 or more points
_ASSERT( pFace->nPointCount >= 3 );
// Read UV settings from face
OBJVERTEX v;
for ( int p = 0; p < 3; p++ ) {
GetPoint( pFace, p, &v );
uvOut[p].cx = (long)v.tu;
uvOut[p].cy = (long)v.tv;
}
// Read point 4 from UV list if present
if ( pFace->nPointCount > 3 ) {
GetPoint( pFace, 3, &v );
uvOut[3].cx = (long)v.tu;
uvOut[3].cy = (long)v.tv;
}
else {
uvOut[3].cx = 0;
uvOut[3].cy = 0;
}
return true;
}/* GetPureFaceUV */
long BSAArch3D::GetPointCount( LPARCH3D_FACE pFace )
{
// Object must be open
if ( !IsObjectOpen() )
return 0;
// pFace cannot be NULL
_ASSERT( pFace );
return pFace->nPointCount;
}/* GetPointCount */
bool BSAArch3D::GetPoint( LPARCH3D_FACE pFace, int nPoint, LPOBJVERTEX pvOut )
{
// Object must be open
if ( !IsObjectOpen() )
return false;
// pvOut cannot be NULL
_ASSERT( pvOut );
// pFace cannot be NULL
_ASSERT( pFace );
// nPoint must be within limits
_ASSERT( nPoint < pFace->nPointCount );
// Locate other resources
LPARCH3D_POINTDESC pFacePointData = (LPARCH3D_POINTDESC)&pFace->Data;
LPARCH3D_POINTDESC pPointDesc = pFacePointData + nPoint;
// Output point information based on version
long nDivisor;
( GetVersion() == ARCH3DVERSION_25 ) ? nDivisor = 4 : nDivisor = 12;
GetDFFP( pvOut->pos.x, m_pPointList[pPointDesc->nOffset/nDivisor].x );
GetDFFP( pvOut->pos.y, m_pPointList[pPointDesc->nOffset/nDivisor].y );
GetDFFP( pvOut->pos.z, m_pPointList[pPointDesc->nOffset/nDivisor].z );
pvOut->normal.x = 0.0f;
pvOut->normal.y = 0.0f;
pvOut->normal.z = 0.0f;
short tu, tv;
tu = pPointDesc->tu;
tv = pPointDesc->tv;
/*
if ( tu > 16384 ) tu -= 32768;
if ( tu < -16384 ) tu += 32768;
if ( tv > 16384 ) tv -= 32768;
if ( tv < -16384 ) tv += 32768;
if ( tu > 8192 ) tu -= 16384;
if ( tu < -8192 ) tu += 16384;
if ( tv > 8192 ) tv -= 16384;
if ( tv < -8192 ) tv += 16384;
if ( tu > 4096 ) tu -= 8192;
if ( tu < -4096 ) tu += 8192;
if ( tv > 4096 ) tv -= 8192;
if ( tv < -4096 ) tv += 8192;
*/
pvOut->tu = (float)tu;
pvOut->tv = (float)tv;
return true;
}/* GetPoint */
bool BSAArch3D::PatchPointUV( LPARCH3D_FACE pFace, int nPoint, int U, int V )
{
// pFace cannot be NULL
_ASSERT( pFace );
// nPoint must be within limits
_ASSERT( nPoint < pFace->nPointCount );
// Locate other resources
LPARCH3D_POINTDESC pFacePointData = (LPARCH3D_POINTDESC)&pFace->Data;
LPARCH3D_POINTDESC pPointDesc = pFacePointData + nPoint;
// Patch U value
if ( U != DFTOOL_NOPATCH ) {
pPointDesc->tu = U;
}
// Patch V value
if ( V != DFTOOL_NOPATCH ) {
pPointDesc->tv = V;
}
return true;
}/* PatchPointUV */
long BSAArch3D::GetCornerPoints( LPARCH3D_FACE pFace, int* pCornerBuffer/*=NULL*/, LPOBJVERTEX pPointBuffer/*=NULL*/ )
{
// Object must be open
if ( !IsObjectOpen() )
return 0;
// pFace cannot be NULL
_ASSERT( pFace );
// Get point count
int nPoints = pFace->nPointCount;
// Must have greater than 3 points
_ASSERT( nPoints >= 3 );
// Get an export of the face
OBJVERTEX vArray[NUM_MAX_CORNER_POINTS];
for ( int p = 0; p < nPoints; p++ ) {
GetPoint( pFace, p, &vArray[p] );
// Scale vertex so very small faces (8906) don't get overlooked
vArray[p].pos.x *= 3;
vArray[p].pos.y *= 3;
vArray[p].pos.z *= 3;
}
// Step through points to count angles
int nAngles = 0, nCorner = 0;
float theta = 0.0f, costheta = 0.0f;
D3DXVECTOR3 v0, v1, v2, l0, l1;
for ( p = 0; p < nPoints; p++ ) {
// Determine angle between current vertex and next two vertices
if ( p < nPoints - 2 ) {
v0 = vArray[p].pos;
v1 = vArray[p+1].pos;
v2 = vArray[p+2].pos;
nCorner = p+1;
}
else if ( p < nPoints - 1 ) {
v0 = vArray[p].pos;
v1 = vArray[p+1].pos;
v2 = vArray[0].pos;
nCorner = p+1;
}
else {
v0 = vArray[p].pos;
v1 = vArray[0].pos;
v2 = vArray[1].pos;
nCorner = 0;
}
// Construct direction vertex for line0 and line1
l0 = v1 - v0;
l1 = v2 - v0;
// Obtain angle between direction vectors
costheta = D3DXVec3Dot(&l0,&l1) / (D3DXVec3Length(&l0) * D3DXVec3Length(&l1));
// Colinear lines have a costheta of 1.0f - threshold is lower to avoid precision errors
if ( costheta < 1.0f ) {
// Write this corner to a buffer
if ( pCornerBuffer ) {
pCornerBuffer[nAngles] = nCorner;
}
// Write this point to a buffer
if ( pPointBuffer ) {
GetPoint( pFace, nCorner, &pPointBuffer[nAngles] );
}
// Increment corner count
nAngles++;
}
}
return nAngles;
}/* GetCornerPoints */
bool BSAArch3D::ExportPureFace( LPARCH3D_FACE pFace, LPOBJVERTEX pvOut, int* pPointCountOut )
{
int nPoint;
int nFacePointCount = pFace->nPointCount;
int nCount = 0, nVertex = 0;
OBJVERTEX vStart, v0, v1;
// Object must be open
if ( !IsObjectOpen() )
return false;
// pvOut cannot be NULL
_ASSERT( pvOut );
// pFace cannot be NULL
_ASSERT( pFace );
// Obtain the point count of this face
GetPoint( pFace, 0, &vStart );
for ( nPoint = 0; nPoint < nFacePointCount - 1; nPoint++ )
{
GetPoint( pFace, nPoint, &v0 );
pvOut[nVertex].pos = -v0.pos;
pvOut[nVertex].normal = -v0.normal;
pvOut[nVertex].tu = 0.0f;
pvOut[nVertex].tv = 0.0f;
GetPoint( pFace, nPoint+1, &v1 );
pvOut[nVertex+1].pos = -v1.pos;
pvOut[nVertex+1].normal = -v1.normal;
pvOut[nVertex+1].tu = 0.0f;
pvOut[nVertex+1].tv = 0.0f;
nCount += 2;
nVertex += 2;
}
// Close polygon
pvOut[nVertex].pos = -v1.pos;
pvOut[nVertex].normal = -v1.normal;
pvOut[nVertex].tu = 0.0f;
pvOut[nVertex].tv = 0.0f;
pvOut[nVertex+1].pos = -vStart.pos;
pvOut[nVertex+1].normal = -vStart.normal;
pvOut[nVertex+1].tu = 0.0f;
pvOut[nVertex+1].tv = 0.0f;
nCount += 2;
*pPointCountOut = nCount;
return true;
}/* ExportPureFace */
bool BSAArch3D::ExportTriangulatedFace( LPARCH3D_FACE pFace, LPOBJVERTEX pvOut, int* pPointCountOut, LPSIZE pSrcSize/*=NULL*/, LPRECT pSubRect/*=NULL*/ )
{
// Validate
_ASSERT( pFace );
_ASSERT( pvOut );
_ASSERT( pPointCountOut );
// Object must be open
if ( !IsObjectOpen() )
return false;
// Get corner points
int CornerBuffer[NUM_MAX_CORNER_POINTS];
OBJVERTEX PointBuffer[NUM_MAX_CORNER_POINTS];
int nCornerCount = GetCornerPoints( pFace, CornerBuffer, PointBuffer );
// Set UV coordinates if texture dimensions are specified
if ( pSrcSize && pSubRect ) {
SetFaceUV( pFace, nCornerCount, PointBuffer, pSrcSize, pSubRect );
}
// Split corner points into triangles
int nFaceCount = 0, nCount = 0;
int s = 0, n = nCornerCount - 1;
while ( nFaceCount != (nCornerCount - 2) )
{
// Write triangle 1
pvOut[nCount].pos = -PointBuffer[s].pos;
pvOut[nCount].normal = -PointBuffer[s].normal;
pvOut[nCount].tu = PointBuffer[s].tu;
pvOut[nCount].tv = PointBuffer[s].tv;
pvOut[nCount+1].pos = -PointBuffer[n].pos;
pvOut[nCount+1].normal = -PointBuffer[n].normal;
pvOut[nCount+1].tu = PointBuffer[n].tu;
pvOut[nCount+1].tv = PointBuffer[n].tv;
pvOut[nCount+2].pos = -PointBuffer[s+1].pos;
pvOut[nCount+2].normal = -PointBuffer[s+1].normal;
pvOut[nCount+2].tu = PointBuffer[s+1].tu;
pvOut[nCount+2].tv = PointBuffer[s+1].tv;
nFaceCount++;
nCount += 3;
if ( nFaceCount == (nCornerCount - 2) )
break;
// Write triangle 2
pvOut[nCount].pos = -PointBuffer[s+1].pos;
pvOut[nCount].normal = -PointBuffer[s+1].normal;
pvOut[nCount].tu = PointBuffer[s+1].tu;
pvOut[nCount].tv = PointBuffer[s+1].tv;
pvOut[nCount+1].pos = -PointBuffer[n].pos;
pvOut[nCount+1].normal = -PointBuffer[n].normal;
pvOut[nCount+1].tu = PointBuffer[n].tu;
pvOut[nCount+1].tv = PointBuffer[n].tv;
pvOut[nCount+2].pos = -PointBuffer[n-1].pos;
pvOut[nCount+2].normal = -PointBuffer[n-1].normal;
pvOut[nCount+2].tu = PointBuffer[n-1].tu;
pvOut[nCount+2].tv = PointBuffer[n-1].tv;
s++;
n--;
nFaceCount++;
nCount += 3;
}
// Store values
*pPointCountOut = nCount;
return true;
}/* ExportTriangulatedFace */
//
// bool SetFaceUV( LPARCH3D_FACE pFace, long nCornerCount, LPOBJVERTEX pCornerVertexBuffer, LPSIZE pSrcSize, LPRECT pSubRect )
// Performs UV setup for the specified face
// Return: true if successful, otherwise false
//
bool BSAArch3D::SetFaceUV( LPARCH3D_FACE pFace, long nCornerCount, LPOBJVERTEX pCornerVertexBuffer, LPSIZE pSrcSize, LPRECT pSubRect )
{
// Validate
_ASSERT( nCornerCount >= 3 );
_ASSERT( pCornerVertexBuffer );
_ASSERT( pSrcSize );
_ASSERT( pSubRect );
// Get first four points of this face
OBJVERTEX verts[4];
GetPoint( pFace, 0, &verts[0] );
GetPoint( pFace, 1, &verts[1] );
GetPoint( pFace, 2, &verts[2] );
if ( pFace->nPointCount > 3 ) {
GetPoint( pFace, 3, &verts[3] );
}
else {
verts[3].pos = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
verts[3].normal = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
verts[3].tu = 0.0f;
verts[3].tv = 0.0f;
}
// Calculate absolute UV coordinates for point 1, 2
verts[1].tu += verts[0].tu;
verts[1].tv += verts[0].tv;
verts[2].tu += verts[1].tu;
verts[2].tv += verts[1].tv;
// Get texture dimensions and divisors
int nTextureWidth = pSubRect->right - pSubRect->left;
int nTextureHeight = pSubRect->bottom - pSubRect->top;
float fWidthDivisor = 16.0f * nTextureWidth;
float fHeightDivisor = 16.0f * nTextureHeight;
// Calculate smallest POW2 texture size and subregion
// This simulates the POW2 logic in Alchemy so we know how large the end texture will be
// This is neccessary because not all DF textures are POW2 and will be resized by Alchemy
int smx = 8, smy = 8;
while ( smx < (int)nTextureWidth ) smx *= 2;
while ( smy < (int)nTextureHeight ) smy *= 2;
float stu = (float)nTextureWidth / smx;
float stv = (float)nTextureHeight / smy;
// Triangles can be handled quickly and easily
long cnr;
if ( pFace->nPointCount == 3 && nCornerCount == 3 ) {
pCornerVertexBuffer[0].tu = verts[1].tu / fWidthDivisor;
pCornerVertexBuffer[0].tv = verts[1].tv / fHeightDivisor;
pCornerVertexBuffer[1].tu = verts[2].tu / fWidthDivisor;
pCornerVertexBuffer[1].tv = verts[2].tv / fHeightDivisor;
pCornerVertexBuffer[2].tu = verts[0].tu / fWidthDivisor;
pCornerVertexBuffer[2].tv = verts[0].tv / fHeightDivisor;
// Adjust UV to reference subregion of texture
for ( cnr = 0; cnr < nCornerCount; cnr++ ) {
pCornerVertexBuffer[cnr].tu *= stu;