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