[2931] | 1 | #include "framework.h" |
---|
[2748] | 2 | |
---|
[2933] | 3 | int verbose = 1; |
---|
[2748] | 4 | |
---|
| 5 | void DrawGLScene() |
---|
| 6 | { |
---|
[2931] | 7 | rotatorP += rotatorV; |
---|
[2759] | 8 | |
---|
[2748] | 9 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
---|
| 10 | glLoadIdentity(); // Reset the view |
---|
| 11 | |
---|
[2794] | 12 | glMatrixMode(GL_PROJECTION); |
---|
[2759] | 13 | glLoadIdentity(); |
---|
[2933] | 14 | gluPerspective(45.0f,500/375,0.1f,dist * 5.0f); |
---|
| 15 | gluLookAt (dist*sin(rotatorP),dist ,dist*cos(rotatorP), 0,0,0, 0,1,0); |
---|
[2794] | 16 | |
---|
[2748] | 17 | obj->draw(); |
---|
| 18 | |
---|
| 19 | SDL_GL_SwapBuffers(); // Swap the buffers |
---|
| 20 | } |
---|
| 21 | |
---|
| 22 | |
---|
| 23 | int main(int argc, char *argv[]) |
---|
| 24 | { |
---|
| 25 | Uint8* keys; // This variable will be used in the keyboard routine |
---|
| 26 | int done=FALSE; // We aren't done yet, are we? |
---|
| 27 | |
---|
| 28 | // Create a new OpenGL window with the title "Cone3D Basecode" at |
---|
| 29 | // 640x480x32, fullscreen and check for errors along the way |
---|
| 30 | if(wHandler.CreateGLWindow("Whandler Basecode", 500, 375, 32, FALSE) == FALSE) |
---|
| 31 | { |
---|
| 32 | // If an error is found, display a message, kill the GL and SDL screens (if they were created) and exit |
---|
| 33 | printf("Could not initalize OpenGL :(\n\n"); |
---|
| 34 | wHandler.KillGLWindow(); |
---|
| 35 | return 0; |
---|
| 36 | } |
---|
[2846] | 37 | if (argc>=3) |
---|
[2837] | 38 | obj = new Object (argv[1], atof(argv[2])); |
---|
[2846] | 39 | else if (argc>=2) |
---|
[2837] | 40 | obj = new Object(argv[1]); |
---|
[2848] | 41 | else |
---|
| 42 | obj = new Object(); |
---|
[2850] | 43 | |
---|
[2931] | 44 | glEnable(GL_LIGHTING); |
---|
| 45 | glEnable(GL_DEPTH_TEST); |
---|
| 46 | |
---|
| 47 | GLfloat whiteLight[] = {1.0, 1.0, 1.0,1.0}; |
---|
| 48 | GLfloat light0Position[] = {10.0, 10.0, 10.0, 0.0}; |
---|
| 49 | GLfloat light1Position[] = {-10.0, -7.0, -6.0, 0.0}; |
---|
[2780] | 50 | GLfloat lmodelAmbient[] = {.1, .1, .1, 1.0}; |
---|
[2931] | 51 | |
---|
| 52 | glEnable(GL_LIGHT0); |
---|
| 53 | glLightfv(GL_LIGHT0, GL_POSITION, light0Position); |
---|
[2780] | 54 | glLightfv(GL_LIGHT0, GL_DIFFUSE, whiteLight); |
---|
| 55 | glLightfv(GL_LIGHT0, GL_SPECULAR, whiteLight); |
---|
[2931] | 56 | |
---|
| 57 | glEnable(GL_LIGHT1); |
---|
| 58 | glLightfv(GL_LIGHT1, GL_POSITION, light1Position); |
---|
| 59 | glLightfv(GL_LIGHT1, GL_DIFFUSE, whiteLight); |
---|
| 60 | glLightfv(GL_LIGHT1, GL_SPECULAR, whiteLight); |
---|
| 61 | |
---|
| 62 | rotatorP = .0; |
---|
| 63 | rotatorV = .0; |
---|
[2933] | 64 | dist = 5.0; |
---|
[2748] | 65 | // Build the font from a TGA image font.tga in the data directory |
---|
| 66 | // Hide the mouse cursor |
---|
[2931] | 67 | SDL_ShowCursor(2); |
---|
[2748] | 68 | |
---|
| 69 | // This is the main loop for the entire program and it will run until done==TRUE |
---|
| 70 | while(!done) |
---|
| 71 | { |
---|
| 72 | // Draw the scene |
---|
| 73 | DrawGLScene(); |
---|
| 74 | // And poll for events |
---|
| 75 | SDL_Event event; |
---|
| 76 | while ( SDL_PollEvent(&event) ) { |
---|
| 77 | switch (event.type) { |
---|
[2931] | 78 | case SDL_MOUSEMOTION: |
---|
| 79 | if (verbose >=3) |
---|
| 80 | printf("Mouse motion about %d,%d Pixels to (%d,%d).\n", |
---|
| 81 | event.motion.xrel, event.motion.yrel, |
---|
| 82 | event.motion.x, event.motion.y); |
---|
| 83 | break; |
---|
| 84 | case SDL_MOUSEBUTTONDOWN: |
---|
[2932] | 85 | if (event.button.button == 4) |
---|
[2933] | 86 | { |
---|
| 87 | printf("MouseWheel up\n"); |
---|
| 88 | dist *= .5; |
---|
| 89 | } |
---|
[2932] | 90 | else if (event.button.button == 5) |
---|
[2933] | 91 | { |
---|
| 92 | printf("MouseWheel down\n"); |
---|
| 93 | dist *= 2; |
---|
| 94 | } |
---|
[2932] | 95 | else |
---|
| 96 | { |
---|
| 97 | printf("MouseButton %d pressed at (%d,%d).\n", |
---|
| 98 | event.button.button, event.button.x, event.button.y); |
---|
| 99 | rotatorV = ( 250-event.button.x) / 500.0 /100; |
---|
| 100 | } |
---|
| 101 | |
---|
[2931] | 102 | break; |
---|
[2932] | 103 | /* case SDL_MOUSEBUTTONUP: |
---|
[2931] | 104 | printf("MouseButton %d released at (%d,%d).\n", |
---|
| 105 | event.button.button, event.button.x, event.button.y); |
---|
| 106 | break; |
---|
[2932] | 107 | */ |
---|
[2748] | 108 | // If a quit event was recieved |
---|
[2863] | 109 | case SDL_QUIT: |
---|
| 110 | // then we're done and we'll end this program |
---|
[2748] | 111 | done=TRUE; |
---|
| 112 | break; |
---|
[2863] | 113 | default: |
---|
[2748] | 114 | break; |
---|
| 115 | } |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | // Get the state of the keyboard keys |
---|
| 119 | keys = SDL_GetKeyState(NULL); |
---|
| 120 | |
---|
| 121 | // and check if ESCAPE has been pressed. If so then quit |
---|
| 122 | if(keys[SDLK_ESCAPE]) done=TRUE; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | // Kill the GL & SDL screens |
---|
[2848] | 126 | delete obj; |
---|
[2748] | 127 | wHandler.KillGLWindow(); |
---|
| 128 | // And quit |
---|
| 129 | return 0; |
---|
| 130 | } |
---|