Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: prepearing for collision detection

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#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();
68  Environment* b = new Environment();
69  b->setRelCoor(0.0, 0.0, -10.0);
70
71  entityList->add(a);
72  entityList->add(b);
73
74  CDEngine::getInstance()->setEntityList(entityList);
75
76  LightManager* lightMan = LightManager::getInstance();
77  lightMan->setAmbientColor(.1,.1,.1);
78  lightMan->addLight();
79  lightMan->setPosition(30, 30, 30);
80  lightMan->addLight();
81  lightMan->setPosition(-30, -30, -30);
82
83  /* properties */
84  drawMode = DRAW_MODEL;
85  depth = 0;
86  dt = lastFrame = currentFrame = 0;
87
88
89  moduleHelp();
90}
91
92
93void Framework::moduleEventHandler(SDL_Event* event)
94{
95  switch (event->type)
96  {
97    case SDL_KEYDOWN:
98      switch (event->key.keysym.sym)
99      {
100        case SDLK_a:
101          drawMode |= DRAW_ALL;
102          break;
103        case SDLK_0:
104        {
105          printf("Setting tree depth = 0\n");
106          depth = 0;
107          int temp = drawMode & DRAW_ALL;
108          drawMode ^= temp;
109          drawMode |= DRAW_SINGLE;
110          break;
111        }
112        case SDLK_1:
113        {
114          printf("Setting tree depth = 1\n");
115          depth = 1;
116          int temp = drawMode & DRAW_ALL;
117          drawMode ^= temp;
118          drawMode |= DRAW_SINGLE;
119          break;
120        }
121        case SDLK_2:
122        {
123          printf("Setting tree depth = 2\n");
124          depth = 2;
125          int temp = drawMode & DRAW_ALL;
126          drawMode ^= temp;
127          drawMode |= DRAW_SINGLE;
128          break;
129        }
130        case SDLK_3:
131        {
132          printf("Setting tree depth = 3\n");
133          depth = 3;
134          int temp = drawMode & DRAW_ALL;
135          drawMode ^= temp;
136          drawMode |= DRAW_SINGLE;
137          break;
138        }
139        case SDLK_4:
140        {
141          printf("Setting tree depth = 4\n");
142          depth = 4;
143          int temp = drawMode & DRAW_ALL;
144          drawMode ^= temp;
145          drawMode |= DRAW_SINGLE;
146          break;
147        }
148        case SDLK_5:
149        {
150          printf("Setting tree depth = 5\n");
151          depth = 5;
152          int temp = drawMode & DRAW_ALL;
153          drawMode ^= temp;
154          drawMode |= DRAW_SINGLE;
155          break;
156        }
157        case SDLK_6:
158        {
159          printf("Setting tree depth = 6\n");
160          depth = 6;
161          int temp = drawMode & DRAW_ALL;
162          drawMode ^= temp;
163          drawMode |= DRAW_SINGLE;
164          break;
165        }
166        case SDLK_s:
167          if(drawMode & DRAW_SEPARATING_PLANE)
168          {
169            int temp = drawMode & DRAW_SEPARATING_PLANE;
170            drawMode ^= temp;
171            printf("Removing Separation Plane\n");
172          }
173          else
174          {
175            drawMode |= DRAW_SEPARATING_PLANE;
176            printf("Drawing Separation Plane\n");
177          }
178
179          break;
180        case SDLK_p:
181          if(drawMode & DRAW_BV_POLYGON)
182          {
183            int temp = drawMode & DRAW_BV_POLYGON;
184            drawMode ^= temp;
185            printf("Removing OBB Polygons\n");
186          }
187          else
188          {
189            drawMode |= DRAW_BV_POLYGON;
190            printf("Drawing OBB Polygons\n");
191          }
192          break;
193
194      }
195  }
196}
197
198
199void Framework::moduleTick(float dt)
200{
201
202  CDEngine::getInstance()->checkCollisions();
203
204  currentFrame = SDL_GetTicks();
205  dt = currentFrame - lastFrame;
206
207  if( dt > 10)
208  {
209    float fps = 1000/dt;
210
211          // temporary, only for showing how fast the text-engine is
212    char tmpChar[20];
213    sprintf(tmpChar, "fps: %4.0f", fps);
214  }
215  else
216  {
217          /* the frame-rate is limited to 100 frames per second, all other things are for nothing.
218          */
219    SDL_Delay((unsigned int)(10-dt));
220    dt = 10;
221  }
222
223
224  tIterator<WorldEntity>* iterator = entityList->getIterator(); /* get the iterator - JAVA style */
225  WorldEntity* entity = iterator->nextElement();   /* this returns the FIRST element */
226  while( entity != NULL)                        /* nextElement() will return NULL at the end */
227  {
228    entity->tick(0.1);
229    entity = iterator->nextElement();       /* give back the next element or NULL if last */
230  }
231  delete iterator;
232
233  lastFrame = currentFrame;
234}
235
236
237void Framework::moduleDraw() const
238{
239  //CDEngine::getInstance()->drawBV(depth, drawMode);
240  LightManager::getInstance()->draw();
241
242  tIterator<WorldEntity>* iterator = entityList->getIterator(); /* get the iterator - JAVA style */
243  WorldEntity* entity = iterator->nextElement();   /* this returns the FIRST element */
244  while( entity != NULL)                        /* nextElement() will return NULL at the end */
245  {
246    if( drawModel)
247    entity->draw();
248    entity->drawBVTree(depth, drawMode);
249    entity = iterator->nextElement();       /* give back the next element or NULL if last */
250  }
251  delete iterator;
252  //model->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.