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