/* * 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; smoothShading = false; } // destructor HeightMapViewer::~HeightMapViewer() { cout << "HeightMapViewer::~HeightMapViewer()" << endl; } // main loop bool HeightMapViewer::init(char* fileName) { cout << "HeightMapViewer init()" << endl; #ifdef FULLSCREEN if (!windowhandler.CreateGLWindow("height map viewer", WIDTH, HEIGHT, 32, true)) #else if (!windowhandler.CreateGLWindow("height map viewer", WIDTH, HEIGHT, 32, false)) #endif { cout << "could not create OpenGL-Window." << endl; return false; } if (terrain.loadBitmap(fileName) == false) { cout << "could not load bitmap." << endl; return false; } terrain.createDisplayLists(128, 128, 1); this->initLighting(); SDL_EnableKeyRepeat(50,10); return true; } bool HeightMapViewer::initLighting(void) { /* glEnable(GL_LIGHTING); glEnable(GL_DEPTH_TEST); GLfloat whiteLight[] = {.2, .2, .2, 1.0}; GLfloat light0Position[] = {10.0, 10.0, 10.0, 0.0}; GLfloat lmodelAmbient[] = {.1, .1, .1, 1.0}; glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0, GL_POSITION, light0Position); glLightfv(GL_LIGHT0, GL_DIFFUSE, whiteLight); glLightfv(GL_LIGHT0, GL_SPECULAR, whiteLight); GLfloat klotz_Ka[] = {0.2,0.2,0.2,1.0}; GLfloat klotz_Kd[] = {0.3,1.0,0.3,1.0}; GLfloat klotz_Ks[] = {0.8,0.8,0.8,1.0}; GLfloat klotz_Ke[] = {0.0,0.0,0.0,1.0}; GLfloat klotz_Se = 20; glMaterialfv(GL_FRONT,GL_AMBIENT,klotz_Ka); glMaterialfv(GL_FRONT,GL_DIFFUSE,klotz_Kd); */ glShadeModel(GL_FLAT); // Enable Smooth Shading glClearColor(0.445f, 0.726f, 1.00f, 0.0f); // Blue Background glClearDepth(1.0f); // Depth Buffer Setup glEnable(GL_DEPTH_TEST); // Enables Depth Testing glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do glEnable(GL_CULL_FACE); // don't draw back faces glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations // fog behaves strange //glEnable(GL_FOG); GLfloat fog_color[4] = {0.7,0.7,0.7,1.0}; glFogfv(GL_FOG_COLOR,fog_color); glFogf(GL_FOG_START, 0.0); glFogf(GL_FOG_END, 100.0); glFogi(GL_FOG_MODE,GL_LINEAR); glEnable(GL_NORMALIZE); // LIGHT SETTINGS glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); // global parameters GLfloat global_Ka[] = {0.25,0.25,0.25,1.0}; // light0 parameters GLfloat light0_pos[] = {0.0,100.0,0.0,0.0}; GLfloat light0_Ka[] = {0.0,0.0,0.0,1.0}; GLfloat light0_Kd[] = {0.6,0.6,0.6,1.0}; GLfloat light0_Ks[] = {0.8,0.8,0.8,1.0}; // klotz default parameters GLfloat klotz_Ka[] = {0.2,0.2,0.2,1.0}; GLfloat klotz_Kd[] = {0.3,1.0,0.3,1.0}; GLfloat klotz_Ks[] = {0.8,0.8,0.8,1.0}; GLfloat klotz_Ke[] = {0.0,0.0,0.0,1.0}; GLfloat klotz_Se = 20; // set light0 options glLightfv(GL_LIGHT0,GL_POSITION,light0_pos); glLightfv(GL_LIGHT0,GL_AMBIENT,light0_Ka); glLightfv(GL_LIGHT0,GL_DIFFUSE,light0_Kd); glLightfv(GL_LIGHT0,GL_SPECULAR,light0_Ks); // MATERIAL SETTINGS glMaterialfv(GL_FRONT,GL_AMBIENT,klotz_Ka); glMaterialfv(GL_FRONT,GL_DIFFUSE,klotz_Kd); //glMaterialfv(GL_FRONT,GL_SPECULAR,klotz_Ks); //glMaterialfv(GL_FRONT,GL_EMISSION,klotz_Ke); //glMaterialf(GL_FRONT,GL_SHININESS,klotz_Se); } 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==true) { angleX = PI / 180 * event.motion.xrel * 0.2; angleY = PI / 180 * event.motion.yrel * 0.2; // Quaternion(angle,axis) rotator = Quaternion(-angleX,Vector(0,1,0)); sightDirection = rotator.apply(sightDirection); rotator = Quaternion(angleY,perpendicular(sightDirection)); 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_UP: // move in direction of sight cameraPos = cameraPos + sightDirection * 0.7; updateView(); break; case SDLK_DOWN: // move in direction of sight cameraPos = cameraPos - sightDirection * 0.7; updateView(); break; case SDLK_LEFT: cameraPos = cameraPos + perpendicular(sightDirection) * 0.7; updateView(); break; case SDLK_RIGHT: cameraPos = cameraPos - perpendicular(sightDirection) * 0.7; updateView(); break; case SDLK_s: smoothShading = !smoothShading; if (smoothShading) glShadeModel(GL_SMOOTH); else glShadeModel(GL_FLAT); break; 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_r: // reset view vectors //cameraPos = Vector(0,0,40); //sightDirection = Vector(0,0,-1); cameraPos = Vector(73.9871,172.496,286.137); sightDirection = Vector(0.23429,-0.736527,-0.625574); updateView(); break; case SDLK_h: cout << "HELP\n" << "display list count: " << terrain.displayListCount << endl << "first display list at: " << terrain.displayListStart << endl << "camera position: " << "(" << cameraPos.x << "," << cameraPos.y << "," << cameraPos.z << ")" << endl << "sightDirection: " << "(" << sightDirection.x << "," << sightDirection.y << "," << sightDirection.z << ")" << 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(1); } return true; } void HeightMapViewer::updateView() { // be sure to use up to date lookAt-vector lookAt = cameraPos + sightDirection; upVector = sightDirection.cross(perpendicular(sightDirection)); glMatrixMode(GL_PROJECTION); // Select The Projection Matrix glLoadIdentity(); // Calculate The Aspect Ratio Of The Window gluPerspective(45.0f,(GLfloat)WIDTH/(GLfloat)HEIGHT,0.5f,300.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); glPushMatrix(); { //glTranslatef(0.0,1.0,0.0); glScalef(1,0.2,1); for (int i=0; i