Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6956 in orxonox.OLD


Ignore:
Timestamp:
Feb 1, 2006, 4:37:08 PM (18 years ago)
Author:
bensch
Message:

orxonox/trunk: merged the HeightMapBranche back to the trunk

Location:
trunk/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/graphics/importer/height_map.cc

    r6100 r6956  
    1010
    1111   ### File Specific:
    12    main-programmer:
     12   main-programmer: bottac@ee.ethz.ch
    1313*/
    1414
     
    1717#include "texture.h"
    1818#include "vector.h"
    19 
     19#include "material.h"
     20#include "p_node.h"
     21#include "state.h"
     22#include "resource_manager.h"
    2023#include "debug.h"
    2124
     
    2730#endif
    2831
    29 HeightMap::HeightMap() : StaticModel()
    30 {
    31        
    32 }
    33 
    34 HeightMap::HeightMap(const char* height_map_name = NULL) : StaticModel()
     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(this->x, hmref->offsetY , this->z) ).len();
     59        if(cameraDistance < 0) cameraDistance = -cameraDistance;
     60    if(cameraDistance > 5000 )
     61    {
     62     return;
     63    }
     64    else if (cameraDistance >= 1000 )
     65    {
     66      this->drawLowRes();
     67    }
     68    else
     69    {
     70      this->drawHighRes();
     71    }
     72}
     73
     74void Tile::drawHighRes()
     75{
     76 highResModel->draw();
     77}
     78
     79void Tile::drawLowRes()
     80{
     81 lowResModel->draw();
     82}
     83
     84/*
     85*/
     86void Tile::load(int i1, int j1, int i2, int j2, HeightMap* hm, VertexArrayModel* model, int Res)
     87{
     88       
     89        #define heightMap hm->heightMap
     90        #define colours   hm->colours
     91        #define scaleX hm->scaleX
     92        #define scaleY hm->scaleY
     93        #define scaleZ hm->scaleZ
     94        #define shiftX hm->shiftX
     95        #define shiftY hm->shiftY
     96        #define shiftZ hm->shiftZ
     97        #define normalVectorField hm->normalVectorField
     98       
     99        hmref = hm; // FIXME
     100       
     101        this->x = hm->offsetX + (heightMap->h - ((i1+i2)/2))*scaleX;
     102        this->z = hm->offsetZ + ((j1 + j2 )/2 ) * scaleZ;
     103       
     104        int sampleRate = Res;
     105       
     106        float height = 0;
     107        int offset = 0;
     108       
     109        float r = 0.0;
     110        float g = 0.0;
     111        float b = 0.0;
     112
     113 
     114
     115 
     116 if(heightMap != NULL && heightMap->format->BitsPerPixel == 8 )
     117       
     118        SDL_LockSurface(heightMap);
     119        SDL_LockSurface(hm->colourMap);
     120       
     121        for(int i = i1 ; i <= i2  ; i +=sampleRate)
     122        {
     123                int w = 0;
     124
     125               
     126               
     127                 if(hm->hasColourMap)
     128                 {
     129                 r = colours[(3*w+2 + 3*i*(heightMap->w )) ];
     130                 g = colours[(3*w+1 + 3*i*(heightMap->w)) ];
     131                 b = colours[(3*w+0 + 3*i*(heightMap->w))];
     132                 }
     133               
     134                w = j1;
     135                model->addVertex(scaleX*(heightMap->h -i)+ shiftX,shiftY,scaleZ*(w)+ shiftZ); // Top Right
     136                model->addNormal(normalVectorField[i % heightMap->h][w % heightMap->w].y,normalVectorField[i % heightMap->h][w % heightMap->w].z,normalVectorField[i % heightMap->h][w % heightMap->w].x);
     137                model->addTexCoor((float)(j1-sampleRate) /(texRate), (float)(i %heightMap->h)/(texRate));
     138               
     139                model->addColor(r/255.0,g/255.0,b/255.0);
     140               
     141            for(int j = j1  ; j <= j2    ;  j += sampleRate)
     142            {
     143           
     144           
     145                 // To be fixed
     146                 if(hm->hasColourMap)
     147                 {
     148                 r = colours[(3*j+2 + 3*i*(heightMap->w )) ];
     149                 g =  colours[(3*j+1 + 3*i*(heightMap->w)) ];
     150                 b =  colours[(3*j+0 + 3*i*(heightMap->w))];
     151                 }
     152                height = (float)(unsigned char) hm->heights[(j +sampleRate+ i*(heightMap->w )) ];
     153                height += (float)(unsigned char) hm->heights[(j+ 1 + sampleRate + (i+1)*(heightMap->w )) ];
     154                height +=  (float) (unsigned char) hm->heights[(j -1+ sampleRate   + (i+1)*(heightMap->w ))];
     155                height +=  (float)(unsigned char)hm->heights[(j +sampleRate+ (i+2)*(heightMap->w )) ];
     156                height +=  (float)(unsigned char)hm->heights[(j+sampleRate + (i)*(heightMap->w )) ];
     157                height=height/5.0;
     158               
     159                model->addVertex(scaleX*(heightMap->h -i) + shiftX ,((double)(height)*scaleY) + shiftY ,scaleZ*(j) + shiftZ); // Top Right     
     160                model->addNormal(normalVectorField[i % heightMap->h][j % heightMap->w].y,normalVectorField[i % heightMap->h][j % heightMap->w].z,normalVectorField[i % heightMap->h][j % heightMap->w].x);
     161                model->addTexCoor((float)(j) /(texRate), (float)(i %heightMap->h)/(texRate));
     162               
     163                //PRINTF(0)("TexCoord:  %f %f \n",(float)j / 100.0, (float)(i %heightMap->h)/100.0);
     164               
     165                model->addColor(r/255.0,g/255.0,b/255.0);
     166                w = j;
     167            }
     168               
     169               
     170                model->addVertex(scaleX*(heightMap->h -i)+ shiftX,shiftY,scaleZ*(w)+ shiftZ); // Top Right
     171                model->addNormal(normalVectorField[i % heightMap->h][w % heightMap->w].y,normalVectorField[i % heightMap->h][w % heightMap->w].z,normalVectorField[i% heightMap->h][w % heightMap->w].x);
     172                model->addTexCoor((float)(j2+sampleRate) /(texRate), (float)(i %heightMap->h)/(texRate));
     173                model->addColor(r/255.0,g/255.0,b/255.0);
     174               
     175        }
     176       
     177       
     178       
     179
     180
     181        SDL_UnlockSurface(heightMap);
     182        int cnt = 0;
     183        for(int i = i1   ; i < i2  ; i +=sampleRate)
     184        {
     185               
     186            for(int j = j1-sampleRate  ; j < j2  + 2*sampleRate  ;  j += sampleRate)
     187            {
     188               
     189                model->addIndice(cnt);
     190                model->addIndice(cnt  + (j2 -j1 + 3* sampleRate  )/ sampleRate );
     191                cnt++;
     192               
     193            }
     194               
     195               
     196               
     197                 model->newStripe();   
     198               
     199               
     200        }
     201         cnt += (j2 -j1 + 3* sampleRate)/ sampleRate;
     202       
     203                for(int j = j1 ; j <= j2    ;  j += sampleRate)
     204            {
     205                int i = i1;
     206           
     207                 // To be fixed
     208                 if(hm->hasColourMap)
     209                 {
     210                 r = (float)colours[(3*j+2 + 3*i*(heightMap->w )) ];
     211                 g = (float)colours[(3*j+1 + 3*i*(heightMap->w)) ];
     212                 b = (float)colours[(3*j+0 + 3*i*(heightMap->w))];
     213                 }
     214               
     215                model->addVertex(scaleX*(heightMap->h -i) + shiftX , shiftY ,scaleZ*(j) + shiftZ); // Top Right
     216                model->addNormal(normalVectorField[i % heightMap->h][j % heightMap->w].y,normalVectorField[i % heightMap->h][j % heightMap->w].z,normalVectorField[i % heightMap->h][j % heightMap->w].x);
     217                model->addTexCoor((float)j /(texRate), (float)((i - sampleRate) %heightMap->h)/(texRate));
     218                model->addColor(r/255.0,g/255.0,b/255.0);               
     219       
     220            }
     221
     222                for(int j = j1  ; j <= j2    ;  j += sampleRate)
     223            {
     224                int i = i1;
     225                height = (float)(unsigned char) hm->heights[(j +sampleRate+ i*(heightMap->w )) ];
     226                height += (float)(unsigned char) hm->heights[(j+ 1 + sampleRate + (i+1)*(heightMap->w )) ];
     227                height +=  (float) (unsigned char) hm->heights[(j -1+ sampleRate   + (i+1)*(heightMap->w ))];
     228                height +=  (float)(unsigned char)hm->heights[(j +sampleRate+ (i+2)*(heightMap->w )) ];
     229                height +=  (float)(unsigned char)hm->heights[(j+sampleRate + (i)*(heightMap->w )) ];
     230                height=height/5.0;
     231               
     232                model->addVertex(scaleX*(heightMap->h -i) + shiftX , ((double)(height)*scaleY) +shiftY ,scaleZ*(j) + shiftZ); // Top Right     
     233                model->addNormal(normalVectorField[i % heightMap->h][j % heightMap->w].y,normalVectorField[i % heightMap->h][j% heightMap->w].z,normalVectorField[i%heightMap->h][j%heightMap->w].x);
     234                model->addTexCoor((float)j /(texRate), (float)(i %heightMap->h)/(texRate));
     235                model->addColor(r/255.0,g/255.0,b/255.0);
     236           
     237            }
     238           
     239               
     240           
     241            for(int j = j1  ; j <= j2    ;  j += sampleRate)
     242            {
     243                int i = i2;
     244           
     245                 // To be fixed
     246                 if(hm->hasColourMap)
     247                 {
     248                 r = (float)colours[3*j+2 + 3*i*(heightMap->w )];
     249                 g = (float)colours[3*j+1 + 3*i*(heightMap->w)];
     250                 b = (float)colours[3*j+0 + 3*i*(heightMap->w)];
     251                 }
     252               
     253                model->addVertex(scaleX*(heightMap->h -i) + shiftX , shiftY ,scaleZ*(j) + shiftZ); // Top Right
     254                model->addNormal(normalVectorField[i%heightMap->h][j%heightMap->w].y,normalVectorField[i%heightMap->h][j%heightMap->w].z,normalVectorField[i%heightMap->h][j%heightMap->w].x);
     255                model->addTexCoor((float)j /(texRate), (float)((i+ sampleRate) %heightMap->h)/(texRate));
     256                model->addColor(r/255.0,g/255.0,b/255.0);               
     257       
     258            }
     259                   
     260           
     261            for(int j = j1 ; j <= j2    ;  j += sampleRate)
     262            {
     263                int i = i2;
     264                height = (float)(unsigned char) hm->heights[(j +sampleRate+ i*(heightMap->w )) ];
     265                height += (float)(unsigned char) hm->heights[(j+ 1 + sampleRate + (i+1)*(heightMap->w )) ];
     266                height +=  (float) (unsigned char) hm->heights[(j -1+ sampleRate   + (i+1)*(heightMap->w ))];
     267                height +=  (float)(unsigned char)hm->heights[(j +sampleRate+ (i+2)*(heightMap->w ))];
     268                height +=  (float)(unsigned char)hm->heights[(j+sampleRate + (i)*(heightMap->w )) ];
     269                height=height/5.0;
     270                model->addVertex(scaleX*(heightMap->h -i) + shiftX , ((double)(height)*scaleY) +shiftY ,scaleZ*(j) + shiftZ); // Top Right     
     271                model->addNormal(normalVectorField[i%heightMap->h][j%heightMap->w].y,normalVectorField[i%heightMap->h][j%heightMap->w].z,normalVectorField[i%heightMap->h][j%heightMap->w].x);
     272                model->addTexCoor((float)j /(texRate), (float)(i %heightMap->h)/(texRate));
     273                model->addColor(r/255.0,g/255.0,b/255.0);
     274           
     275            }
     276           
     277
     278       
     279       
     280         // link Boarder Stripe
     281            for(int j = j1-sampleRate  ; j < j2    ;  j += sampleRate)
     282            {
     283               
     284                model->addIndice(cnt);
     285                model->addIndice(cnt  + (j2 -j1 +  sampleRate  )/ sampleRate );
     286                cnt++;
     287               
     288            }
     289               
     290                cnt++;
     291               
     292                 model->newStripe();   
     293               
     294               
     295       
     296       
     297       
     298         cnt += (j2-j1)/ sampleRate;
     299       
     300        // link 2nd BoarderStripe
     301            for(int j = j1-sampleRate  ; j < j2    ;  j += sampleRate)
     302            {
     303               
     304                model->addIndice(cnt);
     305                model->addIndice(cnt  + (j2 -j1 +  sampleRate  )/ sampleRate );
     306                cnt++;
     307               
     308            }
     309         
     310           
     311            SDL_UnlockSurface(hm->colourMap);
     312       
     313         model->finalize();
     314         
     315         
     316         
     317         
     318        #undef heightMap
     319        #undef colours   
     320        #undef scaleX
     321        #undef scaleY
     322        #undef scaleZ
     323        #undef shiftX
     324        #undef shiftY
     325        #undef shiftZ
     326        #undef normalVectorField
     327         
     328}
     329
     330HeightMap::HeightMap()
     331{
     332
     333}
     334
     335HeightMap::HeightMap(const char* height_map_name = NULL) : VertexArrayModel()
    35336{
    36337   this->setClassID(CL_HEIGHT_MAP, "HeightMap");
    37338   heightMap =  IMG_Load(height_map_name);
    38339   if(heightMap!=NULL) {
    39        
     340
    40341                 PRINTF(0)("loading Image %s\n", height_map_name);
    41342                 PRINTF(0)("width : %i\n", heightMap->w);
    42                  PRINTF(0)("hight : %i\n", heightMap->h);
     343                 PRINTF(0)("height : %i\n", heightMap->h);
    43344                 PRINTF(0)("%i Byte(s) per Pixel \n", heightMap->format->BytesPerPixel);
    44345                 PRINTF(0)("Rshift : %i\n", heightMap->format->Rshift);
     
    46347                 PRINTF(0)("Gshift: %i\n", heightMap->format->Gshift);
    47348                 PRINTF(0)("Rmask: %i\n", heightMap->format->Rmask);
    48                  PRINTF(0)("Gmask: %i\n", heightMap->format->Gmask);           
     349                 PRINTF(0)("Gmask: %i\n", heightMap->format->Gmask);
    49350                }
     351
     352     else       PRINTF(4)("oops! couldn't load %s for some reason.\n", height_map_name);
     353
     354       
     355  generateNormalVectorField();
     356
     357  shiftX = 0;
     358  shiftY = 0;
     359  shiftZ = 0;
     360
     361}
     362
     363HeightMap::HeightMap(const char* height_map_name = NULL, const char* colour_map_name = NULL) : VertexArrayModel()
     364{
     365this->setClassID(CL_HEIGHT_MAP, "HeightMap");
     366
     367   heightMap =  IMG_Load(height_map_name);
     368   if(heightMap!=NULL) {
     369
     370                 PRINTF(0)("loading Image %s\n", height_map_name);
     371                 PRINTF(0)("width : %i\n", heightMap->w);
     372                 PRINTF(0)("height : %i\n", heightMap->h);
     373                 PRINTF(0)("%i Byte(s) per Pixel \n", heightMap->format->BytesPerPixel);
     374                 PRINTF(0)("Rshift : %i\n", heightMap->format->Rshift);
     375                 PRINTF(0)("Bshift: %i\n", heightMap->format->Bshift);
     376                 PRINTF(0)("Gshift: %i\n", heightMap->format->Gshift);
     377                 PRINTF(0)("Rmask: %i\n", heightMap->format->Rmask);
     378                 PRINTF(0)("Gmask: %i\n", heightMap->format->Gmask);
     379                }
     380
     381     else       PRINTF(4)("oops! couldn't load %s for some reason.\n", height_map_name);
     382
     383
     384    generateNormalVectorField();
     385
     386  colourMap=NULL;
     387  if(colour_map_name != NULL)
     388  {
     389  colourMap = IMG_Load(colour_map_name);
     390  }
     391 
     392  if(colourMap != NULL)
     393                {
     394                 PRINTF(0)("loading Image %s\n", colour_map_name);
     395                 PRINTF(0)("width : %i\n", colourMap->w);
     396                 PRINTF(0)("height : %i\n", colourMap->h);
     397                 PRINTF(0)("%i Byte(s) per Pixel \n", colourMap->format->BytesPerPixel);
     398                 PRINTF(0)("Rshift : %i\n", colourMap->format->Rshift);
     399                 PRINTF(0)("Bshift: %i\n", colourMap->format->Bshift);
     400                 PRINTF(0)("Gshift: %i\n", colourMap->format->Gshift);
     401                 PRINTF(0)("Rmask: %i\n", colourMap->format->Rmask);
     402                 PRINTF(0)("Gmask: %i\n", colourMap->format->Gmask);
     403                 }
     404                 else       PRINTF(0)("oops! couldn't load colorMap for some reason.\n");
     405
    50406                 
    51      else       PRINTF(4)("oops! couldn't load %s for some reason.\n", height_map_name);
     407                   
     408  if(colourMap != NULL)
     409  {
     410  colours = (unsigned char *) colourMap->pixels;
     411  hasColourMap = true;
     412  }
     413  else hasColourMap = false;
     414 
     415       
     416  heights  = (unsigned char*) heightMap->pixels;
     417  shiftX = 0;
     418  shiftY = 0;
     419  shiftZ = 0;
    52420}
    53421
     
    56424{
    57425        delete heightMap;
     426        delete colourMap;
     427       
     428
     429        for(int i=0;i <    heightMap->h/tileSize ; i++)
     430        {
     431        for(int j = 0; j < heightMap->w/ tileSize; j++)
     432        {
     433         delete tiles [i][j];
     434        }
     435        }
     436        for(int i=0;i <    heightMap->h/tileSize ; i++)
     437        delete[] tiles[i];
     438        delete[] tiles;
     439       
     440       
     441       
     442
     443
     444for(int i=0;i<heightMap->h;i++)
     445delete[] normalVectorField [i];
     446delete[] normalVectorField;
     447
     448
     449
     450
     451       
     452       
     453       
    58454}
    59455
    60456void HeightMap::load()
    61457{
    62  unsigned char height = 0;
    63  int offset = 0;
    64 
    65  char *  bmp = (char*) heightMap->pixels;
    66 
    67   if(heightMap != NULL && heightMap->format->BitsPerPixel == 8 )
     458
     459//Create a Dynamicly sized 2D-Array for Tiles
     460tiles =  new Tile** [heightMap->h/tileSize];
     461for(int i=0;i <    heightMap->h/tileSize ; i++)
     462tiles [i]= new (Tile* [heightMap->w /tileSize ]);
     463
     464//SetUp Arrays
     465for(int i = 0; i < (heightMap->h  )/ tileSize; i ++)
     466{
     467        for(int j = 0; j < (heightMap->w )/ tileSize; j ++)
     468        {
     469         
     470         tiles[i][j] =    new Tile( i*tileSize ,  j*tileSize , (i+1)*tileSize, (j+1)*tileSize , this ) ;
     471        }
     472}
     473
     474}
     475
     476
     477void HeightMap::draw()
     478{
     479const PNode* camera = State::getCamera();
     480Vector v = camera->getAbsCoor();
     481
     482int i_min = 0;
     483int i_max = (heightMap->h )/ tileSize;
     484int j_min = 0;
     485int j_max= (heightMap->w  ) / tileSize;
     486
     487
     488
     489for(int i = 0; i <  i_max        ; i ++)
     490{
     491        for(int j = 0; j < j_max ; j++)
     492        {
     493         tiles[i][j]->draw();
     494        }       
     495}
     496
     497}
     498void HeightMap::generateNormalVectorField()
     499{
     500int delta = 1;
     501heights  = (unsigned char*) heightMap->pixels;
     502
     503//Create a Dynamicly sized 2D-Array to store our normals
     504normalVectorField =  new Vector* [heightMap->h];
     505for(int i=0;i<heightMap->h;i++)
     506normalVectorField [i]= new (Vector [heightMap->w]);
     507
     508
     509
     510
     511// Initialize
     512for(int i=0; i< heightMap->h; i++)
     513{
     514        for(int j = 0; j> heightMap->w; j++)
     515        {
     516        Vector v = Vector(0.0, 1.0, 0.0);
     517         normalVectorField[i][j] = v;
     518        }
     519}
     520
     521// !!! Does not yet calculate the normals of some border points!!!!!
     522
     523if(heightMap != NULL && heightMap->format->BitsPerPixel == 8 )
    68524        {
    69525        SDL_LockSurface(heightMap);
    70         for(int i = 0 ; i < heightMap->h  ; i +=2)
    71         {
    72             for(int j = 0; j < heightMap->w   ;  j += 2)
    73             {
    74                 // get local hight from heightMap
    75                 // This seems to work only on 8-Bit Grayscale-Bitmaps
    76                 height = bmp[j + i*(heightMap->w )];
    77                
    78                        
    79                 /*height = heightMap->format->palette->colors[offset].r +
    80                            heightMap->format->palette->colors[offset].g +
    81                            heightMap->format->palette->colors[offset].b ; */
    82                                        
    83                                
    84                 this->addVertex( 20*(heightMap->h - i) , (( (double)height)/0.3)-1200  ,20*j);
    85              } 
     526        for(int i = 0 ; i < heightMap->h - 1  ; i ++)
     527        {
     528            for(int j = 0; j < heightMap->w  - 1  ;  j ++)
     529            {
     530
     531
     532                delta = (int)heights[j + (i+1)*(heightMap->w )] -  (int) heights[j + i*(heightMap->w )];
     533                Vector a =  Vector(-scaleX,(float)delta*scaleY  ,0.0f);
     534
     535                delta = (int)heights[j+1 + i*(heightMap->w )] - (int)heights[j + i*(heightMap->w )];
     536                Vector b =  Vector(0.0f,(float) delta*scaleY ,scaleZ);
     537
     538
     539                 normalVectorField[i][j] = b.cross(a);
     540                 normalVectorField[i][j].normalize();
     541                 
     542             }
    86543        }
    87544        SDL_UnlockSurface(heightMap);
    88545
    89 
    90 
    91         int c = (heightMap->w)/2 ; // One line
    92         for(int i = 0; i < (heightMap->w)/2  -2  ; i ++)
    93         {
    94             for(int j = 0; j < (heightMap->h)/2 - 2; j++)
    95             {
    96                  
    97                 /* Two Triangles or ...*/
    98                
    99                 //this->addFace (3, VERTEX_ONLY,j + (i+1)*c,j+1+i*c , j + i*c );
    100                 //this->addFace (3, VERTEX_ONLY,j + (i+1)*c,j + (i+1)*c +1 ,j+i*c +1 );                         
    101                                  
    102                 /* ... one square*/
    103 
    104                 this->addFace (4 ,VERTEX_ONLY,j+i*c,j+(i+1)*c  ,j + (i+1)*c +1, j +i*c+1 );
    105             }
    106                                
    107                        
    108                                
    109         }
    110                
    111 
    112         }//if
    113   else
    114         {
    115 
    116   //make a cube
    117   this->setName("HardCore");
    118   this->addVertex (-0.5, -0.5, 0.5);
    119   this->addVertex (0.5, -0.5, 0.5);
    120   this->addVertex (-0.5, 0.5, 0.5);
    121   this->addVertex (0.5, 0.5, 0.5);
    122   this->addVertex (-0.5, 0.5, -0.5);
    123   this->addVertex (0.5, 0.5, -0.5);
    124   this->addVertex (-0.5, -0.5, -0.5);
    125   this->addVertex (0.5, -0.5, -0.5);
    126        
    127 
    128 
    129 
    130   this->addFace (3, VERTEX_ONLY, 4, 3, 2);
    131 
    132         }
    133       this->finalize();
    134 
    135 
    136 }
     546}
     547
     548       
     549
     550
     551}
     552
     553
     554void HeightMap::scale(Vector v)
     555{
     556 scaleX = v.x;
     557 scaleY = v.y;
     558 scaleZ = v.z;
     559 generateNormalVectorField();
     560}
     561
     562void HeightMap::setAbsCoor(Vector v)
     563{
     564 offsetX = v.x;
     565 offsetY = v.y;
     566 offsetZ  = v.z;
     567}
     568
     569
     570float HeightMap::getHeight(float x, float y)
     571{
     572
     573 x -= offsetX;
     574 y -= offsetZ;
     575 
     576 
     577 int xInt = (int)x / scaleX;  x -= (float)((int)x); xInt = heightMap->h - xInt;
     578 int yInt = (int)y / scaleZ;    y -= (float) ((int) y); /*yInt = heightMap->w - yInt;*/
     579 
     580 //PRINTF(0)("xInt: %i, yInt: %i, x: %f, y: %f\n", xInt, yInt, x, y);
     581 
     582 if(xInt <= 0 || xInt >= heightMap->h || yInt <= 0 || yInt >= heightMap->w  ) return 0;
     583 if( y >= 0.5*x)
     584 {
     585        // Check for ...
     586 }
     587
     588 float height = heights[yInt + (xInt)*heightMap->w]*scaleY;
     589 
     590 
     591 float a = normalVectorField[(xInt)][yInt].x;
     592 float b = normalVectorField [(xInt)][yInt].z;
     593 float c = normalVectorField [(xInt)][yInt].y;
     594 
     595 PRINTF(0)("a: %f \n" ,a);
     596  PRINTF(0)("b: %f \n" ,b);
     597   PRINTF(0)("c: %f \n" ,c);
     598 
     599 height -= ( (a/c)*(x) + (b/c)*(y));
     600 
     601 PRINTF(0)("height: %f \n" ,height );
     602 return (height + offsetZ);
     603}
  • trunk/src/lib/graphics/importer/height_map.h

    r6100 r6956  
    1010
    1111   ### File Specific:
    12    main-programmer:
     12   main-programmer: bottac@ee.ethz.ch
    1313*/
    1414
    1515
    16 #include "static_model.h"
     16#include "vertex_array_model.h"
     17
     18
     19#define texRate      32
     20#define texRatef     4.0f
     21#define tileSize     64
    1722
    1823class SDL_Surface;
    1924class Vector;
     25class Material;
     26class PNode;
     27class Texture;
     28class HeightMap;
    2029
    21 class HeightMap : public StaticModel
     30class Tile
    2231{
    2332public:
     33void draw();
     34void drawHighRes();
     35void drawLowRes();
     36int getRes();
     37int setHighRes(bool b);
     38Tile(int i1, int j1, int i2, int j2, HeightMap* hm ) ;
     39Tile();
     40float x;
     41float z;
     42~Tile();
     43
     44
     45
     46private:
     47HeightMap* hmref;
     48VertexArrayModel* highResModel;
     49VertexArrayModel* lowResModel;
     50int highRes;
     51int lowRes;
     52void load(int i1, int j1, int i2, int i2, HeightMap* hm, VertexArrayModel* model, int Res);
     53};
     54
     55class HeightMap : public VertexArrayModel
     56{
     57public:
     58void draw();
    2459void load();
    25 void load(const char*, int);
     60void load(int Mode);
     61void load(const char*, int Mode);
     62void scale( Vector V);
     63void setAbsCoor(Vector V);
     64float getHeight(float x, float y);
     65float getNormal(float x, float y);
    2666HeightMap();
    2767HeightMap(const char*);
     68HeightMap(const char*, const char*);
    2869~HeightMap();
     70
     71friend class Tile;
    2972
    3073private:
    3174SDL_Surface* heightMap;
     75SDL_Surface* colourMap;
     76unsigned char* heights;
     77unsigned char* colours;
    3278
     79void generateNormalVectorField();
     80void drawRect(int xBottomLeft, int yBottomLeft, int xTopRight, int yTopRight );
     81void fixBoarder(int xBottomLeft, int yBottomLeft, int xTopRight, int yTopRight);
     82Vector  **  normalVectorField ;
     83Tile    ***  tiles;
     84Vector    camCoords;
     85Vector offsetCoords; 
     86Material * tmp_mat;
     87Material* red_mat;
     88Texture* texture;
     89const PNode* camera;
     90float scaleX ;
     91float scaleY ;
     92float scaleZ ;
     93float shiftX ; // to be removed
     94float shiftY ; // to be removed
     95float shiftZ ; // to be removed
     96float offsetX;
     97float offsetY;
     98float offsetZ;
     99int   cmScaleX;
     100int   cmScaleY;
     101bool  hasColourMap;
     102
     103inline int abs(int val)
     104{
     105 if(val < 0) val = -val;
     106 return val;
     107}
     108inline int max(int x, int y)
     109{
     110 return (x>y)? x:y;
     111}
     112inline int min(int x, int y)
     113{
     114return (x<y)? x: y;
     115}
    33116};
  • trunk/src/world_entities/terrain.cc

    r6815 r6956  
    2626#include "network_game_manager.h"
    2727
     28#include "height_map.h"
     29#include "material.h"
    2830
    2931#include "glincl.h"
     32
     33#include "state.h"
    3034
    3135using namespace std;
     
    4145  if( root != NULL)
    4246    this->loadParams(root);
     47
     48   this->heightMapMaterial = new Material();
     49   heightMapMaterial->setTransparency(1.0);
     50
     51   heightMapMaterial->setDiffuse(0.4,0.4,0.4);
     52   heightMapMaterial->setAmbient(0.4,0.4,0.4 );
     53   heightMapMaterial->setSpecular(0.4,0.4,0.4);
     54   heightMapMaterial->setShininess(.5);
     55   heightMapMaterial->setTransparency(1.0);
    4356
    4457//  if (this->model != NULL)
     
    90103    ResourceManager::getInstance()->unload(this->vegetation);
    91104  }
     105
     106  if(this->heightMap)
     107        delete heightMap;
    92108}
    93109
     
    101117  this->ssp = NULL;
    102118  this->vegetation = NULL;
     119
     120  this->heightMap = NULL;
     121  this->heightMapMaterial = NULL;
    103122}
    104123
     
    110129  LoadParam(root, "vegetation", this, Terrain, loadVegetation)
    111130      .describe("the fileName of the vegetation, that should be loaded onto this terrain. (must be relative to the data-dir)") ;
    112 }
     131
     132
     133  LoadParam(root, "height-map", this, Terrain, loadHeightMap)
     134      .describe("The HeightMap, splitted into two strings seperated by ','. 1: HeighMap, 2: ColorMap");
     135
     136
     137  LoadParam(root, "heigt-texture", this, Terrain, loadTexture)
     138      .describe("The name of the Texture for this heightMap");
     139}
     140
     141void Terrain::loadHeightMap(const char* heightMapFile, const char* colorMap)
     142{
     143  if (this->heightMap != NULL)
     144    delete this->heightMap;
     145  this->heightMap = NULL;
     146
     147  char* hmName = ResourceManager::getFullName(heightMapFile);
     148  char* hmColorName = ResourceManager::getFullName(colorMap);
     149
     150
     151  this->heightMap = new HeightMap(hmName, hmColorName);
     152  heightMap->scale(Vector(43.0f,4.7f,43.0f));
     153  heightMap->setAbsCoor(this->getAbsCoor());
     154  heightMap->load();
     155  delete[] hmName;
     156  delete[] hmColorName;
     157
     158}
     159
     160
     161void Terrain::loadTexture(const char* textureName)
     162{
     163  if (this->heightMapMaterial != NULL)
     164      delete this->heightMapMaterial;
     165
     166delete this->heightMapMaterial;
     167
     168   this->heightMapMaterial = new Material();
     169   heightMapMaterial->setTransparency(1.0);
     170
     171   heightMapMaterial->setDiffuse(1.0,1.0,1.0);
     172   heightMapMaterial->setAmbient(1.0,1.0,1.0 );
     173   heightMapMaterial->setSpecular(1.0,1.0,1.0);
     174   heightMapMaterial->setShininess(.5);
     175   heightMapMaterial->setTransparency(1.0);
     176
     177   heightMapMaterial->setDiffuseMap(textureName);
     178   heightMapMaterial->setAmbientMap(textureName);
     179   heightMapMaterial->setSpecularMap(textureName);
     180
     181
     182
     183}
     184
     185
    113186
    114187void Terrain::loadVegetation(const char* vegetationFile)
     
    128201
    129202
     203
     204
    130205void Terrain::draw () const
    131206{
     
    137212                this->getAbsCoor ().z);
    138213  /* rotate */
    139   Vector tmpRot = this->getAbsDir().getSpacialAxis();
    140   glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
     214 // Vector tmpRot = this->getAbsDir().getSpacialAxis();
     215  //glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
    141216
    142217  if (this->objectList)
     
    146221  if (this->vegetation)
    147222    this->vegetation->draw();
     223
     224  if(this->heightMap)
     225        {
     226        this->heightMapMaterial->select();
     227        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
     228        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
     229        glEnable(GL_LIGHTING);
     230        glColorMaterial ( GL_FRONT_AND_BACK, GL_EMISSION ) ;
     231        glEnable (GL_COLOR_MATERIAL) ;
     232        this->heightMap->draw();
     233        }
    148234  glPopMatrix();
    149235
     236
     237
     238
     239
     240 glMatrixMode(GL_MODELVIEW);
     241glPushMatrix();
     242glLoadIdentity();
     243 Vector camera =   State::getCamera()->getAbsCoor(); // Go on here ..........!!!
     244
     245    /*
     246    float height =    heightMap->getHeight(camera.x, camera.z);
     247
     248
     249  glEnable (GL_COLOR_MATERIAL) ;
     250    glBegin(GL_QUADS);            // Draw The Cube Using quads
     251    glColor3f(0.0f,1.0f,0.0f);  // Color Blue
     252    glVertex3f(camera.x + 63.0f,Terrain->getHeight(camera.x+63.0f, camera.z-10.0f)+13.0f,camera.z-10.0f);      // Top Right Of The Quad (Top)
     253    glVertex3f(camera.x-63.0f, getHeight(camera.x+63.0f, camera.z-10.0f)+13.0f,camera.z-10.0f);      // Top Left Of The Quad (Top)
     254    glVertex3f(camera.x-63.0f, getHeight(camera.x+63.0f, camera.z+10.0f)+13.0f, camera.z+10.0f);      // Bottom Left Of The Quad (Top)
     255    glVertex3f(camera.x+ 63.0f, getHeight(camera.x+63.0f, camera.z+10.0f)+13.0f, camera.z+10.0f);      // Bottom Right Of The Quad (Top)
     256   glEnd();                      // End Drawing The Plan
     257   */
     258
     259glPopMatrix();
    150260  /* THIS IS ONLY FOR DEBUGGING INFORMATION */
    151261  if (this->ssp != NULL)
     
    360470{
    361471}
     472
     473float Terrain::getHeight(float x, float y)
     474{
     475        if(this->heightMap != NULL)
     476                return (this->heightMap->getHeight(x, y));
     477        return 0;
     478}
  • trunk/src/world_entities/terrain.h

    r6512 r6956  
    1515// FORWARD DECLARATION
    1616class SpatialSeparation;
     17class HeightMap;
     18class Material;
    1719
    1820//! A simple method to call a desired debug world.
     
    4042  void loadVegetation(const char* vegetationFile);
    4143
     44  void loadHeightMap(const char* heightMapFile, const char* colorMap = NULL);
     45  void loadTexture(const char* textureName);
     46
    4247  void buildDebugTerrain(DebugTerrain debugTerrain);
     48 
     49  float getHeight(float x, float y);
    4350  virtual void draw() const;
    4451
     
    5057   int                 objectList;
    5158
     59   HeightMap*          heightMap;
     60   Material*           heightMapMaterial;
    5261};
    5362
Note: See TracChangeset for help on using the changeset viewer.