[4090] | 1 | |
---|
| 2 | |
---|
| 3 | #include "heightmap.h" |
---|
| 4 | |
---|
| 5 | #include <stdio.h> |
---|
| 6 | #include <string.h> |
---|
| 7 | #include <stdlib.h> |
---|
| 8 | |
---|
| 9 | #include "debug.h" |
---|
| 10 | #include "compiler.h" |
---|
| 11 | |
---|
| 12 | |
---|
| 13 | Heightmap::Heightmap(const char* fileName, float scaling, int displaylistResolution, int vertexResolution) |
---|
| 14 | { |
---|
| 15 | this->scaleFactor = scaling; |
---|
| 16 | this->setName(fileName); |
---|
| 17 | |
---|
[4093] | 18 | this->import(fileName, displaylistResolution, vertexResolution); |
---|
[4090] | 19 | |
---|
[4093] | 20 | this->finalize(); |
---|
[4090] | 21 | } |
---|
| 22 | |
---|
| 23 | Heightmap::~Heightmap () |
---|
| 24 | { |
---|
| 25 | } |
---|
| 26 | |
---|
[4093] | 27 | bool Heightmap::import (const char* fileName, int displaylistResolution, int vertexResolution) |
---|
[4090] | 28 | { |
---|
| 29 | PRINTF(4)("preparing to read in file: %s\n", fileName); |
---|
| 30 | |
---|
| 31 | // using SDL-image to load the bitmap |
---|
| 32 | this->bitmap = IMG_Load(fileName); |
---|
| 33 | |
---|
[4093] | 34 | // how many pixels wide is a display list? |
---|
| 35 | int pixPerDLSide = displaylistResolution * vertexResolution; |
---|
| 36 | |
---|
| 37 | int howManyListsWide = bitmap->w / pixPerDLSide + 1; |
---|
| 38 | int howManyListsHigh = bitmap->h / pixPerDLSide + 1; |
---|
[4090] | 39 | |
---|
[4093] | 40 | // calculated pixel dimensions of one display list. usually equals pixPerDLSide, but at the end of the picture |
---|
| 41 | // this needs to be shorter, that we do not make a segfault |
---|
| 42 | int width = pixPerDLSide; |
---|
| 43 | int height = pixPerDLSide; |
---|
[4090] | 44 | |
---|
[4093] | 45 | for (int i=0; i<howManyListsWide; i++) |
---|
| 46 | { |
---|
| 47 | for (int j=0; j<howManyListsHigh; j++) |
---|
| 48 | { |
---|
| 49 | // catch out of image access in x-direction |
---|
| 50 | if ( (j+1)*pixPerDLSide - 1 >= bitmap->w ) width = bitmap->w - j*pixPerDLSide; |
---|
| 51 | else width = pixPerDLSide; |
---|
| 52 | // y-direction |
---|
| 53 | if ( (i+1)*pixPerDLSide - 1 >= bitmap->h ) height = bitmap->h - i*pixPerDLSide; |
---|
| 54 | else height = pixPerDLSide; |
---|
| 55 | |
---|
| 56 | this->readIntoGroup(j*pixPerDLSide,i*pixPerDLSide,width,height,vertexResolution); |
---|
| 57 | } |
---|
| 58 | } |
---|
[4090] | 59 | |
---|
| 60 | return true; |
---|
| 61 | } |
---|
| 62 | |
---|
[4093] | 63 | void Heightmap::readIntoGroup(int left, int top,int width, int height, int vertexResolution) |
---|
| 64 | { |
---|
| 65 | //this->newGroup(); |
---|
| 66 | |
---|
| 67 | for (int z=top; z < top+height; z++) |
---|
| 68 | { |
---|
| 69 | for (int x=left; x < left+width; x++) |
---|
| 70 | { |
---|
| 71 | this->addVertex(x,this->getHeightAt(x,z),z); |
---|
| 72 | } |
---|
| 73 | } |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | float Heightmap::getHeightAt(int x, int z) |
---|
| 77 | { |
---|
| 78 | Uint8 index; |
---|
| 79 | SDL_Color color; |
---|
| 80 | |
---|
| 81 | SDL_LockSurface(bitmap); |
---|
| 82 | |
---|
| 83 | index = *((Uint8*)bitmap->pixels + z * bitmap->pitch + x * bitmap->format->BytesPerPixel); |
---|
| 84 | color = bitmap->format->palette->colors[index]; |
---|
| 85 | |
---|
| 86 | SDL_UnlockSurface(bitmap); |
---|
| 87 | |
---|
| 88 | // return the red component, because in a grayscale pic, r,g and b are all the same value |
---|
| 89 | return (GLint)color.r; |
---|
| 90 | } |
---|