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