Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: the cd now supports rotations in the detection

File size: 8.9 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
46Environment* 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
75  b->setRelCoor(0.0, 0.0, -10.0);
76  b->setRelDir(Quaternion(-M_PI/2.0f, Vector(0.0, 1.0, 0.0)));
77
78//   TestEntity* c = new TestEntity(); c->setName("Colwn2");
79//   c->setRelCoor(0.0, 0.0, -20.0);
80
81  entityList->add(a);
82  entityList->add(b);
83
84  CDEngine::getInstance()->setEntityList(entityList);
85
86  LightManager* lightMan = LightManager::getInstance();
87  lightMan->setAmbientColor(.1,.1,.1);
88  lightMan->addLight();
89  lightMan->setPosition(30, 30, 30);
90  lightMan->addLight();
91  lightMan->setPosition(-30, -30, -30);
92
93  /* properties */
94  drawMode = DRAW_MODEL;
95  depth = 0;
96  dt = lastFrame = currentFrame = 0;
97  iterata = 0.05f;
98
99  moduleHelp();
100}
101
102
103void Framework::moduleEventHandler(SDL_Event* event)
104{
105  switch (event->type)
106  {
107    case SDL_KEYDOWN:
108      switch (event->key.keysym.sym)
109      {
110        case SDLK_a:
111          drawMode |= DRAW_ALL;
112          break;
113        case SDLK_0:
114        {
115          printf("Setting tree depth = 0\n");
116          depth = 0;
117          int temp = drawMode & DRAW_ALL;
118          drawMode ^= temp;
119          drawMode |= DRAW_SINGLE;
120          break;
121        }
122        case SDLK_1:
123        {
124          printf("Setting tree depth = 1\n");
125          depth = 1;
126          int temp = drawMode & DRAW_ALL;
127          drawMode ^= temp;
128          drawMode |= DRAW_SINGLE;
129          break;
130        }
131        case SDLK_2:
132        {
133          printf("Setting tree depth = 2\n");
134          depth = 2;
135          int temp = drawMode & DRAW_ALL;
136          drawMode ^= temp;
137          drawMode |= DRAW_SINGLE;
138          break;
139        }
140        case SDLK_3:
141        {
142          printf("Setting tree depth = 3\n");
143          depth = 3;
144          int temp = drawMode & DRAW_ALL;
145          drawMode ^= temp;
146          drawMode |= DRAW_SINGLE;
147          break;
148        }
149        case SDLK_4:
150        {
151          printf("Setting tree depth = 4\n");
152          depth = 4;
153          int temp = drawMode & DRAW_ALL;
154          drawMode ^= temp;
155          drawMode |= DRAW_SINGLE;
156          break;
157        }
158        case SDLK_5:
159        {
160          printf("Setting tree depth = 5\n");
161          depth = 5;
162          int temp = drawMode & DRAW_ALL;
163          drawMode ^= temp;
164          drawMode |= DRAW_SINGLE;
165          break;
166        }
167        case SDLK_6:
168        {
169          printf("Setting tree depth = 6\n");
170          depth = 6;
171          int temp = drawMode & DRAW_ALL;
172          drawMode ^= temp;
173          drawMode |= DRAW_SINGLE;
174          break;
175        }
176        case SDLK_s:
177          if(drawMode & DRAW_SEPARATING_PLANE)
178          {
179            int temp = drawMode & DRAW_SEPARATING_PLANE;
180            drawMode ^= temp;
181            printf("Removing Separation Plane\n");
182          }
183          else
184          {
185            drawMode |= DRAW_SEPARATING_PLANE;
186            printf("Drawing Separation Plane\n");
187          }
188
189          break;
190        case SDLK_p:
191          if(drawMode & DRAW_BV_POLYGON)
192          {
193            int temp = drawMode & DRAW_BV_POLYGON;
194            drawMode ^= temp;
195            printf("Removing OBB Polygons\n");
196          }
197          else
198          {
199            drawMode |= DRAW_BV_POLYGON;
200            printf("Drawing OBB Polygons\n");
201          }
202          break;
203        case SDLK_o:
204          if(iterata == 0.0f)
205          {
206            iterata = 0.05f;
207          }
208          else
209          {
210            iterata = 0.0f;
211          }
212          break;
213
214      }
215  }
216}
217
218
219void Framework::moduleTick(float dt)
220{
221
222  CDEngine::getInstance()->checkCollisions();
223
224
225  b->shiftCoor(Vector(0.0, 0.0, iterata));
226
227
228  tIterator<WorldEntity>* iterator = entityList->getIterator();
229  WorldEntity* entity = iterator->nextElement();
230  while( entity != NULL)
231  {
232    entity->tick(dt);
233    entity = iterator->nextElement();
234  }
235  delete iterator;
236}
237
238
239void Framework::moduleDraw() const
240{
241  tIterator<WorldEntity>* iterator = entityList->getIterator();
242  WorldEntity* entity = iterator->nextElement();
243  while( entity != NULL)
244  {
245    if( likely(drawModel))
246    entity->draw();
247    entity->drawBVTree(depth, drawMode);
248    entity = iterator->nextElement();
249  }
250  delete iterator;
251
252  LightManager::getInstance()->draw();
253}
254
255
256void Framework::moduleHelp(void) const
257{
258  printf("\n\n===========================");
259  printf("Collision Detection Modler:\n");
260  printf("Key Bindings:\n");
261  printf(" -| Displaying Polygons\t\t p\n");
262  printf(" -| Displaying Separation Plane\t s\n");
263  printf("\n");
264  printf(" -| Tree Depth 0\t\t 0\n");
265  printf(" -| Tree Depth 1\t\t 1\n");
266  printf(" -| Tree Depth 2\t\t 2\n");
267  printf(" -| Tree Depth 3\t\t 3\n");
268  printf(" -| Tree Depth 4\t\t 4\n");
269  printf(" -| Tree Depth 5\t\t 5\n");
270  printf("===========================\n\n");
271
272}
273
274int boxPolygons(GtkWidget* nonInterest, void* widget)
275{
276  if(drawMode & DRAW_BV_POLYGON)
277  {
278    int temp = drawMode & DRAW_BV_POLYGON;
279    drawMode ^= temp;
280    printf("Removing OBB Polygons\n");
281  }
282  else
283  {
284    drawMode |= DRAW_BV_POLYGON;
285    printf("Drawing OBB Polygons\n");
286  }
287}
288
289int seperatingPlanes(GtkWidget* nonInterest, void* widget)
290{
291  if(drawMode & DRAW_SEPARATING_PLANE)
292  {
293    int temp = drawMode & DRAW_SEPARATING_PLANE;
294    drawMode ^= temp;
295    printf("Removing Separation Plane\n");
296  }
297  else
298  {
299    drawMode |= DRAW_SEPARATING_PLANE;
300    printf("Drawing Separation Plane\n");
301  }
302}
303
304
305int blendedBox(GtkWidget* nonInterest, void* widget)
306{
307  if(drawMode & DRAW_BV_BLENDED)
308  {
309    int temp = drawMode & DRAW_BV_BLENDED;
310    drawMode ^= temp;
311    printf("Removing OBB Polygons\n");
312  }
313  else
314  {
315    drawMode |= DRAW_BV_BLENDED;
316    printf("Drawing OBB Polygons\n");
317  }
318}
319
320
321int drawModels(GtkWidget* nonInterest, void* widget)
322{
323  drawModel = !drawModel;
324}
325
326
327int treeDepth(GtkWidget* nonInterest, void* widget)
328{
329  Option* option = (Option*) widget;
330  const char* name = option->getTitle();
331  char* value = option->save();
332
333  depth = atoi(value);
334  printf("Setting tree depth = %i\n", depth);
335  int temp = drawMode & DRAW_ALL;
336  drawMode ^= temp;
337  drawMode |= DRAW_SINGLE;
338
339  delete value;
340}
341
342
343void Framework::moduleInitGui(int argc, char** argv)
344{
345  Window* guiMainWindow = NULL;
346
347  initGUI(0, NULL);
348
349  guiMainWindow = new Window("Collision_detection");
350  {
351    Box* windowBox = new Box('v');
352    {
353      CheckButton* BoxPolygons = new CheckButton("Draw OBB Polygons");
354      BoxPolygons->connectSignal("clicked", (void*)BoxPolygons, boxPolygons);
355      windowBox->fill(BoxPolygons);
356
357
358      CheckButton* BlendedBox = new CheckButton("Draw OBB Blended");
359      BlendedBox->connectSignal("clicked", (void*)BlendedBox, blendedBox);
360      windowBox->fill(BlendedBox);
361
362
363      CheckButton* DrawModels = new CheckButton("Draw Models");
364      DrawModels->connectSignal("clicked", (void*)DrawModels, drawModels);
365      windowBox->fill(DrawModels);
366
367
368      CheckButton* SeperatingPlanes = new CheckButton("SeperatingPlanes");
369      SeperatingPlanes->connectSignal("clicked", (void*)SeperatingPlanes, seperatingPlanes);
370      windowBox->fill(SeperatingPlanes);
371
372
373      Slider* TreeDepth = new Slider("TreeDepth", 0, 5);
374      TreeDepth->connectSignal("value_changed", (void*)TreeDepth, treeDepth);
375      windowBox->fill(TreeDepth);
376    }
377    guiMainWindow->fill(windowBox);
378  }
379  Window::mainWindow->showall();
380  Window::mainWindow->setSize(300, 500);
381}
Note: See TracBrowser for help on using the repository browser.