Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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