[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 | |
---|
[3479] | 18 | #include "vector.h" |
---|
[3427] | 19 | |
---|
[3657] | 20 | #include "primitive_model.h" |
---|
[4272] | 21 | #include "debug.h" |
---|
| 22 | #include "graphics_engine.h" |
---|
[3657] | 23 | |
---|
| 24 | |
---|
[4272] | 25 | |
---|
[3398] | 26 | int verbose; |
---|
[2748] | 27 | void DrawGLScene() |
---|
| 28 | { |
---|
[2940] | 29 | currFrame = SDL_GetTicks(); |
---|
| 30 | dt = currFrame - lastFrame; |
---|
| 31 | if (dt == 0) |
---|
| 32 | dist += (zoomTo-dist)/500; |
---|
| 33 | else |
---|
| 34 | dist += (zoomTo-dist)/500 *(float)dt; |
---|
[2939] | 35 | |
---|
[2940] | 36 | rotatorP += rotatorV *(float)dt; |
---|
[2759] | 37 | |
---|
[2952] | 38 | |
---|
[2748] | 39 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
---|
| 40 | glLoadIdentity(); // Reset the view |
---|
| 41 | |
---|
[2794] | 42 | glMatrixMode(GL_PROJECTION); |
---|
[2759] | 43 | glLoadIdentity(); |
---|
[2933] | 44 | gluPerspective(45.0f,500/375,0.1f,dist * 5.0f); |
---|
[2963] | 45 | gluLookAt (0, dist , dist, 0,0,0, up.x,up.y,up.z); |
---|
[2794] | 46 | |
---|
[2963] | 47 | glMatrixMode(GL_MODELVIEW); |
---|
| 48 | glPushMatrix(); |
---|
| 49 | // glRotatef (180, dir.x, dir.y, dir.z); |
---|
| 50 | glMultMatrixf (*matQ); |
---|
[3211] | 51 | if (obj) |
---|
| 52 | obj->draw(); |
---|
[2748] | 53 | |
---|
[2963] | 54 | glPopMatrix(); |
---|
| 55 | |
---|
[2748] | 56 | SDL_GL_SwapBuffers(); // Swap the buffers |
---|
[2940] | 57 | lastFrame = currFrame; |
---|
[2748] | 58 | } |
---|
| 59 | |
---|
| 60 | |
---|
| 61 | int main(int argc, char *argv[]) |
---|
| 62 | { |
---|
[3910] | 63 | verbose = 3; |
---|
[3201] | 64 | |
---|
[2748] | 65 | Uint8* keys; // This variable will be used in the keyboard routine |
---|
[4272] | 66 | int done = false; // We aren't done yet, are we? |
---|
[2748] | 67 | |
---|
| 68 | // Create a new OpenGL window with the title "Cone3D Basecode" at |
---|
| 69 | // 640x480x32, fullscreen and check for errors along the way |
---|
[4272] | 70 | GraphicsEngine::getInstance(); |
---|
| 71 | |
---|
[2846] | 72 | if (argc>=3) |
---|
[3396] | 73 | obj = new OBJModel (argv[1], atof(argv[2])); |
---|
[2846] | 74 | else if (argc>=2) |
---|
[3396] | 75 | obj = new OBJModel(argv[1]); |
---|
[2848] | 76 | else |
---|
[4112] | 77 | obj = new PrimitiveModel(CYLINDER); |
---|
[3398] | 78 | |
---|
[4272] | 79 | M = Vector(GraphicsEngine::getInstance()->getResolutionY()/2, GraphicsEngine::getInstance()->getResolutionX()/2, 0); |
---|
[2952] | 80 | rotAxis = Vector (0.0,1.0,0.0); |
---|
| 81 | rotAngle = 0; |
---|
[2963] | 82 | |
---|
| 83 | matQ[0][0] = matQ[1][1] = matQ[2][2] = matQ[3][3] = 1; |
---|
[2952] | 84 | rotQ = Quaternion (rotAngle, rotAxis); |
---|
[2963] | 85 | rotQlast = rotQ; |
---|
[2952] | 86 | dir = Vector (0.0, 0.0, 1.0); |
---|
[2963] | 87 | up = Vector (0.0, 1.0, 0.0); |
---|
[2952] | 88 | |
---|
[2931] | 89 | glEnable(GL_LIGHTING); |
---|
| 90 | glEnable(GL_DEPTH_TEST); |
---|
| 91 | |
---|
| 92 | GLfloat whiteLight[] = {1.0, 1.0, 1.0,1.0}; |
---|
| 93 | GLfloat light0Position[] = {10.0, 10.0, 10.0, 0.0}; |
---|
| 94 | GLfloat light1Position[] = {-10.0, -7.0, -6.0, 0.0}; |
---|
[2780] | 95 | GLfloat lmodelAmbient[] = {.1, .1, .1, 1.0}; |
---|
[2931] | 96 | |
---|
| 97 | glEnable(GL_LIGHT0); |
---|
| 98 | glLightfv(GL_LIGHT0, GL_POSITION, light0Position); |
---|
[2780] | 99 | glLightfv(GL_LIGHT0, GL_DIFFUSE, whiteLight); |
---|
| 100 | glLightfv(GL_LIGHT0, GL_SPECULAR, whiteLight); |
---|
[2931] | 101 | |
---|
| 102 | glEnable(GL_LIGHT1); |
---|
| 103 | glLightfv(GL_LIGHT1, GL_POSITION, light1Position); |
---|
| 104 | glLightfv(GL_LIGHT1, GL_DIFFUSE, whiteLight); |
---|
| 105 | glLightfv(GL_LIGHT1, GL_SPECULAR, whiteLight); |
---|
| 106 | |
---|
[3070] | 107 | |
---|
| 108 | glEnable(GL_TEXTURE_2D); |
---|
[2931] | 109 | rotatorP = .0; |
---|
| 110 | rotatorV = .0; |
---|
[2933] | 111 | dist = 5.0; |
---|
[2938] | 112 | zoomTo = dist; |
---|
[2748] | 113 | // Build the font from a TGA image font.tga in the data directory |
---|
| 114 | // Hide the mouse cursor |
---|
[2931] | 115 | SDL_ShowCursor(2); |
---|
[2952] | 116 | mouse1Down = false; |
---|
[2748] | 117 | |
---|
| 118 | // This is the main loop for the entire program and it will run until done==TRUE |
---|
| 119 | while(!done) |
---|
| 120 | { |
---|
| 121 | // Draw the scene |
---|
| 122 | DrawGLScene(); |
---|
| 123 | // And poll for events |
---|
| 124 | SDL_Event event; |
---|
| 125 | while ( SDL_PollEvent(&event) ) { |
---|
| 126 | switch (event.type) { |
---|
[2931] | 127 | case SDL_MOUSEMOTION: |
---|
[3656] | 128 | PRINTF(4)("Mouse motion about %d,%d Pixels to (%d,%d).\n", |
---|
[3206] | 129 | event.motion.xrel, event.motion.yrel, |
---|
| 130 | event.motion.x, event.motion.y); |
---|
[2952] | 131 | // TRACKBALL |
---|
| 132 | if (mouse1Down) |
---|
| 133 | { |
---|
[2963] | 134 | int mX = event.button.x; |
---|
| 135 | int mY = event.button.y; |
---|
[4272] | 136 | int wH = GraphicsEngine::getInstance()->getResolutionY(); |
---|
| 137 | int wW = GraphicsEngine::getInstance()->getResolutionX(); |
---|
[2963] | 138 | Vector tmpV (mX, mY, sqrt ( (float) abs(wH * wH/4 - (wW/2-mX) * (wW/2-mX) - (wH/2-mY) * (wH/2-mY)) )); |
---|
[3208] | 139 | // PRINTF(0)("tmpV: %f, %f, %f\n", tmpV.x, tmpV.y, tmpV.z); |
---|
[2952] | 140 | p2 = tmpV-M; |
---|
[2966] | 141 | p2.y = -p2.y; |
---|
[2952] | 142 | rotAxis = p1.cross(p2); |
---|
[3208] | 143 | // PRINTF(0)("rotAxis: %f, %f, %f\n", rotAxis.x, rotAxis.y, rotAxis.z); |
---|
[2952] | 144 | |
---|
[2963] | 145 | // in case that there is no rotation-axis defined |
---|
| 146 | if (rotAxis.x != 0 || rotAxis.y != 0 || rotAxis.z != 0) |
---|
| 147 | { |
---|
| 148 | rotAxis.normalize(); |
---|
[3208] | 149 | // PRINTF(0)("rotAxis: %f, %f, %f\n", rotAxis.x, rotAxis.y, rotAxis.z, rotAngle); |
---|
[2963] | 150 | |
---|
[3237] | 151 | rotAngle = angleRad (p1, p2); |
---|
[2963] | 152 | rotQ = Quaternion (rotAngle, rotAxis); |
---|
| 153 | rotQ = rotQ * rotQlast; |
---|
| 154 | rotQ.matrix (matQ); |
---|
| 155 | // dir = rotQ.apply(dir); |
---|
| 156 | // dir.normalize(); |
---|
[3208] | 157 | // PRINTF(0)("rotAxis: %f, %f, %f, %f\n", dir.x, dir.y, dir.z, rotAngle); |
---|
[2963] | 158 | } |
---|
| 159 | rotQlast = rotQ; |
---|
[2952] | 160 | p1 = p2; |
---|
[2963] | 161 | |
---|
[2952] | 162 | } |
---|
[2931] | 163 | break; |
---|
| 164 | case SDL_MOUSEBUTTONDOWN: |
---|
[2932] | 165 | if (event.button.button == 4) |
---|
[2933] | 166 | { |
---|
[3656] | 167 | PRINTF(4)("MouseWheel up\n"); |
---|
[2938] | 168 | zoomTo *= .5; |
---|
[2933] | 169 | } |
---|
[2932] | 170 | else if (event.button.button == 5) |
---|
[2933] | 171 | { |
---|
[3656] | 172 | PRINTF(4)("MouseWheel down\n"); |
---|
[2940] | 173 | zoomTo *= 2.0; |
---|
[2933] | 174 | } |
---|
[2952] | 175 | else if (event.button.button == 1) |
---|
| 176 | { |
---|
| 177 | mouse1Down = true; |
---|
[2963] | 178 | int mX = event.button.x; |
---|
| 179 | int mY = event.button.y; |
---|
[4272] | 180 | int wH = GraphicsEngine::getInstance()->getResolutionY(); |
---|
| 181 | int wW = GraphicsEngine::getInstance()->getResolutionX(); |
---|
[2963] | 182 | Vector tmpV (mX, mY, sqrt ( (float) abs(wH * wH/4 - (wW/2-mX) * (wW/2-mX) - (wH/2-mY) * (wH/2-mY)) )); |
---|
[2952] | 183 | p1 = tmpV-M; |
---|
[2966] | 184 | p1.y = -p1.y; |
---|
[2952] | 185 | |
---|
| 186 | } |
---|
[2932] | 187 | else |
---|
| 188 | { |
---|
[3208] | 189 | PRINTF(0)("MouseButton %d pressed at (%d,%d).\n", |
---|
[2932] | 190 | event.button.button, event.button.x, event.button.y); |
---|
[4272] | 191 | rotatorV = ( (float)GraphicsEngine::getInstance()->getResolutionY()/2 -event.button.x) / (float)GraphicsEngine::getInstance()->getResolutionY() / 100.0; |
---|
[2932] | 192 | } |
---|
| 193 | |
---|
[2931] | 194 | break; |
---|
[2952] | 195 | case SDL_MOUSEBUTTONUP: |
---|
| 196 | if (event.button.button == 4); |
---|
| 197 | else if (event.button.button == 5); |
---|
| 198 | else if (event.button.button == 1) |
---|
| 199 | mouse1Down =false; |
---|
| 200 | else |
---|
| 201 | { |
---|
[3656] | 202 | PRINTF(4)("MouseButton %d released at (%d,%d).\n", |
---|
[3208] | 203 | event.button.button, event.button.x, event.button.y); |
---|
[2952] | 204 | } |
---|
[2931] | 205 | break; |
---|
[3211] | 206 | |
---|
| 207 | |
---|
| 208 | case SDL_KEYDOWN: |
---|
| 209 | switch (event.key.keysym.sym) |
---|
| 210 | { |
---|
| 211 | case SDLK_x: |
---|
| 212 | delete obj; |
---|
| 213 | obj = NULL; |
---|
| 214 | break; |
---|
| 215 | case SDLK_c: |
---|
| 216 | if (!obj) |
---|
[3396] | 217 | obj = new OBJModel(argv[1]); |
---|
[3211] | 218 | break; |
---|
[3418] | 219 | case SDLK_a: |
---|
| 220 | zoomTo /=2; |
---|
| 221 | break; |
---|
| 222 | case SDLK_z: |
---|
| 223 | zoomTo *=2; |
---|
| 224 | |
---|
[3211] | 225 | } |
---|
| 226 | break; |
---|
[2952] | 227 | |
---|
[2748] | 228 | // If a quit event was recieved |
---|
[2863] | 229 | case SDL_QUIT: |
---|
| 230 | // then we're done and we'll end this program |
---|
[4272] | 231 | done=true; |
---|
[2748] | 232 | break; |
---|
[2863] | 233 | default: |
---|
[2748] | 234 | break; |
---|
| 235 | } |
---|
[2952] | 236 | |
---|
| 237 | |
---|
[2748] | 238 | } |
---|
| 239 | |
---|
| 240 | // Get the state of the keyboard keys |
---|
| 241 | keys = SDL_GetKeyState(NULL); |
---|
| 242 | |
---|
| 243 | // and check if ESCAPE has been pressed. If so then quit |
---|
[4272] | 244 | if(keys[SDLK_ESCAPE]) done=true; |
---|
[2748] | 245 | } |
---|
| 246 | |
---|
| 247 | // Kill the GL & SDL screens |
---|
[3211] | 248 | if (obj) |
---|
| 249 | delete obj; |
---|
[4272] | 250 | delete GraphicsEngine::getInstance(); |
---|
[2748] | 251 | // And quit |
---|
| 252 | return 0; |
---|
| 253 | } |
---|