root/trunk/contrib/vmap_debugger/ModelContainerView.cpp @ 230

Revision 44, 21.2 kB (checked in by yumileroy, 17 years ago)

[svn] * Merge Temp dev SVN with Assembla.
* Changes include:

  • Implementation of w12x's Outdoor PvP and Game Event Systems.
  • Temporary removal of IRC Chat Bot (until infinite loop when disabled is fixed).
  • All mangos -> trinity (to convert your mangos_string table, please run mangos_string_to_trinity_string.sql).
  • Improved Config cleanup.
  • And many more changes.

Original author: Seline
Date: 2008-10-14 11:57:03-05:00

Line 
1#include "ModelContainerView.h"
2
3namespace VMAP
4{
5    char* gDataDir = NULL;
6    char* gLogFile = NULL;
7    //==========================================
8
9    ModelContainerView::ModelContainerView(const G3D::GApp::Settings& settings) : GApp(settings) {
10        i_App = this;
11
12        iCommandFileRW.setFileName(gLogFile);
13        iCurrCmdIndex = 0;
14        iVMapManager = new VMapManager();
15        iDrawLine = false;
16
17        iVARAreaRef = VARArea::create(1024*1024*60);
18        iVARAreaRef2 = VARArea::create(1024*1024*2);
19        iInstanceId = -1;
20        iPosSent = false;
21
22    }
23    //===================================================
24
25    ModelContainerView::~ModelContainerView(void)
26    {
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;
33        }
34    }
35
36    //===================================================
37
38    void ModelContainerView::cleanup() {
39    }
40
41    //===================================================
42    Vector3 getViewPos(const ModelContainer* mc, unsigned int pModelNr = 0) {
43        if(mc->getNSubModel() < pModelNr) {
44            pModelNr = mc->getNSubModel();
45        }
46        const SubModel sm = mc->getSubModel(pModelNr);
47        return (sm.getAABoxBounds().low());
48    }
49
50    void ModelContainerView::init() {
51    }
52
53    //==========================================
54
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());
58
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);
64        }
65
66        if(root->getChild((TreeNode *) &pModelContainer->getTreeNode(0), 0)) {
67            fillSubModelArary(pModelContainer, root->getChild((TreeNode *)&pModelContainer->getTreeNode(0), 0), array, lo, hi);
68        }
69        if(root->getChild((TreeNode *)&pModelContainer->getTreeNode(0), 1)) {
70            fillSubModelArary(pModelContainer, root->getChild((TreeNode *)&pModelContainer->getTreeNode(0), 1), array, lo, hi);
71        }
72
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;
80        }
81
82    }
83
84    //==========================================
85    void ModelContainerView::addModelContainer(const std::string& pName,const ModelContainer* pModelContainer) {
86        // VARArea::UsageHint::WRITE_EVERY_FEW_FRAMES
87
88        int offset=0;
89
90        Array<int> iIndexArray;
91        Array<Vector3> iGlobArray;
92
93        Array<SubModel> SMArray;
94        Vector3 lo, hi;
95        fillSubModelArary(pModelContainer, &pModelContainer->getTreeNode(0), SMArray,lo,hi);
96
97
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);
103
104            for(int j=0;j<iArray.size(); ++j) {
105                iIndexArray.append(offset+iArray[j]);
106
107            }
108            for(int j=0;j<vArray.size(); ++j) {
109                iGlobArray.append(vArray[j]);
110            }
111            offset += vArray.size();
112            //break;
113        }
114        iTriVarTable.set(pName, new VAR(iGlobArray ,iVARAreaRef));
115        iTriIndexTable.set(pName, iIndexArray);
116    }
117
118    //===================================================
119
120    void ModelContainerView::removeModelContainer(const std::string& pName, const ModelContainer* pModelContainer) {
121        VAR* var = iTriVarTable.get(pName);
122        iTriVarTable.remove(pName);
123        delete var;
124    }
125
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;
131
132    //===================================================
133
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/");
139
140        iSkyParameters = SkyParameters(G3D::toSeconds(11, 00, 00, AM));
141        iLighting = Lighting::fromSky(iSky, iSkyParameters, Color3::white());
142
143        // This simple demo has no shadowing, so make all lights unshadowed
144        iLighting->lightArray.append(iLighting->shadowedLightArray);
145        iLighting->shadowedLightArray.clear();
146
147        // Example debug GUI:
148        //debugPane->addCheckBox("Use explicit checking", &explicitCheck);
149        debugWindow->setVisible(true);
150
151        toneMap->setEnabled(false);
152    }
153
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);
158
159
160        toneMap->beginFrame(rd);
161        rd->setProjectionAndCameraMatrix(defaultCamera);
162
163        rd->setColorClearValue(Color3::black());
164        rd->clear();
165        //iSky->render(rd, localSky);
166
167        // Setup lighting
168        rd->enableLighting();
169        //rd->setLight(0, localLighting->lightArray[0]);
170        //rd->setAmbientLightColor(localLighting->ambientAverage());
171
172        GLight light =GLight::directional(defaultController.pointer()->position() + defaultController.pointer()->lookVector()*2,Color3::white());
173        rd->setLight(0,light);
174
175        rd->setColor(Color3::blue());
176
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);
182
183            rd->beginIndexedPrimitives();
184            rd->setVertexArray(*var);
185            rd->sendIndices(RenderDevice::LINES, indexArray);
186            rd->endIndexedPrimitives();
187            ++i;
188        }
189        for(int i=0; i<gBoxArray.size(); ++i) {
190            AABox b = gBoxArray[i];
191            Draw::box(b,rd,Color4(Color3::red(),0.9f));
192        }
193        //-------
194        //triangles
195        {
196            if(iTriDebugArray.size() > 0)
197            {
198                rd->setColor(Color3::red());
199                rd->beginIndexedPrimitives();
200                rd->setVertexArray(iTriDebugVar);
201                rd->sendIndices(RenderDevice::LINES, iTriDebugArray);
202                rd->endIndexedPrimitives();
203            }
204        }
205        //--------
206        if(iDrawLine) {
207            Draw::lineSegment(LineSegment::fromTwoPoints(iPos1, iPos2), rd, iColor, 3);
208
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());
215            }
216        }
217
218
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);
224
225            for (int i = 0; i < opaque.size(); ++i) {
226                opaque[i]->render(renderDevice);
227            }
228
229            for (int i = 0; i < transparent.size(); ++i) {
230                transparent[i]->render(renderDevice);
231            }
232        }
233
234        rd->disableLighting();
235
236        toneMap->endFrame(rd);
237        PosedModel2D::sortAndRender(rd, posed2D);
238    }
239
240    //===================================================
241
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()]);
245        }
246
247        if(pTreeNode->getChild(pSm.getTreeNodes(), 0) != 0) {
248            fillRenderArray(pSm, pArray, pTreeNode->getChild(pSm.getTreeNodes(), 0));
249        }
250
251        if(pTreeNode->getChild(pSm.getTreeNodes(), 1) != 0) {
252            fillRenderArray(pSm, pArray, pTreeNode->getChild(pSm.getTreeNodes(), 1));
253        }
254    }
255
256    //===================================================
257
258    void ModelContainerView::fillVertexAndIndexArrays(const SubModel& pSm, Array<Vector3>& vArray, Array<int>& iArray) {
259        Array<TriangleBox> tbarray;
260
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());
269
270            vArray.append(t.vertex(0));
271            vArray.append(t.vertex(1));
272            vArray.append(t.vertex(2));
273
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;
281        }
282    }
283
284    //====================================================================
285
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);
291        }
292        showMap(mt,dirFileName);
293        iInstanceId = pMapId;
294    }
295
296    //====================================================================
297
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);
309        }
310        return(result);
311    }
312
313    //=======================================================================
314
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);
325                    //}
326                }
327            }
328        }
329    }
330
331    //=======================================================================
332
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);
346        }
347        return(result);
348    }
349
350    //====================================================================
351
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));
365                        }
366                        cmdfound = true;
367                    } else {
368                        printf("ignore load tile mapId=%d, %d, %d\n", c.getInt(2), c.getInt(0), c.getInt(1));
369                    }
370                } else if(c.getType() == LOAD_INSTANCE) {
371                    if(loadAndShowTile(c.getInt(0))) {
372                        printf("load instance %d\n", c.getInt(0));
373                    }
374                    cmdfound = true;
375                } else if(c.getType() == UNLOAD_TILE) {
376                    /*
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));
379
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);
388                    }
389                    }
390                    */
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));
402                            }
403                            --count;
404                        }
405                    }
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();
420                    }
421                    cmdfound = true;
422/*
423                    {
424                        // draw debug-lines
425                        int count = 0;
426                        for(int i=0; i<gTriArray.size(); ++i) {
427                            Triangle &t = gTriArray[i];
428
429                            iVTriDebugArray.append(t.vertex(0));
430                            iVTriDebugArray.append(t.vertex(1));
431                            iVTriDebugArray.append(t.vertex(2));
432
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;
440                        }
441                        iTriDebugVar = VAR(iVTriDebugArray ,iVARAreaRef2);
442                    }
443                    */
444                }
445                ++iCurrCmdIndex;
446            }
447        } else {
448            printf("end reached\n");
449        }
450    }
451
452    //====================================================================
453
454    Vector3 ModelContainerView::convertPositionToTrinityRep(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);
463
464        return(Vector3(pos));
465    }
466
467
468    //====================================================================
469
470
471    void ModelContainerView::onUserInput(UserInput* ui) {
472        GApp::onUserInput(ui);
473
474        if(ui->keyPressed(GKey::fromString("l"))) { //load
475            iCmdArray = Array<Command>();
476            iCommandFileRW.getNewCommands(iCmdArray);
477            iCurrCmdIndex = 0;
478            processCommand();
479        }
480
481        if(ui->keyPressed(GKey::fromString("r"))) { //restart
482            iCurrCmdIndex = 0;
483        }
484
485        if(ui->keyPressed(GKey::fromString("1"))) { //inc count1
486            gCount1++;
487        }
488
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 = convertPositionToTrinityRep(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);
496
497            float hight = iVMapManager->getHeight(iInstanceId, pos2.x, pos2.y, pos2.z);
498            printf("Hight = %f\n",hight);
499#endif
500        }
501
502
503        if(ui->keyPressed(GKey::fromString("2"))) { //dec count1
504            gCount1--;
505            if(gCount1 < 0) 
506                gCount1 = 0;
507        }
508
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");
512        }
513
514
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);
526                }
527            }
528        }
529
530
531        if(ui->keyPressed(GKey::LEFT_MOUSE)) {
532            if(         iCurrCmdIndex>0) {
533                --iCurrCmdIndex;
534                printf("restart last command\n");
535                processCommand();
536            }
537        }
538
539        if(ui->keyPressed(GKey::MIDDLE_MOUSE)) {
540            processCommand();
541        }
542
543    }
544    //==========================================
545
546    void ModelContainerView::setViewPosition(const Vector3& pPosition) {
547        //i_App->defaultController.setPosition(pPosition);
548        i_App->defaultCamera.setPosition(pPosition);
549    }
550
551    //==========================================
552    //==========================================
553}
554G3D_START_AT_MAIN();
555int main(int argc, char** argv) {
556    if(argc == 3) {
557        VMAP::gDataDir = argv[1];
558        VMAP::gLogFile = argv[2];
559
560        G3D::GApp::Settings settings;
561        settings.window.width = 1024;
562        settings.window.height = 768;
563        //settings.useDeveloperTools = true;
564
565        VMAP::ModelContainerView modelContainerView(settings);
566        modelContainerView.run();
567    } else {
568        printf("%s <data dir> <vmapcmd.log file>\n",argv[0]);
569    }
570}
Note: See TracBrowser for help on using the browser.