00001 #include "LinearGrid.h"
00002 #define NORM
00003 const double LinearGrid::eps = 1.19209290e-07;
00004
00005
00006
00007 LinearGrid::LinearGrid(const unsigned int _gridSize):gridSize(_gridSize)
00008 {
00009 texture = new Texture();
00010 texture->GenerateTexture(0);
00011
00012
00013 SetGrid();
00014
00015
00016 gridSize = (unsigned int) linearGrid.size();
00017 }
00018
00019 LinearGrid::~LinearGrid(void)
00020 {
00021
00022 }
00023
00024 const Texture* LinearGrid::GetTexture() const
00025 {
00026 return texture;
00027 }
00028
00029
00030 void LinearGrid::SetGrid()
00031 {
00032 TexturedGrid tg;
00033 double v = 0.0;
00034 const double vTick = 1.0/(double)(gridSize);
00035
00036
00037 for (int vIndex = -(int)(gridSize/2); vIndex <= (int)(gridSize/2); vIndex++)
00038 {
00039 if(vIndex == 0)
00040 {
00041
00042 #ifdef NORM
00043 tg.complex.imag(-eps/(gridSize/2.0));
00044 #else
00045 tg.complex.imag(-eps);
00046 #endif
00047 tg.vCoordinate = v;
00048 tg.isCut = true;
00049 SetVector(tg);
00050
00051
00052 #ifdef NORM
00053 tg.complex.imag(eps/(gridSize/2.0));
00054 #else
00055 tg.complex.imag(eps);
00056 #endif
00057 tg.vCoordinate = v;
00058 tg.isCut = false;
00059 SetVector(tg);
00060 }
00061 else
00062 {
00063 tg.isCut = false;
00064
00065 #ifdef NORM
00066 tg.complex.imag((double)vIndex/(gridSize/2.0));
00067 #else
00068 tg.complex.imag((double)vIndex);
00069 #endif
00070 tg.vCoordinate = v;
00071 SetVector(tg);
00072 }
00073
00074
00075 v += vTick;
00076 }
00077 }
00078
00079 void LinearGrid::SetVector(TexturedGrid _tg)
00080 {
00081 double u = 0.0;
00082 const double uTick = 1.0/(double)(gridSize);
00083
00084
00085 for (int hIndex = -(int)(gridSize/2); hIndex <= (int)(gridSize/2); hIndex++)
00086 {
00087 if (hIndex == 0)
00088 {
00089
00090 #ifdef NORM
00091 _tg.complex.real(-eps/(gridSize/2.0));
00092 #else
00093 _tg.complex.real(-eps);
00094 #endif
00095 _tg.uCoordinate = u;
00096 linearVector.push_back(_tg);
00097
00098
00099 #ifdef NORM
00100 _tg.complex.real(eps/(gridSize/2.0));
00101 #else
00102 _tg.complex.real(eps);
00103 #endif
00104 _tg.uCoordinate = u;
00105 linearVector.push_back(_tg);
00106 }
00107 else
00108 {
00109
00110 #ifdef NORM
00111 _tg.complex.real((double) hIndex/(gridSize/2.0));
00112 #else
00113 _tg.complex.real((double)(hIndex));
00114 #endif
00115 _tg.uCoordinate = u;
00116 linearVector.push_back(_tg);
00117 }
00118
00119 u += uTick;
00120 }
00121
00122 linearGrid.push_back(linearVector);
00123 linearVector.clear();
00124 }
00125
00126 void LinearGrid::Draw()
00127 {
00128 double x, y, u, v;
00129
00130 glColor3f(0.0, 0.0, 0.0);
00131 glPointSize(5.0f);
00132 glBegin(GL_POINTS);
00133 glVertex2i(0,0);
00134 glEnd();
00135 glColor3f(1.0, 0.0, 0.0);
00136 glPointSize(1.0f);
00137
00138 glEnable(GL_TEXTURE_2D);
00139 glBindTexture(GL_TEXTURE_2D, texture->GetTextureID());
00140
00141 for(GridIteratorConst gridIterator = linearGrid.begin(); gridIterator != linearGrid.end() - 1; ++gridIterator)
00142 {
00143 VectorIteratorConst vItrCurrentRow = gridIterator->begin();
00144 VectorIteratorConst vItrUpperRow = (gridIterator+1)->begin();
00145
00146 glBegin(GL_QUAD_STRIP);
00147 for (unsigned int i = 0; i < gridIterator->size(); i++)
00148 {
00149
00150 x = vItrCurrentRow->complex.imag();
00151 y = vItrCurrentRow->complex.real();
00152
00153 v = vItrCurrentRow->uCoordinate;
00154 u = vItrCurrentRow->vCoordinate;
00155
00156 glTexCoord2f(u,v);
00157 glVertex2d(x,y);
00158
00160 x = vItrUpperRow->complex.imag();
00161 y = vItrUpperRow->complex.real();
00162
00163 v = vItrUpperRow->uCoordinate;
00164 u = vItrUpperRow->vCoordinate;
00165
00166 glTexCoord2f(u,v);
00167 glVertex2d(x,y);
00168
00169 ++vItrCurrentRow;
00170 ++vItrUpperRow;
00171
00172 }
00173 glEnd();
00174 }
00175
00176 glDisable(GL_TEXTURE_2D);
00177
00178 }