Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

constructor cleanup, some warning eliminated

  • Property svn:executable set to *
File size: 19.5 KB
Line 
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:
12   main-programmer: bottac@ee.ethz.ch
13
14   review: patrick boenzli, patrick@orxonox.ethz.ch
15*/
16
17#include "height_map.h"
18#include "model.h"
19#include "texture.h"
20#include "vector.h"
21#include "material.h"
22#include "p_node.h"
23#include "state.h"
24#include "util/loading/resource_manager.h"
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
34
35/**
36 * default constructor
37 *  @param i1
38 */
39Tile::Tile(int i1, int j1, int i2, int j2, HeightMap* heightMapReference )
40{
41  PRINTF(4)("Tile Constructor\n");
42
43  this->highResModel = new VertexArrayModel();
44  this->lowResModel  = new VertexArrayModel();
45
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);
52}
53
54
55Tile::~Tile()
56{
57  if( highResModel)
58    delete highResModel;
59  if( lowResModel)
60    delete lowResModel;
61}
62
63
64/**
65 * this darws the tile in diefferent resolutions
66 */
67void Tile::draw()
68{
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 )
73  {
74    this->drawHighRes();
75  }
76  else if( cameraDistance < HM_LOD_LOW_RES)
77  {
78    this->drawLowRes();
79  }
80}
81
82
83/**
84 * loads a tile
85 */
86void Tile::load(int i1, int j1, int i2, int j2, VertexArrayModel* model, int sampleRate)
87{
88
89// #define heightMap this->heightMapReference->heightMap
90#define colors   this->heightMapReference->colors
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
98
99
100  //FIXME: OLD implementation
101  this->x = this->heightMapReference->offsetX + ( this->heightMapReference->heightMap->h - ((i1 + i2) / 2)) * scaleX;
102  this->z = this->heightMapReference->offsetZ + ((j1 + j2 ) / 2 ) * scaleZ;
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);
107
108
109  float height = 0;
110  int offset = 0;
111
112  float r = 0.0;
113  float g = 0.0;
114  float b = 0.0;
115
116
117  //if( this->heightMapReference->heightMap != NULL && this->heightMapReference->heightMap->format->BitsPerPixel == 8 )
118
119  SDL_LockSurface(this->heightMapReference->heightMap);
120  SDL_LockSurface(this->heightMapReference->colorMap);
121
122  for( int i = i1 ; i <= i2  ; i +=sampleRate)
123  {
124    int w = 0;
125
126    // adjust the colors acoring to the color map
127    if( this->heightMapReference->hasColourMap)
128    {
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];
132    }
133
134    w = j1;
135
136    // add a vertex to the list
137    model->addVertex(scaleX*(this->heightMapReference->heightMap->h -i)+ shiftX,shiftY,scaleZ*(w)+ shiftZ); // Top Right
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);
141    model->addTexCoor((float)(j1-sampleRate) /(texRate), (float)(i %this->heightMapReference->heightMap->h)/(texRate));
142    model->addColor(r/255.0f, g/255.0f, b/255.0f);
143
144
145    for(int j = j1; j <= j2; j += sampleRate)
146    {
147      // adjust the colors acording to the color map
148      if( this->heightMapReference->hasColourMap)
149      {
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];
153      }
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;
163
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);
170      model->addTexCoor((float)(j) /(texRate), (float)(i %this->heightMapReference->heightMap->h)/(texRate));
171      model->addColor(r/255.0f, g/255.0f, b/255.0f);
172      //PRINTF(0)("TexCoord:  %f %f \n",(float)j / 100.0, (float)(i %this->heightMapReference->h)/100.0);
173
174      w = j;
175    }
176    model->addVertex(scaleX*(this->heightMapReference->heightMap->h -i)+ shiftX,shiftY,scaleZ*(w)+ shiftZ); // Top Right
177    model->addNormal(normalVectorField[i % this->heightMapReference->heightMap->h][w % this->heightMapReference->heightMap->w].y,
178                     normalVectorField[i % this->heightMapReference->heightMap->h][w % this->heightMapReference->heightMap->w].z,
179                     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));
181    model->addColor(r/255.0f, g/255.0f, b/255.0f);
182  }
183
184  SDL_UnlockSurface(this->heightMapReference->heightMap);
185  int cnt = 0;
186
187  for( int i = i1; i < i2; i +=sampleRate)
188  {
189    for( int j = j1-sampleRate; j < j2  + 2 * sampleRate; j += sampleRate)
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;
198
199  for( int j = j1; j <= j2; j += sampleRate)
200  {
201    int i = i1;
202
203    // To be fixed
204    if(this->heightMapReference->hasColourMap)
205    {
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];
209    }
210
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);
217    model->addTexCoor((float)j /(texRate), (float)((i - sampleRate) %this->heightMapReference->heightMap->h)/(texRate));
218    model->addColor(r/255.0f, g/255.0f, b/255.0f);
219  }
220
221
222  for(int j = j1  ; j <= j2    ;  j += sampleRate)
223  {
224    int i = i1;
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) *
227                                                                      this->heightMapReference->heightMap->w];
228    height += (float)(unsigned char)this->heightMapReference->heights[j - 1 + sampleRate + (i + 1) *
229                                                                      this->heightMapReference->heightMap->w];
230    height += (float)(unsigned char)this->heightMapReference->heights[j + sampleRate + (i + 2) *
231                                                                      this->heightMapReference->heightMap->w];
232    height += (float)(unsigned char)this->heightMapReference->heights[j + sampleRate + i * this->heightMapReference->heightMap->w];
233    height=height/5.0;
234
235    model->addVertex(scaleX*(this->heightMapReference->heightMap->h -i) + shiftX ,
236                     ((double)(height)*scaleY) +shiftY ,
237                     scaleZ*(j) + shiftZ); // Top Right
238    model->addNormal(normalVectorField[i % this->heightMapReference->heightMap->h][j % this->heightMapReference->heightMap->w].y,
239                     normalVectorField[i % this->heightMapReference->heightMap->h][j% this->heightMapReference->heightMap->w].z,
240                     normalVectorField[i%this->heightMapReference->heightMap->h][j%this->heightMapReference->heightMap->w].x);
241    model->addTexCoor((float)j /(texRate), (float)(i %this->heightMapReference->heightMap->h)/(texRate));
242    model->addColor(r/255.0f, g/255.0f, b/255.0f);
243  }
244
245
246  for(int j = j1; j <= j2; j += sampleRate)
247  {
248    int i = i2;
249    // To be fixed
250    if( this->heightMapReference->hasColourMap)
251    {
252      r = (float)colors[3 * j + 2 + 3 * i * this->heightMapReference->heightMap->w];
253      g = (float)colors[3 * j + 1 + 3 * i * this->heightMapReference->heightMap->w];
254      b = (float)colors[3 * j + 0 + 3 * i * this->heightMapReference->heightMap->w];
255    }
256
257    model->addVertex(scaleX*(this->heightMapReference->heightMap->h -i) + shiftX ,
258                     shiftY ,
259                     scaleZ*(j) + shiftZ); // Top Right
260    model->addNormal(normalVectorField[i%this->heightMapReference->heightMap->h][j%this->heightMapReference->heightMap->w].y,
261                     normalVectorField[i%this->heightMapReference->heightMap->h][j%this->heightMapReference->heightMap->w].z,
262                     normalVectorField[i%this->heightMapReference->heightMap->h][j%this->heightMapReference->heightMap->w].x);
263    model->addTexCoor((float)j /(texRate), (float)((i+ sampleRate) %this->heightMapReference->heightMap->h)/(texRate));
264    model->addColor(r/255.0f, g/255.0f, b/255.0f);
265  }
266
267
268  for(int j = j1; j <= j2; j += sampleRate)
269  {
270    int i = i2;
271    height =  (float)(unsigned char)this->heightMapReference->heights[j + sampleRate + i * this->heightMapReference->heightMap->w];
272    height += (float)(unsigned char)this->heightMapReference->heights[j + 1 + sampleRate + (i + 1)
273                                                                      * this->heightMapReference->heightMap->w];
274    height += (float)(unsigned char)this->heightMapReference->heights[j - 1 + sampleRate + (i + 1) *
275                                                                      this->heightMapReference->heightMap->w];
276    height += (float)(unsigned char)this->heightMapReference->heights[j + sampleRate + (i + 2) *
277                                                                      this->heightMapReference->heightMap->w];
278    height +=  (float)(unsigned char)this->heightMapReference->heights[j + sampleRate + i * this->heightMapReference->heightMap->w];
279    height /= 5.0;
280    model->addVertex(scaleX*(this->heightMapReference->heightMap->h -i) + shiftX ,
281                     ((double)(height)*scaleY) +shiftY ,
282                     scaleZ*(j) + shiftZ); // Top Right
283    model->addNormal(normalVectorField[i%this->heightMapReference->heightMap->h][j%this->heightMapReference->heightMap->w].y,
284                     normalVectorField[i%this->heightMapReference->heightMap->h][j%this->heightMapReference->heightMap->w].z,
285                     normalVectorField[i%this->heightMapReference->heightMap->h][j%this->heightMapReference->heightMap->w].x);
286    model->addTexCoor((float)j /(texRate), (float)(i %this->heightMapReference->heightMap->h)/(texRate));
287    model->addColor(r/255.0f, g/255.0f, b/255.0f);
288  }
289
290  // link Boarder Stripe
291  for(int j = j1 - sampleRate; j < j2; j += sampleRate)
292  {
293    model->addIndice(cnt);
294    model->addIndice(cnt + (j2 - j1 + sampleRate) / sampleRate );
295    cnt++;
296  }
297  cnt++;
298
299
300  model->newStripe();
301
302
303  cnt += (j2-j1)/ sampleRate;
304  // link 2nd BoarderStripe
305  for(int j = j1-sampleRate; j < j2;  j += sampleRate)
306  {
307    model->addIndice(cnt);
308    model->addIndice(cnt + (j2 - j1 + sampleRate) / sampleRate);
309    cnt++;
310  }
311
312  SDL_UnlockSurface(this->heightMapReference->colorMap);
313
314  model->finalize();
315
316// #undef heightMap
317        #undef colors
318        #undef scaleX
319        #undef scaleY
320        #undef scaleZ
321        #undef shiftX
322        #undef shiftY
323        #undef shiftZ
324        #undef normalVectorField
325}
326
327
328
329
330
331HeightMap::HeightMap(const std::string& heightMapName)
332    : VertexArrayModel()
333{
334  this->init(heightMapName);
335
336  this->colorMap = NULL;
337}
338
339HeightMap::HeightMap(const std::string& heightMapName, const std::string& colorMapName)
340    : VertexArrayModel()
341{
342
343  this->init(heightMapName);
344
345  this->colorMap = IMG_Load(colorMapName.c_str());
346  if( this->colorMap != NULL)
347  {
348    PRINTF(0)("loading Image %s\n", colorMapName.c_str());
349    PRINTF(0)("width : %i\n", this->colorMap->w);
350    PRINTF(0)("height : %i\n", this->colorMap->h);
351    PRINTF(0)("%i Byte(s) per Pixel \n", this->colorMap->format->BytesPerPixel);
352    PRINTF(0)("Rshift : %i\n", this->colorMap->format->Rshift);
353    PRINTF(0)("Bshift: %i\n", this->colorMap->format->Bshift);
354    PRINTF(0)("Gshift: %i\n", this->colorMap->format->Gshift);
355    PRINTF(0)("Rmask: %i\n", this->colorMap->format->Rmask);
356    PRINTF(0)("Gmask: %i\n", this->colorMap->format->Gmask);
357  }
358  else
359    PRINTF(0)("oops! couldn't load colorMap for some reason.\n");
360
361
362
363  if(colorMap != NULL)
364  {
365    colors = (unsigned char *) colorMap->pixels;
366    hasColourMap = true;
367  }
368  else
369    hasColourMap = false;
370
371
372  heights  = (unsigned char*) heightMap->pixels;
373  shiftX = 0;
374  shiftY = 0;
375  shiftZ = 0;
376}
377
378
379HeightMap::~HeightMap()
380{
381  delete heightMap;
382  delete colorMap;
383
384  for(int i=0;i <    heightMap->h/tileSize ; i++)
385  {
386    for(int j = 0; j < heightMap->w/ tileSize; j++)
387    {
388      delete tiles [i][j];
389    }
390  }
391  for(int i=0;i <    heightMap->h/tileSize ; i++)
392    delete[] tiles[i];
393  delete[] tiles;
394
395  for(int i=0;i<heightMap->h;i++)
396    delete[] normalVectorField [i];
397  delete[] normalVectorField;
398}
399
400
401
402void HeightMap::init(const std::string& heightMapName)
403{
404  this->setClassID(CL_HEIGHT_MAP, "HeightMap");
405
406  this->shiftX = 0;
407  this->shiftY = 0;
408  this->shiftZ = 0;
409
410  heightMap =  IMG_Load(heightMapName.c_str());
411  if( heightMap != NULL)
412  {
413    /*
414    WHAT about following checks:
415    - image size (rectangular?)
416    - image file type (bw?)
417    */
418    PRINTF(1)("loading Image %s\n", heightMapName.c_str());
419    PRINTF(1)("width : %i\n", heightMap->w);
420    PRINTF(1)("height : %i\n", heightMap->h);
421    PRINTF(1)("%i Byte(s) per Pixel \n", heightMap->format->BytesPerPixel);
422    PRINTF(1)("Rshift : %i\n", heightMap->format->Rshift);
423    PRINTF(1)("Bshift: %i\n", heightMap->format->Bshift);
424    PRINTF(1)("Gshift: %i\n", heightMap->format->Gshift);
425    PRINTF(1)("Rmask: %i\n", heightMap->format->Rmask);
426    PRINTF(1)("Gmask: %i\n", heightMap->format->Gmask);
427  }
428  else
429    PRINTF(1)("oops! couldn't load %s for some reason.\n", heightMapName.c_str());
430
431  generateNormalVectorField();
432}
433
434
435void HeightMap::load()
436{
437
438  //Create a Dynamicly sized 2D-Array for Tiles
439  tiles =  new Tile** [heightMap->h/tileSize];
440  for(int i=0;i <    heightMap->h/tileSize ; i++)
441    tiles [i]= new (Tile* [heightMap->w /tileSize ]);
442
443  //SetUp Arrays
444  for(int i = 0; i < (heightMap->)/ tileSize; i ++)
445  {
446    for(int j = 0; j < (heightMap->w )/ tileSize; j ++)
447    {
448
449      tiles[i][j] =    new Tile( i*tileSize ,  j*tileSize , (i+1)*tileSize, (j+1)*tileSize , this ) ;
450    }
451  }
452
453}
454
455
456void HeightMap::draw()
457{
458  const PNode* camera = State::getCameraNode();
459  Vector v = camera->getAbsCoor();
460
461  int i_min = 0;
462  int i_max = (heightMap->h )/ tileSize;
463  int j_min = 0;
464  int j_max= (heightMap->) / tileSize;
465
466
467
468  for(int i = 0; i <  i_max        ; i ++)
469  {
470    for(int j = 0; j < j_max ; j++)
471    {
472      tiles[i][j]->draw();
473    }
474  }
475
476}
477void HeightMap::generateNormalVectorField()
478{
479  int delta = 1;
480  heights  = (unsigned char*) heightMap->pixels;
481
482  //Create a Dynamicly sized 2D-Array to store our normals
483  normalVectorField =  new Vector* [heightMap->h];
484  for(int i=0;i<heightMap->h;i++)
485    normalVectorField [i]= new (Vector [heightMap->w]);
486
487
488
489
490  // Initialize
491  for(int i=0; i< heightMap->h; i++)
492  {
493    for(int j = 0; j> heightMap->w; j++)
494    {
495      Vector v = Vector(0.0, 1.0, 0.0);
496      normalVectorField[i][j] = v;
497    }
498  }
499
500  // !!! Does not yet calculate the normals of some border points!!!!!
501
502  if(heightMap != NULL && heightMap->format->BitsPerPixel == 8 )
503  {
504    SDL_LockSurface(heightMap);
505    for(int i = 0 ; i < heightMap->h - 1  ; i ++)
506    {
507      for(int j = 0; j < heightMap->- 1  ;  j ++)
508      {
509
510
511        delta = (int)heights[j + (i+1)*(heightMap->w )] -  (int) heights[j + i*(heightMap->w )];
512        Vector a =  Vector(-scaleX,(float)delta*scaleY  ,0.0f);
513
514        delta = (int)heights[j+1 + i*(heightMap->w )] - (int)heights[j + i*(heightMap->w )];
515        Vector b =  Vector(0.0f,(float) delta*scaleY ,scaleZ);
516
517
518        normalVectorField[i][j] = b.cross(a);
519        normalVectorField[i][j].normalize();
520
521      }
522    }
523    SDL_UnlockSurface(heightMap);
524
525  }
526
527
528
529
530}
531
532
533void HeightMap::scale(Vector v)
534{
535  scaleX = v.x;
536  scaleY = v.y;
537  scaleZ = v.z;
538  generateNormalVectorField();
539}
540
541void HeightMap::setAbsCoor(Vector v)
542{
543  offsetX = v.x;
544  offsetY = v.y;
545  offsetZ  = v.z;
546}
547
548
549float HeightMap::getHeight(float x, float y)
550{
551
552  x -= offsetX;
553  y -= offsetZ;
554
555
556  int xInt = (int)( x / scaleX);
557  x -= (float)((int)x);
558  xInt = heightMap->h - xInt;
559  int yInt = (int)( y / scaleZ);
560  y -= (float) ((int) y); /*yInt = heightMap->w - yInt;*/
561
562  //PRINTF(0)("xInt: %i, yInt: %i, x: %f, y: %f\n", xInt, yInt, x, y);
563
564  if(xInt <= 0 || xInt >= heightMap->h || yInt <= 0 || yInt >= heightMap->)
565    return 0;
566  if( y >= 0.5*x)
567  {
568    // Check for ...
569  }
570
571  float height = heights[yInt + (xInt)*heightMap->w]*scaleY;
572
573
574  float a = normalVectorField[(xInt)][yInt].x;
575  float b = normalVectorField [(xInt)][yInt].z;
576  float c = normalVectorField [(xInt)][yInt].y;
577
578  PRINTF(0)("a: %f \n" ,a);
579  PRINTF(0)("b: %f \n" ,b);
580  PRINTF(0)("c: %f \n" ,c);
581
582  height -= ( (a/c)*(x) + (b/c)*(y));
583
584  PRINTF(0)("height: %f \n" ,height );
585  return (height + offsetZ);
586}
Note: See TracBrowser for help on using the repository browser.