Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: now ready to implement the collision detection algorithm

File size: 9.4 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#include "graphics_engine.h"
33#include "list.h"
34
35
36Model* mod;
37MD2Model* model;
38int drawMode;
39int depth;
40tList<WorldEntity>* entityList;
41
42int lastFrame, currentFrame, dt;
43bool drawModel;
44
45void Framework::moduleInit(int argc, char** argv)
46{
47  GraphicsEngine::getInstance()->setWindowName("Collision Cluster", "collision");
48
49  CDEngine::getInstance();
50
51  /* Simple Test */
52  //CDEngine::getInstance()->debug();
53
54  /* MD2 Model Test */
55//   model = new MD2Model("models/tris.md2", "models/tris.pcx");
56//   model->tick(0.1f);
57//   CDEngine::getInstance()->debugSpawnTree(9, model->data->pVertices, model->data->numVertices);
58
59  /* OBJ - Model Test */
60//   mod = (Model*)ResourceManager::getInstance()->load("models/reaplow.obj", OBJ, RP_CAMPAIGN);
61//   CDEngine::getInstance()->debugSpawnTree(9, (sVec3D*)mod->getVertexArray(), mod->getVertexArrayCount());
62
63
64
65  entityList = new tList<WorldEntity>();
66
67  TestEntity* a = new TestEntity(); a->setName("Clown1");
68  Environment* b = new Environment(); b->setName("Jaeger");
69  b->setRelCoor(0.0, 0.0, -10.0);
70
71  TestEntity* c = new TestEntity(); c->setName("Colwn2");
72  c->setRelCoor(0.0, 0.0, -20.0);
73
74  entityList->add(a);
75  entityList->add(b);
76  entityList->add(c);
77
78  CDEngine::getInstance()->setEntityList(entityList);
79
80  LightManager* lightMan = LightManager::getInstance();
81  lightMan->setAmbientColor(.1,.1,.1);
82  lightMan->addLight();
83  lightMan->setPosition(30, 30, 30);
84  lightMan->addLight();
85  lightMan->setPosition(-30, -30, -30);
86
87  /* properties */
88  drawMode = DRAW_MODEL;
89  depth = 0;
90  dt = lastFrame = currentFrame = 0;
91
92
93  moduleHelp();
94}
95
96
97void Framework::moduleEventHandler(SDL_Event* event)
98{
99  switch (event->type)
100  {
101    case SDL_KEYDOWN:
102      switch (event->key.keysym.sym)
103      {
104        case SDLK_a:
105          drawMode |= DRAW_ALL;
106          break;
107        case SDLK_0:
108        {
109          printf("Setting tree depth = 0\n");
110          depth = 0;
111          int temp = drawMode & DRAW_ALL;
112          drawMode ^= temp;
113          drawMode |= DRAW_SINGLE;
114          break;
115        }
116        case SDLK_1:
117        {
118          printf("Setting tree depth = 1\n");
119          depth = 1;
120          int temp = drawMode & DRAW_ALL;
121          drawMode ^= temp;
122          drawMode |= DRAW_SINGLE;
123          break;
124        }
125        case SDLK_2:
126        {
127          printf("Setting tree depth = 2\n");
128          depth = 2;
129          int temp = drawMode & DRAW_ALL;
130          drawMode ^= temp;
131          drawMode |= DRAW_SINGLE;
132          break;
133        }
134        case SDLK_3:
135        {
136          printf("Setting tree depth = 3\n");
137          depth = 3;
138          int temp = drawMode & DRAW_ALL;
139          drawMode ^= temp;
140          drawMode |= DRAW_SINGLE;
141          break;
142        }
143        case SDLK_4:
144        {
145          printf("Setting tree depth = 4\n");
146          depth = 4;
147          int temp = drawMode & DRAW_ALL;
148          drawMode ^= temp;
149          drawMode |= DRAW_SINGLE;
150          break;
151        }
152        case SDLK_5:
153        {
154          printf("Setting tree depth = 5\n");
155          depth = 5;
156          int temp = drawMode & DRAW_ALL;
157          drawMode ^= temp;
158          drawMode |= DRAW_SINGLE;
159          break;
160        }
161        case SDLK_6:
162        {
163          printf("Setting tree depth = 6\n");
164          depth = 6;
165          int temp = drawMode & DRAW_ALL;
166          drawMode ^= temp;
167          drawMode |= DRAW_SINGLE;
168          break;
169        }
170        case SDLK_s:
171          if(drawMode & DRAW_SEPARATING_PLANE)
172          {
173            int temp = drawMode & DRAW_SEPARATING_PLANE;
174            drawMode ^= temp;
175            printf("Removing Separation Plane\n");
176          }
177          else
178          {
179            drawMode |= DRAW_SEPARATING_PLANE;
180            printf("Drawing Separation Plane\n");
181          }
182
183          break;
184        case SDLK_p:
185          if(drawMode & DRAW_BV_POLYGON)
186          {
187            int temp = drawMode & DRAW_BV_POLYGON;
188            drawMode ^= temp;
189            printf("Removing OBB Polygons\n");
190          }
191          else
192          {
193            drawMode |= DRAW_BV_POLYGON;
194            printf("Drawing OBB Polygons\n");
195          }
196          break;
197
198      }
199  }
200}
201
202
203void Framework::moduleTick(float dt)
204{
205
206  CDEngine::getInstance()->checkCollisions();
207
208  currentFrame = SDL_GetTicks();
209  dt = currentFrame - lastFrame;
210
211  if( dt > 0)
212  {
213    float fps = 1000/dt;
214
215          // temporary, only for showing how fast the text-engine is
216    printf("we got %f fps\n", fps);
217  }
218  else
219  {
220          /* the frame-rate is limited to 100 frames per second, all other things are for nothing.
221          */
222    SDL_Delay((unsigned int)(10-dt));
223    dt = 10;
224  }
225
226
227  tIterator<WorldEntity>* iterator = entityList->getIterator(); /* get the iterator - JAVA style */
228  WorldEntity* entity = iterator->nextElement();   /* this returns the FIRST element */
229  while( entity != NULL)                        /* nextElement() will return NULL at the end */
230  {
231    entity->tick(0.1);
232    entity = iterator->nextElement();       /* give back the next element or NULL if last */
233  }
234  delete iterator;
235
236  lastFrame = currentFrame;
237}
238
239
240void Framework::moduleDraw() const
241{
242  //CDEngine::getInstance()->drawBV(depth, drawMode);
243  LightManager::getInstance()->draw();
244
245  tIterator<WorldEntity>* iterator = entityList->getIterator(); /* get the iterator - JAVA style */
246  WorldEntity* entity = iterator->nextElement();   /* this returns the FIRST element */
247  while( entity != NULL)                        /* nextElement() will return NULL at the end */
248  {
249    if( drawModel)
250    entity->draw();
251    entity->drawBVTree(depth, drawMode);
252    entity = iterator->nextElement();       /* give back the next element or NULL if last */
253  }
254  delete iterator;
255  //model->draw();
256}
257
258
259void Framework::moduleHelp(void) const
260{
261  printf("\n\n===========================");
262  printf("Collision Detection Modler:\n");
263  printf("Key Bindings:\n");
264  printf(" -| Displaying Polygons\t\t p\n");
265  printf(" -| Displaying Separation Plane\t s\n");
266  printf("\n");
267  printf(" -| Tree Depth 0\t\t 0\n");
268  printf(" -| Tree Depth 1\t\t 1\n");
269  printf(" -| Tree Depth 2\t\t 2\n");
270  printf(" -| Tree Depth 3\t\t 3\n");
271  printf(" -| Tree Depth 4\t\t 4\n");
272  printf(" -| Tree Depth 5\t\t 5\n");
273  printf("===========================\n\n");
274
275}
276
277int boxPolygons(GtkWidget* nonInterest, void* widget)
278{
279  if(drawMode & DRAW_BV_POLYGON)
280  {
281    int temp = drawMode & DRAW_BV_POLYGON;
282    drawMode ^= temp;
283    printf("Removing OBB Polygons\n");
284  }
285  else
286  {
287    drawMode |= DRAW_BV_POLYGON;
288    printf("Drawing OBB Polygons\n");
289  }
290}
291
292int seperatingPlanes(GtkWidget* nonInterest, void* widget)
293{
294  if(drawMode & DRAW_SEPARATING_PLANE)
295  {
296    int temp = drawMode & DRAW_SEPARATING_PLANE;
297    drawMode ^= temp;
298    printf("Removing Separation Plane\n");
299  }
300  else
301  {
302    drawMode |= DRAW_SEPARATING_PLANE;
303    printf("Drawing Separation Plane\n");
304  }
305}
306
307
308int blendedBox(GtkWidget* nonInterest, void* widget)
309{
310  if(drawMode & DRAW_BV_BLENDED)
311  {
312    int temp = drawMode & DRAW_BV_BLENDED;
313    drawMode ^= temp;
314    printf("Removing OBB Polygons\n");
315  }
316  else
317  {
318    drawMode |= DRAW_BV_BLENDED;
319    printf("Drawing OBB Polygons\n");
320  }
321}
322
323
324int drawModels(GtkWidget* nonInterest, void* widget)
325{
326  drawModel = !drawModel;
327}
328
329
330int treeDepth(GtkWidget* nonInterest, void* widget)
331{
332  Option* option = (Option*) widget;
333  const char* name = option->getTitle();
334  char* value = option->save();
335
336  depth = atoi(value);
337  printf("Setting tree depth = %i\n", depth);
338  int temp = drawMode & DRAW_ALL;
339  drawMode ^= temp;
340  drawMode |= DRAW_SINGLE;
341
342  delete value;
343}
344
345
346void Framework::moduleInitGui(int argc, char** argv)
347{
348  Window* guiMainWindow = NULL;
349
350  initGUI(0, NULL);
351
352  guiMainWindow = new Window("Collision_detection");
353  {
354    Box* windowBox = new Box('v');
355    {
356      CheckButton* BoxPolygons = new CheckButton("Draw OBB Polygons");
357      BoxPolygons->connectSignal("clicked", (void*)BoxPolygons, boxPolygons);
358      windowBox->fill(BoxPolygons);
359
360
361      CheckButton* BlendedBox = new CheckButton("Draw OBB Blended");
362      BlendedBox->connectSignal("clicked", (void*)BlendedBox, blendedBox);
363      windowBox->fill(BlendedBox);
364
365
366      CheckButton* DrawModels = new CheckButton("Draw Models");
367      DrawModels->connectSignal("clicked", (void*)DrawModels, drawModels);
368      windowBox->fill(DrawModels);
369
370
371      CheckButton* SeperatingPlanes = new CheckButton("SeperatingPlanes");
372      SeperatingPlanes->connectSignal("clicked", (void*)SeperatingPlanes, seperatingPlanes);
373      windowBox->fill(SeperatingPlanes);
374
375
376      Slider* TreeDepth = new Slider("TreeDepth", 0, 5);
377      TreeDepth->connectSignal("value_changed", (void*)TreeDepth, treeDepth);
378      windowBox->fill(TreeDepth);
379    }
380    guiMainWindow->fill(windowBox);
381  }
382  Window::mainWindow->showall();
383  Window::mainWindow->setSize(300, 500);
384}
Note: See TracBrowser for help on using the repository browser.