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