summaryrefslogtreecommitdiffstats
path: root/src/modelinfo/SimpleModelInfo.cpp
blob: 1199c200b2844d4c62a1aeeb6cccfbd071b73dc4 (plain) (blame)
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
#include "common.h"

#include "main.h"
#include "General.h"
#include "Renderer.h"
#include "Camera.h"
#include "ModelInfo.h"
#include "AnimManager.h"
#include "custompipes.h"
#include "Streaming.h"
#include "smallHeap.h"
#include "Leeds.h"

TempIdeData m_sTempIdeData[800];

base::cRelocatableChunkClassInfo CSimpleModelInfo::msClassInfo("CSimpleModelInfo", VTABLE_ADDR(&msClassInstance), sizeof(msClassInstance));
CSimpleModelInfo CSimpleModelInfo::msClassInstance;

void
CSimpleModelInfo::DeleteRwObject(void)
{
	int i;
	RwFrame *f;
	if(m_atomics == nil)
		return;
	if(!gUseChunkFiles){
		for(i = 0; i < m_numAtomics; i++)
			if(m_atomics[i]){
				f = RpAtomicGetFrame(m_atomics[i]);
				RpAtomicDestroy(m_atomics[i]);
				RwFrameDestroy(f);
				m_atomics[i] = nil;
				RemoveTexDictionaryRef();
				if(GetAnimFileIndex() != -1)
					CAnimManager::RemoveAnimBlockRef(GetAnimFileIndex());
			}
	}else if(m_chunk){
		CStreaming::UnregisterPointer(&m_atomics, 2);
		for(i = 0; i < m_numAtomics; i++)
			CStreaming::UnregisterAtomic(m_atomics[i], nil);
		DeleteChunk();
		RemoveTexDictionaryRef();
		if(GetAnimFileIndex() != -1)
			CAnimManager::RemoveAnimBlockRef(GetAnimFileIndex());
	}
	m_atomics = nil;
}

RwObject*
CSimpleModelInfo::CreateInstance(void)
{
	RpAtomic *atomic;
	if(m_atomics && m_atomics[0] == nil)
		return nil;
	atomic = RpAtomicClone(m_atomics[0]);
	RpAtomicSetFrame(atomic, RwFrameCreate());
	CStreaming::RegisterInstance(atomic, nil);
	return (RwObject*)atomic;
}

RwObject*
CSimpleModelInfo::CreateInstance(RwMatrix *matrix)
{
	RpAtomic *atomic;
	RwFrame *frame;

	if(m_atomics && m_atomics[0] == nil)
		return nil;
	atomic = RpAtomicClone(m_atomics[0]);
	frame = RwFrameCreate();
	*RwFrameGetMatrix(frame) = *matrix;
	RpAtomicSetFrame(atomic, frame);
	CStreaming::RegisterInstance(atomic, nil);
	return (RwObject*)atomic;
}

void
CSimpleModelInfo::Init(void)
{
	m_atomics = new RpAtomic*[3];
	m_numAtomics = 0;
	m_firstDamaged  = 0;
	m_wetRoadReflection    = 0;
	m_isDamaged     = 0;
	m_isBigBuilding = 0;
	m_noFade        = 0;
	m_drawLast      = 0;
	m_additive      = 0;
	m_isSubway      = 0;
	m_ignoreLight   = 0;
	m_noZwrite      = 0;
	m_noShadows     = 0;
	m_ignoreDrawDist  = 0;
	m_isCodeGlass     = 0;
	m_isArtistGlass   = 0;
	m_relatedModel = nil;
}

void
CSimpleModelInfo::SetAtomic(int n, RpAtomic *atomic)
{
	if(m_atomics == nil){
		m_atomics = new RpAtomic*[3];
		m_atomics[0] = nil;
		m_atomics[1] = nil;
		m_atomics[2] = nil;
	}
	m_atomics[n] = atomic;
	AddTexDictionaryRef();
	if(GetAnimFileIndex() != -1)
		CAnimManager::AddAnimBlockRef(GetAnimFileIndex());
	RpGeometry *geo = RpAtomicGetGeometry(atomic);
	if(m_ignoreLight)
		RpGeometrySetFlags(geo, RpGeometryGetFlags(geo) & ~rpGEOMETRYLIGHT);
/*
	if(RpGeometryGetFlags(geo) & rpGEOMETRYNORMALS &&
	   RpGeometryGetNumTriangles(geo) > 200)
		debug("%s has %d polys\n", m_name, RpGeometryGetNumTriangles(geo));
*/

#ifdef EXTENDED_PIPELINES
	if(m_wetRoadReflection)
		CustomPipes::AttachGlossPipe(atomic);
	else
		CustomPipes::AttachWorldPipe(atomic);
#endif
}

void
CSimpleModelInfo::SetLodDistances(float *dist)
{
	m_lodDistances[0] = dist[0];
	m_lodDistances[1] = dist[1];
	m_lodDistances[2] = dist[2];
}

void
CSimpleModelInfo::IncreaseAlpha(void)
{
	if(m_alpha >= 0xEF)
		m_alpha = 0xFF;
	else
		m_alpha += 0x10;
}

float
CSimpleModelInfo::GetLodDistance(int i)
{
	return m_lodDistances[i] * TheCamera.LODDistMultiplier;
}

float
CSimpleModelInfo::GetNearDistance(void)
{
	return m_lodDistances[2] * TheCamera.LODDistMultiplier;
}

float
CSimpleModelInfo::GetLargestLodDistance(void)
{
	float d;
	if(m_firstDamaged == 0 || m_isDamaged)
		d = m_lodDistances[m_numAtomics-1];
	else
		d = m_lodDistances[m_firstDamaged-1];
	return d * TheCamera.LODDistMultiplier;
}

RpAtomic*
CSimpleModelInfo::GetLodAtomic(int n)
{
	if(m_atomics == nil || n >= m_numAtomics)
		return nil;
	return m_atomics[n];
}

RpAtomic*
CSimpleModelInfo::GetLastAtomic(void)
{
	if(m_atomics == nil)
		return nil;
	if(m_firstDamaged == 0 || m_isDamaged)
		return m_atomics[m_numAtomics-1];
	else
		return m_atomics[m_firstDamaged-1];
}

RpAtomic*
CSimpleModelInfo::GetLastAtomic(float dist)
{
	int n;
	if(m_atomics == nil)
		return nil;
	if(m_firstDamaged == 0 || m_isDamaged)
		n = m_numAtomics-1;
	else
		n = m_firstDamaged-1;
	if(dist < m_lodDistances[n] * TheCamera.LODDistMultiplier)
		return m_atomics[n];
	return nil;
}

RpAtomic*
CSimpleModelInfo::GetAtomicFromDistance(float dist)
{
	int i;
	i = 0;
	if(m_isDamaged)
		i = m_firstDamaged;
	for(; i < m_numAtomics; i++)
		if(dist < m_lodDistances[i] * TheCamera.LODDistMultiplier)
			return m_atomics[i];
	return nil;
}

RpAtomic*
CSimpleModelInfo::GetFirstAtomicFromDistance(float dist)
{
// HACk until we figure out what's going on
if(m_atomics == nil) return nil;
	if(dist < m_lodDistances[0] * TheCamera.LODDistMultiplier)
		return m_atomics[0];
	return nil;
}

void
CSimpleModelInfo::FindRelatedModel(void)
{
	CBaseModelInfo *mi;
	int thisIndex, otherIndex;

	// find our own index in temp data
	for(thisIndex = 0; thisIndex < ARRAY_SIZE(m_sTempIdeData); thisIndex++){
		if(m_sTempIdeData[thisIndex].id == -1)
			break;
		if(this == CModelInfo::GetModelInfo(m_sTempIdeData[thisIndex].id))
			goto found;
	}
	thisIndex = -1;
found:
#ifdef FIX_BUGS
	if(thisIndex == -1)
		return;
#endif

	for(otherIndex = 0; otherIndex < ARRAY_SIZE(m_sTempIdeData); otherIndex++){
		if(m_sTempIdeData[otherIndex].id == -1)
			break;

		mi = CModelInfo::GetModelInfo(m_sTempIdeData[otherIndex].id);
		if(mi && mi != this &&
		   !CGeneral::faststrcmp(m_sTempIdeData[thisIndex].name+3, m_sTempIdeData[otherIndex].name+3)){
			assert(mi->IsSimple());
			this->SetRelatedModel((CSimpleModelInfo*)mi);
			return;
		}
	}
}

#define NEAR_DRAW_DIST 100.0f	// 0.0f in vice city

void
CSimpleModelInfo::SetupBigBuilding(void)
{
	CSimpleModelInfo *related;
	if(m_lodDistances[0] < 0.0f)
		m_lodDistances[0] = -m_lodDistances[0];	// what?
	else if(m_lodDistances[0] > LOD_DISTANCE && GetRelatedModel() == nil){
		m_isBigBuilding = 1;
		FindRelatedModel();
		related = GetRelatedModel();
		if(related){
			m_lodDistances[2] = related->GetLargestLodDistance()/TheCamera.LODDistMultiplier;
			if(m_drawLast){
				m_drawLast = false;
				debug("%s was draw last\n", GetModelName());
			}
		}else
			m_lodDistances[2] = NEAR_DRAW_DIST;
	}
}


void
CSimpleModelInfo::LoadModel(void *atomics, const void *chunk)
{
	int i;
	m_chunk = (void*)chunk;
	m_atomics = (RpAtomic**)atomics;
	CStreaming::RegisterPointer(m_chunk, 2, true);
	CStreaming::RegisterPointer(m_atomics, 2, true);
	for(i = 0; i < m_numAtomics; i++){
		LoadResource(m_atomics[i]);
		CStreaming::RegisterAtomic(m_atomics[i], nil);
	}
}

void
CSimpleModelInfo::Write(base::cRelocatableChunkWriter &writer)
{
	CBaseModelInfo::Write(writer);
	if(WriteModel(writer))
		writer.AddPatch(&m_atomics);
	else
		m_atomics = nil;
	if(m_isBigBuilding)
		writer.AddPatch(&m_relatedModel);
}

void*
CSimpleModelInfo::WriteModel(base::cRelocatableChunkWriter &writer)
{
	int i;
	if(m_atomics == nil || m_atomics[0] == nil)
		return nil;

	// remove empty atomics
	int numAtomics = 0;
	for(i = 0; i < m_numAtomics; i++)
		if(m_atomics[i]){
			m_atomics[numAtomics] = m_atomics[i];
#ifdef FIX_BUGS
			m_lodDistances[numAtomics] = m_lodDistances[i];
#endif
			numAtomics++;
		}
	if(m_firstDamaged){
		int firstDam = m_firstDamaged - m_numAtomics + numAtomics;
		if(firstDam < numAtomics)
			m_firstDamaged = firstDam;
		else
			m_firstDamaged = 0;
	}
	m_numAtomics = numAtomics;

	// write the actual models
	writer.AllocateRaw(m_atomics, m_numAtomics*sizeof(void*), sizeof(void*), false, true);
	for(i = 0; m_numAtomics; i++){
		writer.AddPatch(&m_atomics[i]);
		SaveResource(m_atomics[i], writer);
	}
	return m_atomics;
}

void
CSimpleModelInfo::RcWriteThis(base::cRelocatableChunkWriter &writer)
{
	writer.AllocateRaw(this, sizeof(*this), 0x10, false, true);
	writer.Class(VTABLE_ADDR(this), msClassInfo);
}

void
CSimpleModelInfo::RcWriteEmpty(base::cRelocatableChunkWriter &writer)
{
	writer.AllocateRaw(this, sizeof(*this), 0x10, false, true);
	writer.Class(VTABLE_ADDR(this), msClassInfo);
}