Home || Architecture || Video Search || Visual Search || Scripts || Applications || Important Messages || OGL || Src

HeightMap.h

Go to the documentation of this file.
00001 #ifndef Impala_Visualisation_Plot_HeightMap_h
00002 #define Impala_Visualisation_Plot_HeightMap_h
00003 
00004 #include "Visualization/Plot/Plottable.h"
00005 #include "Core/Array/PixMinMax.h"
00006 
00007 namespace Impala
00008 {
00009 namespace Visualization
00010 {
00011 namespace Plot
00012 {
00013 
00014 
00020 class HeightMap : public Plottable
00021 {
00022 public:
00023     HeightMap()
00024     {
00025         mData = 0;
00026         mScanX = 0;
00027         mScanY = 0;
00028     }
00029 
00030     virtual void
00031     Draw(Plot* plot)
00032     {
00033         if (mData == 0)
00034             return;
00035 
00036         // adjust rendering settings
00037         glPushAttrib(GL_ALL_ATTRIB_BITS);
00038         glEnable(GL_LIGHTING);
00039         glEnable(GL_LIGHT0);
00040         glEnable(GL_NORMALIZE);
00041         // draw surface sligtly behind lines
00042         glEnable(GL_POLYGON_OFFSET_FILL);
00043         glPolygonOffset(1,1);
00044 
00045         // setup light independent of model view
00046         glMatrixMode (GL_MODELVIEW);
00047         glPushMatrix();
00048         glLoadIdentity();
00049         GLfloat materialSpecular[] = { 1.0, 1.0, 1.0, 1.0 };
00050         GLfloat materialShininess[] = { 10.0 };
00051         GLfloat lightPosition[] = { 0.0, 0.0, 1000.0, 0.0 };
00052         glClearColor (0.0, 0.0, 0.0, 0.0);
00053         glShadeModel (GL_SMOOTH);
00054         glMaterialfv(GL_FRONT, GL_SPECULAR, materialSpecular);
00055         glMaterialfv(GL_FRONT, GL_SHININESS, materialShininess);
00056         glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
00057         glPopMatrix();
00058 
00059 
00060         int h = mData->H()-1;
00061         int w = mData->W()-1;
00062         double x,z;
00063         double dx = ((double)mMaxX-mMinX) / (double)w;
00064         double dz = ((double)mMaxZ-mMinZ) / (double)h;
00065         double v1,v2,v3,v4;
00066         z = mMinZ;
00067         for (int i=0 ; i<h ; i++)
00068         {
00069             x = mMinX;
00070             for (int j=0 ; j<w ; j++)
00071             {
00072                 v1 = mData->Val(j,i);
00073                 v2 = mData->Val(j,i+1);
00074                 v3 = mData->Val(j+1,i+1);
00075                 v4 = mData->Val(j+1,i);
00076 
00077                 // outlines
00078                 glBegin(GL_LINE_LOOP);
00079                 glColor3f(0, 0, 0);
00080                 glMaterialfv(GL_FRONT, GL_AMBIENT, materialSpecular);
00081                 glVertex3d(x,   v1,z);
00082                 glVertex3d(x,   v2,z+dz);
00083                 glVertex3d(x+dx,v3,z+dz);
00084                 glVertex3d(x+dx,v4,z);
00085                 glEnd();
00086 
00087                 // surface
00088                 glBegin(GL_QUADS);
00089                 glNormal3d(-dz*(v4-v1), -dx*(v2-v1), dx*dz);
00090                 Val2Material(v1); glVertex3d(x,   v1,z);
00091                 Val2Material(v2); glVertex3d(x,   v2,z+dz);
00092                 Val2Material(v3); glVertex3d(x+dx,v3,z+dz);
00093                 Val2Material(v4); glVertex3d(x+dx,v4,z);
00094                 glEnd();
00095 
00096                 x += dx;
00097             }
00098             z += dz;
00099         }
00100 
00101         // prevent our render settings from affecting the rest
00102         glPopAttrib();
00103     }
00104 
00105     void
00106     SetData(Core::Array::Array2dScalarReal64* data)
00107     {
00108         mData = data;
00109         double min, max;
00110         Core::Array::PixMinMax(data, &min, &max);
00111         int w = data->W();
00112         int h = data->H();
00113         SetDimensionsX(-w/2, w/2);
00114         SetDimensionsY(min, max);
00115         SetDimensionsZ(-h/2, h/2);
00116     }
00117 
00118     void
00119     Val2Color(double v)
00120     {
00121         v -= mMinY;
00122         v /= (mMaxY - mMinY);
00123         double r,g,b;
00124         r = v;
00125         g = 1.-v;
00126         b = -fabs(v - .5)*2.;
00127         glColor3f(r, g, b);
00128     }
00129 
00130     void
00131     Val2Material(double v)
00132     {
00133         GLfloat material[4] = { 1.0, 1.0, 1.0, 1.0 };
00134         v -= mMinY;
00135         v /= (mMaxY - mMinY);
00136         material[0] = v;
00137         material[1] = 1.-v;
00138         material[2] = -fabs(v - .5)*2.;
00139         glMaterialfv(GL_FRONT, GL_AMBIENT, material);
00140     }
00141 
00142 protected:
00143     Core::Array::Array2dScalarReal64* mData;
00144     int mScanX, mScanY;
00145 };
00146 
00147 }//namespace Impala
00148 }//namespace Visulization
00149 }//namespace Plot
00150 
00151 #endif Impala_Visualisation_Plot_HeightMap_h

Generated on Fri Mar 19 09:31:51 2010 for ImpalaSrc by  doxygen 1.5.1