[7297] Fixed profession spells sorting in trainer spell list at client.
[getmangos.git] / contrib / vmap_debugger / ModelContainerView.cpp
blobc29666c0bcabf0d726954a45252102b94c4a0846
1 #include "ModelContainerView.h"
3 namespace VMAP
5 char* gDataDir = NULL;
6 char* gLogFile = NULL;
7 //==========================================
9 ModelContainerView::ModelContainerView(const G3D::GApp::Settings& settings) : GApp(settings) {
10 i_App = this;
12 iCommandFileRW.setFileName(gLogFile);
13 iCurrCmdIndex = 0;
14 iVMapManager = new VMapManager();
15 iDrawLine = false;
17 iVARAreaRef = VARArea::create(1024*1024*60);
18 iVARAreaRef2 = VARArea::create(1024*1024*2);
19 iInstanceId = -1;
20 iPosSent = false;
23 //===================================================
25 ModelContainerView::~ModelContainerView(void)
27 Array<std::string > keys = iTriVarTable.getKeys();
28 Array<std::string>::ConstIterator i = keys.begin();
29 while(i != keys.end()) {
30 VAR* var = iTriVarTable.get(*i);
31 delete var;
32 ++i;
36 //===================================================
38 void ModelContainerView::cleanup() {
41 //===================================================
42 Vector3 getViewPos(const ModelContainer* mc, unsigned int pModelNr = 0) {
43 if(mc->getNSubModel() < pModelNr) {
44 pModelNr = mc->getNSubModel();
46 const SubModel sm = mc->getSubModel(pModelNr);
47 return (sm.getAABoxBounds().low());
50 void ModelContainerView::init() {
53 //==========================================
55 void fillSubModelArary(const ModelContainer* pModelContainer, const TreeNode *root, Array<SubModel>& array, Vector3& pLo, Vector3& pHi) {
56 Vector3 lo = Vector3(inf(), inf(), inf());
57 Vector3 hi = Vector3(-inf(), -inf(), -inf());
59 for(int i=0; i< root->getNValues(); i++) {
60 SubModel sm = pModelContainer->getSubModel(root->getStartPosition() + i);
61 lo = lo.min(sm.getAABoxBounds().low());
62 hi = hi.max(sm.getAABoxBounds().high());
63 array.append(sm);
66 if(root->getChild((TreeNode *) &pModelContainer->getTreeNode(0), 0)) {
67 fillSubModelArary(pModelContainer, root->getChild((TreeNode *)&pModelContainer->getTreeNode(0), 0), array, lo, hi);
69 if(root->getChild((TreeNode *)&pModelContainer->getTreeNode(0), 1)) {
70 fillSubModelArary(pModelContainer, root->getChild((TreeNode *)&pModelContainer->getTreeNode(0), 1), array, lo, hi);
73 float dist1 = (hi -lo).magnitude();
74 AABox b;
75 root->getBounds(b);
76 float dist2 = (b.high() -b.low()).magnitude();
77 if(dist1 > dist2) {
78 // error
79 int xxx = 0;
84 //==========================================
85 void ModelContainerView::addModelContainer(const std::string& pName,const ModelContainer* pModelContainer) {
86 // VARArea::UsageHint::WRITE_EVERY_FEW_FRAMES
88 int offset=0;
90 Array<int> iIndexArray;
91 Array<Vector3> iGlobArray;
93 Array<SubModel> SMArray;
94 Vector3 lo, hi;
95 fillSubModelArary(pModelContainer, &pModelContainer->getTreeNode(0), SMArray,lo,hi);
98 for(int i=0; i<SMArray.size(); ++i) {
99 SubModel sm = SMArray[i];
100 Array<Vector3> vArray;
101 Array<int> iArray;
102 fillVertexAndIndexArrays(sm, vArray, iArray);
104 for(int j=0;j<iArray.size(); ++j) {
105 iIndexArray.append(offset+iArray[j]);
108 for(int j=0;j<vArray.size(); ++j) {
109 iGlobArray.append(vArray[j]);
111 offset += vArray.size();
112 //break;
114 iTriVarTable.set(pName, new VAR(iGlobArray ,iVARAreaRef));
115 iTriIndexTable.set(pName, iIndexArray);
118 //===================================================
120 void ModelContainerView::removeModelContainer(const std::string& pName, const ModelContainer* pModelContainer) {
121 VAR* var = iTriVarTable.get(pName);
122 iTriVarTable.remove(pName);
123 delete var;
126 Vector3 p1,p2,p3,p4,p5,p6,p7;
127 Array<AABox>gBoxArray;
128 Array<G3D::Triangle>gTriArray;
129 int gCount1 = 0, gCount2 = 0 , gCount3 = 0, gCount4 = 0;
130 bool myfound=false;
132 //===================================================
134 void ModelContainerView::onInit() {
135 // Called before the application loop beings. Load data here and
136 // not in the constructor so that common exceptions will be
137 // automatically caught.
138 iSky = Sky::fromFile("../../data/sky/");
140 iSkyParameters = SkyParameters(G3D::toSeconds(11, 00, 00, AM));
141 iLighting = Lighting::fromSky(iSky, iSkyParameters, Color3::white());
143 // This simple demo has no shadowing, so make all lights unshadowed
144 iLighting->lightArray.append(iLighting->shadowedLightArray);
145 iLighting->shadowedLightArray.clear();
147 // Example debug GUI:
148 //debugPane->addCheckBox("Use explicit checking", &explicitCheck);
149 debugWindow->setVisible(true);
151 toneMap->setEnabled(false);
154 void ModelContainerView::onGraphics(RenderDevice* rd, Array<PosedModelRef> &posed3D, Array<PosedModel2DRef> &posed2D) {
155 Array<PosedModel::Ref> opaque, transparent;
156 LightingRef localLighting = toneMap->prepareLighting(iLighting);
157 SkyParameters localSky = toneMap->prepareSkyParameters(iSkyParameters);
160 toneMap->beginFrame(rd);
161 rd->setProjectionAndCameraMatrix(defaultCamera);
163 rd->setColorClearValue(Color3::black());
164 rd->clear();
165 //iSky->render(rd, localSky);
167 // Setup lighting
168 rd->enableLighting();
169 //rd->setLight(0, localLighting->lightArray[0]);
170 //rd->setAmbientLightColor(localLighting->ambientAverage());
172 GLight light =GLight::directional(defaultController.pointer()->position() + defaultController.pointer()->lookVector()*2,Color3::white());
173 rd->setLight(0,light);
175 rd->setColor(Color3::blue());
177 Array<std::string > keys = iTriVarTable.getKeys();
178 Array<std::string>::ConstIterator i = keys.begin();
179 while(i != keys.end()) {
180 VAR* var = iTriVarTable.get(*i);
181 Array<int> indexArray = iTriIndexTable.get(*i);
183 rd->beginIndexedPrimitives();
184 rd->setVertexArray(*var);
185 rd->sendIndices(RenderDevice::LINES, indexArray);
186 rd->endIndexedPrimitives();
187 ++i;
189 for(int i=0; i<gBoxArray.size(); ++i) {
190 AABox b = gBoxArray[i];
191 Draw::box(b,rd,Color4(Color3::red(),0.9f));
193 //-------
194 //triangles
196 if(iTriDebugArray.size() > 0)
198 rd->setColor(Color3::red());
199 rd->beginIndexedPrimitives();
200 rd->setVertexArray(iTriDebugVar);
201 rd->sendIndices(RenderDevice::LINES, iTriDebugArray);
202 rd->endIndexedPrimitives();
205 //--------
206 if(iDrawLine) {
207 Draw::lineSegment(LineSegment::fromTwoPoints(iPos1, iPos2), rd, iColor, 3);
209 if(myfound) {
210 //Draw::lineSegment(LineSegment::fromTwoPoints(p1, p2), rd, iColor, 3);
211 //Draw::lineSegment(LineSegment::fromTwoPoints(p2, p3), rd, iColor, 3);
212 //Draw::lineSegment(LineSegment::fromTwoPoints(p3, p1), rd, iColor, 3);
213 Draw::sphere(Sphere(p4,0.5),rd, iColor);
214 //Draw::sphere(Sphere(p5,0.5),rd, Color3::green());
219 // Always render the posed models passed in or the Developer Window and
220 // other Widget features will not appear.
221 if (posed3D.size() > 0) {
222 Vector3 lookVector = renderDevice->getCameraToWorldMatrix().lookVector();
223 PosedModel::sort(posed3D, lookVector, opaque, transparent);
225 for (int i = 0; i < opaque.size(); ++i) {
226 opaque[i]->render(renderDevice);
229 for (int i = 0; i < transparent.size(); ++i) {
230 transparent[i]->render(renderDevice);
234 rd->disableLighting();
236 toneMap->endFrame(rd);
237 PosedModel2D::sortAndRender(rd, posed2D);
240 //===================================================
242 void ModelContainerView::fillRenderArray(const SubModel& pSm, Array<TriangleBox> &pArray, const TreeNode* pTreeNode) {
243 for(int i=0;i<pTreeNode->getNValues(); i++) {
244 pArray.append(pSm.getTriangles()[i+pTreeNode->getStartPosition()]);
247 if(pTreeNode->getChild(pSm.getTreeNodes(), 0) != 0) {
248 fillRenderArray(pSm, pArray, pTreeNode->getChild(pSm.getTreeNodes(), 0));
251 if(pTreeNode->getChild(pSm.getTreeNodes(), 1) != 0) {
252 fillRenderArray(pSm, pArray, pTreeNode->getChild(pSm.getTreeNodes(), 1));
256 //===================================================
258 void ModelContainerView::fillVertexAndIndexArrays(const SubModel& pSm, Array<Vector3>& vArray, Array<int>& iArray) {
259 Array<TriangleBox> tbarray;
261 fillRenderArray(pSm, tbarray, &pSm.getTreeNode(0));
262 MeshBuilder builder;
263 int len = tbarray.size();
264 int count = 0;
265 for(int i=0;i<len;++i) {
266 Triangle t = Triangle(tbarray[i].vertex(0).getVector3() + pSm.getBasePosition(),
267 tbarray[i].vertex(1).getVector3() + pSm.getBasePosition(),
268 tbarray[i].vertex(2).getVector3() + pSm.getBasePosition());
270 vArray.append(t.vertex(0));
271 vArray.append(t.vertex(1));
272 vArray.append(t.vertex(2));
274 iArray.append(count+0);
275 iArray.append(count+1);
276 iArray.append(count+1);
277 iArray.append(count+2);
278 iArray.append(count+2);
279 iArray.append(count+0);
280 count+=3;
284 //====================================================================
286 void ModelContainerView::showMap(int pMapId, int x, int y) {
287 MapTree* mt = iVMapManager->getInstanceMapTree(pMapId);
288 std::string dirFileName = iVMapManager->getDirFileName(pMapId);
289 if(!mt->hasDirFile(dirFileName)) {
290 dirFileName = iVMapManager->getDirFileName(pMapId, x, y);
292 showMap(mt,dirFileName);
293 iInstanceId = pMapId;
296 //====================================================================
298 bool ModelContainerView::loadAndShowTile(int pMapId, int x, int y) {
299 char buffer[500];
300 sprintf(buffer, "%s/vmaps",gDataDir);
301 bool result = false;
302 //if(pMapId == 1) return true; //+++
303 int val = iVMapManager->loadMap(buffer,(unsigned int) pMapId, x,y);
304 if(val == VMAP_LOAD_RESULT_OK) {
305 result = true;
306 showMap(pMapId,x,y);
307 } else {
308 printf("Unable to load %s\n", buffer);
310 return(result);
313 //=======================================================================
315 void ModelContainerView::showMap(MapTree* mt, std::string dirFileName) {
316 if(mt->hasDirFile(dirFileName)) {
317 FilesInDir filesInDir = mt->getDirFiles(dirFileName);
318 if(filesInDir.getRefCount() == 1) {
319 Array<std::string> fileNames = filesInDir.getFiles();
320 for(int i=0; i<fileNames.size(); ++i) {
321 std::string name = fileNames[i];
322 ManagedModelContainer* mc = mt->getModelContainer(name);
323 //if(mc->getNSubModel() == 791) {
324 addModelContainer(name, mc);
331 //=======================================================================
333 bool ModelContainerView::loadAndShowTile(int pMapId) {
334 char buffer[500];
335 sprintf(buffer, "%s/vmaps",gDataDir);
336 bool result = false;
337 int val = iVMapManager->loadMap(buffer, (unsigned int) pMapId,-1,-1);
338 if(val == VMAP_LOAD_RESULT_OK) {
339 result = true;
340 MapTree* mt = iVMapManager->getInstanceMapTree(pMapId);
341 std::string dirFileName = iVMapManager->getDirFileName(pMapId);
342 iTriVarTable = Table<std::string , VAR*>();
343 iTriIndexTable = Table<std::string , Array<int> >(); // reset table
344 iInstanceId = pMapId;
345 showMap(mt,dirFileName);
347 return(result);
350 //====================================================================
352 void ModelContainerView::processCommand() {
353 iDrawLine = false;
354 if(iCurrCmdIndex < iCmdArray.size()) {
355 bool cmdfound = false;
356 while(!cmdfound && (iCurrCmdIndex < iCmdArray.size())) {
357 Command c = iCmdArray[iCurrCmdIndex];
358 if(c.getType() == LOAD_TILE) {
359 iPrevLoadCommands.push_back(c);
360 if(iPosSent) {
361 if(loadAndShowTile(c.getInt(2), c.getInt(0), c.getInt(1))) {
362 printf("load tile mapId=%d, %d, %d\n", c.getInt(2), c.getInt(0), c.getInt(1));
363 } else {
364 printf("ERROR: unable to load tile mapId= %d, %d, %d\n", c.getInt(2), c.getInt(0), c.getInt(1));
366 cmdfound = true;
367 } else {
368 printf("ignore load tile mapId=%d, %d, %d\n", c.getInt(2), c.getInt(0), c.getInt(1));
370 } else if(c.getType() == LOAD_INSTANCE) {
371 if(loadAndShowTile(c.getInt(0))) {
372 printf("load instance %d\n", c.getInt(0));
374 cmdfound = true;
375 } else if(c.getType() == UNLOAD_TILE) {
377 iVMapManager->unloadMap(c.getInt(2), c.getInt(0), c.getInt(1));
378 printf("unload tile %d, %d\n", c.getInt(0), c.getInt(1));
380 std::string dirFileName = iVMapManager->getDirFileName(iVMapManager->getMapIdNames(c.getInt(2)).iMapGroupName.c_str(), c.getInt(0), c.getInt(1));
381 MapTree* mt = iVMapManager->getInstanceMapTree(c.getInt(2));
382 if(mt->hasDirFile(dirFileName)) {
383 Array<std::string> fileNames = mt->getDirFiles(dirFileName).getFiles();
384 for(int i=0; i<fileNames.size(); ++i) {
385 std::string name = fileNames[i];
386 ManagedModelContainer* mc = mt->getModelContainer(name);
387 removeModelContainer(name, mc);
391 } else if(c.getType() == SET_POS) {
392 if(!iPosSent) {
393 int count = 3;
394 while(iPrevLoadCommands.size() > 0 && count>0) {
395 Command lc = iPrevLoadCommands.last();
396 iPrevLoadCommands.pop_back();
397 // first time, load the last map needed
398 if(loadAndShowTile(lc.getInt(2), lc.getInt(0), lc.getInt(1))) {
399 printf("load tile mapid=%d, %d, %d\n", lc.getInt(2), lc.getInt(0), lc.getInt(1));
400 } else {
401 printf("ERROR: unable to load tile mapid=%d, %d, %d\n", lc.getInt(2), lc.getInt(0), lc.getInt(1));
403 --count;
406 iPosSent = true;
407 defaultCamera.setPosition(Vector3(c.getVector(0).x,c.getVector(0).y+3, c.getVector(0).z));
408 defaultController.pointer()->setPosition(Vector3(c.getVector(0).x,c.getVector(0).y+3, c.getVector(0).z));
409 printf("set pos to %f, %f, %f\n",c.getVector(0).x, c.getVector(0).y, c.getVector(0).z );
410 cmdfound = true;
411 } else if(c.getType() == TEST_VIS) {
412 printf("TEST line of sight\n");
413 iDrawLine = true;
414 iPos1 = iVMapManager->convertPositionToInternalRep(c.getVector(0).x, c.getVector(0).y, c.getVector(0).z);
415 iPos2 = iVMapManager->convertPositionToInternalRep(c.getVector(1).x, c.getVector(1).y, c.getVector(1).z);
416 if(c.getInt(0) != 0) {
417 iColor = Color3::green();
418 } else {
419 iColor = Color3::red();
421 cmdfound = true;
424 // draw debug-lines
425 int count = 0;
426 for(int i=0; i<gTriArray.size(); ++i) {
427 Triangle &t = gTriArray[i];
429 iVTriDebugArray.append(t.vertex(0));
430 iVTriDebugArray.append(t.vertex(1));
431 iVTriDebugArray.append(t.vertex(2));
433 iTriDebugArray.append(count+0);
434 iTriDebugArray.append(count+1);
435 iTriDebugArray.append(count+1);
436 iTriDebugArray.append(count+2);
437 iTriDebugArray.append(count+2);
438 iTriDebugArray.append(count+0);
439 count+=3;
441 iTriDebugVar = VAR(iVTriDebugArray ,iVARAreaRef2);
445 ++iCurrCmdIndex;
447 } else {
448 printf("end reached\n");
452 //====================================================================
454 Vector3 ModelContainerView::convertPositionToMangosRep(float x, float y, float z) const {
455 float pos[3];
456 pos[0] = z;
457 pos[1] = x;
458 pos[2] = y;
459 double full = 64.0*533.33333333;
460 double mid = full/2.0;
461 pos[0] = -((mid+pos[0])-full);
462 pos[1] = -((mid+pos[1])-full);
464 return(Vector3(pos));
468 //====================================================================
471 void ModelContainerView::onUserInput(UserInput* ui) {
472 GApp::onUserInput(ui);
474 if(ui->keyPressed(GKey::fromString("l"))) { //load
475 iCmdArray = Array<Command>();
476 iCommandFileRW.getNewCommands(iCmdArray);
477 iCurrCmdIndex = 0;
478 processCommand();
481 if(ui->keyPressed(GKey::fromString("r"))) { //restart
482 iCurrCmdIndex = 0;
485 if(ui->keyPressed(GKey::fromString("1"))) { //inc count1
486 gCount1++;
489 if(ui->keyPressed(GKey::fromString("h"))) { //inc count1
490 #if 0
491 i_App->defaultController.getPosition();
492 Vector3 pos = i_App->defaultController.getPosition();
493 Vector3 pos2 = convertPositionToMangosRep(pos.x, pos.y, pos.z);
494 //Vector3 pos3 = iVMapManager->convertPositionToInternalRep(pos2.x, pos2.y, pos2.z);
495 //pos3 = iVMapManager->convertPositionToInternalRep(pos2.x, pos2.y, pos2.z);
497 float hight = iVMapManager->getHeight(iInstanceId, pos2.x, pos2.y, pos2.z);
498 printf("Hight = %f\n",hight);
499 #endif
503 if(ui->keyPressed(GKey::fromString("2"))) { //dec count1
504 gCount1--;
505 if(gCount1 < 0)
506 gCount1 = 0;
509 if(ui->keyPressed(GKey::fromString("z"))) { //zero pos
510 i_App->defaultCamera.setPosition(Vector3(0,0,0));
511 printf("set pos to 0, 0, 0\n");
515 if(ui->keyPressed(GKey::fromString("c"))) { //restart
516 if(iCurrCmdIndex > 0) {
517 if(iCmdArray[iCurrCmdIndex-1].getType() == TEST_VIS) {
518 Vector3 p1 = iCmdArray[iCurrCmdIndex-1].getVector(0);
519 Vector3 p2 = iCmdArray[iCurrCmdIndex-1].getVector(1);
520 bool result;
521 int mapId = iCmdArray[iCurrCmdIndex-1].getInt(1);
522 gCount3 = gCount2 = 0;// debug counter
523 gBoxArray = Array<AABox>();
524 result = iVMapManager->isInLineOfSight(mapId, p1.x,p1.y,p1.z,p2.x,p2.y,p2.z);
525 printf("recalc last line of light: result = %d\n", result);
531 if(ui->keyPressed(GKey::LEFT_MOUSE)) {
532 if( iCurrCmdIndex>0) {
533 --iCurrCmdIndex;
534 printf("restart last command\n");
535 processCommand();
539 if(ui->keyPressed(GKey::MIDDLE_MOUSE)) {
540 processCommand();
544 //==========================================
546 void ModelContainerView::setViewPosition(const Vector3& pPosition) {
547 //i_App->defaultController.setPosition(pPosition);
548 i_App->defaultCamera.setPosition(pPosition);
551 //==========================================
552 //==========================================
554 G3D_START_AT_MAIN();
555 int main(int argc, char** argv) {
556 if(argc == 3) {
557 VMAP::gDataDir = argv[1];
558 VMAP::gLogFile = argv[2];
560 G3D::GApp::Settings settings;
561 settings.window.width = 1024;
562 settings.window.height = 768;
563 //settings.useDeveloperTools = true;
565 VMAP::ModelContainerView modelContainerView(settings);
566 modelContainerView.run();
567 } else {
568 printf("%s <data dir> <vmapcmd.log file>\n",argv[0]);