00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "ExCHeightMap.h"
00025
00026 ExCHeightMap::ExCHeightMap(void)
00027 {
00028 m_FogDepthLevel = 55.0f;
00029 m_MapDetailLevel = 8;
00030 m_MapResolution = 1024;
00031 m_Texture2Repetition = 50;
00032 m_Fog=true;
00033 m_MapScale=4;
00034 }
00035
00036 ExCHeightMap::~ExCHeightMap(void)
00037 {
00038 }
00039
00040
00041 void ExCHeightMap::Load(std::string FileName)
00042 {
00043 Guard(void ExCHeightMap::Load(std::string FileName))
00044 g_HeightMap = new byte[m_MapResolution*m_MapResolution];
00045 char buffer[255],b[255];
00046 std::string buffstring;
00047 std::ifstream file;
00048
00049 #ifdef UNIX_SRC
00050 sprintf(buffer, PREFIX "/ExNihilo/Data/Map/%s", FileName.data());
00051 #else
00052 sprintf(buffer, "../Data/Map/%s", FileName.data());
00053 #endif
00054 file.open(buffer,std::ios::in);
00055 if(file.is_open())
00056 {
00057 memset(b,0,255);file.getline(b,256,'\n');
00058 try
00059 {
00060 std::cout<<"Plok1"<<std::endl;
00061 if(ExNihilo::ExtractIntValueFromSring(b,"<FogEnable>","<#FogEnable>")==1)
00062 {
00063 m_Fog=true;
00064 }else
00065 {
00066 m_Fog=false;
00067 }
00068 memset(b,0,255);file.getline(b,256,'\n');
00069 m_FogDepthLevel=ExNihilo::ExtractFloatValueFromSring(b,"<FogDepthLevel>","<#FogDepthLevel>");
00070 memset(b,0,255);file.getline(b,256,'\n');
00071 m_MapDetailLevel=ExNihilo::ExtractFloatValueFromSring(b,"<MapDetailLevel>","<#MapDetailLevel>");
00072 memset(b,0,255);file.getline(b,256,'\n');
00073 m_MapResolution=ExNihilo::ExtractFloatValueFromSring(b,"<MapResolution>","<#MapResolution>");
00074 memset(b,0,255);file.getline(b,256,'\n');
00075 m_MapScale=ExNihilo::ExtractFloatValueFromSring(b,"<MapScale>","<#MapScale>");
00076 memset(b,0,255);file.getline(b,256,'\n');
00077 m_TextureMap="../Data/Map/"+ExNihilo::ExtracValueFromSring(b,"<HeightTexture>","<#HeightTexture>");
00078 FILE *pFile = NULL;
00079 pFile = fopen(m_TextureMap.data(), "rb" );
00080 if ( pFile == NULL )
00081 {
00082 std::cout<<"Can't find the height map! Error"<<std::endl;
00083 return;
00084 }
00085
00086 fread( g_HeightMap, 1, m_MapResolution * m_MapResolution, pFile );
00087 if(ferror( pFile ))
00088 {
00089 std::cout<<"Can't get data! Error"<<std::endl;
00090 }
00091 fclose(pFile);
00092
00093 m_VecTexture.resize(ManagerTexture->GetMaxMultitexturingLevel());
00094 memset(b,0,255);file.getline(b,256,'\n');
00095 SetTextureLand(0,ExNihilo::ExtracValueFromSring(b,"<TextureLand1>","<#TextureLand1>"));
00096 memset(b,0,255);file.getline(b,256,'\n');
00097 SetTextureLand(1,ExNihilo::ExtracValueFromSring(b,"<TextureLand2>","<#TextureLand2>"));
00098 memset(b,0,255);file.getline(b,256,'\n');
00099 SetTextureLand(2,ExNihilo::ExtracValueFromSring(b,"<TextureLand3>","<#TextureLand3>"));
00100 memset(b,0,255);file.getline(b,256,'\n');
00101 SetTextureLand(3,ExNihilo::ExtracValueFromSring(b,"<TextureLand4>","<#TextureLand4>"));
00102
00103
00104 memset(b,0,255);file.getline(b,256,'\n');
00105 buffstring=ExNihilo::ExtracValueFromSring(b,"<Position>","<#Position>");
00106 m_Position.SetX(ExNihilo::ExtractFloatValueFromSring(buffstring,"<X>","<#X>"));
00107 m_Position.SetY(ExNihilo::ExtractFloatValueFromSring(buffstring,"<Y>","<#Y>"));
00108 m_Position.SetZ(ExNihilo::ExtractFloatValueFromSring(buffstring,"<Z>","<#Z>"));
00109
00110 }catch(ExCExpStringNotFound){std::cout<<"Bad file type "<<buffer<<std::endl;}
00111 }else
00112 {
00113 std::cout<<"File not found:"<<buffer<<std::endl;
00114 throw ExCExpFileNotFound();
00115 }
00116 m_Position.SetX(m_Position.GetX()-(m_MapResolution*m_MapScale/2));
00117 m_Position.SetZ(m_Position.GetZ()-(m_MapResolution*m_MapScale/2));
00118
00119
00120 UnGuard
00121 }
00122
00123 void ExCHeightMap::SetTextureLand(unsigned int Number,std::string FileName)
00124 {
00125 Guard(void ExCHeightMap::SetTextureLand(unsigned int Number,std::string FileName))
00126 if (m_VecTexture.size()>Number)
00127 {
00128 m_VecTexture.at(Number)=FileName;
00129 }
00130 UnGuard
00131 }
00132
00133 void ExCHeightMap::Draw(void)
00134 {
00135 Guard(void ExCHeightMap::Draw(void))
00136 glPushAttrib(GL_ALL_ATTRIB_BITS);
00137 glPushMatrix();
00138 glTranslatef(m_Position.GetX(),m_Position.GetY(),m_Position.GetZ());
00139 glScalef(m_MapScale,m_MapScale,m_MapScale);
00140 glDisable(GL_LIGHTING);
00141 glEnable(GL_TEXTURE_2D);
00142 glColor3f(1.0f,1.0f,1.0f);
00143
00144
00145
00146
00147 try
00148 {
00149 RenderHeightMap();
00150 }catch (...)
00151 {
00152 std::cout<<"Exception render heigtmap"<<std::endl;
00153 }
00154 glPopMatrix();
00155 glPopAttrib();
00156 UnGuard
00157 }
00158
00159 void ExCHeightMap::IncreaseDetailLevel(void)
00160 {
00161 Guard(void ExCHeightMap::IncreaseDetailLevel(void))
00162 m_MapDetailLevel=m_MapDetailLevel*2;
00163 if(m_MapDetailLevel>=m_MapResolution)m_MapDetailLevel=m_MapResolution;
00164 UnGuard
00165 }
00166
00167 void ExCHeightMap::DereaseDetailLevel(void)
00168 {
00169 Guard(void ExCHeightMap::DereaseDetailLevel(void))
00170 m_MapDetailLevel=m_MapDetailLevel/2;
00171 if (m_MapDetailLevel<=0)m_MapDetailLevel=1;
00172 UnGuard
00173 }
00174
00175 void ExCHeightMap::IncreaseFogDepthLevel(void)
00176 {
00177 Guard(void ExCHeightMap::IncreaseFogDepthLevel(void))
00178 m_FogDepthLevel++;
00179 UnGuard
00180 }
00181
00182 void ExCHeightMap::DereaseFogDepthLevel(void)
00183 {
00184 Guard(void ExCHeightMap::DereaseFogDepthLevel(void))
00185 m_FogDepthLevel--;
00186 if (m_FogDepthLevel<=0)m_FogDepthLevel=1;
00187 UnGuard
00188 }
00189
00190 void ExCHeightMap::IncreaseTexture2Repetition(void)
00191 {
00192 Guard(void ExCHeightMap::IncreaseTexture2Repetition(void))
00193 m_Texture2Repetition++;
00194 UnGuard
00195 }
00196
00197 void ExCHeightMap::DereaseTexture2Repetition(void)
00198 {
00199 Guard(void ExCHeightMap::DereaseTexture2Repetition(void))
00200 m_Texture2Repetition--;
00201 if (m_Texture2Repetition<=0)m_Texture2Repetition=1;
00202 UnGuard
00203 }
00204
00205
00211
00212 int ExCHeightMap::Height(int X, int Y)
00213 {
00214
00215
00216
00217
00218
00219 int x = X % m_MapResolution;
00220 int y = Y % m_MapResolution;
00221
00222 if(!g_HeightMap) return 0;
00223
00224
00225
00226
00227
00228
00229
00230 return g_HeightMap[x + (y * m_MapResolution)];
00231 }
00232
00233 void ExCHeightMap::SetTextureCoord(float CoordU,float CoordV)
00234 {
00235 Guard(void ExCHeightMap::SetTextureCoord(float CoordU,float CoordV))
00236 for(unsigned int i=0;i<m_VecTexture.size();i++){ManagerTexture->SetTextureCoord(i,CoordU,CoordV);}
00237 UnGuard
00238 }
00239 void ExCHeightMap::BuildArray(void)
00240 {
00241 int verticecount=0;
00242 float xmul=1;
00243 float ymul=1.5;
00244 float zmul=1;
00245 int X = 0, Y = 0;
00246 int x, y, z;
00247 float fogY = 0;
00248 bool bSwitchSides = false;
00249
00250 ExCVec3D Vec;
00251
00252 TabVertices= new float[6*(33282)];
00253 TabTextures= new float[4*(33282)];
00254
00255
00256 int i=0;
00257 for ( X = 0; X <= m_MapResolution; X += m_MapDetailLevel )
00258 {
00259
00260 if(bSwitchSides)
00261 {
00262 for ( Y = m_MapResolution; Y >= 0; Y -= m_MapDetailLevel )
00263 {
00264 x = X;
00265 y = Height(X, Y );
00266 z = Y;
00267
00268 TabTextures[0+i*4]=(float)x / (float)m_MapResolution;
00269 TabTextures[1+i*4]=-(float)z / (float)m_MapResolution;
00270
00271 Vec.SetValue(x*xmul, y*ymul, z*zmul);
00272 TabVertices[0+i*6]=Vec.GetX();
00273 TabVertices[1+i*6]=Vec.GetY();
00274 TabVertices[2+i*6]=Vec.GetZ();
00275
00276 verticecount++;
00277 x = X + m_MapDetailLevel;
00278 y = Height(X + m_MapDetailLevel, Y );
00279 z = Y;
00280
00281 TabTextures[2+i*4]=(float)x / (float)m_MapResolution;
00282 TabTextures[3+i*4]=-(float)z / (float)m_MapResolution;
00283
00284 Vec.SetValue(x*xmul, y*ymul, z*zmul);
00285 TabVertices[3+i*6]=Vec.GetX();
00286 TabVertices[4+i*6]=Vec.GetY();
00287 TabVertices[5+i*6]=Vec.GetZ();
00288
00289 verticecount++;
00290 i++;
00291 }
00292 }
00293 else
00294 {
00295 for ( Y = 0; Y <= m_MapResolution; Y += m_MapDetailLevel )
00296 {
00297 x = X + m_MapDetailLevel;
00298 y = Height(X + m_MapDetailLevel, Y );
00299 z = Y;
00300
00301 TabTextures[0+i*4]=(float)x / (float)m_MapResolution;
00302 TabTextures[1+i*4]=-(float)z / (float)m_MapResolution;
00303
00304 Vec.SetValue(x, y*ymul, z);
00305 TabVertices[0+i*6]=Vec.GetX();
00306 TabVertices[1+i*6]=Vec.GetY();
00307 TabVertices[2+i*6]=Vec.GetZ();
00308
00309 verticecount++;
00310 x = X;
00311 y = Height(X, Y );
00312 z = Y;
00313
00314 TabTextures[2+i*4]=(float)x / (float)m_MapResolution;
00315 TabTextures[3+i*4]=-(float)z / (float)m_MapResolution;
00316
00317 Vec.SetValue(x*xmul, y*ymul, z*zmul);
00318 TabVertices[3+i*6]=Vec.GetX();
00319 TabVertices[4+i*6]=Vec.GetY();
00320 TabVertices[5+i*6]=Vec.GetZ();
00321
00322 verticecount++;
00323 i++;
00324 }
00325 }
00326 bSwitchSides = !bSwitchSides;
00327 std::cout<<i<<":"<<verticecount<<std::endl;
00328 }
00329
00330 m_Array=true;
00331 }
00332 void ExCHeightMap::RenderArray()
00333 {
00334 Guard(void ExCHeightMap::RenderArray())
00335 glPushMatrix();
00336 glTranslatef(0,-10,0);
00337 glScalef(1.4,1.5,3);
00338 ManagerTexture->SetTexture(0,m_VecTexture.at(0));
00339 ManagerTexture->SetTexture(1,m_VecTexture.at(1));
00340
00341 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
00342 glTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 2);
00343 glMatrixMode(GL_TEXTURE);
00344 glPushMatrix();
00345 glLoadIdentity();
00346 glScalef((float)m_Texture2Repetition, (float)m_Texture2Repetition, 1);
00347 glMatrixMode(GL_MODELVIEW);
00348 ManagerTexture->SetTexture(2,m_VecTexture.at(2));
00349 ManagerTexture->SetTexture(3,m_VecTexture.at(3));
00350
00351
00352 glTexCoordPointer(2,GL_FLOAT,0,TabTextures);
00353 glVertexPointer(2,GL_FLOAT,0,TabVertices);
00354 glDrawArrays( GL_TRIANGLE_STRIP, 0, 11094 );
00355
00356 glMatrixMode(GL_TEXTURE);
00357 ManagerTexture->SetTexture(1,m_VecTexture.at(1));
00358 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
00359 glPopMatrix();
00360 glMatrixMode(GL_MODELVIEW);
00361 glMatrixMode(GL_TEXTURE);
00362 ManagerTexture->SetTexture(2,m_VecTexture.at(2));
00363 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
00364 glPopMatrix();
00365 glMatrixMode(GL_MODELVIEW);
00366 ManagerTexture->ResetMultitexture();
00367
00368 glPopMatrix();
00369 UnGuard
00370 }
00371
00372 void ExCHeightMap::RenderHeightMap()
00373 {
00374 Guard(void ExCHeightMap::RenderHeightMap())
00375 float xmul=1;
00376 float ymul=1.5;
00377 float zmul=1;
00378
00379
00380
00381 int X = 0, Y = 0;
00382 int x, y, z;
00383 float fogY = 0;
00384 bool bSwitchSides = false;
00385
00386 glPushMatrix();
00387 glTranslatef(0,-10,0);
00388 glScalef(1.4,1.5,3);
00389
00390 if(m_Fog){ManagerFog->EnableFog();}
00391
00392 ManagerTexture->SetTexture(0,m_VecTexture.at(0));
00393 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
00394
00395
00396
00397 ManagerTexture->SetTexture(1,m_VecTexture.at(1));
00398 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
00399
00400
00401
00402 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
00403 glTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 2);
00404 glMatrixMode(GL_TEXTURE);
00405 glPushMatrix();
00406 glLoadIdentity();
00407 glScalef((float)m_Texture2Repetition, (float)m_Texture2Repetition, 1);
00408 glMatrixMode(GL_MODELVIEW);
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422 glBegin( GL_TRIANGLE_STRIP );
00423
00424 for ( X = 0; X <= m_MapResolution; X += m_MapDetailLevel )
00425 {
00426
00427 if(bSwitchSides)
00428 {
00429
00430
00431
00432 for ( Y = m_MapResolution; Y >= 0; Y -= m_MapDetailLevel )
00433 {
00434
00435 x = X;
00436 y = Height(X, Y );
00437 z = Y;
00438
00439
00440 if(m_Fog)
00441 y > m_FogDepthLevel ? ManagerFog->SetVolumetricFogCoord(0) : ManagerFog->SetVolumetricFogCoord( -((float)y - m_FogDepthLevel));
00442
00443
00444 SetTextureCoord((float)x / (float)m_MapResolution,-(float)z / (float)m_MapResolution);
00445 glVertex3i(x*xmul, y*ymul, z*zmul);
00446
00447 x = X + m_MapDetailLevel;
00448 y = Height(X + m_MapDetailLevel, Y );
00449 z = Y;
00450
00451 if(m_Fog)
00452 y > m_FogDepthLevel ? ManagerFog->SetVolumetricFogCoord(0) : ManagerFog->SetVolumetricFogCoord( -((float)y - m_FogDepthLevel));
00453
00454 SetTextureCoord((float)x / (float)m_MapResolution,-(float)z / (float)m_MapResolution);
00455 glVertex3i(x*xmul, y*ymul, z*zmul);
00456 }
00457 }
00458 else
00459 {
00460
00461
00462
00463 for ( Y = 0; Y <= m_MapResolution; Y += m_MapDetailLevel )
00464 {
00465
00466 x = X + m_MapDetailLevel;
00467 y = Height(X + m_MapDetailLevel, Y );
00468 z = Y;
00469
00470 if(m_Fog)
00471 y > m_FogDepthLevel ? ManagerFog->SetVolumetricFogCoord(0) : ManagerFog->SetVolumetricFogCoord( -((float)y - m_FogDepthLevel));
00472
00473 SetTextureCoord((float)x / (float)m_MapResolution,-(float)z / (float)m_MapResolution);
00474 glVertex3i(x, y*ymul, z);
00475
00476 x = X;
00477 y = Height(X, Y );
00478 z = Y;
00479 if(m_Fog)
00480 y > m_FogDepthLevel ? ManagerFog->SetVolumetricFogCoord(0) : ManagerFog->SetVolumetricFogCoord( -((float)y - m_FogDepthLevel));
00481
00482
00483 SetTextureCoord((float)x / (float)m_MapResolution,-(float)z / (float)m_MapResolution);
00484 glVertex3i(x*xmul, y*ymul, z*zmul);
00485 }
00486 }
00487
00488 bSwitchSides = !bSwitchSides;
00489 }
00490 glEnd();
00491 if(m_Fog)ManagerFog->DisableFog();
00492 glMatrixMode(GL_TEXTURE);
00493 ManagerTexture->SetTexture(1,m_VecTexture.at(1));
00494 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
00495 glPopMatrix();
00496 glMatrixMode(GL_MODELVIEW);
00497
00498
00499
00500
00501
00502
00503 ManagerTexture->ResetMultitexture();
00504 glPopMatrix();
00505 UnGuard
00506 }
00507