| 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 | void DrawGLScene() |
|---|
| 19 | { |
|---|
| 20 | currFrame = SDL_GetTicks(); |
|---|
| 21 | dt = currFrame - lastFrame; |
|---|
| 22 | if (dt == 0) |
|---|
| 23 | dist += (zoomTo-dist)/500; |
|---|
| 24 | else |
|---|
| 25 | dist += (zoomTo-dist)/500 *(float)dt; |
|---|
| 26 | |
|---|
| 27 | rotatorP += rotatorV *(float)dt; |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
|---|
| 31 | glLoadIdentity(); // Reset the view |
|---|
| 32 | |
|---|
| 33 | glMatrixMode(GL_PROJECTION); |
|---|
| 34 | glLoadIdentity(); |
|---|
| 35 | gluPerspective(45.0f,500/375,0.1f,dist * 5.0f); |
|---|
| 36 | gluLookAt (0, dist , dist, 0,0,0, up.x,up.y,up.z); |
|---|
| 37 | |
|---|
| 38 | glMatrixMode(GL_MODELVIEW); |
|---|
| 39 | glPushMatrix(); |
|---|
| 40 | // glRotatef (180, dir.x, dir.y, dir.z); |
|---|
| 41 | glMultMatrixf (*matQ); |
|---|
| 42 | if (obj) |
|---|
| 43 | obj->draw(); |
|---|
| 44 | |
|---|
| 45 | glPopMatrix(); |
|---|
| 46 | |
|---|
| 47 | SDL_GL_SwapBuffers(); // Swap the buffers |
|---|
| 48 | lastFrame = currFrame; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | int main(int argc, char *argv[]) |
|---|
| 53 | { |
|---|
| 54 | verbose = 2; |
|---|
| 55 | |
|---|
| 56 | PRINTF(2)("This is The big verbose-Test %i, %s\n", 1, "cool"); |
|---|
| 57 | |
|---|
| 58 | Uint8* keys; // This variable will be used in the keyboard routine |
|---|
| 59 | int done=FALSE; // We aren't done yet, are we? |
|---|
| 60 | |
|---|
| 61 | // Create a new OpenGL window with the title "Cone3D Basecode" at |
|---|
| 62 | // 640x480x32, fullscreen and check for errors along the way |
|---|
| 63 | if(wHandler.CreateGLWindow("Whandler Basecode", 800, 600, 32, FALSE) == FALSE) |
|---|
| 64 | { |
|---|
| 65 | // If an error is found, display a message, kill the GL and SDL screens (if they were created) and exit |
|---|
| 66 | PRINTF(1)("Could not initalize OpenGL :(\n\n"); |
|---|
| 67 | wHandler.KillGLWindow(); |
|---|
| 68 | return 0; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | PRINTF(2)("screensize: %i, %i\n", wHandler.screen->w, wHandler.screen->h); |
|---|
| 72 | if (argc>=3) |
|---|
| 73 | obj = new Model (argv[1], atof(argv[2])); |
|---|
| 74 | else if (argc>=2) |
|---|
| 75 | obj = new Model(argv[1]); |
|---|
| 76 | else |
|---|
| 77 | obj = new Model(); |
|---|
| 78 | |
|---|
| 79 | M = Vector(wHandler.screen->w/2, wHandler.screen->h/2, 0); |
|---|
| 80 | rotAxis = Vector (0.0,1.0,0.0); |
|---|
| 81 | rotAngle = 0; |
|---|
| 82 | |
|---|
| 83 | matQ[0][0] = matQ[1][1] = matQ[2][2] = matQ[3][3] = 1; |
|---|
| 84 | rotQ = Quaternion (rotAngle, rotAxis); |
|---|
| 85 | rotQlast = rotQ; |
|---|
| 86 | dir = Vector (0.0, 0.0, 1.0); |
|---|
| 87 | up = Vector (0.0, 1.0, 0.0); |
|---|
| 88 | |
|---|
| 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}; |
|---|
| 95 | GLfloat lmodelAmbient[] = {.1, .1, .1, 1.0}; |
|---|
| 96 | |
|---|
| 97 | glEnable(GL_LIGHT0); |
|---|
| 98 | glLightfv(GL_LIGHT0, GL_POSITION, light0Position); |
|---|
| 99 | glLightfv(GL_LIGHT0, GL_DIFFUSE, whiteLight); |
|---|
| 100 | glLightfv(GL_LIGHT0, GL_SPECULAR, whiteLight); |
|---|
| 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 | |
|---|
| 107 | |
|---|
| 108 | glEnable(GL_TEXTURE_2D); |
|---|
| 109 | rotatorP = .0; |
|---|
| 110 | rotatorV = .0; |
|---|
| 111 | dist = 5.0; |
|---|
| 112 | zoomTo = dist; |
|---|
| 113 | // Build the font from a TGA image font.tga in the data directory |
|---|
| 114 | // Hide the mouse cursor |
|---|
| 115 | SDL_ShowCursor(2); |
|---|
| 116 | mouse1Down = false; |
|---|
| 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) { |
|---|
| 127 | case SDL_MOUSEMOTION: |
|---|
| 128 | PRINTF(3)("Mouse motion about %d,%d Pixels to (%d,%d).\n", |
|---|
| 129 | event.motion.xrel, event.motion.yrel, |
|---|
| 130 | event.motion.x, event.motion.y); |
|---|
| 131 | // TRACKBALL |
|---|
| 132 | if (mouse1Down) |
|---|
| 133 | { |
|---|
| 134 | int mX = event.button.x; |
|---|
| 135 | int mY = event.button.y; |
|---|
| 136 | int wH = wHandler.screen->h; |
|---|
| 137 | int wW = wHandler.screen->w; |
|---|
| 138 | Vector tmpV (mX, mY, sqrt ( (float) abs(wH * wH/4 - (wW/2-mX) * (wW/2-mX) - (wH/2-mY) * (wH/2-mY)) )); |
|---|
| 139 | // PRINTF(0)("tmpV: %f, %f, %f\n", tmpV.x, tmpV.y, tmpV.z); |
|---|
| 140 | p2 = tmpV-M; |
|---|
| 141 | p2.y = -p2.y; |
|---|
| 142 | rotAxis = p1.cross(p2); |
|---|
| 143 | // PRINTF(0)("rotAxis: %f, %f, %f\n", rotAxis.x, rotAxis.y, rotAxis.z); |
|---|
| 144 | |
|---|
| 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(); |
|---|
| 149 | // PRINTF(0)("rotAxis: %f, %f, %f\n", rotAxis.x, rotAxis.y, rotAxis.z, rotAngle); |
|---|
| 150 | |
|---|
| 151 | rotAngle = angleRad (p1, p2); |
|---|
| 152 | rotQ = Quaternion (rotAngle, rotAxis); |
|---|
| 153 | rotQ = rotQ * rotQlast; |
|---|
| 154 | rotQ.matrix (matQ); |
|---|
| 155 | // dir = rotQ.apply(dir); |
|---|
| 156 | // dir.normalize(); |
|---|
| 157 | // PRINTF(0)("rotAxis: %f, %f, %f, %f\n", dir.x, dir.y, dir.z, rotAngle); |
|---|
| 158 | } |
|---|
| 159 | rotQlast = rotQ; |
|---|
| 160 | p1 = p2; |
|---|
| 161 | |
|---|
| 162 | } |
|---|
| 163 | break; |
|---|
| 164 | case SDL_MOUSEBUTTONDOWN: |
|---|
| 165 | if (event.button.button == 4) |
|---|
| 166 | { |
|---|
| 167 | PRINTF(0)("MouseWheel up\n"); |
|---|
| 168 | zoomTo *= .5; |
|---|
| 169 | } |
|---|
| 170 | else if (event.button.button == 5) |
|---|
| 171 | { |
|---|
| 172 | PRINTF(2)("MouseWheel down\n"); |
|---|
| 173 | zoomTo *= 2.0; |
|---|
| 174 | } |
|---|
| 175 | else if (event.button.button == 1) |
|---|
| 176 | { |
|---|
| 177 | mouse1Down = true; |
|---|
| 178 | int mX = event.button.x; |
|---|
| 179 | int mY = event.button.y; |
|---|
| 180 | int wH = wHandler.screen->h; |
|---|
| 181 | int wW = wHandler.screen->w; |
|---|
| 182 | Vector tmpV (mX, mY, sqrt ( (float) abs(wH * wH/4 - (wW/2-mX) * (wW/2-mX) - (wH/2-mY) * (wH/2-mY)) )); |
|---|
| 183 | p1 = tmpV-M; |
|---|
| 184 | p1.y = -p1.y; |
|---|
| 185 | |
|---|
| 186 | } |
|---|
| 187 | else |
|---|
| 188 | { |
|---|
| 189 | PRINTF(0)("MouseButton %d pressed at (%d,%d).\n", |
|---|
| 190 | event.button.button, event.button.x, event.button.y); |
|---|
| 191 | rotatorV = ( (float)wHandler.screen->w/2 -event.button.x) / (float)wHandler.screen->w / 100.0; |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | break; |
|---|
| 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 | { |
|---|
| 202 | PRINTF(2)("MouseButton %d released at (%d,%d).\n", |
|---|
| 203 | event.button.button, event.button.x, event.button.y); |
|---|
| 204 | } |
|---|
| 205 | break; |
|---|
| 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) |
|---|
| 217 | obj = new Model(argv[1]); |
|---|
| 218 | break; |
|---|
| 219 | } |
|---|
| 220 | break; |
|---|
| 221 | |
|---|
| 222 | // If a quit event was recieved |
|---|
| 223 | case SDL_QUIT: |
|---|
| 224 | // then we're done and we'll end this program |
|---|
| 225 | done=TRUE; |
|---|
| 226 | break; |
|---|
| 227 | default: |
|---|
| 228 | break; |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | // Get the state of the keyboard keys |
|---|
| 235 | keys = SDL_GetKeyState(NULL); |
|---|
| 236 | |
|---|
| 237 | // and check if ESCAPE has been pressed. If so then quit |
|---|
| 238 | if(keys[SDLK_ESCAPE]) done=TRUE; |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | // Kill the GL & SDL screens |
|---|
| 242 | if (obj) |
|---|
| 243 | delete obj; |
|---|
| 244 | wHandler.KillGLWindow(); |
|---|
| 245 | // And quit |
|---|
| 246 | return 0; |
|---|
| 247 | } |
|---|