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

OpenGL

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "LiFeng.h"
  3. #include "LiFeng_Terrain.h"
  4. #include <fstream.h>
  5. LiFeng_TIN::LiFeng_TIN()
  6. {
  7. m_Point=NULL;
  8. m_Triangle=NULL;
  9. Point_Count=0;
  10. Triangle_Count=0;
  11. Max_X=0;
  12. Min_X=0;
  13. Max_Y=0;
  14. Min_Y=0;
  15. Max_Z=0;
  16. Min_Z=0;
  17. }
  18. LiFeng_TIN::~LiFeng_TIN()
  19. {
  20. delete [] m_Point;
  21. delete [] m_Triangle;
  22. }
  23. BOOL LiFeng_TIN::LiFeng_ReadTin(CString filename)
  24. {
  25. ifstream fin(filename);
  26. fin >>Point_Count>>Triangle_Count;
  27. m_Point=new Cvpoint [Point_Count];
  28. m_Triangle=new Triangle [Triangle_Count];
  29. for(int i=0;i<Point_Count;i++)
  30. {
  31. fin>>m_Point[i].x>>m_Point[i].y>>m_Point[i].z;
  32. }
  33. for(int j=0;j<Triangle_Count;j++)
  34. {
  35. fin>>m_Triangle[j].p1>>m_Triangle[j].p2>>m_Triangle[j].p3;
  36. }
  37. return true;
  38. }
  39. BOOL LiFeng_TIN::LiFeng_Limit()
  40. {
  41. Max_X=m_Point[0].x;
  42. Min_X=m_Point[0].x;
  43. Max_Y=m_Point[0].y;
  44. Min_Y=m_Point[0].y;
  45. Max_Z=m_Point[0].z;
  46. Min_Z=m_Point[0].z;
  47. for(int i=0;i<Point_Count;i++)
  48. {
  49. if(m_Point[i].x>Max_X)
  50. Max_X=m_Point[i].x;
  51. if(m_Point[i].x<Min_X)
  52. Min_X=m_Point[i].x;
  53. if(m_Point[i].y>Max_Y)
  54. Max_Y=m_Point[i].y;
  55. if(m_Point[i].y<Min_Y)
  56. Min_Y=m_Point[i].y;
  57. if(m_Point[i].z>Max_Z)
  58. Max_Z=m_Point[i].z;
  59. if(m_Point[i].z<Min_Z)
  60. Min_Z=m_Point[i].z;
  61. }
  62. return true;
  63. }
  64. BOOL LiFeng_TIN::LiFeng_Compile()
  65. {
  66. glNewList(1,GL_COMPILE);
  67. glBegin(GL_TRIANGLES);
  68. Cvpoint p[3];
  69. Cvpoint vect[3];
  70. float r[3];
  71. float s[3];
  72. float dx=Max_X-Min_X;
  73. float dy=Max_Y-Min_Y;
  74. glColor3f(1.0,0,0);
  75. for(int i=0;i<Triangle_Count;i++)
  76. {
  77. p[0].x=m_Point[m_Triangle[i].p1].x;
  78. p[0].y=m_Point[m_Triangle[i].p1].y;
  79. p[0].z=m_Point[m_Triangle[i].p1].z;
  80. p[1].x=m_Point[m_Triangle[i].p2].x;
  81. p[1].y=m_Point[m_Triangle[i].p2].y;
  82. p[1].z=m_Point[m_Triangle[i].p2].z;
  83. p[2].x=m_Point[m_Triangle[i].p3].x;
  84. p[2].y=m_Point[m_Triangle[i].p3].y;
  85. p[2].z=m_Point[m_Triangle[i].p3].z;
  86. vect[0].x=p[2].x-p[0].x;
  87. vect[0].y=p[2].y-p[0].y;
  88. vect[0].z=p[2].z-p[0].z;
  89. vect[1].x=p[2].x-p[1].x;
  90. vect[1].y=p[2].y-p[1].y;
  91. vect[1].z=p[2].z-p[1].z;
  92. vect[2].x=vect[0].y*vect[1].z-vect[0].z*vect[1].y;
  93. vect[2].y=vect[0].z*vect[1].x-vect[0].x*vect[1].z;
  94. vect[2].z=vect[0].x*vect[1].y-vect[0].y*vect[1].x;
  95. r[0]=p[0].x/dx;
  96. r[1]=p[1].x/dx;
  97. r[2]=p[2].x/dx;
  98. s[0]=p[0].y/dy;
  99. s[1]=p[1].y/dy;
  100. s[2]=p[2].y/dy;
  101. glNormal3f(vect[2].x,vect[2].z,vect[2].y);
  102. glTexCoord2f(r[0],s[0]);
  103. glVertex3f(p[0].x,p[0].z,p[0].y);
  104. glTexCoord2f(r[1],s[1]);
  105. glVertex3f(p[1].x,p[1].z,p[1].y);
  106. glTexCoord2f(r[2],s[2]);
  107. glVertex3f(p[2].x,p[2].z,p[2].y);
  108.   
  109. }
  110. glEnd();
  111. glEndList();
  112. return true;
  113. }
  114. BOOL LiFeng_TIN::LiFeng_DisplayTin()
  115. {
  116. glCallList(1);
  117. return true;
  118. }
  119. LiFeng_DEM::LiFeng_DEM()
  120. {
  121. DEMHEADER header;
  122. header.dem_Height=0;
  123. header.LeftDown_X=0;
  124. header.LeftDown_Y=0;
  125. header.Max_Height=0;
  126. header.Min_Height=0;
  127. header.Real_Distance=0;
  128. header.RightUp_X=0;
  129. header.RightUp_Y=0;
  130. header.X_Count=0;
  131. header.X_Interval=0;
  132. header.Y_Count=0;
  133. header.Y_Interval=0;
  134. Dem_Header=&header;
  135. }
  136. LiFeng_DEM::~LiFeng_DEM()
  137. {
  138. delete  Dem_Height;
  139. }
  140. BOOL LiFeng_DEM::LiFeng_ReadDem(CString filename)
  141. {
  142. CFile findem;
  143. findem.Open(filename,CFile::modeRead);
  144. findem.Read(&Dem_Header->LeftDown_X,8);
  145. findem.Read(&Dem_Header->LeftDown_Y,8);
  146. findem.Read(&Dem_Header->RightUp_X,8);
  147. findem.Read(&Dem_Header->RightUp_Y,8);
  148. findem.Read(&Dem_Header->X_Count,4);
  149. findem.Read(&Dem_Header->Y_Count,4);
  150. findem.Read(&Dem_Header->X_Interval,8);
  151. findem.Read(&Dem_Header->Y_Interval,8);
  152. findem.Read(&Dem_Header->Real_Distance,8);
  153. findem.Read(&Dem_Header->Max_Height,4);
  154. findem.Read(&Dem_Header->Min_Height,4);
  155. Dem_Height=new float [Dem_Header->X_Count*Dem_Header->Y_Count];
  156. findem.ReadHuge(Dem_Height,Dem_Header->X_Count*Dem_Header->Y_Count*4);
  157. findem.Close();
  158. return true;
  159. }
  160. BOOL LiFeng_DEM::LiFeng_Compile()
  161. {
  162. Cvpoint p[4];
  163. Cvpoint vect[5];
  164. float r[4];
  165. float s[4];
  166. float dx=Dem_Header->X_Interval*Dem_Header->Real_Distance*Dem_Header->X_Count;
  167. float dy=Dem_Header->Y_Interval*Dem_Header->Real_Distance*Dem_Header->Y_Count;
  168. glNewList(1,GL_COMPILE);
  169. glBegin(GL_TRIANGLES);
  170. glColor3f(1.0,0,0);
  171. for(int j=0;j<Dem_Header->Y_Count-1;j++)
  172. {
  173. for(int i=0;i<Dem_Header->X_Count-1;i++)
  174. {
  175. p[0].x=i*Dem_Header->X_Interval*Dem_Header->Real_Distance;
  176. p[0].y=Dem_Height[j*Dem_Header->X_Count+i];
  177. p[0].z=j*Dem_Header->Y_Interval*Dem_Header->Real_Distance;
  178. p[1].x=(i+1)*Dem_Header->X_Interval*Dem_Header->Real_Distance;
  179. p[1].y=Dem_Height[j*Dem_Header->X_Count+i+1];
  180. p[1].z=j*Dem_Header->Y_Interval*Dem_Header->Real_Distance;
  181. p[2].x=(i+1)*Dem_Header->X_Interval*Dem_Header->Real_Distance;
  182. p[2].y=Dem_Height[(j+1)*Dem_Header->X_Count+i+1];
  183. p[2].z=(j+1)*Dem_Header->Y_Interval*Dem_Header->Real_Distance;
  184. p[3].x=i*Dem_Header->X_Interval*Dem_Header->Real_Distance;
  185. p[3].y=Dem_Height[(j+1)*Dem_Header->X_Count+i];
  186. p[3].z=(j+1)*Dem_Header->Y_Interval*Dem_Header->Real_Distance;
  187. vect[0].x=p[2].x-p[0].x;
  188. vect[0].y=p[2].y-p[0].y;
  189. vect[0].z=p[2].z-p[0].z;
  190. vect[1].x=p[1].x-p[0].x;
  191. vect[1].y=p[1].y-p[0].y;
  192. vect[1].z=p[1].z-p[0].z;
  193. vect[3].x=p[3].x-p[0].x;
  194. vect[3].y=p[3].y-p[0].y;
  195. vect[3].z=p[3].z-p[0].z;
  196. vect[2].x=vect[0].y*vect[1].z-vect[0].z*vect[1].y;
  197. vect[2].y=vect[0].z*vect[1].x-vect[0].x*vect[1].z;
  198. vect[2].z=vect[0].x*vect[1].y-vect[0].y*vect[1].x;
  199. vect[4].x=vect[0].y*vect[3].z-vect[0].z*vect[1].y;
  200. vect[4].y=vect[0].z*vect[3].x-vect[0].x*vect[1].z;
  201. vect[4].z=vect[0].x*vect[3].y-vect[0].y*vect[1].x;
  202. r[0]=p[0].x/dx;
  203. r[1]=p[1].x/dx;
  204. r[2]=p[2].x/dx;
  205. r[3]=p[3].x/dx;
  206. s[0]=p[0].y/dy;
  207. s[1]=p[1].y/dy;
  208. s[2]=p[2].y/dy;
  209. s[3]=p[3].y/dy;
  210. glNormal3f(vect[2].x,vect[2].z,vect[2].y);
  211. glTexCoord2f(r[0],s[0]);
  212. glVertex3f(p[0].x,p[0].z,p[0].y);
  213. glTexCoord2f(r[1],s[1]);
  214. glVertex3f(p[1].x,p[1].z,p[1].y);
  215. glTexCoord2f(r[2],s[2]);
  216. glVertex3f(p[2].x,p[2].z,p[2].y);
  217. glNormal3f(vect[4].x,vect[4].z,vect[4].y);
  218. glTexCoord2f(r[0],s[0]);
  219. glVertex3f(p[0].x,p[0].z,p[0].y);
  220. glTexCoord2f(r[2],s[2]);
  221. glVertex3f(p[2].x,p[2].z,p[2].y);
  222. glTexCoord2f(r[3],s[3]);
  223. glVertex3f(p[3].x,p[3].z,p[3].y);
  224. }
  225. }
  226. glEnd();
  227. glEndList();
  228. return true;
  229. }
  230. BOOL LiFeng_DEM::LiFeng_DisplayDem()
  231. {
  232. glCallList(1);
  233. return true;
  234. }