/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: */ #include "height_map.h" #include "model.h" #include "texture.h" #include "vector.h" #include "debug.h" // INCLUDING SDL_Image #ifdef HAVE_SDL_IMAGE_H #include #else #include #endif HeightMap::HeightMap() : Model() { } HeightMap::HeightMap(const char* height_map_name = NULL) : Model() { this->setClassID(CL_HEIGHT_MAP, "HeightMap"); heightMap = IMG_Load(height_map_name); if(heightMap!=NULL) { PRINTF(0)("loading Image %s\n", height_map_name); PRINTF(0)("width : %i\n", heightMap->w); PRINTF(0)("hight : %i\n", heightMap->h); PRINTF(0)("%i Byte(s) per Pixel \n", heightMap->format->BytesPerPixel); } else PRINTF(4)("oops! couldn't load %s for some reason.\n", height_map_name); } HeightMap::~HeightMap() { delete heightMap; } void HeightMap::load() { long height = 0; int offset = 0; char * bmp = (char*) heightMap->pixels; if(heightMap != NULL /* && heightMap->format->BitsPerPixel == 8 */) { SDL_LockSurface(heightMap); for(int i = 0 ; i < heightMap->h ; i +=1) { for(int j = 0; j < heightMap->w ; j += 1) { // get local hight from heightMap offset = bmp[j + i*(heightMap->w )]; height = (unsigned int)heightMap->format->palette->colors[offset].r + (unsigned int)heightMap->format->palette->colors[offset].g + (unsigned int) heightMap->format->palette->colors[offset].b ; this->addVertex( 20*(heightMap->h - i) , (( height)/15) - 30 ,20*j); } } SDL_UnlockSurface(heightMap); int c = (heightMap->w) ; int g = 0; for(int i = 0; i < (heightMap->w) -1 ; i += 1) { for(int j = 0; j < (heightMap->h)/1 - 1; j++) { //this->addFace (3, VERTEX_ONLY,j+1+i*c ,j + (i+1)*c, j + i*c ); //this->addFace (3, VERTEX_ONLY,j + (i+1)*c +1 ,j + (i+1)*c,j+i*c +1 ); this->addFace (4 ,VERTEX_ONLY,j+i*c,j+(i+1)*c ,j + (i+1)*c +1, j +i*c+1 ); } } } else { //this->setName("HardCore"); this->addVertex (-0.5, -0.5, 0.5); this->addVertex (0.5, -0.5, 0.5); this->addVertex (-0.5, 0.5, 0.5); this->addVertex (0.5, 0.5, 0.5); this->addVertex (-0.5, 0.5, -0.5); this->addVertex (0.5, 0.5, -0.5); this->addVertex (-0.5, -0.5, -0.5); this->addVertex (0.5, -0.5, -0.5); this->addFace (3, VERTEX_ONLY, 4, 3, 2); } this->finalize(); }