Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/physics/src/subprojects/particles/framework.cc @ 4296

Last change on this file since 4296 was 4296, checked in by bensch, 19 years ago

orxonox/branches/physics: flush the work… i go now to the ETH

File size: 7.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
16#include "framework.h"
17
18#include "physics_engine.h"
19#include "particle_engine.h"
20
21#include "p_node.h"
22#include "null_parent.h"
23#include "state.h"
24#include "debug.h"
25#include "graphics_engine.h"
26#include "resource_manager.h"
27
28
29
30int verbose;
31
32bool mainLoop()
33{
34  while(true)
35    {
36      if (!keyHandler())
37        return false;
38
39      float dt = tick();
40      // Draw the scene
41      draw(dt);
42     
43    }
44
45}
46
47
48bool draw(float dt)
49{
50 
51
52  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
53  glLoadIdentity(); // Reset the view
54 
55  glMatrixMode(GL_PROJECTION);
56  glLoadIdentity();     
57  gluPerspective(45.0f,500/375,0.1f,dist * 5.0f);
58  gluLookAt (0, dist , dist, 0,0,0, up.x,up.y,up.z);
59 
60  ParticleEngine::getInstance()->draw(dt);
61 
62  SDL_GL_SwapBuffers(); // Swap the buffers
63
64}
65float tick()
66{
67  currFrame = SDL_GetTicks();
68  float dt = (float)(currFrame - lastFrame) /1000.0;
69  if (dt == 0)
70    dist += (zoomTo-dist);
71  else 
72    dist += (zoomTo-dist) *(float)dt;
73  rotatorP += rotatorV *(float)dt;
74
75  ParticleEngine::getInstance()->tick(dt);
76
77  lastFrame = currFrame;
78
79  NullParent::getInstance()->update(dt);
80  return dt;
81}
82
83
84bool keyHandler()
85{
86  // This is the main loop for the entire program and it will run until done==TRUE
87  {
88    // And poll for events
89    SDL_Event event;
90    while ( SDL_PollEvent(&event) ) {
91      switch (event.type) {
92      case SDL_MOUSEMOTION:
93        PRINTF(4)("Mouse motion about %d,%d Pixels to (%d,%d).\n", 
94                  event.motion.xrel, event.motion.yrel,
95                  event.motion.x, event.motion.y);
96        // TRACKBALL
97        if (mouse1Down)
98          {
99            int mX = event.button.x;
100            int mY = event.button.y;
101            int wH = GraphicsEngine::getInstance()->getResolutionY();
102            int wW = GraphicsEngine::getInstance()->getResolutionX();
103            Vector tmpV (mX, mY, sqrt ( (float) abs(wH * wH/4 - (wW/2-mX) * (wW/2-mX) - (wH/2-mY) * (wH/2-mY)) ));
104            //      PRINTF(0)("tmpV: %f, %f, %f\n", tmpV.x, tmpV.y, tmpV.z);
105            p2 = tmpV-M;
106            p2.y = -p2.y;
107            rotAxis = p1.cross(p2);
108            //  PRINTF(0)("rotAxis: %f, %f, %f\n", rotAxis.x, rotAxis.y, rotAxis.z);
109
110            // in case that there is no rotation-axis defined
111            if (rotAxis.x != 0 || rotAxis.y != 0 || rotAxis.z != 0)
112              {
113                rotAxis.normalize();
114                //              PRINTF(0)("rotAxis: %f, %f, %f\n", rotAxis.x, rotAxis.y, rotAxis.z, rotAngle);
115                               
116                rotAngle = angleRad (p1, p2);
117                rotQ = Quaternion (rotAngle, rotAxis);
118                rotQ = rotQ * rotQlast;
119                rotQ.matrix (matQ);
120                //      dir = rotQ.apply(dir);
121                //      dir.normalize();
122                //      PRINTF(0)("rotAxis: %f, %f, %f, %f\n", dir.x, dir.y, dir.z, rotAngle);
123              }
124            rotQlast = rotQ;
125            p1 = p2;
126
127          }
128        break;
129      case SDL_MOUSEBUTTONDOWN:
130        if (event.button.button == 4)
131          {
132            PRINTF(4)("MouseWheel up\n");
133            zoomTo *= .5;
134          }
135        else if (event.button.button == 5)
136          {
137            PRINTF(4)("MouseWheel down\n");
138            zoomTo *= 2.0;
139          }
140        else if (event.button.button == 1)
141          {
142            mouse1Down = true;
143            int mX = event.button.x;
144            int mY = event.button.y;
145            int wH = GraphicsEngine::getInstance()->getResolutionY();
146            int wW = GraphicsEngine::getInstance()->getResolutionX();
147            Vector tmpV (mX, mY, sqrt ( (float) abs(wH * wH/4 - (wW/2-mX) * (wW/2-mX) - (wH/2-mY) * (wH/2-mY)) ));
148            p1 = tmpV-M;
149            p1.y = -p1.y;
150
151          }
152        else
153          {
154            PRINTF(0)("MouseButton %d pressed at (%d,%d).\n",
155                   event.button.button, event.button.x, event.button.y);
156            rotatorV = ( (float)GraphicsEngine::getInstance()->getResolutionY()/2 -event.button.x) / (float)GraphicsEngine::getInstance()->getResolutionY() / 100.0;
157          }
158           
159        break;
160      case SDL_MOUSEBUTTONUP:
161        if (event.button.button == 4);
162        else if (event.button.button == 5);
163        else if (event.button.button == 1)
164          mouse1Down =false;
165        else 
166            {
167              PRINTF(4)("MouseButton %d released at (%d,%d).\n",
168                        event.button.button, event.button.x, event.button.y);
169            }
170        break;
171
172
173      case SDL_KEYDOWN:
174        switch (event.key.keysym.sym)
175          {
176          case SDLK_x:
177
178            break;
179          case SDLK_c:
180
181            break;
182          case SDLK_i:
183            ParticleEngine::getInstance()->debug();
184            break;
185          case SDLK_a:
186            zoomTo /=2;
187            break;
188          case SDLK_z:
189            zoomTo *=2;
190
191          }
192        break;
193           
194        // If a quit event was recieved
195      case SDL_QUIT:
196        // then we're done and we'll end this program
197        return false;
198        break;
199      default:
200        break;
201      }
202
203    }
204
205    // Get the state of the keyboard keys
206    keys = SDL_GetKeyState(NULL);
207
208    // and check if ESCAPE has been pressed. If so then quit
209    if(keys[SDLK_ESCAPE]) return false;
210  }
211  return true;
212}
213
214
215int main(int argc, char *argv[])
216{
217  verbose = 3;
218
219
220  // Create a new OpenGL window with the title "Cone3D Basecode" at
221  // 640x480x32, fullscreen and check for errors along the way
222  GraphicsEngine::getInstance();
223
224 
225
226  M = Vector(GraphicsEngine::getInstance()->getResolutionY()/2, GraphicsEngine::getInstance()->getResolutionX()/2, 0); 
227  rotAxis = Vector (0.0,1.0,0.0);
228  rotAngle = 0;
229
230  matQ[0][0] = matQ[1][1] = matQ[2][2] = matQ[3][3] = 1;
231  rotQ = Quaternion (rotAngle, rotAxis);
232  rotQlast = rotQ;
233  dir = Vector (0.0, 0.0, 1.0);
234  up = Vector (0.0, 1.0, 0.0);
235
236  glEnable(GL_LIGHTING);
237  glEnable(GL_DEPTH_TEST);
238
239  GLfloat whiteLight[] = {1.0, 1.0, 1.0,1.0};
240  GLfloat light0Position[] = {10.0, 10.0, 10.0, 0.0};
241  GLfloat light1Position[] = {-10.0, -7.0, -6.0, 0.0};
242  GLfloat lmodelAmbient[] = {.1, .1, .1, 1.0};
243
244  glEnable(GL_LIGHT0);
245  glLightfv(GL_LIGHT0, GL_POSITION, light0Position);
246  glLightfv(GL_LIGHT0, GL_DIFFUSE, whiteLight);
247  glLightfv(GL_LIGHT0, GL_SPECULAR, whiteLight);
248 
249  glEnable(GL_LIGHT1);
250  glLightfv(GL_LIGHT1, GL_POSITION, light1Position);
251  glLightfv(GL_LIGHT1, GL_DIFFUSE, whiteLight);
252  glLightfv(GL_LIGHT1, GL_SPECULAR, whiteLight);
253 
254
255  glEnable(GL_TEXTURE_2D);
256  rotatorP = .0;
257  rotatorV = .0;
258  dist = 5.0;
259  zoomTo = dist;
260  // Build the font from a TGA image font.tga in the data directory
261  // Hide the mouse cursor
262  SDL_ShowCursor(2);
263  mouse1Down = false;
264
265  ResourceManager::getInstance()->setDataDir(DATA_DIRECTORY);
266 
267    // Creating a Test Particle System
268  ParticleSystem* system = new ParticleSystem(100000, PARTICLE_SPRITE);
269  system->setLifeSpan(5);
270  system->setConserve(.8);
271  system->setRadius(4, 3, 1, 2);
272  system->setColor(.5,0,0,.5, 1,1,0,1, 0,0,0,0);
273
274  // Creating a Test Particle Emitter
275  ParticleEmitter* emitter = new ParticleEmitter(Vector(0 , 0, -1), 0, 100, 1);
276  emitter->setType(EMITTER_DOT  );
277  emitter->setSize(0);
278  emitter->setRelCoor(Vector(0,0,0));
279 
280  // Add the Flow from the Emitter into the System
281  ParticleEngine::getInstance()->addConnection(emitter, system);
282
283  PNode* camera = new PNode();
284
285  State::getInstance()->setCamera(camera, emitter);
286
287  camera->setAbsCoor(Vector(10, 10, 0));
288
289  mainLoop();
290   
291  // Kill the GL & SDL screens
292  delete GraphicsEngine::getInstance();
293  // And quit
294  return 0;
295}
Note: See TracBrowser for help on using the repository browser.