-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcsg.cpp
277 lines (216 loc) · 5.69 KB
/
csg.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
////////////////////////////////////////////////////////////////////
// Filename: csg.cpp
//
// Author: Stefan Hajnoczi
//
// Date: 26 May 2001
//
// Description: Program to parse WorldCraft 3.3 .MAP files and
// convert them into .CMF files to be used by various
// BSP tree, PVS, and lighting programs.
//
////////////////////////////////////////////////////////////////////
#include <iostream>
#include <fstream>
using namespace std;
////////////////////////////////////////////////////////////////////
// Includes
////////////////////////////////////////////////////////////////////
#include "map.h"
////////////////////////////////////////////////////////////////////
// Function: WriteCMF
// Description: Function that saves a .CMF file from a
// given entity tree.
////////////////////////////////////////////////////////////////////
void WriteCMF ( Entity *pEntity_, Texture *pTextures_, char *pacFilename_ )
{
/*
Header:
3 char CMF ID ("CMF")
1 char Version byte
1 uint Number of WAD files
1 uint Number of entities
1 uint Number of textures
x char WAD filenames (zero terminated)
x char Texture names (zero terminated)
x Entity Entities
*/
ofstream ofsFile;
ofsFile.open ( pacFilename_, ios::out | ios::binary );
//
// Write header ID and version
//
ofsFile << "CMF" << ( char )2;
//
// Find "wad" property
//
Property *pProperty = pEntity_->GetProperties ( );
bool bFound = false;
while ( ( pProperty != NULL ) && ( !bFound ) )
{
if ( strcmp ( "wad", pProperty->GetName ( ) ) == 0 )
{
bFound = true;
}
else
{
pProperty = pProperty->GetNext ( );
}
}
if ( !bFound )
{
cout << "Unable to find WAD files used in map!" << endl;
return;
}
//
// Count and write the number of WAD files
//
char acBuffer[ MAX_TOKEN_LENGTH + 1 ];
const char *pWAD = pProperty->GetValue ( );
unsigned int uiWADFiles = 0;
int iToken = 0;
for ( int i = 0; i < strlen ( pWAD ) + 1; i++ )
{
if ( ( pWAD[ i ] == ';' ) || ( pWAD[ i ] == 0x00 ) )
{
uiWADFiles++;
}
}
ofsFile.write ( ( char * )&uiWADFiles, sizeof ( uiWADFiles ) );
//
// Count and write the number of entities
//
Entity *pEntity = pEntity_;
unsigned int uiEntities = 0;
while ( pEntity != NULL )
{
uiEntities++;
pEntity = pEntity->GetNext ( );
}
ofsFile.write ( ( char * )&uiEntities, sizeof ( uiEntities ) );
//
// Count and write the number of textures
//
Texture *pTexture = pTextures_;
unsigned int uiTextures = 0;
while ( pTexture != NULL )
{
uiTextures++;
pTexture = pTexture->GetNext ( );
}
ofsFile.write ( ( char * )&uiTextures, sizeof ( uiTextures ) );
//
// Write the WAD filenames
//
memset ( acBuffer, 0, MAX_TOKEN_LENGTH + 1 );
for ( i = 0; i < strlen ( pWAD ) + 1; i++ )
{
if ( ( pWAD[ i ] == ';' ) || ( pWAD[ i ] == 0x00 ) )
{
ofsFile << acBuffer << ( char )0x00;
iToken = 0;
memset ( acBuffer, 0, MAX_TOKEN_LENGTH + 1 );
}
else
{
acBuffer[ iToken ] = pWAD[ i ];
iToken++;
}
}
//
// Write the texture names
//
pTexture = pTextures_;
for ( i = 0; i < uiTextures; i++ )
{
ofsFile << pTexture->name << ( char )0x00;
pTexture = pTexture->GetNext ( );
}
//
// Write the entities
//
pEntity_->WriteEntity ( ofsFile );
//
// Clean up and return
//
ofsFile.close ( );
}
////////////////////////////////////////////////////////////////////
// Function: main
// Description: Program to load, parse, CSG union, and save
// WorldCraft 3.3 .MAP files as .CMF files.
////////////////////////////////////////////////////////////////////
void main ( int nArgs_, char *pcArgs[ ] )
{
//
// If in debug mode, turn on memory leak checking
//
#ifdef _DEBUG
int flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); // Get current flag
flag |= _CRTDBG_LEAK_CHECK_DF; // Turn on leak-checking bit
_CrtSetDbgFlag(flag); // Set flag to the new value
#endif
//
// Make sure parameters are valid
//
if ( nArgs_ != 3 )
{
cout << "csg in out" << endl;
cout << "in\t- MAP file" << endl;
cout << "out\t- CMF file" << endl;
return;
}
//
// Parse .MAP file
//
Entity *pEntities = NULL;
Texture *pTextures = NULL;
MAPFile map;
cout << "Parsing " << pcArgs[ 1 ] << "..." << endl;
if ( !map.Load ( pcArgs[ 1 ], &pEntities, &pTextures ) )
{
cout << "Error parsing " << pcArgs[ 1 ] << "!" << endl;
return;
}
//
// Save .CMF file
//
WriteCMF ( pEntities, pTextures, pcArgs[ 2 ] );
#ifdef _DEBUG
//
// Debug dump
//
ofstream ofs ( "Debug.txt" );
Entity *pEntity = pEntities;
while ( pEntity != NULL )
{
ofs << "{" << endl;
Property *pProperty = pEntity->GetProperties ( );
while ( pProperty != NULL )
{
ofs << pProperty->GetName ( ) << " = " << pProperty->GetValue ( ) << endl;
pProperty = pProperty->GetNext ( );
}
Poly *pPoly = pEntity->GetPolys ( );
while ( pPoly != NULL )
{
ofs << endl;
ofs << "tID = " << pPoly->TextureID << endl;
ofs << "plane = " << pPoly->plane.n.x << "x + " << pPoly->plane.n.y << "y + " << pPoly->plane.n.z << "z + " << pPoly->plane.d << " = 0" << endl;
for ( int i = 0; i < pPoly->GetNumberOfVertices ( ); i++ )
{
ofs << pPoly->verts[ i ].p.x << ", " << pPoly->verts[ i ].p.y << ", " << pPoly->verts[ i ].p.z << " [" << pPoly->verts[ i ].tex[ 0 ] << ", " << pPoly->verts[ i ].tex[ 1 ] << "]" << endl;
}
pPoly = pPoly->GetNext ( );
}
ofs << "}" << endl;
pEntity = pEntity->GetNext ( );
}
#endif
//
// Clean up and quit
//
delete pEntities;
delete pTextures;
return;
}