Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/graphics/importer/height_map.cc @ 7492

Last change on this file since 7492 was 7492, checked in by patrick, 18 years ago

resuming work on the hm

  • Property svn:executable set to *
File size: 19.5 KB
RevLine 
[5942]1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
[6956]12   main-programmer: bottac@ee.ethz.ch
[7468]13
14   review: patrick boenzli, patrick@orxonox.ethz.ch
[5942]15*/
16
17#include "height_map.h"
18#include "model.h"
[5967]19#include "texture.h"
20#include "vector.h"
[6956]21#include "material.h"
22#include "p_node.h"
23#include "state.h"
[7193]24#include "util/loading/resource_manager.h"
[5967]25#include "debug.h"
26
27// INCLUDING SDL_Image
28#ifdef HAVE_SDL_IMAGE_H
29#include <SDL_image.h>
30#else
31#include <SDL/SDL_image.h>
32#endif
33
[7467]34
[7468]35/**
36 * default constructor
37 *  @param i1
38 */
[7469]39Tile::Tile(int i1, int j1, int i2, int j2, HeightMap* heightMapReference )
[5942]40{
[7490]41  PRINTF(4)("Tile Constructor\n");
42
[7469]43  this->highResModel = new VertexArrayModel();
44  this->lowResModel  = new VertexArrayModel();
[6956]45
[7469]46  this->heightMapReference = heightMapReference;
47
48  // create high res model
49  this->load(i1, j1, i2, j2, this->highResModel, 4);
50  // create low res model
51  this->load(i1, j1, i2, j2, this->lowResModel, 8);
[6956]52}
53
[7468]54
[6956]55Tile::~Tile()
56{
[7468]57  if( highResModel)
58    delete highResModel;
59  if( lowResModel)
60    delete lowResModel;
[6956]61}
62
[7468]63
[7469]64/**
65 * this darws the tile in diefferent resolutions
66 */
[6956]67void Tile::draw()
68{
[7468]69  // draw the tile depending on the distance from the camera with different LOD (level of details)
70  float cameraDistance = fabs((State::getCameraNode()->getAbsCoor() - Vector(this->x, heightMapReference->offsetY , this->z) ).len());
71
72  if (cameraDistance < HM_LOD_HIGH_RES )
[7467]73  {
[7468]74    this->drawHighRes();
[7467]75  }
[7468]76  else if( cameraDistance < HM_LOD_LOW_RES)
[7467]77  {
78    this->drawLowRes();
79  }
[6956]80}
81
82
[7469]83/**
84 * loads a tile
85 */
86void Tile::load(int i1, int j1, int i2, int j2, VertexArrayModel* model, int sampleRate)
[6956]87{
88
[7469]89// #define heightMap this->heightMapReference->heightMap
[7490]90#define colors   this->heightMapReference->colors
[7469]91#define scaleX this->heightMapReference->scaleX
92#define scaleY this->heightMapReference->scaleY
93#define scaleZ this->heightMapReference->scaleZ
94#define shiftX this->heightMapReference->shiftX
95#define shiftY this->heightMapReference->shiftY
96#define shiftZ this->heightMapReference->shiftZ
97#define normalVectorField this->heightMapReference->normalVectorField
[6956]98
[7482]99
[7490]100  //FIXME: OLD implementation
[7469]101  this->x = this->heightMapReference->offsetX + ( this->heightMapReference->heightMap->h - ((i1 + i2) / 2)) * scaleX;
102  this->z = this->heightMapReference->offsetZ + ((j1 + j2 ) / 2 ) * scaleZ;
[7490]103  //NEW:
104  this->setAbsCoor(this->heightMapReference->offsetX + ( this->heightMapReference->heightMap->h - ((i1 + i2) / 2)) * scaleX,
105                   0,
106                   this->heightMapReference->offsetZ + ((j1 + j2 ) / 2 ) * scaleZ);
[7014]107
[7490]108
[7467]109  float height = 0;
110  int offset = 0;
[7014]111
[7467]112  float r = 0.0;
113  float g = 0.0;
114  float b = 0.0;
[7014]115
116
[7490]117  //if( this->heightMapReference->heightMap != NULL && this->heightMapReference->heightMap->format->BitsPerPixel == 8 )
[7014]118
[7469]119  SDL_LockSurface(this->heightMapReference->heightMap);
[7490]120  SDL_LockSurface(this->heightMapReference->colorMap);
[6956]121
[7490]122  for( int i = i1 ; i <= i2  ; i +=sampleRate)
[7467]123  {
124    int w = 0;
[6956]125
[7490]126    // adjust the colors acoring to the color map
127    if( this->heightMapReference->hasColourMap)
[7467]128    {
[7492]129      r = colors[3 * w + 2 + 3 * i * this->heightMapReference->heightMap->w];
130      g = colors[3 * w + 1 + 3 * i * this->heightMapReference->heightMap->w];
131      b = colors[3 * w + 0 + 3 * i * this->heightMapReference->heightMap->w];
[7467]132    }
[7014]133
[7467]134    w = j1;
[7491]135
136    // add a vertex to the list
[7469]137    model->addVertex(scaleX*(this->heightMapReference->heightMap->h -i)+ shiftX,shiftY,scaleZ*(w)+ shiftZ); // Top Right
[7491]138    model->addNormal(normalVectorField[i % this->heightMapReference->heightMap->h][w % this->heightMapReference->heightMap->w].y,
139                     normalVectorField[i % this->heightMapReference->heightMap->h][w % this->heightMapReference->heightMap->w].z,
140                     normalVectorField[i % this->heightMapReference->heightMap->h][w % this->heightMapReference->heightMap->w].x);
[7469]141    model->addTexCoor((float)(j1-sampleRate) /(texRate), (float)(i %this->heightMapReference->heightMap->h)/(texRate));
[7492]142    model->addColor(r/255.0f, g/255.0f, b/255.0f);
[7014]143
144
[7491]145    for(int j = j1; j <= j2; j += sampleRate)
[7467]146    {
[7491]147      // adjust the colors acording to the color map
148      if( this->heightMapReference->hasColourMap)
[7467]149      {
[7492]150        r = colors[3 * j + 2 + 3 * i * this->heightMapReference->heightMap->w];
151        g = colors[3 * j + 1 + 3 * i * this->heightMapReference->heightMap->w];
152        b = colors[3 * j + 0 + 3 * i * this->heightMapReference->heightMap->w];
[7467]153      }
[7491]154      height =  (float)(unsigned char)this->heightMapReference->heights[j + sampleRate + i * (this->heightMapReference->heightMap->w)];
155      height += (float)(unsigned char)this->heightMapReference->heights[j + 1 + sampleRate + (i + 1) *
156                                                                        (this->heightMapReference->heightMap->w)];
157      height += (float)(unsigned char)this->heightMapReference->heights[j - 1 + sampleRate + (i + 1) *
158                                                                        (this->heightMapReference->heightMap->w )];
159      height += (float)(unsigned char)this->heightMapReference->heights[j + sampleRate + (i + 2) *
160                                                                        (this->heightMapReference->heightMap->w)];
161      height += (float)(unsigned char)this->heightMapReference->heights[j + sampleRate + i * (this->heightMapReference->heightMap->w)];
162      height /= 5.0;
[7014]163
[7492]164      model->addVertex(scaleX * (this->heightMapReference->heightMap->h -i) + shiftX ,
165                       ((double)(height)*scaleY) + shiftY ,
166                       scaleZ*(j) + shiftZ); // Top Right
167      model->addNormal(normalVectorField[i % this->heightMapReference->heightMap->h][j % this->heightMapReference->heightMap->w].y,
168                       normalVectorField[i % this->heightMapReference->heightMap->h][j % this->heightMapReference->heightMap->w].z,
169                       normalVectorField[i % this->heightMapReference->heightMap->h][j % this->heightMapReference->heightMap->w].x);
[7469]170      model->addTexCoor((float)(j) /(texRate), (float)(i %this->heightMapReference->heightMap->h)/(texRate));
[7492]171      model->addColor(r/255.0f, g/255.0f, b/255.0f);
[7469]172      //PRINTF(0)("TexCoord:  %f %f \n",(float)j / 100.0, (float)(i %this->heightMapReference->h)/100.0);
[7014]173
[7467]174      w = j;
175    }
[7014]176
177
[7469]178    model->addVertex(scaleX*(this->heightMapReference->heightMap->h -i)+ shiftX,shiftY,scaleZ*(w)+ shiftZ); // Top Right
179    model->addNormal(normalVectorField[i % this->heightMapReference->heightMap->h][w % this->heightMapReference->heightMap->w].y,normalVectorField[i % this->heightMapReference->heightMap->h][w % this->heightMapReference->heightMap->w].z,normalVectorField[i% this->heightMapReference->heightMap->h][w % this->heightMapReference->heightMap->w].x);
180    model->addTexCoor((float)(j2+sampleRate) /(texRate), (float)(i %this->heightMapReference->heightMap->h)/(texRate));
[7467]181    model->addColor(r/255.0,g/255.0,b/255.0);
182  }
[7014]183
[7469]184  SDL_UnlockSurface(this->heightMapReference->heightMap);
[7467]185  int cnt = 0;
[7492]186
187  for( int i = i1; i < i2; i +=sampleRate)
[7467]188  {
[7492]189    for( int j = j1-sampleRate; j < j2  + 2 * sampleRate; j += sampleRate)
[7467]190    {
191      model->addIndice(cnt);
192      model->addIndice(cnt  + (j2 -j1 + 3* sampleRate  )/ sampleRate );
193      cnt++;
194    }
195    model->newStripe();
196  }
197  cnt += (j2 -j1 + 3* sampleRate)/ sampleRate;
[7014]198
[7492]199  for( int j = j1; j <= j2; j += sampleRate)
[7467]200  {
201    int i = i1;
[7014]202
[7467]203    // To be fixed
[7469]204    if(this->heightMapReference->hasColourMap)
[7467]205    {
[7492]206      r = (float)colors[3 * j + 2 + 3 * i * this->heightMapReference->heightMap->w];
207      g = (float)colors[3 * j + 1 + 3 * i * this->heightMapReference->heightMap->w];
208      b = (float)colors[3 * j + 0 + 3 * i * this->heightMapReference->heightMap->w];
[7467]209    }
[7014]210
[7492]211    model->addVertex(scaleX*(this->heightMapReference->heightMap->h -i) + shiftX ,
212                     shiftY ,
213                     scaleZ*(j) + shiftZ); // Top Right
214    model->addNormal(normalVectorField[i % this->heightMapReference->heightMap->h][j % this->heightMapReference->heightMap->w].y,
215                     normalVectorField[i % this->heightMapReference->heightMap->h][j % this->heightMapReference->heightMap->w].z,
216                     normalVectorField[i % this->heightMapReference->heightMap->h][j % this->heightMapReference->heightMap->w].x);
[7469]217    model->addTexCoor((float)j /(texRate), (float)((i - sampleRate) %this->heightMapReference->heightMap->h)/(texRate));
[7492]218    model->addColor(r/255.0f, g/255.0f, b/255.0f);
[7014]219
[7467]220  }
[7014]221
[7467]222  for(int j = j1  ; j <= j2    ;  j += sampleRate)
223  {
224    int i = i1;
[7469]225    height = (float)(unsigned char) this->heightMapReference->heights[(j +sampleRate+ i*(this->heightMapReference->heightMap->w )) ];
226    height += (float)(unsigned char) this->heightMapReference->heights[(j+ 1 + sampleRate + (i+1)*(this->heightMapReference->heightMap->w )) ];
227    height +=  (float) (unsigned char) this->heightMapReference->heights[(j -1+ sampleRate   + (i+1)*(this->heightMapReference->heightMap->w ))];
228    height +=  (float)(unsigned char)this->heightMapReference->heights[(j +sampleRate+ (i+2)*(this->heightMapReference->heightMap->w )) ];
229    height +=  (float)(unsigned char)this->heightMapReference->heights[(j+sampleRate + (i)*(this->heightMapReference->heightMap->w )) ];
[7467]230    height=height/5.0;
[7014]231
[7469]232    model->addVertex(scaleX*(this->heightMapReference->heightMap->h -i) + shiftX , ((double)(height)*scaleY) +shiftY ,scaleZ*(j) + shiftZ); // Top Right
233    model->addNormal(normalVectorField[i % this->heightMapReference->heightMap->h][j % this->heightMapReference->heightMap->w].y,normalVectorField[i % this->heightMapReference->heightMap->h][j% this->heightMapReference->heightMap->w].z,normalVectorField[i%this->heightMapReference->heightMap->h][j%this->heightMapReference->heightMap->w].x);
234    model->addTexCoor((float)j /(texRate), (float)(i %this->heightMapReference->heightMap->h)/(texRate));
[7467]235    model->addColor(r/255.0,g/255.0,b/255.0);
[7014]236
[7467]237  }
[7014]238
239
240
[7467]241  for(int j = j1  ; j <= j2    ;  j += sampleRate)
242  {
243    int i = i2;
[7014]244
[7467]245    // To be fixed
[7469]246    if(this->heightMapReference->hasColourMap)
[7467]247    {
[7490]248      r = (float)colors[3*j+2 + 3*i*(this->heightMapReference->heightMap->w )];
249      g = (float)colors[3*j+1 + 3*i*(this->heightMapReference->heightMap->w)];
250      b = (float)colors[3*j+0 + 3*i*(this->heightMapReference->heightMap->w)];
[7467]251    }
[7014]252
[7469]253    model->addVertex(scaleX*(this->heightMapReference->heightMap->h -i) + shiftX , shiftY ,scaleZ*(j) + shiftZ); // Top Right
254    model->addNormal(normalVectorField[i%this->heightMapReference->heightMap->h][j%this->heightMapReference->heightMap->w].y,normalVectorField[i%this->heightMapReference->heightMap->h][j%this->heightMapReference->heightMap->w].z,normalVectorField[i%this->heightMapReference->heightMap->h][j%this->heightMapReference->heightMap->w].x);
255    model->addTexCoor((float)j /(texRate), (float)((i+ sampleRate) %this->heightMapReference->heightMap->h)/(texRate));
[7467]256    model->addColor(r/255.0,g/255.0,b/255.0);
[7014]257
[7467]258  }
[7014]259
260
[7467]261  for(int j = j1 ; j <= j2    ;  j += sampleRate)
262  {
263    int i = i2;
[7469]264    height = (float)(unsigned char) this->heightMapReference->heights[(j +sampleRate+ i*(this->heightMapReference->heightMap->w )) ];
265    height += (float)(unsigned char) this->heightMapReference->heights[(j+ 1 + sampleRate + (i+1)*(this->heightMapReference->heightMap->w )) ];
266    height +=  (float) (unsigned char) this->heightMapReference->heights[(j -1+ sampleRate   + (i+1)*(this->heightMapReference->heightMap->w ))];
267    height +=  (float)(unsigned char)this->heightMapReference->heights[(j +sampleRate+ (i+2)*(this->heightMapReference->heightMap->w ))];
268    height +=  (float)(unsigned char)this->heightMapReference->heights[(j+sampleRate + (i)*(this->heightMapReference->heightMap->w )) ];
[7467]269    height=height/5.0;
[7469]270    model->addVertex(scaleX*(this->heightMapReference->heightMap->h -i) + shiftX , ((double)(height)*scaleY) +shiftY ,scaleZ*(j) + shiftZ); // Top Right
271    model->addNormal(normalVectorField[i%this->heightMapReference->heightMap->h][j%this->heightMapReference->heightMap->w].y,normalVectorField[i%this->heightMapReference->heightMap->h][j%this->heightMapReference->heightMap->w].z,normalVectorField[i%this->heightMapReference->heightMap->h][j%this->heightMapReference->heightMap->w].x);
272    model->addTexCoor((float)j /(texRate), (float)(i %this->heightMapReference->heightMap->h)/(texRate));
[7467]273    model->addColor(r/255.0,g/255.0,b/255.0);
[7014]274
[7467]275  }
[7014]276
277
278
279
[7467]280  // link Boarder Stripe
281  for(int j = j1-sampleRate  ; j < j2    ;  j += sampleRate)
282  {
[7014]283
[7467]284    model->addIndice(cnt);
285    model->addIndice(cnt  + (j2 -j1 +  sampleRate  )/ sampleRate );
286    cnt++;
[7014]287
[7467]288  }
[7014]289
[7467]290  cnt++;
[7014]291
[7467]292  model->newStripe();
[7014]293
294
295
296
297
[7467]298  cnt += (j2-j1)/ sampleRate;
[7014]299
[7467]300  // link 2nd BoarderStripe
301  for(int j = j1-sampleRate  ; j < j2    ;  j += sampleRate)
302  {
[7014]303
[7467]304    model->addIndice(cnt);
305    model->addIndice(cnt  + (j2 -j1 +  sampleRate  )/ sampleRate );
306    cnt++;
[7014]307
[7467]308  }
[7014]309
310
[7490]311  SDL_UnlockSurface(this->heightMapReference->colorMap);
[7014]312
[7467]313  model->finalize();
[7014]314
315
316
317
[7469]318// #undef heightMap
[7490]319        #undef colors
[7014]320        #undef scaleX
321        #undef scaleY
322        #undef scaleZ
323        #undef shiftX
324        #undef shiftY
325        #undef shiftZ
326        #undef normalVectorField
327
[5942]328}
329
[7469]330
[6956]331HeightMap::HeightMap()
[5967]332{
[6956]333}
334
[7469]335
[7221]336HeightMap::HeightMap(const std::string& height_map_name = "")
[7467]337    : VertexArrayModel()
[6956]338{
[7467]339  this->setClassID(CL_HEIGHT_MAP, "HeightMap");
340  heightMap =  IMG_Load(height_map_name.c_str());
341  if(heightMap!=NULL)
342  {
[7491]343    /*
344        WHAT about following checks:
345    - image size (rectangular?)
346    - image file type (bw?)
[6956]347
[7491]348    */
349
[7467]350    PRINTF(0)("loading Image %s\n", height_map_name.c_str());
351    PRINTF(0)("width : %i\n", heightMap->w);
352    PRINTF(0)("height : %i\n", heightMap->h);
353    PRINTF(0)("%i Byte(s) per Pixel \n", heightMap->format->BytesPerPixel);
354    PRINTF(0)("Rshift : %i\n", heightMap->format->Rshift);
355    PRINTF(0)("Bshift: %i\n", heightMap->format->Bshift);
356    PRINTF(0)("Gshift: %i\n", heightMap->format->Gshift);
357    PRINTF(0)("Rmask: %i\n", heightMap->format->Rmask);
358    PRINTF(0)("Gmask: %i\n", heightMap->format->Gmask);
359  }
[6956]360
[7467]361  else
362    PRINTF(4)("oops! couldn't load %s for some reason.\n", height_map_name.c_str());
[6956]363
[7014]364
[6956]365  generateNormalVectorField();
366
367  shiftX = 0;
368  shiftY = 0;
369  shiftZ = 0;
370
371}
372
[7490]373HeightMap::HeightMap(const std::string& height_map_name = NULL, const std::string& color_map_name = NULL)
[7467]374    : VertexArrayModel()
[6956]375{
[7467]376  this->setClassID(CL_HEIGHT_MAP, "HeightMap");
[6956]377
[7467]378  heightMap =  IMG_Load(height_map_name.c_str());
379  if(heightMap!=NULL)
380  {
[6956]381
[7467]382    PRINTF(0)("loading Image %s\n", height_map_name.c_str());
383    PRINTF(0)("width : %i\n", heightMap->w);
384    PRINTF(0)("height : %i\n", heightMap->h);
385    PRINTF(0)("%i Byte(s) per Pixel \n", heightMap->format->BytesPerPixel);
386    PRINTF(0)("Rshift : %i\n", heightMap->format->Rshift);
387    PRINTF(0)("Bshift: %i\n", heightMap->format->Bshift);
388    PRINTF(0)("Gshift: %i\n", heightMap->format->Gshift);
389    PRINTF(0)("Rmask: %i\n", heightMap->format->Rmask);
390    PRINTF(0)("Gmask: %i\n", heightMap->format->Gmask);
391  }
[6956]392
[7467]393  else
394    PRINTF(4)("oops! couldn't load %s for some reason.\n", height_map_name.c_str());
[6956]395
396
[7467]397  generateNormalVectorField();
[6956]398
[7490]399  colorMap=NULL;
400  if(color_map_name != "")
[6956]401  {
[7490]402    colorMap = IMG_Load(color_map_name.c_str());
[6956]403  }
[7014]404
[7490]405  if(colorMap != NULL)
[7467]406  {
[7490]407    PRINTF(0)("loading Image %s\n", color_map_name.c_str());
408    PRINTF(0)("width : %i\n", colorMap->w);
409    PRINTF(0)("height : %i\n", colorMap->h);
410    PRINTF(0)("%i Byte(s) per Pixel \n", colorMap->format->BytesPerPixel);
411    PRINTF(0)("Rshift : %i\n", colorMap->format->Rshift);
412    PRINTF(0)("Bshift: %i\n", colorMap->format->Bshift);
413    PRINTF(0)("Gshift: %i\n", colorMap->format->Gshift);
414    PRINTF(0)("Rmask: %i\n", colorMap->format->Rmask);
415    PRINTF(0)("Gmask: %i\n", colorMap->format->Gmask);
[7467]416  }
417  else
418    PRINTF(0)("oops! couldn't load colorMap for some reason.\n");
[6956]419
[7014]420
421
[7490]422  if(colorMap != NULL)
[6956]423  {
[7490]424    colors = (unsigned char *) colorMap->pixels;
[7467]425    hasColourMap = true;
[6956]426  }
[7467]427  else
428    hasColourMap = false;
[7014]429
430
[6956]431  heights  = (unsigned char*) heightMap->pixels;
432  shiftX = 0;
433  shiftY = 0;
434  shiftZ = 0;
[5967]435}
436
437
438HeightMap::~HeightMap()
439{
[7467]440  delete heightMap;
[7490]441  delete colorMap;
[6956]442
443
[7467]444  for(int i=0;i <    heightMap->h/tileSize ; i++)
445  {
446    for(int j = 0; j < heightMap->w/ tileSize; j++)
447    {
448      delete tiles [i][j];
449    }
450  }
451  for(int i=0;i <    heightMap->h/tileSize ; i++)
452    delete[] tiles[i];
453  delete[] tiles;
[6956]454
[7014]455
456
457
458
[7467]459  for(int i=0;i<heightMap->h;i++)
460    delete[] normalVectorField [i];
461  delete[] normalVectorField;
[6956]462
463
464
465
[7014]466
467
468
[5967]469}
470
[5942]471void HeightMap::load()
472{
[5993]473
[7467]474  //Create a Dynamicly sized 2D-Array for Tiles
475  tiles =  new Tile** [heightMap->h/tileSize];
476  for(int i=0;i <    heightMap->h/tileSize ; i++)
477    tiles [i]= new (Tile* [heightMap->w /tileSize ]);
[5967]478
[7467]479  //SetUp Arrays
480  for(int i = 0; i < (heightMap->)/ tileSize; i ++)
481  {
482    for(int j = 0; j < (heightMap->w )/ tileSize; j ++)
483    {
[7014]484
[7467]485      tiles[i][j] =    new Tile( i*tileSize ,  j*tileSize , (i+1)*tileSize, (j+1)*tileSize , this ) ;
486    }
487  }
[5980]488
[6956]489}
[5993]490
491
[7014]492void HeightMap::draw()
[6956]493{
[7467]494  const PNode* camera = State::getCameraNode();
495  Vector v = camera->getAbsCoor();
[6956]496
[7467]497  int i_min = 0;
498  int i_max = (heightMap->h )/ tileSize;
499  int j_min = 0;
500  int j_max= (heightMap->) / tileSize;
[6956]501
502
503
[7467]504  for(int i = 0; i <  i_max        ; i ++)
505  {
506    for(int j = 0; j < j_max ; j++)
507    {
508      tiles[i][j]->draw();
509    }
510  }
[5980]511
[6956]512}
513void HeightMap::generateNormalVectorField()
514{
[7467]515  int delta = 1;
516  heights  = (unsigned char*) heightMap->pixels;
[6956]517
[7467]518  //Create a Dynamicly sized 2D-Array to store our normals
519  normalVectorField =  new Vector* [heightMap->h];
520  for(int i=0;i<heightMap->h;i++)
521    normalVectorField [i]= new (Vector [heightMap->w]);
[6956]522
523
524
525
[7467]526  // Initialize
527  for(int i=0; i< heightMap->h; i++)
528  {
529    for(int j = 0; j> heightMap->w; j++)
530    {
531      Vector v = Vector(0.0, 1.0, 0.0);
532      normalVectorField[i][j] = v;
533    }
534  }
[5967]535
[7467]536  // !!! Does not yet calculate the normals of some border points!!!!!
[5990]537
[7467]538  if(heightMap != NULL && heightMap->format->BitsPerPixel == 8 )
539  {
540    SDL_LockSurface(heightMap);
541    for(int i = 0 ; i < heightMap->h - 1  ; i ++)
542    {
543      for(int j = 0; j < heightMap->- 1  ;  j ++)
544      {
[5942]545
546
[7467]547        delta = (int)heights[j + (i+1)*(heightMap->w )] -  (int) heights[j + i*(heightMap->w )];
548        Vector a =  Vector(-scaleX,(float)delta*scaleY  ,0.0f);
[5942]549
[7467]550        delta = (int)heights[j+1 + i*(heightMap->w )] - (int)heights[j + i*(heightMap->w )];
551        Vector b =  Vector(0.0f,(float) delta*scaleY ,scaleZ);
[5942]552
[6956]553
[7467]554        normalVectorField[i][j] = b.cross(a);
555        normalVectorField[i][j].normalize();
[5942]556
[7467]557      }
558    }
559    SDL_UnlockSurface(heightMap);
[7014]560
[7467]561  }
[5942]562
[6956]563
564
[7014]565
[5942]566}
[6956]567
568
569void HeightMap::scale(Vector v)
570{
[7467]571  scaleX = v.x;
572  scaleY = v.y;
573  scaleZ = v.z;
574  generateNormalVectorField();
[6956]575}
576
577void HeightMap::setAbsCoor(Vector v)
578{
[7467]579  offsetX = v.x;
580  offsetY = v.y;
581  offsetZ  = v.z;
[6956]582}
583
584
[7014]585float HeightMap::getHeight(float x, float y)
[6956]586{
587
[7467]588  x -= offsetX;
589  y -= offsetZ;
[7014]590
591
[7467]592  int xInt = (int)x / scaleX;
593  x -= (float)((int)x);
594  xInt = heightMap->h - xInt;
595  int yInt = (int)y / scaleZ;
596  y -= (float) ((int) y); /*yInt = heightMap->w - yInt;*/
[7014]597
[7467]598  //PRINTF(0)("xInt: %i, yInt: %i, x: %f, y: %f\n", xInt, yInt, x, y);
[7014]599
[7467]600  if(xInt <= 0 || xInt >= heightMap->h || yInt <= 0 || yInt >= heightMap->)
601    return 0;
602  if( y >= 0.5*x)
603  {
604    // Check for ...
605  }
[6956]606
[7467]607  float height = heights[yInt + (xInt)*heightMap->w]*scaleY;
[7014]608
609
[7467]610  float a = normalVectorField[(xInt)][yInt].x;
611  float b = normalVectorField [(xInt)][yInt].z;
612  float c = normalVectorField [(xInt)][yInt].y;
[7014]613
[7467]614  PRINTF(0)("a: %f \n" ,a);
[6956]615  PRINTF(0)("b: %f \n" ,b);
[7467]616  PRINTF(0)("c: %f \n" ,c);
[7014]617
[7467]618  height -= ( (a/c)*(x) + (b/c)*(y));
[7014]619
[7467]620  PRINTF(0)("height: %f \n" ,height );
621  return (height + offsetZ);
[6956]622}
Note: See TracBrowser for help on using the repository browser.