Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: the collision detection engine works again and is ready for the presentation

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