Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/physics: one is seeing particles

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