[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); |
---|
[3390] | 23 | |
---|
[3381] | 24 | wireframe = false; |
---|
| 25 | mousedown = false; |
---|
[3390] | 26 | smoothShading = false; |
---|
[3381] | 27 | |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | // destructor |
---|
| 31 | HeightMapViewer::~HeightMapViewer() |
---|
| 32 | { |
---|
| 33 | cout << "HeightMapViewer::~HeightMapViewer()" << endl; |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | // main loop |
---|
| 37 | bool HeightMapViewer::init(char* fileName) |
---|
| 38 | { |
---|
[3393] | 39 | cout << "HeightMapViewer init()" << endl; |
---|
[3381] | 40 | |
---|
[3390] | 41 | #ifdef FULLSCREEN |
---|
| 42 | if (!windowhandler.CreateGLWindow("height map viewer", WIDTH, HEIGHT, 32, true)) |
---|
| 43 | #else |
---|
| 44 | if (!windowhandler.CreateGLWindow("height map viewer", WIDTH, HEIGHT, 32, false)) |
---|
| 45 | #endif |
---|
[3381] | 46 | { |
---|
| 47 | cout << "could not create OpenGL-Window." << endl; |
---|
| 48 | return false; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | if (terrain.loadBitmap(fileName) == false) |
---|
| 52 | { |
---|
[3390] | 53 | cout << "could not load bitmap." << endl; |
---|
[3381] | 54 | return false; |
---|
| 55 | } |
---|
| 56 | |
---|
[3390] | 57 | terrain.createDisplayLists(128, 128, 1); |
---|
[3381] | 58 | |
---|
[3393] | 59 | this->initLighting(); |
---|
| 60 | SDL_EnableKeyRepeat(50,10); |
---|
[3381] | 61 | return true; |
---|
| 62 | } |
---|
| 63 | |
---|
[3393] | 64 | bool HeightMapViewer::initLighting(void) |
---|
| 65 | { |
---|
| 66 | /* glEnable(GL_LIGHTING); |
---|
| 67 | glEnable(GL_DEPTH_TEST); |
---|
| 68 | |
---|
| 69 | GLfloat whiteLight[] = {.2, .2, .2, 1.0}; |
---|
| 70 | GLfloat light0Position[] = {10.0, 10.0, 10.0, 0.0}; |
---|
| 71 | GLfloat lmodelAmbient[] = {.1, .1, .1, 1.0}; |
---|
| 72 | |
---|
| 73 | glEnable(GL_LIGHT0); |
---|
| 74 | glLightfv(GL_LIGHT0, GL_POSITION, light0Position); |
---|
| 75 | glLightfv(GL_LIGHT0, GL_DIFFUSE, whiteLight); |
---|
| 76 | glLightfv(GL_LIGHT0, GL_SPECULAR, whiteLight); |
---|
| 77 | |
---|
| 78 | GLfloat klotz_Ka[] = {0.2,0.2,0.2,1.0}; |
---|
| 79 | GLfloat klotz_Kd[] = {0.3,1.0,0.3,1.0}; |
---|
| 80 | GLfloat klotz_Ks[] = {0.8,0.8,0.8,1.0}; |
---|
| 81 | GLfloat klotz_Ke[] = {0.0,0.0,0.0,1.0}; |
---|
| 82 | GLfloat klotz_Se = 20; |
---|
| 83 | glMaterialfv(GL_FRONT,GL_AMBIENT,klotz_Ka); |
---|
| 84 | glMaterialfv(GL_FRONT,GL_DIFFUSE,klotz_Kd); |
---|
| 85 | */ |
---|
| 86 | |
---|
| 87 | glShadeModel(GL_FLAT); // Enable Smooth Shading |
---|
| 88 | |
---|
| 89 | glClearColor(0.445f, 0.726f, 1.00f, 0.0f); // Blue Background |
---|
| 90 | |
---|
| 91 | glClearDepth(1.0f); // Depth Buffer Setup |
---|
| 92 | glEnable(GL_DEPTH_TEST); // Enables Depth Testing |
---|
| 93 | glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do |
---|
| 94 | |
---|
| 95 | glEnable(GL_CULL_FACE); // don't draw back faces |
---|
| 96 | |
---|
| 97 | glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | // fog behaves strange |
---|
| 101 | //glEnable(GL_FOG); |
---|
| 102 | GLfloat fog_color[4] = {0.7,0.7,0.7,1.0}; |
---|
| 103 | glFogfv(GL_FOG_COLOR,fog_color); |
---|
| 104 | glFogf(GL_FOG_START, 0.0); |
---|
| 105 | glFogf(GL_FOG_END, 100.0); |
---|
| 106 | glFogi(GL_FOG_MODE,GL_LINEAR); |
---|
| 107 | |
---|
| 108 | |
---|
| 109 | glEnable(GL_NORMALIZE); |
---|
| 110 | |
---|
| 111 | // LIGHT SETTINGS |
---|
| 112 | glEnable(GL_LIGHTING); |
---|
| 113 | glEnable(GL_LIGHT0); |
---|
| 114 | |
---|
| 115 | // global parameters |
---|
| 116 | GLfloat global_Ka[] = {0.25,0.25,0.25,1.0}; |
---|
| 117 | |
---|
| 118 | // light0 parameters |
---|
| 119 | GLfloat light0_pos[] = {0.0,100.0,0.0,0.0}; |
---|
| 120 | GLfloat light0_Ka[] = {0.0,0.0,0.0,1.0}; |
---|
| 121 | GLfloat light0_Kd[] = {0.6,0.6,0.6,1.0}; |
---|
| 122 | GLfloat light0_Ks[] = {0.8,0.8,0.8,1.0}; |
---|
| 123 | |
---|
| 124 | // klotz default parameters |
---|
| 125 | GLfloat klotz_Ka[] = {0.2,0.2,0.2,1.0}; |
---|
| 126 | GLfloat klotz_Kd[] = {0.3,1.0,0.3,1.0}; |
---|
| 127 | GLfloat klotz_Ks[] = {0.8,0.8,0.8,1.0}; |
---|
| 128 | GLfloat klotz_Ke[] = {0.0,0.0,0.0,1.0}; |
---|
| 129 | GLfloat klotz_Se = 20; |
---|
| 130 | |
---|
| 131 | // set light0 options |
---|
| 132 | glLightfv(GL_LIGHT0,GL_POSITION,light0_pos); |
---|
| 133 | glLightfv(GL_LIGHT0,GL_AMBIENT,light0_Ka); |
---|
| 134 | glLightfv(GL_LIGHT0,GL_DIFFUSE,light0_Kd); |
---|
| 135 | glLightfv(GL_LIGHT0,GL_SPECULAR,light0_Ks); |
---|
| 136 | |
---|
| 137 | // MATERIAL SETTINGS |
---|
| 138 | glMaterialfv(GL_FRONT,GL_AMBIENT,klotz_Ka); |
---|
| 139 | glMaterialfv(GL_FRONT,GL_DIFFUSE,klotz_Kd); |
---|
| 140 | //glMaterialfv(GL_FRONT,GL_SPECULAR,klotz_Ks); |
---|
| 141 | //glMaterialfv(GL_FRONT,GL_EMISSION,klotz_Ke); |
---|
| 142 | //glMaterialf(GL_FRONT,GL_SHININESS,klotz_Se); |
---|
| 143 | |
---|
| 144 | |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | |
---|
[3381] | 148 | bool HeightMapViewer::run() |
---|
| 149 | { |
---|
| 150 | cout << "HeightMapViewer run()" << endl; |
---|
| 151 | |
---|
| 152 | bool done = false; |
---|
| 153 | SDL_Event event; |
---|
| 154 | |
---|
| 155 | float angleX; |
---|
| 156 | float angleY; |
---|
| 157 | |
---|
| 158 | while(!done) |
---|
| 159 | { |
---|
| 160 | /* Check for events */ |
---|
| 161 | while (SDL_PollEvent(&event)) |
---|
| 162 | { |
---|
| 163 | switch (event.type) |
---|
| 164 | { |
---|
| 165 | case SDL_MOUSEMOTION: |
---|
[3390] | 166 | if (mousedown==true) |
---|
[3381] | 167 | { |
---|
[3390] | 168 | angleX = PI / 180 * event.motion.xrel * 0.2; |
---|
| 169 | angleY = PI / 180 * event.motion.yrel * 0.2; |
---|
[3381] | 170 | |
---|
| 171 | // Quaternion(angle,axis) |
---|
[3390] | 172 | rotator = Quaternion(-angleX,Vector(0,1,0)); |
---|
[3381] | 173 | sightDirection = rotator.apply(sightDirection); |
---|
[3390] | 174 | |
---|
| 175 | rotator = Quaternion(angleY,perpendicular(sightDirection)); |
---|
[3381] | 176 | |
---|
| 177 | sightDirection = rotator.apply(sightDirection); |
---|
| 178 | |
---|
| 179 | updateView(); |
---|
| 180 | } |
---|
| 181 | break; |
---|
| 182 | case SDL_MOUSEBUTTONDOWN: |
---|
| 183 | mousedown = true; |
---|
| 184 | break; |
---|
| 185 | case SDL_MOUSEBUTTONUP: |
---|
| 186 | mousedown = false; |
---|
| 187 | break; |
---|
| 188 | |
---|
| 189 | case SDL_KEYDOWN: |
---|
| 190 | |
---|
| 191 | switch(event.key.keysym.sym) |
---|
| 192 | { |
---|
| 193 | case SDLK_UP: |
---|
| 194 | // move in direction of sight |
---|
[3390] | 195 | cameraPos = cameraPos + sightDirection * 0.7; |
---|
[3381] | 196 | updateView(); |
---|
| 197 | break; |
---|
| 198 | |
---|
| 199 | case SDLK_DOWN: |
---|
| 200 | // move in direction of sight |
---|
[3390] | 201 | cameraPos = cameraPos - sightDirection * 0.7; |
---|
[3381] | 202 | updateView(); |
---|
| 203 | break; |
---|
| 204 | |
---|
| 205 | case SDLK_LEFT: |
---|
[3390] | 206 | cameraPos = cameraPos + perpendicular(sightDirection) * 0.7; |
---|
[3381] | 207 | updateView(); |
---|
| 208 | break; |
---|
| 209 | |
---|
| 210 | case SDLK_RIGHT: |
---|
[3390] | 211 | cameraPos = cameraPos - perpendicular(sightDirection) * 0.7; |
---|
[3381] | 212 | updateView(); |
---|
| 213 | break; |
---|
| 214 | |
---|
[3390] | 215 | case SDLK_s: |
---|
| 216 | smoothShading = !smoothShading; |
---|
| 217 | if (smoothShading) glShadeModel(GL_SMOOTH); |
---|
| 218 | else glShadeModel(GL_FLAT); |
---|
| 219 | break; |
---|
| 220 | |
---|
| 221 | case SDLK_w: |
---|
| 222 | wireframe = !wireframe; |
---|
| 223 | if (wireframe) glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); |
---|
| 224 | else glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); |
---|
| 225 | break; |
---|
| 226 | |
---|
[3381] | 227 | case SDLK_r: |
---|
[3390] | 228 | // reset view vectors |
---|
| 229 | //cameraPos = Vector(0,0,40); |
---|
| 230 | //sightDirection = Vector(0,0,-1); |
---|
| 231 | cameraPos = Vector(73.9871,172.496,286.137); |
---|
| 232 | sightDirection = Vector(0.23429,-0.736527,-0.625574); |
---|
| 233 | |
---|
[3381] | 234 | updateView(); |
---|
| 235 | break; |
---|
| 236 | |
---|
| 237 | case SDLK_h: |
---|
| 238 | cout << "HELP\n" |
---|
| 239 | << "display list count: " << terrain.displayListCount << endl |
---|
| 240 | << "first display list at: " << terrain.displayListStart << endl |
---|
[3390] | 241 | << "camera position: " << "(" << cameraPos.x << "," << cameraPos.y << "," << cameraPos.z << ")" << endl |
---|
| 242 | << "sightDirection: " << "(" << sightDirection.x << "," << sightDirection.y << "," << sightDirection.z << ")" << endl |
---|
[3381] | 243 | << endl; |
---|
| 244 | |
---|
| 245 | break; |
---|
| 246 | |
---|
| 247 | case SDLK_ESCAPE: |
---|
| 248 | case SDLK_q: |
---|
| 249 | done = 1; |
---|
| 250 | break; |
---|
| 251 | |
---|
| 252 | default: |
---|
| 253 | break; |
---|
| 254 | } |
---|
| 255 | break; |
---|
| 256 | |
---|
| 257 | case SDL_QUIT: |
---|
| 258 | done = 1; |
---|
| 259 | break; |
---|
| 260 | |
---|
| 261 | default: |
---|
| 262 | break; |
---|
| 263 | } |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | drawScene(); |
---|
| 267 | |
---|
| 268 | SDL_GL_SwapBuffers(); |
---|
| 269 | |
---|
[3390] | 270 | SDL_Delay(1); |
---|
[3381] | 271 | } |
---|
| 272 | |
---|
| 273 | return true; |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | void HeightMapViewer::updateView() |
---|
| 277 | { |
---|
| 278 | // be sure to use up to date lookAt-vector |
---|
| 279 | lookAt = cameraPos + sightDirection; |
---|
[3390] | 280 | upVector = sightDirection.cross(perpendicular(sightDirection)); |
---|
[3381] | 281 | |
---|
| 282 | glMatrixMode(GL_PROJECTION); // Select The Projection Matrix |
---|
| 283 | glLoadIdentity(); |
---|
| 284 | |
---|
| 285 | // Calculate The Aspect Ratio Of The Window |
---|
[3390] | 286 | gluPerspective(45.0f,(GLfloat)WIDTH/(GLfloat)HEIGHT,0.5f,300.0f); |
---|
[3381] | 287 | gluLookAt (cameraPos.x,cameraPos.y,cameraPos.z, |
---|
| 288 | lookAt.x,lookAt.y,lookAt.z, |
---|
| 289 | upVector.x,upVector.y,upVector.z); |
---|
| 290 | |
---|
| 291 | glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix |
---|
| 292 | glLoadIdentity(); // Reset The Modelview Matrix |
---|
| 293 | } |
---|
| 294 | |
---|
| 295 | void HeightMapViewer::drawScene() |
---|
[3390] | 296 | { |
---|
[3381] | 297 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
---|
| 298 | |
---|
[3390] | 299 | glPushMatrix(); |
---|
| 300 | { |
---|
| 301 | //glTranslatef(0.0,1.0,0.0); |
---|
| 302 | glScalef(1,0.2,1); |
---|
[3381] | 303 | |
---|
[3390] | 304 | for (int i=0; i<terrain.displayListCount; i++) |
---|
| 305 | { |
---|
| 306 | glCallList(terrain.displayListStart + i); |
---|
| 307 | } |
---|
| 308 | } |
---|
| 309 | glPopMatrix(); |
---|
[3381] | 310 | |
---|
[3390] | 311 | glColor3f(0.0,1.0,0.0); |
---|
| 312 | // glutWireCube(1.0); |
---|
| 313 | |
---|
| 314 | glColor3f(0.0,0.0,1.0); |
---|
| 315 | glBegin(GL_POINTS); |
---|
| 316 | for (float z=0;z<10;z++) |
---|
| 317 | for (float x=0;x<10;x++) |
---|
| 318 | glVertex3f(x,0,z); |
---|
| 319 | glEnd(); |
---|
| 320 | |
---|
| 321 | } |
---|
| 322 | |
---|
| 323 | |
---|
| 324 | |
---|
| 325 | /** |
---|
| 326 | \brief returns a vector that is perpendicular and lies in the xz-plane |
---|
| 327 | */ |
---|
| 328 | Vector perpendicular (Vector perpendic) |
---|
| 329 | { |
---|
| 330 | Vector r; |
---|
| 331 | |
---|
| 332 | r.x = perpendic.z; |
---|
| 333 | r.z = -perpendic.x; |
---|
| 334 | |
---|
| 335 | r.normalize(); |
---|
| 336 | |
---|
| 337 | return r; |
---|
| 338 | } |
---|