Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: some speed improvements of the cd

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