#include "heightmap.h" #include #include #include #include "debug.h" #include "compiler.h" Heightmap::Heightmap(const char* fileName, float scaling, int displaylistResolution, int vertexResolution) { this->scaleFactor = scaling; this->setName(fileName); this->import(fileName, displaylistResolution, vertexResolution); this->finalize(); } Heightmap::~Heightmap () { } bool Heightmap::import (const char* fileName, int displaylistResolution, int vertexResolution) { PRINTF(4)("preparing to read in file: %s\n", fileName); // using SDL-image to load the bitmap this->bitmap = IMG_Load(fileName); // how many pixels wide is a display list? int pixPerDLSide = displaylistResolution * vertexResolution; int howManyListsWide = bitmap->w / pixPerDLSide + 1; int howManyListsHigh = bitmap->h / pixPerDLSide + 1; // calculated pixel dimensions of one display list. usually equals pixPerDLSide, but at the end of the picture // this needs to be shorter, that we do not make a segfault int width = pixPerDLSide; int height = pixPerDLSide; for (int i=0; i= bitmap->w ) width = bitmap->w - j*pixPerDLSide; else width = pixPerDLSide; // y-direction if ( (i+1)*pixPerDLSide - 1 >= bitmap->h ) height = bitmap->h - i*pixPerDLSide; else height = pixPerDLSide; this->readIntoGroup(j*pixPerDLSide,i*pixPerDLSide,width,height,vertexResolution); } } return true; } void Heightmap::readIntoGroup(int left, int top,int width, int height, int vertexResolution) { //this->newGroup(); for (int z=top; z < top+height; z++) { for (int x=left; x < left+width; x++) { this->addVertex(x,this->getHeightAt(x,z),z); } } } float Heightmap::getHeightAt(int x, int z) { Uint8 index; SDL_Color color; SDL_LockSurface(bitmap); index = *((Uint8*)bitmap->pixels + z * bitmap->pitch + x * bitmap->format->BytesPerPixel); color = bitmap->format->palette->colors[index]; SDL_UnlockSurface(bitmap); // return the red component, because in a grayscale pic, r,g and b are all the same value return (GLint)color.r; }