| [3381] | 1 | /* | 
|---|
 | 2 |  *  legotown.cpp | 
|---|
 | 3 |  *  legotown3d | 
|---|
 | 4 |  * | 
|---|
 | 5 |  *  Created by Nico Bernold on 08.01.05. | 
|---|
 | 6 |  *  Copyright 2005 __MyCompanyName__. All rights reserved. | 
|---|
 | 7 |  * | 
|---|
 | 8 |  */ | 
|---|
 | 9 |  | 
|---|
 | 10 | #include "heightMapViewer.h" | 
|---|
 | 11 |  | 
|---|
 | 12 | // constructor | 
|---|
 | 13 | HeightMapViewer::HeightMapViewer() | 
|---|
 | 14 | { | 
|---|
 | 15 |         cout << "HeightMapViewer::HeightMapViewer()" << endl; | 
|---|
 | 16 |          | 
|---|
 | 17 |         cameraPos = Vector(0,0,40); | 
|---|
 | 18 |         sightDirection = Vector(0,0,-1); | 
|---|
 | 19 |  | 
|---|
 | 20 |         lookAt = cameraPos + sightDirection; | 
|---|
 | 21 |  | 
|---|
 | 22 |         upVector = Vector(0,1,0); | 
|---|
 | 23 |  | 
|---|
 | 24 |         wireframe = false; | 
|---|
 | 25 |         mousedown = false; | 
|---|
 | 26 |          | 
|---|
 | 27 | } | 
|---|
 | 28 |  | 
|---|
 | 29 | // destructor | 
|---|
 | 30 | HeightMapViewer::~HeightMapViewer() | 
|---|
 | 31 | { | 
|---|
 | 32 |         cout << "HeightMapViewer::~HeightMapViewer()" << endl; | 
|---|
 | 33 | } | 
|---|
 | 34 |  | 
|---|
 | 35 | // main loop | 
|---|
 | 36 | bool HeightMapViewer::init(char* fileName) | 
|---|
 | 37 | { | 
|---|
 | 38 |         cout << "HeightMapViewer init()" << endl; | 
|---|
 | 39 |          | 
|---|
 | 40 |         if (windowhandler.createOpenGLWindow(WIDTH,HEIGHT,FULLSCREEN) == false) | 
|---|
 | 41 |         { | 
|---|
 | 42 |                 cout << "could not create OpenGL-Window." << endl; | 
|---|
 | 43 |                 return false; | 
|---|
 | 44 |         } | 
|---|
 | 45 |  | 
|---|
 | 46 |         if (terrain.loadBitmap(fileName) == false) | 
|---|
 | 47 |         { | 
|---|
 | 48 |                 cout << "could not create OpenGL-Window." << endl; | 
|---|
 | 49 |                 return false; | 
|---|
 | 50 |         } | 
|---|
 | 51 |          | 
|---|
 | 52 |         terrain.createDisplayLists(64, 64, 1); | 
|---|
 | 53 |          | 
|---|
 | 54 |         return true; | 
|---|
 | 55 | } | 
|---|
 | 56 |  | 
|---|
 | 57 | bool HeightMapViewer::run() | 
|---|
 | 58 | { | 
|---|
 | 59 |         cout << "HeightMapViewer run()" << endl; | 
|---|
 | 60 |  | 
|---|
 | 61 |         bool done = false; | 
|---|
 | 62 |         SDL_Event event; | 
|---|
 | 63 |  | 
|---|
 | 64 |         float angleX; | 
|---|
 | 65 |         float angleY; | 
|---|
 | 66 |          | 
|---|
 | 67 |         while(!done) | 
|---|
 | 68 |         { | 
|---|
 | 69 |                 /* Check for events */ | 
|---|
 | 70 |                 while (SDL_PollEvent(&event)) | 
|---|
 | 71 |                 { | 
|---|
 | 72 |                         switch (event.type) | 
|---|
 | 73 |                         { | 
|---|
 | 74 |                                 case SDL_MOUSEMOTION: | 
|---|
 | 75 |                                         if(mousedown) | 
|---|
 | 76 |                                         { | 
|---|
 | 77 |                                                 angleX = PI / 180 * event.motion.xrel * 0.1; | 
|---|
 | 78 |                                                 angleY = PI / 180 * event.motion.yrel * 0.1; | 
|---|
 | 79 |  | 
|---|
 | 80 |                                                 // Quaternion(angle,axis) | 
|---|
 | 81 |                                                 rotator = Quaternion(angleX,Vector(0,1,0)); | 
|---|
 | 82 |                                                 sightDirection = rotator.apply(sightDirection); | 
|---|
 | 83 |                                                  | 
|---|
 | 84 |                                                 rotator = Quaternion(-angleY,sightDirection.perpendicular()); | 
|---|
 | 85 |                                                 sightDirection = rotator.apply(sightDirection); | 
|---|
 | 86 |                                                  | 
|---|
 | 87 |                                                 updateView();                                            | 
|---|
 | 88 |                                         } | 
|---|
 | 89 |                                         break; | 
|---|
 | 90 |                                 case SDL_MOUSEBUTTONDOWN: | 
|---|
 | 91 |                                         mousedown = true; | 
|---|
 | 92 |                                         break; | 
|---|
 | 93 |                                 case SDL_MOUSEBUTTONUP: | 
|---|
 | 94 |                                         mousedown = false; | 
|---|
 | 95 |                                         break; | 
|---|
 | 96 |                                          | 
|---|
 | 97 |                                 case SDL_KEYDOWN: | 
|---|
 | 98 |  | 
|---|
 | 99 |                                         switch(event.key.keysym.sym) | 
|---|
 | 100 |                                         { | 
|---|
 | 101 |                                                 case SDLK_w: | 
|---|
 | 102 |                                                         wireframe = !wireframe; | 
|---|
 | 103 |                                                         if (wireframe) glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); | 
|---|
 | 104 |                                                         else glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); | 
|---|
 | 105 |                                                         break; | 
|---|
 | 106 |                                                          | 
|---|
 | 107 |                                                 case SDLK_UP: | 
|---|
 | 108 |                                                         // move in direction of sight | 
|---|
 | 109 |                                                         cameraPos = cameraPos + sightDirection; | 
|---|
 | 110 |                                                         updateView(); | 
|---|
 | 111 |                                                         break; | 
|---|
 | 112 |  | 
|---|
 | 113 |                                                 case SDLK_DOWN: | 
|---|
 | 114 |                                                         // move in direction of sight | 
|---|
 | 115 |                                                         cameraPos = cameraPos - sightDirection; | 
|---|
 | 116 |                                                         updateView(); | 
|---|
 | 117 |                                                         break; | 
|---|
 | 118 |                                                          | 
|---|
 | 119 |                                                 case SDLK_LEFT: | 
|---|
 | 120 |                                                         cameraPos = cameraPos - sightDirection.perpendicular(); | 
|---|
 | 121 |                                                         updateView(); | 
|---|
 | 122 |                                                         break; | 
|---|
 | 123 |                                                          | 
|---|
 | 124 |                                                 case SDLK_RIGHT: | 
|---|
 | 125 |                                                         cameraPos = cameraPos + sightDirection.perpendicular(); | 
|---|
 | 126 |                                                         updateView(); | 
|---|
 | 127 |                                                         break; | 
|---|
 | 128 |                                                          | 
|---|
 | 129 |                                                 case SDLK_r: | 
|---|
 | 130 |                                                         // restore original view vectors | 
|---|
 | 131 |                                                         cameraPos = Vector(0,0,40); | 
|---|
 | 132 |                                                         sightDirection = Vector(0,0,-1); | 
|---|
 | 133 |                                                         updateView(); | 
|---|
 | 134 |                                                         break; | 
|---|
 | 135 |  | 
|---|
 | 136 |                                                 case SDLK_h: | 
|---|
 | 137 |                                                         cout << "HELP\n" | 
|---|
 | 138 |                                                         << "display list count:    " << terrain.displayListCount << endl | 
|---|
 | 139 |                                                         << "first display list at: " << terrain.displayListStart << endl | 
|---|
 | 140 |                                                         << endl; | 
|---|
 | 141 |                                                          | 
|---|
 | 142 |                                                         break; | 
|---|
 | 143 |                                                          | 
|---|
 | 144 |                                                 case SDLK_ESCAPE: | 
|---|
 | 145 |                                                 case SDLK_q: | 
|---|
 | 146 |                                                         done = 1; | 
|---|
 | 147 |                                                         break; | 
|---|
 | 148 |                                                          | 
|---|
 | 149 |                                                 default: | 
|---|
 | 150 |                                                         break; | 
|---|
 | 151 |                                         }        | 
|---|
 | 152 |                                         break; | 
|---|
 | 153 |                                          | 
|---|
 | 154 |                                 case SDL_QUIT: | 
|---|
 | 155 |                                         done = 1; | 
|---|
 | 156 |                                         break; | 
|---|
 | 157 |                                          | 
|---|
 | 158 |                                 default: | 
|---|
 | 159 |                                         break; | 
|---|
 | 160 |                         } | 
|---|
 | 161 |                 } | 
|---|
 | 162 |                  | 
|---|
 | 163 |                 drawScene(); | 
|---|
 | 164 |  | 
|---|
 | 165 |                 SDL_GL_SwapBuffers();    | 
|---|
 | 166 |  | 
|---|
 | 167 |                 //SDL_Delay(100); | 
|---|
 | 168 |         } | 
|---|
 | 169 |          | 
|---|
 | 170 |         return true; | 
|---|
 | 171 | } | 
|---|
 | 172 |  | 
|---|
 | 173 | void HeightMapViewer::updateView() | 
|---|
 | 174 | { | 
|---|
 | 175 |         // be sure to use up to date lookAt-vector | 
|---|
 | 176 |         lookAt = cameraPos + sightDirection; | 
|---|
 | 177 |         upVector = sightDirection.cross(sightDirection.perpendicular()) * -1; | 
|---|
 | 178 |          | 
|---|
 | 179 |         glMatrixMode(GL_PROJECTION); // Select The Projection Matrix | 
|---|
 | 180 |         glLoadIdentity(); | 
|---|
 | 181 |          | 
|---|
 | 182 |         // Calculate The Aspect Ratio Of The Window | 
|---|
 | 183 |         gluPerspective(45.0f,(GLfloat)WIDTH/(GLfloat)HEIGHT,0.1f,100.0f); | 
|---|
 | 184 |         gluLookAt (cameraPos.x,cameraPos.y,cameraPos.z, | 
|---|
 | 185 |                            lookAt.x,lookAt.y,lookAt.z, | 
|---|
 | 186 |                            upVector.x,upVector.y,upVector.z); | 
|---|
 | 187 |          | 
|---|
 | 188 |         glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix | 
|---|
 | 189 |         glLoadIdentity(); // Reset The Modelview Matrix | 
|---|
 | 190 | } | 
|---|
 | 191 |  | 
|---|
 | 192 | void HeightMapViewer::drawScene() | 
|---|
 | 193 | { | 
|---|
 | 194 |         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | 
|---|
 | 195 |  | 
|---|
 | 196 |         glutWireCube(1.0); | 
|---|
 | 197 |          | 
|---|
 | 198 |         int i; | 
|---|
 | 199 |          | 
|---|
 | 200 |         for (i=0; i<terrain.displayListCount; i++) | 
|---|
 | 201 |         { | 
|---|
 | 202 |                 glCallList(terrain.displayListStart + i); | 
|---|
 | 203 |         } | 
|---|
 | 204 | } | 
|---|