Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/physics: framework is a class now

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