Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Uses VertexArrayModel now

  • Property svn:executable set to *
File size: 16.0 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
32HeightMap::HeightMap()
33{
34
35}
36
37HeightMap::HeightMap(const char* height_map_name = NULL) 
38{
39   this->setClassID(CL_HEIGHT_MAP, "HeightMap");
40   heightMap =  IMG_Load(height_map_name);
41   if(heightMap!=NULL) {
42       
43                 PRINTF(0)("loading Image %s\n", height_map_name);
44                 PRINTF(0)("width : %i\n", heightMap->w);
45                 PRINTF(0)("hight : %i\n", heightMap->h);
46                 PRINTF(0)("%i Byte(s) per Pixel \n", heightMap->format->BytesPerPixel);
47                 PRINTF(0)("Rshift : %i\n", heightMap->format->Rshift);
48                 PRINTF(0)("Bshift: %i\n", heightMap->format->Bshift);
49                 PRINTF(0)("Gshift: %i\n", heightMap->format->Gshift);
50                 PRINTF(0)("Rmask: %i\n", heightMap->format->Rmask);
51                 PRINTF(0)("Gmask: %i\n", heightMap->format->Gmask);           
52                }
53                 
54     else       PRINTF(4)("oops! couldn't load %s for some reason.\n", height_map_name);
55
56               
57           generateNormalVectorField();
58           
59           
60 //Defines 2 Materials (for testing only!)
61 this->tmp_mat = new Material(); 
62  tmp_mat->setTransparency(1.0);
63  tmp_mat->setIllum(0.3);
64  tmp_mat->setDiffuse(0.55,0.4,0.2);
65  tmp_mat->setAmbient(0.6,0.4,0.2);
66  tmp_mat->setSpecular(0.02,0.02,0.02);
67  tmp_mat->setShininess(.1);
68  tmp_mat->setTransparency(1.0);
69
70  tmp_mat->diffuseTexture = NULL;
71  tmp_mat->ambientTexture = NULL;
72  tmp_mat->specularTexture = NULL;
73
74 this->red_mat = new Material(); 
75  red_mat->setTransparency(1.0);
76  red_mat->setIllum(0.3);
77  red_mat->setDiffuse(0.55,0.1,0.1);
78  red_mat->setAmbient(0.55,0.1,0.1);
79  red_mat->setSpecular(0.02,0.02,0.02);
80  red_mat->setShininess(.1);
81  red_mat->setTransparency(1.0);
82
83  red_mat->diffuseTexture = NULL;
84  red_mat->ambientTexture = NULL;
85  red_mat->specularTexture = NULL;
86
87}
88
89HeightMap::HeightMap(const char* height_map_name = NULL, const char* colour_map_name = NULL) : VertexArrayModel()
90{
91this->setClassID(CL_HEIGHT_MAP, "HeightMap");
92
93   heightMap =  IMG_Load(height_map_name);
94   if(heightMap!=NULL) {
95       
96                 PRINTF(0)("loading Image %s\n", height_map_name);
97                 PRINTF(0)("width : %i\n", heightMap->w);
98                 PRINTF(0)("hight : %i\n", heightMap->h);
99                 PRINTF(0)("%i Byte(s) per Pixel \n", heightMap->format->BytesPerPixel);
100                 PRINTF(0)("Rshift : %i\n", heightMap->format->Rshift);
101                 PRINTF(0)("Bshift: %i\n", heightMap->format->Bshift);
102                 PRINTF(0)("Gshift: %i\n", heightMap->format->Gshift);
103                 PRINTF(0)("Rmask: %i\n", heightMap->format->Rmask);
104                 PRINTF(0)("Gmask: %i\n", heightMap->format->Gmask);           
105                }
106                 
107     else       PRINTF(4)("oops! couldn't load %s for some reason.\n", height_map_name);
108
109               
110           generateNormalVectorField();
111           
112           
113 //Defines 2 Materials (for testing only!)
114 this->tmp_mat = new Material("tmp"); 
115  tmp_mat->setTransparency(1.0);
116  tmp_mat->setIllum(0.3);
117  tmp_mat->setDiffuse(1.0,1.0,1.0);
118 tmp_mat->setAmbient(1.0,1.0,1.0);
119
120  tmp_mat->setSpecular(0.0,0.00,0.00);
121  tmp_mat->setShininess(1.0);
122 
123
124
125
126 this->red_mat = new Material("red"); 
127  red_mat->setTransparency(1.0);
128  red_mat->setIllum(3);
129  red_mat->setDiffuse(0.55,0.1,0.1);
130  red_mat->setAmbient(0.55,0.1,0.1);
131  red_mat->setSpecular(0.0,0.0,0.0);
132  red_mat->setShininess(1.0);
133  red_mat->setTransparency(1.0);
134
135 // red_mat->diffuseTexture = NULL;
136 // red_mat->ambientTexture = NULL;
137  //red_mat->specularTexture = NULL;
138 
139  if(colour_map_name != NULL)
140  {
141  colourMap = IMG_Load(colour_map_name);
142  if(colourMap != NULL)
143                {
144                 PRINTF(0)("loading Image %s\n", colour_map_name);
145                 PRINTF(0)("width : %i\n", colourMap->w);
146                 PRINTF(0)("hight : %i\n", colourMap->h);
147                 PRINTF(0)("%i Byte(s) per Pixel \n", colourMap->format->BytesPerPixel);
148                 PRINTF(0)("Rshift : %i\n", colourMap->format->Rshift);
149                 PRINTF(0)("Bshift: %i\n", colourMap->format->Bshift);
150                 PRINTF(0)("Gshift: %i\n", colourMap->format->Gshift);
151                 PRINTF(0)("Rmask: %i\n", colourMap->format->Rmask);
152                 PRINTF(0)("Gmask: %i\n", colourMap->format->Gmask);
153                 }
154                 else       PRINTF(4)("oops! couldn't load %s for some reason.\n", colour_map_name);
155                 
156  const  char* texture_name = "pictures/ground1.tga";
157  tmp_mat->setDiffuseMap(texture_name);
158  if(colourMap != NULL)
159  {
160  colours = (unsigned char *) colourMap->pixels;
161  hasColourMap = true;
162  }
163  else
164  {
165  hasColourMap = false;
166 
167 
168  }
169 
170 // tmp_mat->setAmbientMap(texture_name);
171  //tmp_mat->setSpecularMap(texture_name);
172 
173  // red_mat->setDiffuseMap(texture_name);
174  // red_mat->setAmbientMap(texture_name);
175  //red_mat->setSpecularMap(texture_name);                             
176  }
177 
178 
179}
180HeightMap::~HeightMap()
181{
182        delete heightMap;
183}
184
185void HeightMap::load()
186{
187
188this->camera = State::getCamera();
189this->camCoords = this->camera->getAbsCoor();
190float x = this->camCoords.x + 300;
191float y = this->camCoords.+ 100;
192
193tmp_mat->select();
194
195unsigned char height = 0; 
196 int offset = 0;
197 int g = 0;
198 float old_r = 0.0f;
199 float old_g = 0.0f;
200 float old_b = 0.0f;
201 heights  = (unsigned char*) heightMap->pixels;
202 bool colourChanged = true;
203 
204 
205 /*
206 if(heightMap != NULL && heightMap->format->BitsPerPixel == 8 )
207        {}
208        SDL_LockSurface(heightMap);
209       
210       
211               
212        for(int i = 0 ; i < heightMap->h -sampleRate ; i +=sampleRate)
213        {
214               
215                tmp_mat->select();
216
217
218               
219                int j = 0;
220       
221
222            for(int j = 0 ; j < heightMap->w -sampleRate  ;  j += sampleRate)
223            {
224                if(true)
225                {
226                float r = (float)colours[3*j+2  + 3*i*(heightMap->w )];
227                float g = (float)colours[3*j+1 + 3*i*(heightMap->w)];
228                float b = (float)colours[3*j+0 + 3*i*(heightMap->w)];
229                colourChanged = old_r != r || old_g != g || old_b != b;
230                old_r = r;
231                old_g = g;
232                old_b = b;
233                if(colourChanged)
234                {
235                tmp_mat->setAmbient(r/255.0,g/255.0,b/255.0);
236                tmp_mat->setDiffuse(r/255.0,g/255.0,b/255.0);
237                tmp_mat->select();     
238                }
239                }
240               
241               
242                height = heights[j+sampleRate + i*(heightMap->w )];
243                this->addNormal(normalVectorField[i][j+sampleRate].y,normalVectorField[i][j+sampleRate].z,normalVectorField[i][j+2].x);
244                this->addTexCoor(((j/sampleRate)%4)/4.0,((i/sampleRate)%4)/4.0);
245               
246                this->addVertex(scaleX*(heightMap->h -i),(double)((double)(height)*scaleY),scaleZ*(j+sampleRate)); // Top Right         
247                               
248               
249               
250            }
251               
252                       
253                       
254
255                       
256                       
257       
258
259               
260                }
261               
262           
263       
264        SDL_UnlockSurface(heightMap);
265       
266       
267        int cnt = 0;
268        for(int i = 0 ; i < heightMap->h -sampleRate ; i +=sampleRate)
269        {
270
271            for(int j = 0 ; j < heightMap->w -sampleRate  ;  j += sampleRate)
272            {
273               
274                this->addIndice(cnt);   
275                cnt ++;
276            }
277                if(i%2 == 1)
278                {
279                this->newStripe();
280                }
281        }       
282       
283       
284       
285       
286                     */
287       
288        this->planeModel();
289
290        this->finalize();
291
292
293}
294
295/*
296void HeightMap::draw()
297{
298
299this->camera = State::getCamera();
300this->camCoords = this->camera->getAbsCoor();
301float x = this->camCoords.x + 300;
302float y = this->camCoords.z  + 100;
303
304// Draw a red rectangle to test getHeight
305
306red_mat->select();
307glBegin(GL_TRIANGLE_STRIP);
308glNormal3f(0,1,0);
309glTexCoord2d(1.0,0.0);
310 glVertex3f(x-10,getHeight(x-10,y+10)+5.0f       ,y+10); // Top Left
311 
312
313glNormal3f(0,1,0);
314glTexCoord2d(1.0,1.0);
315 glVertex3f(x+10,getHeight(x+10,y+10)+5.0f ,y+10); // Top Left
316 
317
318glNormal3f(0,1,0);
319glTexCoord2d(0.0,0.0);
320 glVertex3f(x-10,getHeight(x-10,y-10)+5.0f ,y-10); // Top Left
321 
322
323glNormal3f(0,1,0);
324glTexCoord2d(0.0,1.0);
325 glVertex3f(x+10,getHeight(x+10,y-10)+5.0f ,y-10); // Top Left
326 
327glEnd();
328
329tmp_mat->select();
330
331unsigned char height = 0;
332 int offset = 0;
333 int g = 0;
334 float old_r = 0.0f;
335 float old_g = 0.0f;
336 float old_b = 0.0f;
337 heights  = (unsigned char*) heightMap->pixels;
338
339 bool colourChanged = true;
340 
341  if(heightMap != NULL && heightMap->format->BitsPerPixel == 8 )
342        {
343        SDL_LockSurface(heightMap);
344
345                   glBegin(GL_TRIANGLE_STRIP); 
346        for(int i = 0 ; i < heightMap->h -sampleRate ; i +=sampleRate)
347        {
348
349
350
351               
352                int j = 0;
353       
354/*
355                height = heights[j + i*(heightMap->w )];
356                glNormal3f(normalVectorField[i][j].y,normalVectorField[i][j].z,normalVectorField[i][j].x);
357                glVertex3f(20*(heightMap->h -(i))-1000,(double)((double)(height)/1-300),20*(j)-1000); // Top Left
358
359                        height = heights[j + (i+2)*(heightMap->w )];
360                glNormal3f(normalVectorField[i+2][j].y,normalVectorField[i+2][j].z,normalVectorField[i+2][j].x);
361                glVertex3f(20*(heightMap->h -(i+2))-1000,(double)((double)(height)/1-300),20*(j)-1000); // Bottom Left
362
363*/             
364/*
365            for(int j = 0 ; j < heightMap->w -sampleRate  ;  j += sampleRate)
366            {
367                if(hasColourMap)
368                {
369                float r = (float)colours[3*j+2  + 3*i*(heightMap->w )];
370                float g = (float)colours[3*j+1 + 3*i*(heightMap->w)];
371                float b = (float)colours[3*j+0 + 3*i*(heightMap->w)];
372                colourChanged = old_r != r || old_g != g || old_b != b;
373                old_r = r;
374                old_g = g;
375                old_b = b;
376                if(colourChanged)
377                {
378                 tmp_mat->setAmbient(r/255.0,g/255.0,b/255.0);
379                 tmp_mat->setDiffuse(r/255.0,g/255.0,b/255.0);
380                tmp_mat->select();     
381                }
382                }
383                */
384                //if(true/*(abs(-scaleX*i-x+5000) > 1100 || abs(scaleZ*j-y-2000)  > 1100 )*/){ //subdivide??
385                /*
386                height = heights[j+sampleRate + i*(heightMap->w )];
387                glNormal3f(normalVectorField[i][j+sampleRate].y,normalVectorField[i][j+sampleRate].z,normalVectorField[i][j+2].x);
388                glTexCoord2f(((j/sampleRate)%4)/4.0,((i/sampleRate)%4)/4.0);
389               
390                glVertex3f(scaleX*(heightMap->h -i),(double)((double)(height)*scaleY),scaleZ*(j+sampleRate)); // Top Right             
391                               
392                height = heights[j+sampleRate + (i+sampleRate)*(heightMap->w )];
393                glNormal3f(normalVectorField[i+sampleRate][j+sampleRate].y,normalVectorField[i+sampleRate][j+sampleRate].z,normalVectorField[i+sampleRate][j+sampleRate].x);
394                glTexCoord2f(((j/sampleRate)%4)/4.0,1/4.0+((i/sampleRate)%4)/4.0);
395                //glTexCoord2f(0.0,0.0);
396                glVertex3f(scaleX*(heightMap->h -(i+sampleRate)),(double)((double)(height)*scaleY),scaleZ*(j+sampleRate)); // Bottom Right
397 
398                }
399                else
400                {
401                        //red_mat->select();
402                        //glEnd();
403                       
404                       
405                       
406                        glBegin(GL_TRIANGLE_STRIP);     
407                       
408                                drawRect(j,i+sampleRate/2,j+sampleRate/2,i);
409                        glEnd();
410                        glBegin(GL_TRIANGLE_STRIP);
411                               
412                               
413                                drawRect(j+sampleRate/2,i+sampleRate/2,j+sampleRate,i);
414                        glEnd();
415                        glBegin(GL_TRIANGLE_STRIP);
416                               
417                       
418                                drawRect(j,i+sampleRate,j+sampleRate/2,i+sampleRate/2);
419                        glEnd();
420                        glBegin(GL_TRIANGLE_STRIP);
421                                drawRect(j+sampleRate/2,i+sampleRate,j+sampleRate,i+sampleRate/2);
422                       
423                        glEnd();
424                       
425                       
426
427                       
428                       
429                        //subdivide
430                }
431
432               
433                }
434                j =  heightMap->w -sampleRate;
435               
436               
437                glNormal3f(normalVectorField[i+sampleRate][j+sampleRate].y,normalVectorField[i+sampleRate][j+sampleRate].z,normalVectorField[i+sampleRate][j+sampleRate].x);
438                //glTexCoord2f(((j/sampleRate)%4)/4.0,1/4.0+((i/sampleRate)%4)/4.0);
439                //glTexCoord2f(0.0,0.0);
440                glTexCoord2f(((j/sampleRate)%4)/4.0,((i/sampleRate)%4)/4.0);
441                glVertex3f(scaleX*(heightMap->h -i),(double)((double)-1000.0),scaleZ*(j+sampleRate)); // Top Right
442               
443                glNormal3f(normalVectorField[i+sampleRate][j+sampleRate].y,normalVectorField[i+sampleRate][j+sampleRate].z,normalVectorField[i+sampleRate][j+sampleRate].x);
444                                glTexCoord2f(((j/sampleRate)%4)/4.0,1/4.0+((i/sampleRate)%4)/4.0);
445       
446                glVertex3f(scaleX*(heightMap->h -(i+sampleRate)),(double)-1000.0,scaleZ*(j+sampleRate)); // Bottom Right
447
448               
449               
450                               
451             
452                //glEnd();     
453           
454        }
455        SDL_UnlockSurface(heightMap);
456        glEnd();
457}
458
459}
460*/
461void HeightMap::generateNormalVectorField()
462{
463int delta = 1;
464heights  = (unsigned char*) heightMap->pixels;
465
466//Create a Dynamicly sized 2D-Array to store our normals
467normalVectorField =  new Vector* [heightMap->h];
468for(int i=0;i<heightMap->h;i++)
469normalVectorField [i]= new (Vector [heightMap->w]);
470
471
472
473// !!! Does not yet calculate any normals for some border points!!!!!
474
475if(heightMap != NULL && heightMap->format->BitsPerPixel == 8 )
476        {
477        SDL_LockSurface(heightMap);
478        for(int i = 0 ; i < heightMap->h -1  ; i ++)
479        {
480            for(int j = 0; j < heightMap->-1  ;  j ++)
481            {
482               
483
484                delta = (int)heights[j + (i+1)*(heightMap->w )] -  (int) heights[j + i*(heightMap->w )];
485                Vector a =  Vector(-20.0,(float)delta  ,0.0f);
486       
487                delta = (int)heights[j+1 + i*(heightMap->w )] - (int)heights[j + i*(heightMap->w )];
488                Vector b =  Vector(0.0f,(float) delta ,20.0);
489               
490       
491                 normalVectorField[i][j] = b.cross(a);
492                 normalVectorField[i][j].normalize();
493       
494             } 
495        }
496        SDL_UnlockSurface(heightMap);
497
498}
499
500}
501
502void HeightMap::drawRect(int xBottom, int yBottom, int xTop, int yTop )
503{
504        int height = 0;
505        if(true)
506        {
507               
508                height = heights[xTop + yTop*(heightMap->w )];
509                glNormal3f(normalVectorField[yTop][xTop].y,normalVectorField[yTop][xTop].z,normalVectorField[yTop][xTop].x);
510                glTexCoord2f(((yBottom/sampleRate)%8)/8.0+0.125,((xBottom/sampleRate)%8)/8.0+0.125);
511                glVertex3f(scaleX*(heightMap->h - yTop),(double)((double)(height)*scaleY),scaleZ*(xTop)); // Top Right
512
513                height = heights[xBottom + yTop*(heightMap->w )];
514                glNormal3f(normalVectorField[yTop][xBottom].y,normalVectorField[yTop][xBottom].z,normalVectorField[yTop][xBottom].x);
515                glTexCoord2f(((yBottom/sampleRate)%8)/8.0,((xBottom/sampleRate)%8)/8.0+0.125);
516                glVertex3f(scaleX*(heightMap->h -(yTop)),(double)((double)(height)*scaleY),scaleZ*(xBottom)); // Top Left
517               
518                height = heights[xTop + (yBottom)*(heightMap->w )];
519                glNormal3f(normalVectorField[yBottom][xTop].y,normalVectorField[yBottom][xTop].z,normalVectorField[yBottom][xTop].x);
520                glTexCoord2f(((yBottom/sampleRate)%8)/8.0+0.125,((yBottom/sampleRate)%8)/8.0);
521                glVertex3f(scaleX*(heightMap->h -(yBottom)),(double)((double)(height)*scaleY),scaleZ*(xTop)); // Bottom Right
522               
523height = heights[xBottom + (yBottom)*(heightMap->w )];
524                glNormal3f(normalVectorField[yBottom][xBottom].y,normalVectorField[yBottom][xBottom].z,normalVectorField[yBottom][xBottom].x);
525                glTexCoord2f(((yBottom/sampleRate)%8)/8.0,((yBottom/sampleRate)%8)/8.0);
526                glVertex3f(scaleX*(heightMap->h -(yBottom)),(double)((double)(height)*scaleY),scaleZ*(xBottom)); // Bottom Left
527
528               
529
530        }
531        else
532        {
533                // subdivide
534        }
535       
536}
537
538
539void HeightMap::fixBoarder(int xBottomLeft, int yBottomLeft, int xTopRight, int yTopRight)
540{
541
542        int height = 0;
543       
544        for(int i= xTopRight; i < xBottomLeft ; i+= sampleRate)
545        {
546       
547        glBegin(GL_TRIANGLES);
548                       
549                        height = heights[yBottomLeft + (i + sampleRate)*(heightMap->w )];
550                        glNormal3f(0,1,0);
551                        glVertex3f(scaleX*(heightMap->h -i-sampleRate),scaleY*height,scaleZ*(yBottomLeft));
552                       
553                        height = heights[yBottomLeft + i*(heightMap->w )];
554                        glNormal3f(0,1,0);
555                        glVertex3f(scaleX*(heightMap->h -i),scaleY*height,scaleZ*(yBottomLeft)); 
556                       
557                        height = heights[yBottomLeft + (i + sampleRate/2)*(heightMap->w )];
558                        glNormal3f(0,1,0);
559                        glVertex3f(scaleX*(heightMap->h -i - sampleRate/2),scaleY*height,scaleZ*(yBottomLeft)); 
560                       
561                         
562                       
563                        glEnd();
564                               
565                       
566                               
567       
568        }
569        for(int j= yBottomLeft; j < yTopRight; j+= sampleRate)
570        {
571                   glBegin(GL_TRIANGLES);
572                        height = heights[j + (xBottomLeft+sampleRate)*(heightMap->w )];
573                        glNormal3f(-1,0,0);
574                        glVertex3f(scaleX*(heightMap->h -xBottomLeft -sampleRate),scaleY*height,scaleZ*(j)); 
575                       
576                        height = heights[j + sampleRate/2  + (xBottomLeft + sampleRate)*(heightMap->w )];
577                        glNormal3f(-1,0,0);
578                        glVertex3f(scaleX*(heightMap->h -xBottomLeft - sampleRate),scaleY*height,scaleZ*(j+sampleRate/2)); 
579                       
580                        height = heights[j + sampleRate + (xBottomLeft + sampleRate)*(heightMap->w )];
581                        glNormal3f(-1,0,0);
582                        glVertex3f(scaleX*(heightMap->h -xBottomLeft-sampleRate),scaleY*height,scaleZ*(j+sampleRate)); 
583                       
584                        glEnd();
585        }
586}
587
588void HeightMap::scale(Vector v)
589{
590 scaleX = v.x;
591 scaleY = v.y;
592 scaleZ = v.z;
593}
594
595// Accepts Coordinates relative to HeightMap
596float HeightMap::getHeight(float x, float y)
597{
598 int xInt = (int)x / scaleX;  x -= (float)xInt*scaleX; xInt = heightMap->h - xInt;
599 int yInt = (int)y / scaleZ;    y -= (float)yInt*scaleZ; 
600 if(xInt <= 0 || xInt >= heightMap->h || yInt <= 0 || yInt >= heightMap->) return 0.0f;
601 float height = heights[yInt + (xInt)*heightMap->w]*scaleY;
602 float a = normalVectorField[(xInt)][yInt].x;
603 float b = normalVectorField [(xInt)][yInt].z;
604 float c = normalVectorField [(xInt)][yInt].y;
605  height -= ( (a/c)*(x) + (b/c)*(y))*scaleY;
606 return height;
607}
Note: See TracBrowser for help on using the repository browser.