Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6529 was 6529, checked in by bottac, 18 years ago

Some bugs fixed

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