Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

resuming work on the hm

  • Property svn:executable set to *
File size: 19.3 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    {
[7490]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));
[7491]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      {
[7491]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
[7469]164      model->addVertex(scaleX*(this->heightMapReference->heightMap->h -i) + shiftX ,((double)(height)*scaleY) + shiftY ,scaleZ*(j) + shiftZ); // Top Right
165      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);
166      model->addTexCoor((float)(j) /(texRate), (float)(i %this->heightMapReference->heightMap->h)/(texRate));
[7014]167
[7469]168      //PRINTF(0)("TexCoord:  %f %f \n",(float)j / 100.0, (float)(i %this->heightMapReference->h)/100.0);
[7014]169
[7467]170      model->addColor(r/255.0,g/255.0,b/255.0);
171      w = j;
172    }
[7014]173
174
[7469]175    model->addVertex(scaleX*(this->heightMapReference->heightMap->h -i)+ shiftX,shiftY,scaleZ*(w)+ shiftZ); // Top Right
176    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);
177    model->addTexCoor((float)(j2+sampleRate) /(texRate), (float)(i %this->heightMapReference->heightMap->h)/(texRate));
[7467]178    model->addColor(r/255.0,g/255.0,b/255.0);
[7014]179
[7467]180  }
[7014]181
182
183
184
185
[7469]186  SDL_UnlockSurface(this->heightMapReference->heightMap);
[7467]187  int cnt = 0;
188  for(int i = i1   ; i < i2  ; i +=sampleRate)
189  {
[7014]190
[7467]191    for(int j = j1-sampleRate  ; j < j2  + 2*sampleRate  ;  j += sampleRate)
192    {
[7014]193
[7467]194      model->addIndice(cnt);
195      model->addIndice(cnt  + (j2 -j1 + 3* sampleRate  )/ sampleRate );
196      cnt++;
[7014]197
[7467]198    }
[7014]199
200
201
[7467]202    model->newStripe();
[7014]203
204
[7467]205  }
206  cnt += (j2 -j1 + 3* sampleRate)/ sampleRate;
[7014]207
[7467]208  for(int j = j1 ; j <= j2    ;  j += sampleRate)
209  {
210    int i = i1;
[7014]211
[7467]212    // To be fixed
[7469]213    if(this->heightMapReference->hasColourMap)
[7467]214    {
[7490]215      r = (float)colors[(3*j+2 + 3*i*(this->heightMapReference->heightMap->w )) ];
216      g = (float)colors[(3*j+1 + 3*i*(this->heightMapReference->heightMap->w)) ];
217      b = (float)colors[(3*j+0 + 3*i*(this->heightMapReference->heightMap->w))];
[7467]218    }
[7014]219
[7469]220    model->addVertex(scaleX*(this->heightMapReference->heightMap->h -i) + shiftX , shiftY ,scaleZ*(j) + shiftZ); // Top Right
221    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);
222    model->addTexCoor((float)j /(texRate), (float)((i - sampleRate) %this->heightMapReference->heightMap->h)/(texRate));
[7467]223    model->addColor(r/255.0,g/255.0,b/255.0);
[7014]224
[7467]225  }
[7014]226
[7467]227  for(int j = j1  ; j <= j2    ;  j += sampleRate)
228  {
229    int i = i1;
[7469]230    height = (float)(unsigned char) this->heightMapReference->heights[(j +sampleRate+ i*(this->heightMapReference->heightMap->w )) ];
231    height += (float)(unsigned char) this->heightMapReference->heights[(j+ 1 + sampleRate + (i+1)*(this->heightMapReference->heightMap->w )) ];
232    height +=  (float) (unsigned char) this->heightMapReference->heights[(j -1+ sampleRate   + (i+1)*(this->heightMapReference->heightMap->w ))];
233    height +=  (float)(unsigned char)this->heightMapReference->heights[(j +sampleRate+ (i+2)*(this->heightMapReference->heightMap->w )) ];
234    height +=  (float)(unsigned char)this->heightMapReference->heights[(j+sampleRate + (i)*(this->heightMapReference->heightMap->w )) ];
[7467]235    height=height/5.0;
[7014]236
[7469]237    model->addVertex(scaleX*(this->heightMapReference->heightMap->h -i) + shiftX , ((double)(height)*scaleY) +shiftY ,scaleZ*(j) + shiftZ); // Top Right
238    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);
239    model->addTexCoor((float)j /(texRate), (float)(i %this->heightMapReference->heightMap->h)/(texRate));
[7467]240    model->addColor(r/255.0,g/255.0,b/255.0);
[7014]241
[7467]242  }
[7014]243
244
245
[7467]246  for(int j = j1  ; j <= j2    ;  j += sampleRate)
247  {
248    int i = i2;
[7014]249
[7467]250    // To be fixed
[7469]251    if(this->heightMapReference->hasColourMap)
[7467]252    {
[7490]253      r = (float)colors[3*j+2 + 3*i*(this->heightMapReference->heightMap->w )];
254      g = (float)colors[3*j+1 + 3*i*(this->heightMapReference->heightMap->w)];
255      b = (float)colors[3*j+0 + 3*i*(this->heightMapReference->heightMap->w)];
[7467]256    }
[7014]257
[7469]258    model->addVertex(scaleX*(this->heightMapReference->heightMap->h -i) + shiftX , shiftY ,scaleZ*(j) + shiftZ); // Top Right
259    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);
260    model->addTexCoor((float)j /(texRate), (float)((i+ sampleRate) %this->heightMapReference->heightMap->h)/(texRate));
[7467]261    model->addColor(r/255.0,g/255.0,b/255.0);
[7014]262
[7467]263  }
[7014]264
265
[7467]266  for(int j = j1 ; j <= j2    ;  j += sampleRate)
267  {
268    int i = i2;
[7469]269    height = (float)(unsigned char) this->heightMapReference->heights[(j +sampleRate+ i*(this->heightMapReference->heightMap->w )) ];
270    height += (float)(unsigned char) this->heightMapReference->heights[(j+ 1 + sampleRate + (i+1)*(this->heightMapReference->heightMap->w )) ];
271    height +=  (float) (unsigned char) this->heightMapReference->heights[(j -1+ sampleRate   + (i+1)*(this->heightMapReference->heightMap->w ))];
272    height +=  (float)(unsigned char)this->heightMapReference->heights[(j +sampleRate+ (i+2)*(this->heightMapReference->heightMap->w ))];
273    height +=  (float)(unsigned char)this->heightMapReference->heights[(j+sampleRate + (i)*(this->heightMapReference->heightMap->w )) ];
[7467]274    height=height/5.0;
[7469]275    model->addVertex(scaleX*(this->heightMapReference->heightMap->h -i) + shiftX , ((double)(height)*scaleY) +shiftY ,scaleZ*(j) + shiftZ); // Top Right
276    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);
277    model->addTexCoor((float)j /(texRate), (float)(i %this->heightMapReference->heightMap->h)/(texRate));
[7467]278    model->addColor(r/255.0,g/255.0,b/255.0);
[7014]279
[7467]280  }
[7014]281
282
283
284
[7467]285  // link Boarder Stripe
286  for(int j = j1-sampleRate  ; j < j2    ;  j += sampleRate)
287  {
[7014]288
[7467]289    model->addIndice(cnt);
290    model->addIndice(cnt  + (j2 -j1 +  sampleRate  )/ sampleRate );
291    cnt++;
[7014]292
[7467]293  }
[7014]294
[7467]295  cnt++;
[7014]296
[7467]297  model->newStripe();
[7014]298
299
300
301
302
[7467]303  cnt += (j2-j1)/ sampleRate;
[7014]304
[7467]305  // link 2nd BoarderStripe
306  for(int j = j1-sampleRate  ; j < j2    ;  j += sampleRate)
307  {
[7014]308
[7467]309    model->addIndice(cnt);
310    model->addIndice(cnt  + (j2 -j1 +  sampleRate  )/ sampleRate );
311    cnt++;
[7014]312
[7467]313  }
[7014]314
315
[7490]316  SDL_UnlockSurface(this->heightMapReference->colorMap);
[7014]317
[7467]318  model->finalize();
[7014]319
320
321
322
[7469]323// #undef heightMap
[7490]324        #undef colors
[7014]325        #undef scaleX
326        #undef scaleY
327        #undef scaleZ
328        #undef shiftX
329        #undef shiftY
330        #undef shiftZ
331        #undef normalVectorField
332
[5942]333}
334
[7469]335
[6956]336HeightMap::HeightMap()
[5967]337{
[6956]338}
339
[7469]340
[7221]341HeightMap::HeightMap(const std::string& height_map_name = "")
[7467]342    : VertexArrayModel()
[6956]343{
[7467]344  this->setClassID(CL_HEIGHT_MAP, "HeightMap");
345  heightMap =  IMG_Load(height_map_name.c_str());
346  if(heightMap!=NULL)
347  {
[7491]348    /*
349        WHAT about following checks:
350    - image size (rectangular?)
351    - image file type (bw?)
[6956]352
[7491]353    */
354
[7467]355    PRINTF(0)("loading Image %s\n", height_map_name.c_str());
356    PRINTF(0)("width : %i\n", heightMap->w);
357    PRINTF(0)("height : %i\n", heightMap->h);
358    PRINTF(0)("%i Byte(s) per Pixel \n", heightMap->format->BytesPerPixel);
359    PRINTF(0)("Rshift : %i\n", heightMap->format->Rshift);
360    PRINTF(0)("Bshift: %i\n", heightMap->format->Bshift);
361    PRINTF(0)("Gshift: %i\n", heightMap->format->Gshift);
362    PRINTF(0)("Rmask: %i\n", heightMap->format->Rmask);
363    PRINTF(0)("Gmask: %i\n", heightMap->format->Gmask);
364  }
[6956]365
[7467]366  else
367    PRINTF(4)("oops! couldn't load %s for some reason.\n", height_map_name.c_str());
[6956]368
[7014]369
[6956]370  generateNormalVectorField();
371
372  shiftX = 0;
373  shiftY = 0;
374  shiftZ = 0;
375
376}
377
[7490]378HeightMap::HeightMap(const std::string& height_map_name = NULL, const std::string& color_map_name = NULL)
[7467]379    : VertexArrayModel()
[6956]380{
[7467]381  this->setClassID(CL_HEIGHT_MAP, "HeightMap");
[6956]382
[7467]383  heightMap =  IMG_Load(height_map_name.c_str());
384  if(heightMap!=NULL)
385  {
[6956]386
[7467]387    PRINTF(0)("loading Image %s\n", height_map_name.c_str());
388    PRINTF(0)("width : %i\n", heightMap->w);
389    PRINTF(0)("height : %i\n", heightMap->h);
390    PRINTF(0)("%i Byte(s) per Pixel \n", heightMap->format->BytesPerPixel);
391    PRINTF(0)("Rshift : %i\n", heightMap->format->Rshift);
392    PRINTF(0)("Bshift: %i\n", heightMap->format->Bshift);
393    PRINTF(0)("Gshift: %i\n", heightMap->format->Gshift);
394    PRINTF(0)("Rmask: %i\n", heightMap->format->Rmask);
395    PRINTF(0)("Gmask: %i\n", heightMap->format->Gmask);
396  }
[6956]397
[7467]398  else
399    PRINTF(4)("oops! couldn't load %s for some reason.\n", height_map_name.c_str());
[6956]400
401
[7467]402  generateNormalVectorField();
[6956]403
[7490]404  colorMap=NULL;
405  if(color_map_name != "")
[6956]406  {
[7490]407    colorMap = IMG_Load(color_map_name.c_str());
[6956]408  }
[7014]409
[7490]410  if(colorMap != NULL)
[7467]411  {
[7490]412    PRINTF(0)("loading Image %s\n", color_map_name.c_str());
413    PRINTF(0)("width : %i\n", colorMap->w);
414    PRINTF(0)("height : %i\n", colorMap->h);
415    PRINTF(0)("%i Byte(s) per Pixel \n", colorMap->format->BytesPerPixel);
416    PRINTF(0)("Rshift : %i\n", colorMap->format->Rshift);
417    PRINTF(0)("Bshift: %i\n", colorMap->format->Bshift);
418    PRINTF(0)("Gshift: %i\n", colorMap->format->Gshift);
419    PRINTF(0)("Rmask: %i\n", colorMap->format->Rmask);
420    PRINTF(0)("Gmask: %i\n", colorMap->format->Gmask);
[7467]421  }
422  else
423    PRINTF(0)("oops! couldn't load colorMap for some reason.\n");
[6956]424
[7014]425
426
[7490]427  if(colorMap != NULL)
[6956]428  {
[7490]429    colors = (unsigned char *) colorMap->pixels;
[7467]430    hasColourMap = true;
[6956]431  }
[7467]432  else
433    hasColourMap = false;
[7014]434
435
[6956]436  heights  = (unsigned char*) heightMap->pixels;
437  shiftX = 0;
438  shiftY = 0;
439  shiftZ = 0;
[5967]440}
441
442
443HeightMap::~HeightMap()
444{
[7467]445  delete heightMap;
[7490]446  delete colorMap;
[6956]447
448
[7467]449  for(int i=0;i <    heightMap->h/tileSize ; i++)
450  {
451    for(int j = 0; j < heightMap->w/ tileSize; j++)
452    {
453      delete tiles [i][j];
454    }
455  }
456  for(int i=0;i <    heightMap->h/tileSize ; i++)
457    delete[] tiles[i];
458  delete[] tiles;
[6956]459
[7014]460
461
462
463
[7467]464  for(int i=0;i<heightMap->h;i++)
465    delete[] normalVectorField [i];
466  delete[] normalVectorField;
[6956]467
468
469
470
[7014]471
472
473
[5967]474}
475
[5942]476void HeightMap::load()
477{
[5993]478
[7467]479  //Create a Dynamicly sized 2D-Array for Tiles
480  tiles =  new Tile** [heightMap->h/tileSize];
481  for(int i=0;i <    heightMap->h/tileSize ; i++)
482    tiles [i]= new (Tile* [heightMap->w /tileSize ]);
[5967]483
[7467]484  //SetUp Arrays
485  for(int i = 0; i < (heightMap->)/ tileSize; i ++)
486  {
487    for(int j = 0; j < (heightMap->w )/ tileSize; j ++)
488    {
[7014]489
[7467]490      tiles[i][j] =    new Tile( i*tileSize ,  j*tileSize , (i+1)*tileSize, (j+1)*tileSize , this ) ;
491    }
492  }
[5980]493
[6956]494}
[5993]495
496
[7014]497void HeightMap::draw()
[6956]498{
[7467]499  const PNode* camera = State::getCameraNode();
500  Vector v = camera->getAbsCoor();
[6956]501
[7467]502  int i_min = 0;
503  int i_max = (heightMap->h )/ tileSize;
504  int j_min = 0;
505  int j_max= (heightMap->) / tileSize;
[6956]506
507
508
[7467]509  for(int i = 0; i <  i_max        ; i ++)
510  {
511    for(int j = 0; j < j_max ; j++)
512    {
513      tiles[i][j]->draw();
514    }
515  }
[5980]516
[6956]517}
518void HeightMap::generateNormalVectorField()
519{
[7467]520  int delta = 1;
521  heights  = (unsigned char*) heightMap->pixels;
[6956]522
[7467]523  //Create a Dynamicly sized 2D-Array to store our normals
524  normalVectorField =  new Vector* [heightMap->h];
525  for(int i=0;i<heightMap->h;i++)
526    normalVectorField [i]= new (Vector [heightMap->w]);
[6956]527
528
529
530
[7467]531  // Initialize
532  for(int i=0; i< heightMap->h; i++)
533  {
534    for(int j = 0; j> heightMap->w; j++)
535    {
536      Vector v = Vector(0.0, 1.0, 0.0);
537      normalVectorField[i][j] = v;
538    }
539  }
[5967]540
[7467]541  // !!! Does not yet calculate the normals of some border points!!!!!
[5990]542
[7467]543  if(heightMap != NULL && heightMap->format->BitsPerPixel == 8 )
544  {
545    SDL_LockSurface(heightMap);
546    for(int i = 0 ; i < heightMap->h - 1  ; i ++)
547    {
548      for(int j = 0; j < heightMap->- 1  ;  j ++)
549      {
[5942]550
551
[7467]552        delta = (int)heights[j + (i+1)*(heightMap->w )] -  (int) heights[j + i*(heightMap->w )];
553        Vector a =  Vector(-scaleX,(float)delta*scaleY  ,0.0f);
[5942]554
[7467]555        delta = (int)heights[j+1 + i*(heightMap->w )] - (int)heights[j + i*(heightMap->w )];
556        Vector b =  Vector(0.0f,(float) delta*scaleY ,scaleZ);
[5942]557
[6956]558
[7467]559        normalVectorField[i][j] = b.cross(a);
560        normalVectorField[i][j].normalize();
[5942]561
[7467]562      }
563    }
564    SDL_UnlockSurface(heightMap);
[7014]565
[7467]566  }
[5942]567
[6956]568
569
[7014]570
[5942]571}
[6956]572
573
574void HeightMap::scale(Vector v)
575{
[7467]576  scaleX = v.x;
577  scaleY = v.y;
578  scaleZ = v.z;
579  generateNormalVectorField();
[6956]580}
581
582void HeightMap::setAbsCoor(Vector v)
583{
[7467]584  offsetX = v.x;
585  offsetY = v.y;
586  offsetZ  = v.z;
[6956]587}
588
589
[7014]590float HeightMap::getHeight(float x, float y)
[6956]591{
592
[7467]593  x -= offsetX;
594  y -= offsetZ;
[7014]595
596
[7467]597  int xInt = (int)x / scaleX;
598  x -= (float)((int)x);
599  xInt = heightMap->h - xInt;
600  int yInt = (int)y / scaleZ;
601  y -= (float) ((int) y); /*yInt = heightMap->w - yInt;*/
[7014]602
[7467]603  //PRINTF(0)("xInt: %i, yInt: %i, x: %f, y: %f\n", xInt, yInt, x, y);
[7014]604
[7467]605  if(xInt <= 0 || xInt >= heightMap->h || yInt <= 0 || yInt >= heightMap->)
606    return 0;
607  if( y >= 0.5*x)
608  {
609    // Check for ...
610  }
[6956]611
[7467]612  float height = heights[yInt + (xInt)*heightMap->w]*scaleY;
[7014]613
614
[7467]615  float a = normalVectorField[(xInt)][yInt].x;
616  float b = normalVectorField [(xInt)][yInt].z;
617  float c = normalVectorField [(xInt)][yInt].y;
[7014]618
[7467]619  PRINTF(0)("a: %f \n" ,a);
[6956]620  PRINTF(0)("b: %f \n" ,b);
[7467]621  PRINTF(0)("c: %f \n" ,c);
[7014]622
[7467]623  height -= ( (a/c)*(x) + (b/c)*(y));
[7014]624
[7467]625  PRINTF(0)("height: %f \n" ,height );
626  return (height + offsetZ);
[6956]627}
Note: See TracBrowser for help on using the repository browser.