Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Improved texturemapping

  • Property svn:executable set to *
File size: 13.7 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         // This Draws the LOD's
57                                                                //FIXME
58    float cameraDistance = (State::getCamera()->getAbsCoor() - Vector(0.0,100.0,0.0)).len();
59    if (cameraDistance > 5000 )
60    {
61   
62    }
63    else if (cameraDistance > 1000 )
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((((w -sampleRate)/sampleRate)%4)/4.0,(((i-sampleRate)/sampleRate)%4)/4.0);
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               
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);
154                model->addTexCoor((float)j /(texRate), (float)(i %heightMap->h)/(texRate));
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);
165                model->addTexCoor((((w+sampleRate)/sampleRate))/4.0,(((i+ sampleRate)/sampleRate))/4.0);
166                model->addColor(r/255.0,g/255.0,b/255.0);
167               
168        }
169       
170       
171       
172
173
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);
210                model->addTexCoor(((j/sampleRate))/texRatef,(((i - sampleRate)/sampleRate))/texRatef);
211                model->addColor(r/255.0,g/255.0,b/255.0);               
212       
213            }
214
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);
227                model->addTexCoor(((j/sampleRate))/texRatef,(((i)/sampleRate))/texRatef);
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);
248                model->addTexCoor(((j/sampleRate)%texRate)/texRatef,(((i- sampleRate)/sampleRate)%texRate)/texRatef);
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;
257                        height = (float)(unsigned char) hm->heights[j +sampleRate+ i*(heightMap->w )];
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);
265                model->addTexCoor(((j/sampleRate)%texRate)/texRatef,((i/sampleRate)%texRate)/texRatef);
266                model->addColor(r/255.0,g/255.0,b/255.0);
267           
268            }
269           
270
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         
320}
321
322HeightMap::HeightMap()
323{
324
325}
326
327HeightMap::HeightMap(const char* height_map_name = NULL) : VertexArrayModel()
328{
329   this->setClassID(CL_HEIGHT_MAP, "HeightMap");
330   heightMap =  IMG_Load(height_map_name);
331   if(heightMap!=NULL) {
332
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                }
343
344     else       PRINTF(4)("oops! couldn't load %s for some reason.\n", height_map_name);
345
346       
347  generateNormalVectorField();
348
349  shiftX = 0;
350  shiftY = 0;
351  shiftZ = 0;
352
353}
354
355HeightMap::HeightMap(const char* height_map_name = NULL, const char* colour_map_name = NULL) : VertexArrayModel()
356{
357this->setClassID(CL_HEIGHT_MAP, "HeightMap");
358
359   heightMap =  IMG_Load(height_map_name);
360   if(heightMap!=NULL) {
361
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                }
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  }
383 
384  if(colourMap != NULL)
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");
397
398                 
399                   
400  if(colourMap != NULL)
401  {
402  colours = (unsigned char *) colourMap->pixels;
403  hasColourMap = true;
404  }
405  else hasColourMap = false;
406 
407       
408  heights  = (unsigned char*) heightMap->pixels;
409
410
411}
412HeightMap::~HeightMap()
413{
414        delete heightMap;
415}
416
417void HeightMap::load()
418{
419
420//Create a Dynamicly sized 2D-Array for Tiles
421tiles =  new Tile** [heightMap->h/tileSize];
422for(int i=0;i <    heightMap->h/tileSize ; i++)
423tiles [i]= new (Tile* [heightMap->w /tileSize ]);
424
425//SetUp Arrays
426for(int i = 0; i < (heightMap->h - tileSize )/ tileSize; i ++)
427{
428        for(int j = 0; j < (heightMap->w - tileSize )/ tileSize; j ++)
429        {
430         
431         tiles[i][j] =    new Tile( i*tileSize ,  j*tileSize , (i+1)*tileSize, (j+1)*tileSize , this ) ;
432        }
433}
434
435}
436
437
438void HeightMap::draw() 
439{
440const PNode* camera = State::getCamera();
441Vector v = camera->getAbsCoor();
442
443int i_min = 1;
444int i_max = (heightMap->h - tileSize )/ tileSize;
445int j_min = 1;
446int j_max= (heightMap->w - tileSize) / tileSize;
447
448
449
450for(int i = 1; i <  i_max        ; i ++)
451{
452        for(int j = 1; j < j_max ; j++)
453        {
454               
455         tiles[i][j]->draw();
456        }       
457} 
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 )
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            {
481
482
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);
485
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);
488
489
490                 normalVectorField[i][j] = b.cross(a);
491                 normalVectorField[i][j].normalize();
492                 
493             }
494        }
495        SDL_UnlockSurface(heightMap);
496
497}
498
499}
500
501
502
503
504
505void HeightMap::scale(Vector v)
506{
507 scaleX = v.x;
508 scaleY = v.y;
509 scaleZ = v.z;
510}
511
512void HeightMap::shift(Vector v)
513{
514 shiftX = v.x;
515 shiftY = v.y;
516 shiftZ = v.z;
517}
518
519
520float HeightMap::getHeight(float x, float y)
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;
524 if(xInt <= 0 || xInt >= heightMap->h || yInt <= 0 || yInt >= heightMap->) return 0.0f;
525 float height = heights[yInt + (xInt)*heightMap->w]*scaleY;
526 float a = normalVectorField[(xInt)][yInt].x;
527 float b = normalVectorField [(xInt)][yInt].z;
528 float c = normalVectorField [(xInt)][yInt].y;
529  height -= ( (a/c)*(x) + (b/c)*(y))*scaleY;
530 return height;
531}
Note: See TracBrowser for help on using the repository browser.