-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBase.cpp
More file actions
356 lines (301 loc) · 10.6 KB
/
Base.cpp
File metadata and controls
356 lines (301 loc) · 10.6 KB
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
#include "stdafx.h"
#include "Base.h"
classDrawer<classBase *>::drawer_data_type classBase::data_travKey; //mask 1
classDrawer<classBase *>::drawer_data_type classBase::data_travCullEvent; //mask 2
classDrawer<classBase *>::drawer_data_type classBase::data_travDrawEvent; //mask 4
classDrawer<classBase *>::drawer_data_type classBase::data_travRunloop; //mask 8
classDrawer<classBase *>::drawer_data_type classBase::data_travConfigure; //mask 16
std::map<std::string, classBase::EventRefCount> classBase::data_eventRefCount;
classBase::classBase(const char* _name, vsChannel::Subscriber *_subscriber, size_t _mask, bool _isShowFound, const char * _object_name):traversalMask(_mask),isShowFound(_isShowFound)
{
init( vpObserver::find(_name), _subscriber, _mask, _isShowFound, _object_name);
}
classBase::classBase(vpObserver *_observer, vsChannel::Subscriber *_subscriber, size_t _mask, bool _isShowFound, const char * _object_name):traversalMask(_mask),isShowFound(_isShowFound)
{
init(_observer, _subscriber, _mask, _isShowFound, _object_name);
}
void classBase::init(vpObserver *_observer, vsChannel::Subscriber *_subscriber, size_t _mask, bool _isShowFound, const char * _object_name) //场景中的信息在这个函数里提取 必须在pObserver初始化之后调用
{
pSubscriber = _subscriber;
addTraversal(_mask); //这两句将本对象加入了遍历列表
pStart1 = NULL; //节点初始化为NULL,并将found标志置false
pStart2 = NULL;
pTerrain = NULL;
pBuilding = NULL;
pCar = NULL;
pPowerLine = NULL;
pTree = NULL;
pGrass = NULL;
pStone = NULL;
isStartFound = false;
isTerrainFound = false;
isBuildingFound = false;
isCarFound = false;
isPowerLineFound = false;
isTreeFound = false;
isGrassFound = false;
isStoneFound = false;
pShaderDeployer = classShaderDeployer::instance();
pPanel = classPanel::instance();
pSearchPath = vpSearchPath::find("mySearchPath");
if (pSearchPath == NULL) {std::cout << "pSearchPath is NULL!" << std::endl;system("pause");}
else pSearchPath->ref();
if((pObserver = _observer) == NULL) //如果pObserver为null,则表明继承了这个基类的派生类不需要相关信息,如classPanel,这个类只使用了遍历,不需要节点信息
{
std::cout << "pObserver is NULL! Automatically ignore node information!" << std::endl;
//system("pause");
return;
}
else
pObserver->ref();
pChannel = pObserver->getChannel(0);
if (pChannel == NULL) {std::cout << "pChannel is NULL!" << std::endl;system("pause");}
else pChannel->ref();
pScene = pObserver->getScene();
if (pScene == NULL) {std::cout << "pScene is NULL!" << std::endl;system("pause");}
else pScene->ref();
pEnv = static_cast<vpEnv *>(pObserver->getAttachment(0));
if (pEnv == NULL) {std::cout << "pEnv is NULL!" << std::endl;system("pause");}
else pEnv->ref();
pObject = vpObject::find(_object_name);
//pObject = dynamic_cast<vpObject *>(pObserver->getScene());
if (pObject == NULL) {std::cout << "pObject is NULL!" << std::endl;system("pause");}
else pObject->ref();
pStart1 = pObject->find_named("start1");
if (pStart1 != NULL)
{
pStart2 = pObject->find_named("start2");
if (pStart2 != NULL)
{
pStart1->ref();
pStart2->ref();
isStartFound = true;
if (isShowFound)
std::cout << "Start is found!" << std::endl;
}
}
pTerrain = pObject->find_named("terrain");
if (pTerrain != NULL)
{
pTerrain->ref();
isTerrainFound = true;
if (isShowFound)
std::cout << "Terrain is found!" << std::endl;
}
pBuilding = pObject->find_named("building");
if (pBuilding != NULL)
{
pBuilding->ref();
isBuildingFound = true;
if (isShowFound)
std::cout << "Building is found!" << std::endl;
}
pPowerLine = pObject->find_named("powerline");
if (pPowerLine != NULL)
{
pPowerLine->ref();
isPowerLineFound = true;
if (isShowFound)
std::cout << "Powerline is found!" << std::endl;
}
pCar = pObject->find_named("car");
if (pCar != NULL)
{
pCar->ref();
isCarFound = true;
if (isShowFound)
std::cout << "Car is found!" << std::endl;
}
pTree = pObject->find_named("tree");
if (pTree != NULL)
{
pTree->ref();
isTreeFound = true;
if (isShowFound)
std::cout << "Tree is found!" << std::endl;
}
pGrass = pObject->find_named("grass");
if (pGrass != NULL)
{
pGrass->ref();
isGrassFound = true;
if (isShowFound)
std::cout << "Grass is found!" << std::endl;
}
pStone = pObject->find_named("stone");
if (pStone != NULL)
{
pStone->ref();
isStoneFound = true;
if (isShowFound)
std::cout << "Stone is found!" << std::endl;
}
}
classBase::~classBase()
{
removeTraversal(traversalMask);
if(pSearchPath != NULL) pSearchPath->unref();
if(pObserver != NULL) pObserver->unref();
if(pChannel != NULL) pChannel->unref();
if(pScene != NULL) pScene->unref();
if(pEnv != NULL) pEnv->unref();
if(pObject != NULL) pObject->unref();
if(pTerrain!=NULL) pTerrain->unref();
if(pBuilding!=NULL) pBuilding->unref();
if(pCar!=NULL) pCar->unref();
if(pTree!=NULL) pTree->unref();
if(pGrass!=NULL) pGrass->unref();
if(pPowerLine!=NULL) pPowerLine->unref();
if(pStart1!=NULL) pStart1->unref();
if(pStart2!=NULL) pStart2->unref();
}
void classBase::addEvent( vsChannel::Event _event, vpChannel * _channel)
{
vsChannel *__channel = NULL;
if (_channel == NULL)
__channel = pChannel;
else
__channel = _channel;
std::map<std::string, classBase::EventRefCount>::iterator it;
it = data_eventRefCount.find(__channel->getName());
if (it != data_eventRefCount.end())
{
if ( ((size_t *)&(it->second))[(size_t)_event]++ != 0)
return; //如果事件的Ref不为零,只需++不必再addSubscriber
}
else
{
classBase::EventRefCount __temp;
__temp.PreCullEventRefCount = 0;
__temp.PostCullEventRefCount = 0;
__temp.PreDrawEventRefCount = 0;
__temp.PostDrawEventRefCount = 0;
((size_t *)&__temp)[(size_t)_event]++;
data_eventRefCount[__channel->getName()] = __temp;
}
__channel->addSubscriber(_event, pSubscriber);
}
void classBase::removeEvent( vsChannel::Event _event, vpChannel * _channel) //note:如果某个channel的Event ref变成全零,这个函数不会把它从map中剔除
{
vsChannel *__channel = NULL;
if (_channel == NULL)
__channel = pChannel;
else
__channel = _channel;
std::map<std::string, classBase::EventRefCount>::iterator it;
it = data_eventRefCount.find(__channel->getName());
if (it != data_eventRefCount.end())
{
size_t *__p = &(((size_t *)&(it->second))[(size_t)_event]);
if((*__p) == 0)
{std::cout<< "classBase: removeEvent() error : "<< __channel->getName() <<"'s RefCount is already Zero!"<<std::endl;}
else (*__p)--;
if ((*__p) == 0)
__channel->removeSubscriber(_event, pSubscriber, true);
}
else
std::cout<< "classBase: removeEvent() error : Can't find information about " << __channel->getName() << "." <<std::endl;
}
void classBase::addTraversal(size_t _mask)
{
if(_mask & 0x00000001)
index_key = classDrawer<classBase *>::add(this,data_travKey);
if(_mask & 0x00000002)
index_cullEvent = classDrawer<classBase *>::add(this,data_travCullEvent);
if(_mask & 0x00000004)
index_drawEvent = classDrawer<classBase *>::add(this,data_travDrawEvent);
if(_mask & 0x00000008)
index_runLoop = classDrawer<classBase *>::add(this,data_travRunloop);
if(_mask & 0x00000010)
index_configure = classDrawer<classBase *>::add(this,data_travConfigure);
}
void classBase::removeTraversal(size_t _mask)
{
if(_mask & 0x00000001)
classDrawer<classBase *>::erase(index_key, data_travKey);
if(_mask & 0x00000002)
classDrawer<classBase *>::erase(index_cullEvent, data_travCullEvent);
if(_mask & 0x00000004)
classDrawer<classBase *>::erase(index_drawEvent, data_travDrawEvent);
if(_mask & 0x00000008)
classDrawer<classBase *>::erase(index_runLoop, data_travRunloop);
if(_mask & 0x00000010)
classDrawer<classBase *>::erase(index_configure, data_travConfigure);
}
void classBase::handleKey(vrWindow::Key _key, int _mod)
{std::cout << "This function has not yet been implemented in the subclass and nothing will be done." << std::endl;}
void classBase::handleDrawEvent(vsChannel::Event _event, const vsChannel *_channel, vrDrawContext *_context)
{std::cout << "This function has not yet been implemented in the subclass and nothing will be done." << std::endl;}
void classBase::handleCullEvent(vsChannel::Event _event, const vsChannel *_channel, vsTraversalCull *_traversal)
{std::cout << "This function has not yet been implemented in the subclass and nothing will be done." << std::endl;}
void classBase::handleRunLoop(void)
{std::cout << "This function has not yet been implemented in the subclass and nothing will be done." << std::endl;}
void classBase::handleConfigure(void)
{std::cout << "This function has not yet been implemented in the subclass and nothing will be done." << std::endl;}
void classBase::traversalKey(vrWindow::Key _key, int _mod)
{
classDrawer<classBase *>::drawer_data_iterator ib = data_travKey.begin();
classDrawer<classBase *>::drawer_data_iterator ie = data_travKey.end();
while (ib != ie)
{
(*ib)._data->handleKey(_key, _mod);
ib++;
}
}
void classBase::traversalDrawEvent(vsChannel::Event _event, const vsChannel *_channel, vrDrawContext *_context)
{
classDrawer<classBase *>::drawer_data_iterator ib = data_travDrawEvent.begin();
classDrawer<classBase *>::drawer_data_iterator ie = data_travDrawEvent.end();
while (ib != ie)
{
(*ib)._data->handleDrawEvent(_event, _channel, _context);
ib++;
}
}
void classBase::traversalRunLoop(void)
{
classDrawer<classBase *>::drawer_data_iterator ib = data_travRunloop.begin();
classDrawer<classBase *>::drawer_data_iterator ie = data_travRunloop.end();
while (ib != ie)
{
(*ib)._data->handleRunLoop();
ib++;
}
}
void classBase::traversalConfigure(void)
{
classDrawer<classBase *>::drawer_data_iterator ib = data_travConfigure.begin();
classDrawer<classBase *>::drawer_data_iterator ie = data_travConfigure.end();
while (ib != ie)
{
(*ib)._data->handleConfigure();
ib++;
}
}
bool classBase::getPositionHprFromStart(double *_pos, double *_hpr, size_t _choose)
{
if(isStartFound != true)
{
printf("getPositionHprFromStart(): Warn: pStart1 and pStart2 are not found! Check the .flt!\n");
system("pause");
return false;
}
const double * __pos[2] = {0};
__pos[0] = pStart1->getBounds().m_ctr.m_vec;
__pos[1] = pStart2->getBounds().m_ctr.m_vec;
if (_choose == 1 || _choose == 2)
{
memcpy(_pos, __pos[_choose-1], 3*sizeof(double));
}
else
{
printf("getPositionHprFromStart(): Wrong _choose!");
system("pause");
return false;
}
vuMatrixd __m;
__m.makeRotate( vuVec3d(0,1,0), vuVec3d(__pos[1][0] - __pos[0][0], __pos[1][1] - __pos[0][1], __pos[1][2] - __pos[0][2]) );
__m.getRotate(&_hpr[0], &_hpr[1], &_hpr[2]);
//printf("%f, %f, %f\n", _hpr[0], _hpr[1], _hpr[2] );
return true;
}