Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/subprojects/collision_detection/collision_detection.cc @ 4710

Last change on this file since 4710 was 4710, checked in by patrick, 19 years ago

orxonox/trunk: weeped out some very serious bugs, now the tree separation werks flawless, hf

File size: 9.2 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14
15   this file extends the framework file, so it renders what i want.
16*/
17
18#include "framework.h"
19
20#include "fields.h"
21#include "stdlibincl.h"
22#include "light.h"
23
24#include "cd_engine.h"
25#include "bv_tree.h"
26
27#include "md2Model.h"
28#include "model.h"
29#include "test_entity.h"
30#include "environment.h"
31
32
33#include "graphics_engine.h"
34#include "list.h"
35
36
37Model* mod;
38MD2Model* model;
39int drawMode;
40int depth;
41float iterata;
42tList<WorldEntity>* entityList;
43
44int lastFrame, currentFrame, dt;
45bool drawModel;
46
47WorldEntity* a;
48WorldEntity* b;
49//Terrain* c;
50
51void Framework::moduleInit(int argc, char** argv)
52{
53  GraphicsEngine::getInstance()->setWindowName("Collision Cluster", "collision");
54
55  CDEngine::getInstance();
56
57  /* Primitive Data Test */
58//   sVec3D* data = new sVec3D[6];
59//   float tmp[6][3] = {{0.0, 4.2, 2.4}, {3.0, 3.0, 2.0}, {5.0, 1.0, 6.0}, {6.5, 1.0, 3.0}, {7.0, 3.0, 5.0}, {8.0, 6.0, 6.0}};
60//   for(int i = 0; i < 6; ++i)
61//   {
62//     data[i][0] = tmp[i][0];
63//     data[i][1] = tmp[i][1];
64//     data[i][2] = tmp[i][2];
65//   }
66//   CDEngine::getInstance()->debugSpawnTree(5, data, 6);
67
68  /* MD2 Model Test */
69//   model = new MD2Model("models/tris.md2", "models/tris.pcx");
70//   model->tick(0.1f);
71//   CDEngine::getInstance()->debugSpawnTree(9, model->data->pVertices, model->data->numVertices);
72
73  /* OBJ - Model Test */
74//   mod = (Model*)ResourceManager::getInstance()->load("models/reaplow.obj", OBJ, RP_CAMPAIGN);
75//   CDEngine::getInstance()->debugSpawnTree(9, (sVec3D*)mod->getVertexArray(), mod->getVertexArrayCount());
76
77
78
79  entityList = new tList<WorldEntity>();
80
81  a = new TestEntity(); a->setName("Clown1");
82  b = new Environment(); a->setName("Clown");
83  //b = new Environment(); b->setName("Jaeger");
84  //c = new Terrain();
85
86  b->setRelCoor(0.0, 0.0, -10.0);
87  //b->setRelDir(Quaternion(-M_PI/2.0f, Vector(0.0, 1.0, 0.0)));
88
89  entityList->add(a);
90  entityList->add(b);
91
92  CDEngine::getInstance()->setEntityList(entityList);
93
94  LightManager* lightMan = LightManager::getInstance();
95    lightMan->setAmbientColor(.1,.1,.1);
96  lightMan->addLight();
97  lightMan->setPosition(-5.0, 50.0, -40.0);
98  lightMan->addLight();
99  lightMan->setPosition(100, 80, 60);
100
101
102  /* properties */
103  drawMode = DRAW_MODEL;
104  depth = 0;
105  dt = lastFrame = currentFrame = 0;
106  iterata = 0.05f;
107
108  moduleHelp();
109}
110
111
112void Framework::moduleEventHandler(SDL_Event* event)
113{
114  switch (event->type)
115  {
116    case SDL_KEYDOWN:
117      switch (event->key.keysym.sym)
118      {
119        case SDLK_a:
120          drawMode |= DRAW_ALL;
121          break;
122        case SDLK_0:
123        {
124          printf("Setting tree depth = 0\n");
125          depth = 0;
126          int temp = drawMode & DRAW_ALL;
127          drawMode ^= temp;
128          drawMode |= DRAW_SINGLE;
129          break;
130        }
131        case SDLK_1:
132        {
133          printf("Setting tree depth = 1\n");
134          depth = 1;
135          int temp = drawMode & DRAW_ALL;
136          drawMode ^= temp;
137          drawMode |= DRAW_SINGLE;
138          break;
139        }
140        case SDLK_2:
141        {
142          printf("Setting tree depth = 2\n");
143          depth = 2;
144          int temp = drawMode & DRAW_ALL;
145          drawMode ^= temp;
146          drawMode |= DRAW_SINGLE;
147          break;
148        }
149        case SDLK_3:
150        {
151          printf("Setting tree depth = 3\n");
152          depth = 3;
153          int temp = drawMode & DRAW_ALL;
154          drawMode ^= temp;
155          drawMode |= DRAW_SINGLE;
156          break;
157        }
158        case SDLK_4:
159        {
160          printf("Setting tree depth = 4\n");
161          depth = 4;
162          int temp = drawMode & DRAW_ALL;
163          drawMode ^= temp;
164          drawMode |= DRAW_SINGLE;
165          break;
166        }
167        case SDLK_5:
168        {
169          printf("Setting tree depth = 5\n");
170          depth = 5;
171          int temp = drawMode & DRAW_ALL;
172          drawMode ^= temp;
173          drawMode |= DRAW_SINGLE;
174          break;
175        }
176        case SDLK_6:
177        {
178          printf("Setting tree depth = 6\n");
179          depth = 6;
180          int temp = drawMode & DRAW_ALL;
181          drawMode ^= temp;
182          drawMode |= DRAW_SINGLE;
183          break;
184        }
185        case SDLK_s:
186          if(drawMode & DRAW_SEPARATING_PLANE)
187          {
188            int temp = drawMode & DRAW_SEPARATING_PLANE;
189            drawMode ^= temp;
190            printf("Removing Separation Plane\n");
191          }
192          else
193          {
194            drawMode |= DRAW_SEPARATING_PLANE;
195            printf("Drawing Separation Plane\n");
196          }
197
198          break;
199        case SDLK_p:
200          if(drawMode & DRAW_BV_POLYGON)
201          {
202            int temp = drawMode & DRAW_BV_POLYGON;
203            drawMode ^= temp;
204            printf("Removing OBB Polygons\n");
205          }
206          else
207          {
208            drawMode |= DRAW_BV_POLYGON;
209            printf("Drawing OBB Polygons\n");
210          }
211          break;
212        case SDLK_o:
213          if(iterata == 0.0f)
214          {
215            iterata = 0.05f;
216          }
217          else
218          {
219            iterata = 0.0f;
220          }
221          break;
222
223      }
224  }
225}
226
227
228void Framework::moduleTick(float dt)
229{
230
231  CDEngine::getInstance()->checkCollisions();
232
233
234  //b->shiftCoor(Vector(0.0, 0.0, iterata));
235
236
237  tIterator<WorldEntity>* iterator = entityList->getIterator();
238  WorldEntity* entity = iterator->nextElement();
239  while( entity != NULL)
240  {
241    entity->tick(dt);
242    entity = iterator->nextElement();
243  }
244  delete iterator;
245}
246
247
248void Framework::moduleDraw() const
249{
250  //CDEngine::getInstance()->debugDraw(depth, drawMode);
251
252  tIterator<WorldEntity>* iterator = entityList->getIterator();
253  WorldEntity* entity = iterator->nextElement();
254  while( entity != NULL)
255  {
256    if( likely(drawModel))
257    entity->draw();
258    entity->drawBVTree(depth, drawMode);
259    entity = iterator->nextElement();
260  }
261  delete iterator;
262
263  LightManager::getInstance()->draw();
264}
265
266
267void Framework::moduleHelp(void) const
268{
269  printf("\n\n===========================");
270  printf("Collision Detection Modler:\n");
271  printf("Key Bindings:\n");
272  printf(" -| Displaying Polygons\t\t p\n");
273  printf(" -| Displaying Separation Plane\t s\n");
274  printf("\n");
275  printf(" -| Tree Depth 0\t\t 0\n");
276  printf(" -| Tree Depth 1\t\t 1\n");
277  printf(" -| Tree Depth 2\t\t 2\n");
278  printf(" -| Tree Depth 3\t\t 3\n");
279  printf(" -| Tree Depth 4\t\t 4\n");
280  printf(" -| Tree Depth 5\t\t 5\n");
281  printf("===========================\n\n");
282
283}
284
285int boxPolygons(GtkWidget* nonInterest, void* widget)
286{
287  if(drawMode & DRAW_BV_POLYGON)
288  {
289    int temp = drawMode & DRAW_BV_POLYGON;
290    drawMode ^= temp;
291    printf("Removing OBB Polygons\n");
292  }
293  else
294  {
295    drawMode |= DRAW_BV_POLYGON;
296    printf("Drawing OBB Polygons\n");
297  }
298}
299
300int seperatingPlanes(GtkWidget* nonInterest, void* widget)
301{
302  if(drawMode & DRAW_SEPARATING_PLANE)
303  {
304    int temp = drawMode & DRAW_SEPARATING_PLANE;
305    drawMode ^= temp;
306    printf("Removing Separation Plane\n");
307  }
308  else
309  {
310    drawMode |= DRAW_SEPARATING_PLANE;
311    printf("Drawing Separation Plane\n");
312  }
313}
314
315
316int blendedBox(GtkWidget* nonInterest, void* widget)
317{
318  if(drawMode & DRAW_BV_BLENDED)
319  {
320    int temp = drawMode & DRAW_BV_BLENDED;
321    drawMode ^= temp;
322    printf("Removing OBB Polygons\n");
323  }
324  else
325  {
326    drawMode |= DRAW_BV_BLENDED;
327    printf("Drawing OBB Polygons\n");
328  }
329}
330
331
332int drawModels(GtkWidget* nonInterest, void* widget)
333{
334  drawModel = !drawModel;
335}
336
337
338int treeDepth(GtkWidget* nonInterest, void* widget)
339{
340  Option* option = (Option*) widget;
341  const char* name = option->getTitle();
342  char* value = option->save();
343
344  depth = atoi(value);
345  printf("Setting tree depth = %i\n", depth);
346  int temp = drawMode & DRAW_ALL;
347  drawMode ^= temp;
348  drawMode |= DRAW_SINGLE;
349
350  delete value;
351}
352
353
354void Framework::moduleInitGui(int argc, char** argv)
355{
356  Window* guiMainWindow = NULL;
357
358  initGUI(0, NULL);
359
360  guiMainWindow = new Window("Collision_detection");
361  {
362    Box* windowBox = new Box('v');
363    {
364      CheckButton* BoxPolygons = new CheckButton("Draw OBB Polygons");
365      BoxPolygons->connectSignal("clicked", (void*)BoxPolygons, boxPolygons);
366      windowBox->fill(BoxPolygons);
367
368
369      CheckButton* BlendedBox = new CheckButton("Draw OBB Blended");
370      BlendedBox->connectSignal("clicked", (void*)BlendedBox, blendedBox);
371      windowBox->fill(BlendedBox);
372
373
374      CheckButton* DrawModels = new CheckButton("Draw Models");
375      DrawModels->connectSignal("clicked", (void*)DrawModels, drawModels);
376      windowBox->fill(DrawModels);
377
378
379      CheckButton* SeperatingPlanes = new CheckButton("SeperatingPlanes");
380      SeperatingPlanes->connectSignal("clicked", (void*)SeperatingPlanes, seperatingPlanes);
381      windowBox->fill(SeperatingPlanes);
382
383
384      Slider* TreeDepth = new Slider("TreeDepth", 0, 10);
385      TreeDepth->connectSignal("value_changed", (void*)TreeDepth, treeDepth);
386      windowBox->fill(TreeDepth);
387    }
388    guiMainWindow->fill(windowBox);
389  }
390  Window::mainWindow->showall();
391  Window::mainWindow->setSize(300, 500);
392}
Note: See TracBrowser for help on using the repository browser.