Dem.cpp
上传用户:dfjhuyju
上传日期:2013-03-13
资源大小:11035k
文件大小:7k
源码类别:

OpenGL

开发平台:

Visual C++

  1. // Dem.cpp: implementation of the CDem class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "LiFeng.h"
  6. #include "Dem.h"
  7. #ifdef _DEBUG
  8. #undef THIS_FILE
  9. static char THIS_FILE[]=__FILE__;
  10. #define new DEBUG_NEW
  11. #endif
  12. //////////////////////////////////////////////////////////////////////
  13. // Construction/Destruction
  14. //////////////////////////////////////////////////////////////////////
  15. CDem::CDem()
  16. {
  17. m_fHeight = NULL;
  18. }
  19. CDem::~CDem()
  20. {
  21. if(m_fHeight!=NULL)
  22. {
  23. delete [] m_fHeight;
  24. }
  25. }
  26. void CDem::ReadDem(char *demfile)
  27. {
  28. CFile findem;
  29. findem.Open(demfile,CFile::modeRead);
  30. findem.Read(&m_fLeftDownX,sizeof(double));
  31. findem.Read(&m_fLeftDownY,sizeof(double));
  32. findem.Read(&m_fRightUpX,sizeof(double));
  33. findem.Read(&m_fRightUpY,sizeof(double));
  34. findem.Read(&m_iCountX,sizeof(int));
  35. findem.Read(&m_iCountY,sizeof(int));
  36. float ss = m_iCountX;
  37. float ll= m_iCountY;
  38. findem.Read(&m_fIntervalX,sizeof(double));
  39. findem.Read(&m_fIntervalY,sizeof(double));
  40. findem.Read(&m_fRealDistance,sizeof(double));
  41. findem.Read(&m_fMax_Height,sizeof(float));
  42. findem.Read(&m_fMin_Height,sizeof(float));
  43. m_fHeight = new float [m_iCountX*m_iCountY];
  44. findem.Read(m_fHeight,m_iCountX*m_iCountY*sizeof(float));
  45. m_fMax_Height = 0;
  46. m_fMin_Height = 10000;
  47. for(int i=0; i<m_iCountX*m_iCountY; i++)
  48. {
  49. if(m_fHeight[i] > m_fMax_Height)
  50. {
  51. m_fMax_Height = m_fHeight[i];
  52. }
  53. if(m_fHeight[i] < m_fMin_Height)
  54. {
  55. m_fMin_Height = m_fHeight[i];
  56. }
  57. }
  58. m_fMin_Height = 200;
  59. findem.Close();
  60. m_fIntervalX = m_fIntervalX * m_fRealDistance * 50;
  61. m_fIntervalY = m_fIntervalY * m_fRealDistance * 50;
  62. }
  63. void CDem::ReadTextDem(char *demfile)
  64. {
  65. FILE *fp = NULL;
  66. fp = fopen(demfile, "r");
  67. fscanf(fp, "%lf %lf", &m_fLeftDownX, &m_fLeftDownY);
  68. fscanf(fp, "%d %d", &m_iCountX, &m_iCountY);
  69.     m_fHeight = new float [m_iCountX * m_iCountY];
  70. fscanf(fp, "%lf %lf", &m_fIntervalX, &m_fIntervalY);
  71. m_fMax_Height = 0;
  72. m_fMin_Height = 10000;
  73. for(int i=0; i<m_iCountX*m_iCountY; i++)
  74. {
  75. fscanf(fp, "%f", &m_fHeight[i]);
  76. if(m_fHeight[i] > m_fMax_Height)
  77. {
  78. m_fMax_Height = m_fHeight[i];
  79. }
  80. if(m_fHeight[i] < m_fMin_Height)
  81. {
  82. m_fMin_Height = m_fHeight[i];
  83. }
  84. }
  85. m_fRealDistance = 20;
  86. m_fIntervalX = 50;
  87. m_fIntervalY = 50;
  88. fclose(fp);
  89. }
  90. void CDem::CreateCompileList()
  91. {
  92. m_tTexure = CreateTexture("terrain/085008.bmp");
  93. Cvpoint p[4];
  94. float r[4];
  95. float s[4];
  96. float dx = m_fIntervalX * m_iCountX;
  97. float dy = m_fIntervalY * m_iCountY;
  98. m_iList = glGenLists(1);
  99. glNewList(m_iList,GL_COMPILE);
  100. glBegin(GL_TRIANGLES);
  101. for(int j=0; j<m_iCountY-1; j++)
  102. {
  103. for(int i=0; i<m_iCountX-1; i++)
  104. {
  105. p[0].x = i*m_fIntervalX;
  106. p[0].y = m_fHeight[j*m_iCountX + i];
  107. p[0].z = j*m_fIntervalY;
  108. p[1].x=(i+1)*m_fIntervalX;
  109. p[1].y=m_fHeight[j*m_iCountX+i+1];
  110. p[1].z=j*m_fIntervalY;
  111. p[2].x=(i+1)*m_fIntervalX;
  112. p[2].y=m_fHeight[(j+1)*m_iCountX+i+1];
  113. p[2].z=(j+1)*m_fIntervalY;
  114. p[3].x=i*m_fIntervalX;
  115. p[3].y=m_fHeight[(j+1)*m_iCountX+i];
  116. p[3].z=(j+1)*m_fIntervalY;
  117. r[0]=p[0].x/dx;
  118. r[1]=p[1].x/dx;
  119. r[2]=p[2].x/dx;
  120. r[3]=p[3].x/dx;
  121. s[0]=p[0].z/dy;
  122. s[1]=p[1].z/dy;
  123. s[2]=p[2].z/dy;
  124. s[3]=p[3].z/dy;
  125. SetNormal( i,j);
  126. SetColor(p[0].y);
  127. glTexCoord2f(r[0],s[0]);
  128. glVertex3f(p[0].x,p[0].y,p[0].z);
  129. SetNormal( i+1,j);
  130. SetColor(p[1].y);
  131. glTexCoord2f(r[1],s[1]);
  132. glVertex3f(p[1].x,p[1].y,p[1].z);
  133. SetNormal( i+1,j+1);
  134. SetColor(p[2].y);
  135. glTexCoord2f(r[2],s[2]);
  136. glVertex3f(p[2].x,p[2].y,p[2].z);
  137. SetNormal( i,j);
  138. SetColor(p[0].y);
  139. glTexCoord2f(r[0],s[0]);
  140. glVertex3f(p[0].x,p[0].y,p[0].z);
  141. SetNormal( i+1,j+1);
  142. SetColor(p[2].y);
  143. glTexCoord2f(r[2],s[2]);
  144. glVertex3f(p[2].x,p[2].y,p[2].z);
  145. SetNormal( i,j+1);
  146. SetColor(p[3].y);
  147. glTexCoord2f(r[3],s[3]);
  148. glVertex3f(p[3].x,p[3].y,p[3].z);
  149. }
  150. }
  151. glEnd();
  152. glEndList();
  153. }
  154. GLuint CDem::CreateTexture( CString filename )
  155. {
  156.     GLuint      texture;     
  157. _AUX_RGBImageRec *Image;
  158.     Image = auxDIBImageLoadA( (const char*) filename );
  159.     glGenTextures(1, &texture);
  160.     glBindTexture(GL_TEXTURE_2D, texture);
  161.    
  162.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_CLAMP);
  163. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_CLAMP);
  164. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_NEAREST);
  165. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
  166. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  167. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  168. gluBuild2DMipmaps(GL_TEXTURE_2D, 3, Image->sizeX,
  169.                               Image->sizeY, GL_RGB,
  170.                               GL_UNSIGNED_BYTE, Image->data);
  171.    
  172.    delete Image;
  173. return texture;
  174. }
  175. void CDem::RenderDem()
  176. {
  177. glBindTexture(GL_TEXTURE_2D, m_tTexure);
  178.     glEnable(GL_TEXTURE_2D); 
  179. // glDisable(GL_TEXTURE_2D);
  180. glCallList(m_iList);
  181. }
  182. void CDem::SetColor(float height)
  183. {
  184. float r,g,b;
  185. float rL=80,rH=200;
  186. float gL=128,gH=130;
  187. float bL=62,bH=70;
  188. r=(rL+(rH-rL)*(height-m_fMin_Height)/
  189. (m_fMax_Height-m_fMin_Height))/255;
  190. g=(gL+(gH-gL)*(height-m_fMin_Height)/
  191. (m_fMax_Height-m_fMin_Height))/255;
  192. b=(bL+(bH-bL)*(height-m_fMin_Height)/
  193. (m_fMax_Height-m_fMin_Height))/255;
  194. glColor3f(r,g,b);
  195. }
  196. void CDem::SetViewPosition(float *eye_x, float *eye_y, float *eye_z)
  197. {
  198. Cvpoint viewpoint;
  199. viewpoint.x=((m_iCountX/2)*m_fIntervalX);
  200. viewpoint.y=((m_iCountY/2)*m_fIntervalY);
  201. viewpoint.z=m_fHeight[(m_iCountY/2)*m_iCountX
  202. +(m_iCountX/2)];
  203. *eye_x = viewpoint.x;
  204. *eye_y = viewpoint.z;
  205. *eye_z = viewpoint.y;
  206. }
  207. void CDem::SetNormal(int i, int j)
  208. {
  209. CVector3 VecNormal;
  210. if(i==0||i==m_iCountX||j==0||j==m_iCountY)
  211. {
  212. VecNormal.x = 0;
  213. VecNormal.y = 1;
  214. VecNormal.z = 0;
  215. }
  216. else
  217. {
  218. CVector3 n1((i-1)*m_fIntervalX, m_fHeight[(j-1)*m_iCountX+i-1], (j-1)*m_fIntervalY);
  219. CVector3 n2(i*m_fIntervalX, m_fHeight[(j-1)*m_iCountX+i], (j-1)*m_fIntervalY);
  220. CVector3 n3((i+1)*m_fIntervalX, m_fHeight[(j-1)*m_iCountX+i+1], (j-1)*m_fIntervalY);
  221. CVector3 n4((i-1)*m_fIntervalX, m_fHeight[j*m_iCountX+i-1], j*m_fIntervalY);
  222. CVector3 n5(i*m_fIntervalX, m_fHeight[j*m_iCountX+i], j*m_fIntervalY);
  223. CVector3 n6((i+1)*m_fIntervalX, m_fHeight[j*m_iCountX+i+1], j*m_fIntervalY);
  224. CVector3 n7((i-1)*m_fIntervalX, m_fHeight[(j+1)*m_iCountX+i-1], (j+1)*m_fIntervalY);
  225. CVector3 n8(i*m_fIntervalX, m_fHeight[(j+1)*m_iCountX+i], (j+1)*m_fIntervalY);
  226. CVector3 n9((i+1)*m_fIntervalX, m_fHeight[(j+1)*m_iCountX+i+1], (j+1)*m_fIntervalY);
  227. VecNormal = (CalNormal(n1,n2,n5) + CalNormal(n2,n3,n5) 
  228. + CalNormal(n3,n6,n5) + CalNormal(n6,n9,n5)
  229. + CalNormal(n9,n8,n5) + CalNormal(n8,n7,n5)
  230. + CalNormal(n7,n4,n5) + CalNormal(n4,n1,n5))/8.0;
  231. Normalize(VecNormal);
  232. glNormal3f(-VecNormal.x,-VecNormal.y,-VecNormal.z);
  233. }
  234. }
  235. CVector3 CDem::CalNormal(CVector3 p1,CVector3 p2,CVector3 p3)
  236. {
  237. CVector3 vec1=p1-p2;
  238. CVector3 vec2=p3-p2;
  239. CVector3 vec=Cross(vec2,vec1);
  240. return vec;
  241. }