Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/height_map_merge/src/lib/graphics/importer/height_map.cc @ 6730

Last change on this file since 6730 was 6730, checked in by bottac, 18 years ago
  • Property svn:executable set to *
File size: 14.0 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:
[6494]12   main-programmer: bottac@ee.ethz.ch
[5942]13*/
14
15#include "height_map.h"
16#include "model.h"
[5967]17#include "texture.h"
18#include "vector.h"
[6494]19#include "material.h"
20#include "p_node.h"
21#include "state.h"
22#include "resource_manager.h"
[5967]23#include "debug.h"
24
25// INCLUDING SDL_Image
26#ifdef HAVE_SDL_IMAGE_H
27#include <SDL_image.h>
28#else
29#include <SDL/SDL_image.h>
30#endif
31
[6579]32Tile::Tile(int i1, int j1, int i2, int j2, HeightMap* hm ) 
[5942]33{
[6494]34
35PRINTF(0)("Tile Constructor\n");
36highResModel = new VertexArrayModel();
37lowResModel  = new VertexArrayModel();
38
[6727]39this->load(i1,j1,i2,j2,hm,highResModel,4);
[6579]40this->load(i1,j1,i2,j2,hm,lowResModel, 8);
[5942]41}
42
[6494]43Tile::Tile()
[5967]44{
[6494]45PRINTF(0)("ooops *********************************************************************************************************************************************************************************************************** \n");
46}
47
48Tile::~Tile()
49{
50 delete highResModel;
51 delete lowResModel;
52}
53
54void Tile::draw()
55{
[6603]56         // This Draws the LOD's
57                                                                //FIXME
58    float cameraDistance = (State::getCamera()->getAbsCoor() - Vector(0.0,100.0,0.0)).len();
[6727]59    if (cameraDistance > 10000 )
[6603]60    {
61   
62    }
[6727]63    else if (cameraDistance >= 50000 )
[6603]64    {
65      this->drawLowRes();
66    }
67    else 
68    {
69      this->drawHighRes();
70    }
[6494]71}
72
73void Tile::drawHighRes()
74{
75 highResModel->draw();
76}
77
78void Tile::drawLowRes()
79{
80 lowResModel->draw();
81}
82
83/*
84*/
85void Tile::load(int i1, int j1, int i2, int j2, HeightMap* hm, VertexArrayModel* model, int Res)
86{
[6579]87       
88        #define heightMap hm->heightMap
89        #define colours   hm->colours
90        #define scaleX hm->scaleX
91        #define scaleY hm->scaleY
92        #define scaleZ hm->scaleZ
93        #define shiftX hm->shiftX
94        #define shiftY hm->shiftY
95        #define shiftZ hm->shiftZ
96        #define normalVectorField hm->normalVectorField
97       
98        int sampleRate = Res;
99       
[6727]100        float height = 0;
[6579]101        int offset = 0;
102       
103        float r = 0.0;
104        float g = 0.0;
105        float b = 0.0;
[6494]106
[6579]107 
[6494]108PRINTF(0)("loadin * \n");
[6579]109 
[6494]110 if(heightMap != NULL && heightMap->format->BitsPerPixel == 8 )
[6579]111       
112        SDL_LockSurface(heightMap);
[6494]113
[6579]114        for(int i = i1 ; i <= i2  ; i +=sampleRate)
115        {
116                int w = 0;
[6494]117
[6579]118               
119               
120                 if(hm->hasColourMap)
121                 {
122                 r = (float)colours[3*w+2 + 3*i*(heightMap->w )];
123                 g = (float)colours[3*w+1 + 3*i*(heightMap->w)];
124                 b = (float)colours[3*w+0 + 3*i*(heightMap->w)];
125                 }
126               
127                w = j1;
128                model->addVertex(scaleX*(heightMap->h -i)+ shiftX,shiftY,scaleZ*(w)+ shiftZ); // Top Right
129                model->addNormal(normalVectorField[i][w].y,normalVectorField[i][w].z,normalVectorField[i][w].x);
[6727]130                model->addTexCoor((float)(j1-sampleRate) /(texRate), (float)(i %heightMap->h)/(texRate));
[6730]131                PRINTF(0)("shiftX %f,shiftY %f,shiftZ %f\n", shiftX, shiftY ,shiftZ);
[6579]132                model->addColor(r/255.0,g/255.0,b/255.0);
133               
134            for(int j = j1  ; j <= j2    ;  j += sampleRate)
135            {
136           
137           
138                 // To be fixed
139                 if(hm->hasColourMap)
140                 {
141                 r = (float)colours[3*j+2 + 3*i*(heightMap->w )];
142                 g = (float)colours[3*j+1 + 3*i*(heightMap->w)];
143                 b = (float)colours[3*j+0 + 3*i*(heightMap->w)];
144                 }
145                height = (float)(unsigned char) hm->heights[j +sampleRate+ i*(heightMap->w )];
146                height += (float)(unsigned char) hm->heights[j+ 1 + sampleRate + (i+1)*(heightMap->w )];
147                height +=  (float) (unsigned char) hm->heights[j -1+ sampleRate   + (i+1)*(heightMap->w )];
148                height +=  (float)(unsigned char)hm->heights[j +sampleRate+ (i+2)*(heightMap->w )];
149                height +=  (float)(unsigned char)hm->heights[j+sampleRate + (i)*(heightMap->w )];
150                height=height/5.0;
151               
152                model->addVertex(scaleX*(heightMap->h -i) + shiftX ,((double)(height)*scaleY) + shiftY ,scaleZ*(j) + shiftZ); // Top Right     
153                model->addNormal(normalVectorField[i][j].y,normalVectorField[i][j].z,normalVectorField[i][j].x);
[6727]154                model->addTexCoor((float)(j) /(texRate), (float)(i %heightMap->h)/(texRate));
[6579]155               
156                //PRINTF(0)("TexCoord:  %f %f \n",(float)j / 100.0, (float)(i %heightMap->h)/100.0);
157               
158                model->addColor(r/255.0,g/255.0,b/255.0);
159                w = j;
160            }
161               
162               
163                model->addVertex(scaleX*(heightMap->h -i)+ shiftX,shiftY,scaleZ*(w)+ shiftZ); // Top Right
164                model->addNormal(normalVectorField[i][w].y,normalVectorField[i][w].z,normalVectorField[i][w].x);
[6727]165                model->addTexCoor((float)(j2+sampleRate) /(texRate), (float)(i %heightMap->h)/(texRate));
[6579]166                model->addColor(r/255.0,g/255.0,b/255.0);
167               
168        }
169       
170       
171       
[6494]172
173
[6579]174        SDL_UnlockSurface(heightMap);
175        int cnt = 0;
176        for(int i = i1   ; i < i2  ; i +=sampleRate)
177        {
178               
179            for(int j = j1-sampleRate  ; j < j2  + 2*sampleRate  ;  j += sampleRate)
180            {
181               
182                model->addIndice(cnt);
183                model->addIndice(cnt  + (j2 -j1 + 3* sampleRate  )/ sampleRate );
184                cnt++;
185               
186            }
187               
188               
189               
190                 model->newStripe();   
191               
192               
193        }
194         cnt += (j2 -j1 + 3* sampleRate)/ sampleRate;
195       
196                for(int j = j1 ; j <= j2    ;  j += sampleRate)
197            {
198                int i = i1;
199           
200                 // To be fixed
201                 if(hm->hasColourMap)
202                 {
203                 r = (float)colours[3*j+2 + 3*i*(heightMap->w )];
204                 g = (float)colours[3*j+1 + 3*i*(heightMap->w)];
205                 b = (float)colours[3*j+0 + 3*i*(heightMap->w)];
206                 }
207               
208                model->addVertex(scaleX*(heightMap->h -i) + shiftX , shiftY ,scaleZ*(j) + shiftZ); // Top Right
209                model->addNormal(normalVectorField[i][j].y,normalVectorField[i][j].z,normalVectorField[i][j].x);
[6727]210                model->addTexCoor((float)j /(texRate), (float)((i - sampleRate) %heightMap->h)/(texRate));
[6579]211                model->addColor(r/255.0,g/255.0,b/255.0);               
212       
213            }
[6494]214
[6579]215                for(int j = j1  ; j <= j2    ;  j += sampleRate)
216            {
217                int i = i1;
218                height = (float)(unsigned char) hm->heights[j +sampleRate+ i*(heightMap->w )];
219                height += (float)(unsigned char) hm->heights[j+ 1 + sampleRate + (i+1)*(heightMap->w )];
220                height +=  (float) (unsigned char) hm->heights[j -1+ sampleRate   + (i+1)*(heightMap->w )];
221                height +=  (float)(unsigned char)hm->heights[j +sampleRate+ (i+2)*(heightMap->w )];
222                height +=  (float)(unsigned char)hm->heights[j+sampleRate + (i)*(heightMap->w )];
223                height=height/5.0;
224               
225                model->addVertex(scaleX*(heightMap->h -i) + shiftX , ((double)(height)*scaleY) +shiftY ,scaleZ*(j) + shiftZ); // Top Right     
226                model->addNormal(normalVectorField[i][j].y,normalVectorField[i][j].z,normalVectorField[i][j].x);
[6727]227                model->addTexCoor((float)j /(texRate), (float)(i %heightMap->h)/(texRate));
[6579]228                model->addColor(r/255.0,g/255.0,b/255.0);
229           
230            }
231           
232               
233           
234            for(int j = j1  ; j <= j2    ;  j += sampleRate)
235            {
236                int i = i2;
237           
238                 // To be fixed
239                 if(hm->hasColourMap)
240                 {
241                 r = (float)colours[3*j+2 + 3*i*(heightMap->w )];
242                 g = (float)colours[3*j+1 + 3*i*(heightMap->w)];
243                 b = (float)colours[3*j+0 + 3*i*(heightMap->w)];
244                 }
245               
246                model->addVertex(scaleX*(heightMap->h -i) + shiftX , shiftY ,scaleZ*(j) + shiftZ); // Top Right
247                model->addNormal(normalVectorField[i][j].y,normalVectorField[i][j].z,normalVectorField[i][j].x);
[6727]248                model->addTexCoor((float)j /(texRate), (float)((i+ sampleRate) %heightMap->h)/(texRate));
[6579]249                model->addColor(r/255.0,g/255.0,b/255.0);               
250       
251            }
252                   
253           
254            for(int j = j1 ; j <= j2    ;  j += sampleRate)
255            {
256                int i = i2;
[6727]257                height = (float)(unsigned char) hm->heights[j +sampleRate+ i*(heightMap->w )];
[6579]258                height += (float)(unsigned char) hm->heights[j+ 1 + sampleRate + (i+1)*(heightMap->w )];
259                height +=  (float) (unsigned char) hm->heights[j -1+ sampleRate   + (i+1)*(heightMap->w )];
260                height +=  (float)(unsigned char)hm->heights[j +sampleRate+ (i+2)*(heightMap->w )];
261                height +=  (float)(unsigned char)hm->heights[j+sampleRate + (i)*(heightMap->w )];
262                height=height/5.0;
263                model->addVertex(scaleX*(heightMap->h -i) + shiftX , ((double)(height)*scaleY) +shiftY ,scaleZ*(j) + shiftZ); // Top Right     
264                model->addNormal(normalVectorField[i][j].y,normalVectorField[i][j].z,normalVectorField[i][j].x);
[6727]265                model->addTexCoor((float)j /(texRate), (float)(i %heightMap->h)/(texRate));
[6579]266                model->addColor(r/255.0,g/255.0,b/255.0);
267           
268            }
269           
[6494]270
[6579]271       
272       
273         // link Boarder Stripe
274            for(int j = j1-sampleRate  ; j < j2    ;  j += sampleRate)
275            {
276               
277                model->addIndice(cnt);
278                model->addIndice(cnt  + (j2 -j1 +  sampleRate  )/ sampleRate );
279                cnt++;
280               
281            }
282               
283                cnt++;
284               
285                 model->newStripe();   
286               
287               
288       
289       
290       
291         cnt += (j2-j1)/ sampleRate;
292       
293        // link 2nd BoarderStripe
294            for(int j = j1-sampleRate  ; j < j2    ;  j += sampleRate)
295            {
296               
297                model->addIndice(cnt);
298                model->addIndice(cnt  + (j2 -j1 +  sampleRate  )/ sampleRate );
299                cnt++;
300               
301            }
302         
303           
304       
305         model->finalize();
306         
307         
308         
309         
310        #undef heightMap
311        #undef colours   
312        #undef scaleX
313        #undef scaleY
314        #undef scaleZ
315        #undef shiftX
316        #undef shiftY
317        #undef shiftZ
318        #undef normalVectorField
319         
[6494]320}
321
322HeightMap::HeightMap()
323{
324
325}
326
327HeightMap::HeightMap(const char* height_map_name = NULL) : VertexArrayModel()
328{
[5976]329   this->setClassID(CL_HEIGHT_MAP, "HeightMap");
[5990]330   heightMap =  IMG_Load(height_map_name);
331   if(heightMap!=NULL) {
[6494]332
[6579]333                 PRINTF(0)("loading Image %s\n", height_map_name);
334                 PRINTF(0)("width : %i\n", heightMap->w);
335                 PRINTF(0)("height : %i\n", heightMap->h);
336                 PRINTF(0)("%i Byte(s) per Pixel \n", heightMap->format->BytesPerPixel);
337                 PRINTF(0)("Rshift : %i\n", heightMap->format->Rshift);
338                 PRINTF(0)("Bshift: %i\n", heightMap->format->Bshift);
339                 PRINTF(0)("Gshift: %i\n", heightMap->format->Gshift);
340                 PRINTF(0)("Rmask: %i\n", heightMap->format->Rmask);
341                 PRINTF(0)("Gmask: %i\n", heightMap->format->Gmask);
342                }
[6494]343
[5990]344     else       PRINTF(4)("oops! couldn't load %s for some reason.\n", height_map_name);
[6494]345
[6579]346       
[6494]347  generateNormalVectorField();
348
349  shiftX = 0;
350  shiftY = 0;
351  shiftZ = 0;
352
[5967]353}
354
[6494]355HeightMap::HeightMap(const char* height_map_name = NULL, const char* colour_map_name = NULL) : VertexArrayModel()
356{
357this->setClassID(CL_HEIGHT_MAP, "HeightMap");
[5967]358
[6494]359   heightMap =  IMG_Load(height_map_name);
360   if(heightMap!=NULL) {
361
[6579]362                 PRINTF(0)("loading Image %s\n", height_map_name);
363                 PRINTF(0)("width : %i\n", heightMap->w);
364                 PRINTF(0)("height : %i\n", heightMap->h);
365                 PRINTF(0)("%i Byte(s) per Pixel \n", heightMap->format->BytesPerPixel);
366                 PRINTF(0)("Rshift : %i\n", heightMap->format->Rshift);
367                 PRINTF(0)("Bshift: %i\n", heightMap->format->Bshift);
368                 PRINTF(0)("Gshift: %i\n", heightMap->format->Gshift);
369                 PRINTF(0)("Rmask: %i\n", heightMap->format->Rmask);
370                 PRINTF(0)("Gmask: %i\n", heightMap->format->Gmask);
371                }
[6494]372
373     else       PRINTF(4)("oops! couldn't load %s for some reason.\n", height_map_name);
374
375
376    generateNormalVectorField();
377
378  colourMap=NULL;
379  if(colour_map_name != NULL)
380  {
381  colourMap = IMG_Load(colour_map_name);
382  }
[6579]383 
[6494]384  if(colourMap != NULL)
[6579]385                {
386                 PRINTF(0)("loading Image %s\n", colour_map_name);
387                 PRINTF(0)("width : %i\n", colourMap->w);
388                 PRINTF(0)("height : %i\n", colourMap->h);
389                 PRINTF(0)("%i Byte(s) per Pixel \n", colourMap->format->BytesPerPixel);
390                 PRINTF(0)("Rshift : %i\n", colourMap->format->Rshift);
391                 PRINTF(0)("Bshift: %i\n", colourMap->format->Bshift);
392                 PRINTF(0)("Gshift: %i\n", colourMap->format->Gshift);
393                 PRINTF(0)("Rmask: %i\n", colourMap->format->Rmask);
394                 PRINTF(0)("Gmask: %i\n", colourMap->format->Gmask);
395                 }
396                 else       PRINTF(0)("oops! couldn't load colorMap for some reason.\n");
[6494]397
[6579]398                 
399                   
[6494]400  if(colourMap != NULL)
401  {
402  colours = (unsigned char *) colourMap->pixels;
403  hasColourMap = true;
404  }
405  else hasColourMap = false;
[6579]406 
407       
408  heights  = (unsigned char*) heightMap->pixels;
[6730]409  shiftX = 0;
410  shiftY = 0;
411  shiftZ = 0;
[6494]412}
[5967]413HeightMap::~HeightMap()
414{
[6579]415        delete heightMap;
[5967]416}
417
[5942]418void HeightMap::load()
419{
[5993]420
[6494]421//Create a Dynamicly sized 2D-Array for Tiles
422tiles =  new Tile** [heightMap->h/tileSize];
423for(int i=0;i <    heightMap->h/tileSize ; i++)
424tiles [i]= new (Tile* [heightMap->w /tileSize ]);
[5967]425
[6494]426//SetUp Arrays
427for(int i = 0; i < (heightMap->h - tileSize )/ tileSize; i ++)
428{
[6579]429        for(int j = 0; j < (heightMap->w - tileSize )/ tileSize; j ++)
430        {
431         
432         tiles[i][j] =    new Tile( i*tileSize ,  j*tileSize , (i+1)*tileSize, (j+1)*tileSize , this ) ;
433        }
[6494]434}
[5993]435
[6494]436}
[5993]437
[5980]438
[6579]439void HeightMap::draw() 
[6494]440{
441const PNode* camera = State::getCamera();
442Vector v = camera->getAbsCoor();
[5967]443
[6494]444int i_min = 1;
[6579]445int i_max = (heightMap->h - tileSize )/ tileSize;
[6494]446int j_min = 1;
447int j_max= (heightMap->w - tileSize) / tileSize;
[5990]448
[5942]449
450
[6603]451for(int i = 1; i <  i_max        ; i ++)
[6494]452{
[6579]453        for(int j = 1; j < j_max ; j++)
454        {
[6603]455         tiles[i][j]->draw();
[6579]456        }       
457} 
[6494]458
459}
460void HeightMap::generateNormalVectorField()
461{
462int delta = 1;
463heights  = (unsigned char*) heightMap->pixels;
464
465//Create a Dynamicly sized 2D-Array to store our normals
466normalVectorField =  new Vector* [heightMap->h];
467for(int i=0;i<heightMap->h;i++)
468normalVectorField [i]= new (Vector [heightMap->w]);
469
470
471
472// !!! Does not yet calculate the normals of some border points!!!!!
473
474if(heightMap != NULL && heightMap->format->BitsPerPixel == 8 )
[6579]475        {
476        SDL_LockSurface(heightMap);
477        for(int i = 0 ; i < heightMap->h - 1  ; i ++)
478        {
479            for(int j = 0; j < heightMap->- 1  ;  j ++)
480            {
[6494]481
482
[6579]483                delta = (int)heights[j + (i+1)*(heightMap->w )] -  (int) heights[j + i*(heightMap->w )];
484                Vector a =  Vector(-scaleX,(float)delta*scaleY  ,0.0f);
[6494]485
[6579]486                delta = (int)heights[j+1 + i*(heightMap->w )] - (int)heights[j + i*(heightMap->w )];
487                Vector b =  Vector(0.0f,(float) delta*scaleY ,scaleZ);
[6494]488
489
[6579]490                 normalVectorField[i][j] = b.cross(a);
491                 normalVectorField[i][j].normalize();
492                 
493             }
494        }
495        SDL_UnlockSurface(heightMap);
[6494]496
497}
498
499}
500
501
502void HeightMap::scale(Vector v)
503{
504 scaleX = v.x;
505 scaleY = v.y;
506 scaleZ = v.z;
507}
508
[6727]509void HeightMap::setAbsCoor(Vector v)
[6494]510{
[6727]511 offsetX = v.x;
512 offsetY = v.y;
513 offsetZ  = v.z;
[6494]514}
515
516
[6727]517float HeightMap::getHeight(float x, float y) 
[6494]518{
[6727]519
520 //x -= offsetX;
521 //y -= offsetZ;
522 
523 
[6730]524 int xInt = (int)x / scaleX;  x -= (float)((int)x); xInt = heightMap->h - xInt;
525 int yInt = (int)y / scaleZ;    y -= (float) ((int) y); /*yInt = heightMap->w - yInt;*/
[6727]526 
[6730]527 PRINTF(0)("xInt: %i, yInt: %i, x: %f, y: %f\n", xInt, yInt, x, y);
[6727]528 
[6730]529 if(xInt <= 0 || xInt >= heightMap->h || yInt <= 0 || yInt >= heightMap->) return 0;
530 
[6494]531 float height = heights[yInt + (xInt)*heightMap->w]*scaleY;
[6727]532 
[6730]533 /*
[6494]534 float a = normalVectorField[(xInt)][yInt].x;
535 float b = normalVectorField [(xInt)][yInt].z;
536 float c = normalVectorField [(xInt)][yInt].y;
[6727]537 
[6730]538 height -= ( (a/c)*(x) + (b/c)*(y));
539  */
540 PRINTF(0)("height: %f \n" ,height );
[6727]541 return (height );
[6494]542}
Note: See TracBrowser for help on using the repository browser.