/* * legotown.cpp * legotown3d * * Created by Nico Bernold on 08.01.05. * Copyright 2005 __MyCompanyName__. All rights reserved. * */ #include "heightMapViewer.h" // constructor HeightMapViewer::HeightMapViewer() { cout << "HeightMapViewer::HeightMapViewer()" << endl; cameraPos = Vector(0,0,40); sightDirection = Vector(0,0,-1); lookAt = cameraPos + sightDirection; upVector = Vector(0,1,0); wireframe = false; mousedown = false; } // destructor HeightMapViewer::~HeightMapViewer() { cout << "HeightMapViewer::~HeightMapViewer()" << endl; } // main loop bool HeightMapViewer::init(char* fileName) { cout << "HeightMapViewer init()" << endl; if (windowhandler.createOpenGLWindow(WIDTH,HEIGHT,FULLSCREEN) == false) { cout << "could not create OpenGL-Window." << endl; return false; } if (terrain.loadBitmap(fileName) == false) { cout << "could not create OpenGL-Window." << endl; return false; } terrain.createDisplayLists(64, 64, 1); return true; } bool HeightMapViewer::run() { cout << "HeightMapViewer run()" << endl; bool done = false; SDL_Event event; float angleX; float angleY; while(!done) { /* Check for events */ while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_MOUSEMOTION: if(mousedown) { angleX = PI / 180 * event.motion.xrel * 0.1; angleY = PI / 180 * event.motion.yrel * 0.1; // Quaternion(angle,axis) rotator = Quaternion(angleX,Vector(0,1,0)); sightDirection = rotator.apply(sightDirection); rotator = Quaternion(-angleY,sightDirection.perpendicular()); sightDirection = rotator.apply(sightDirection); updateView(); } break; case SDL_MOUSEBUTTONDOWN: mousedown = true; break; case SDL_MOUSEBUTTONUP: mousedown = false; break; case SDL_KEYDOWN: switch(event.key.keysym.sym) { case SDLK_w: wireframe = !wireframe; if (wireframe) glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); else glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); break; case SDLK_UP: // move in direction of sight cameraPos = cameraPos + sightDirection; updateView(); break; case SDLK_DOWN: // move in direction of sight cameraPos = cameraPos - sightDirection; updateView(); break; case SDLK_LEFT: cameraPos = cameraPos - sightDirection.perpendicular(); updateView(); break; case SDLK_RIGHT: cameraPos = cameraPos + sightDirection.perpendicular(); updateView(); break; case SDLK_r: // restore original view vectors cameraPos = Vector(0,0,40); sightDirection = Vector(0,0,-1); updateView(); break; case SDLK_h: cout << "HELP\n" << "display list count: " << terrain.displayListCount << endl << "first display list at: " << terrain.displayListStart << endl << endl; break; case SDLK_ESCAPE: case SDLK_q: done = 1; break; default: break; } break; case SDL_QUIT: done = 1; break; default: break; } } drawScene(); SDL_GL_SwapBuffers(); //SDL_Delay(100); } return true; } void HeightMapViewer::updateView() { // be sure to use up to date lookAt-vector lookAt = cameraPos + sightDirection; upVector = sightDirection.cross(sightDirection.perpendicular()) * -1; glMatrixMode(GL_PROJECTION); // Select The Projection Matrix glLoadIdentity(); // Calculate The Aspect Ratio Of The Window gluPerspective(45.0f,(GLfloat)WIDTH/(GLfloat)HEIGHT,0.1f,100.0f); gluLookAt (cameraPos.x,cameraPos.y,cameraPos.z, lookAt.x,lookAt.y,lookAt.z, upVector.x,upVector.y,upVector.z); glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix glLoadIdentity(); // Reset The Modelview Matrix } void HeightMapViewer::drawScene() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glutWireCube(1.0); int i; for (i=0; i