Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: added the model draw functionality to the framework

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