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