LiFeng_Terrain.cpp
资源名称:虚拟地形建模.rar [点击查看]
上传用户:dfjhuyju
上传日期:2013-03-13
资源大小:11035k
文件大小:6k
源码类别:
OpenGL
开发平台:
Visual C++
- #include "stdafx.h"
- #include "LiFeng.h"
- #include "LiFeng_Terrain.h"
- #include <fstream.h>
- LiFeng_TIN::LiFeng_TIN()
- {
- m_Point=NULL;
- m_Triangle=NULL;
- Point_Count=0;
- Triangle_Count=0;
- Max_X=0;
- Min_X=0;
- Max_Y=0;
- Min_Y=0;
- Max_Z=0;
- Min_Z=0;
- }
- LiFeng_TIN::~LiFeng_TIN()
- {
- delete [] m_Point;
- delete [] m_Triangle;
- }
- BOOL LiFeng_TIN::LiFeng_ReadTin(CString filename)
- {
- ifstream fin(filename);
- fin >>Point_Count>>Triangle_Count;
- m_Point=new Cvpoint [Point_Count];
- m_Triangle=new Triangle [Triangle_Count];
- for(int i=0;i<Point_Count;i++)
- {
- fin>>m_Point[i].x>>m_Point[i].y>>m_Point[i].z;
- }
- for(int j=0;j<Triangle_Count;j++)
- {
- fin>>m_Triangle[j].p1>>m_Triangle[j].p2>>m_Triangle[j].p3;
- }
- return true;
- }
- BOOL LiFeng_TIN::LiFeng_Limit()
- {
- Max_X=m_Point[0].x;
- Min_X=m_Point[0].x;
- Max_Y=m_Point[0].y;
- Min_Y=m_Point[0].y;
- Max_Z=m_Point[0].z;
- Min_Z=m_Point[0].z;
- for(int i=0;i<Point_Count;i++)
- {
- if(m_Point[i].x>Max_X)
- Max_X=m_Point[i].x;
- if(m_Point[i].x<Min_X)
- Min_X=m_Point[i].x;
- if(m_Point[i].y>Max_Y)
- Max_Y=m_Point[i].y;
- if(m_Point[i].y<Min_Y)
- Min_Y=m_Point[i].y;
- if(m_Point[i].z>Max_Z)
- Max_Z=m_Point[i].z;
- if(m_Point[i].z<Min_Z)
- Min_Z=m_Point[i].z;
- }
- return true;
- }
- BOOL LiFeng_TIN::LiFeng_Compile()
- {
- glNewList(1,GL_COMPILE);
- glBegin(GL_TRIANGLES);
- Cvpoint p[3];
- Cvpoint vect[3];
- float r[3];
- float s[3];
- float dx=Max_X-Min_X;
- float dy=Max_Y-Min_Y;
- glColor3f(1.0,0,0);
- for(int i=0;i<Triangle_Count;i++)
- {
- p[0].x=m_Point[m_Triangle[i].p1].x;
- p[0].y=m_Point[m_Triangle[i].p1].y;
- p[0].z=m_Point[m_Triangle[i].p1].z;
- p[1].x=m_Point[m_Triangle[i].p2].x;
- p[1].y=m_Point[m_Triangle[i].p2].y;
- p[1].z=m_Point[m_Triangle[i].p2].z;
- p[2].x=m_Point[m_Triangle[i].p3].x;
- p[2].y=m_Point[m_Triangle[i].p3].y;
- p[2].z=m_Point[m_Triangle[i].p3].z;
- vect[0].x=p[2].x-p[0].x;
- vect[0].y=p[2].y-p[0].y;
- vect[0].z=p[2].z-p[0].z;
- vect[1].x=p[2].x-p[1].x;
- vect[1].y=p[2].y-p[1].y;
- vect[1].z=p[2].z-p[1].z;
- vect[2].x=vect[0].y*vect[1].z-vect[0].z*vect[1].y;
- vect[2].y=vect[0].z*vect[1].x-vect[0].x*vect[1].z;
- vect[2].z=vect[0].x*vect[1].y-vect[0].y*vect[1].x;
- r[0]=p[0].x/dx;
- r[1]=p[1].x/dx;
- r[2]=p[2].x/dx;
- s[0]=p[0].y/dy;
- s[1]=p[1].y/dy;
- s[2]=p[2].y/dy;
- glNormal3f(vect[2].x,vect[2].z,vect[2].y);
- glTexCoord2f(r[0],s[0]);
- glVertex3f(p[0].x,p[0].z,p[0].y);
- glTexCoord2f(r[1],s[1]);
- glVertex3f(p[1].x,p[1].z,p[1].y);
- glTexCoord2f(r[2],s[2]);
- glVertex3f(p[2].x,p[2].z,p[2].y);
- }
- glEnd();
- glEndList();
- return true;
- }
- BOOL LiFeng_TIN::LiFeng_DisplayTin()
- {
- glCallList(1);
- return true;
- }
- LiFeng_DEM::LiFeng_DEM()
- {
- DEMHEADER header;
- header.dem_Height=0;
- header.LeftDown_X=0;
- header.LeftDown_Y=0;
- header.Max_Height=0;
- header.Min_Height=0;
- header.Real_Distance=0;
- header.RightUp_X=0;
- header.RightUp_Y=0;
- header.X_Count=0;
- header.X_Interval=0;
- header.Y_Count=0;
- header.Y_Interval=0;
- Dem_Header=&header;
- }
- LiFeng_DEM::~LiFeng_DEM()
- {
- delete Dem_Height;
- }
- BOOL LiFeng_DEM::LiFeng_ReadDem(CString filename)
- {
- CFile findem;
- findem.Open(filename,CFile::modeRead);
- findem.Read(&Dem_Header->LeftDown_X,8);
- findem.Read(&Dem_Header->LeftDown_Y,8);
- findem.Read(&Dem_Header->RightUp_X,8);
- findem.Read(&Dem_Header->RightUp_Y,8);
- findem.Read(&Dem_Header->X_Count,4);
- findem.Read(&Dem_Header->Y_Count,4);
- findem.Read(&Dem_Header->X_Interval,8);
- findem.Read(&Dem_Header->Y_Interval,8);
- findem.Read(&Dem_Header->Real_Distance,8);
- findem.Read(&Dem_Header->Max_Height,4);
- findem.Read(&Dem_Header->Min_Height,4);
- Dem_Height=new float [Dem_Header->X_Count*Dem_Header->Y_Count];
- findem.ReadHuge(Dem_Height,Dem_Header->X_Count*Dem_Header->Y_Count*4);
- findem.Close();
- return true;
- }
- BOOL LiFeng_DEM::LiFeng_Compile()
- {
- Cvpoint p[4];
- Cvpoint vect[5];
- float r[4];
- float s[4];
- float dx=Dem_Header->X_Interval*Dem_Header->Real_Distance*Dem_Header->X_Count;
- float dy=Dem_Header->Y_Interval*Dem_Header->Real_Distance*Dem_Header->Y_Count;
- glNewList(1,GL_COMPILE);
- glBegin(GL_TRIANGLES);
- glColor3f(1.0,0,0);
- for(int j=0;j<Dem_Header->Y_Count-1;j++)
- {
- for(int i=0;i<Dem_Header->X_Count-1;i++)
- {
- p[0].x=i*Dem_Header->X_Interval*Dem_Header->Real_Distance;
- p[0].y=Dem_Height[j*Dem_Header->X_Count+i];
- p[0].z=j*Dem_Header->Y_Interval*Dem_Header->Real_Distance;
- p[1].x=(i+1)*Dem_Header->X_Interval*Dem_Header->Real_Distance;
- p[1].y=Dem_Height[j*Dem_Header->X_Count+i+1];
- p[1].z=j*Dem_Header->Y_Interval*Dem_Header->Real_Distance;
- p[2].x=(i+1)*Dem_Header->X_Interval*Dem_Header->Real_Distance;
- p[2].y=Dem_Height[(j+1)*Dem_Header->X_Count+i+1];
- p[2].z=(j+1)*Dem_Header->Y_Interval*Dem_Header->Real_Distance;
- p[3].x=i*Dem_Header->X_Interval*Dem_Header->Real_Distance;
- p[3].y=Dem_Height[(j+1)*Dem_Header->X_Count+i];
- p[3].z=(j+1)*Dem_Header->Y_Interval*Dem_Header->Real_Distance;
- vect[0].x=p[2].x-p[0].x;
- vect[0].y=p[2].y-p[0].y;
- vect[0].z=p[2].z-p[0].z;
- vect[1].x=p[1].x-p[0].x;
- vect[1].y=p[1].y-p[0].y;
- vect[1].z=p[1].z-p[0].z;
- vect[3].x=p[3].x-p[0].x;
- vect[3].y=p[3].y-p[0].y;
- vect[3].z=p[3].z-p[0].z;
- vect[2].x=vect[0].y*vect[1].z-vect[0].z*vect[1].y;
- vect[2].y=vect[0].z*vect[1].x-vect[0].x*vect[1].z;
- vect[2].z=vect[0].x*vect[1].y-vect[0].y*vect[1].x;
- vect[4].x=vect[0].y*vect[3].z-vect[0].z*vect[1].y;
- vect[4].y=vect[0].z*vect[3].x-vect[0].x*vect[1].z;
- vect[4].z=vect[0].x*vect[3].y-vect[0].y*vect[1].x;
- r[0]=p[0].x/dx;
- r[1]=p[1].x/dx;
- r[2]=p[2].x/dx;
- r[3]=p[3].x/dx;
- s[0]=p[0].y/dy;
- s[1]=p[1].y/dy;
- s[2]=p[2].y/dy;
- s[3]=p[3].y/dy;
- glNormal3f(vect[2].x,vect[2].z,vect[2].y);
- glTexCoord2f(r[0],s[0]);
- glVertex3f(p[0].x,p[0].z,p[0].y);
- glTexCoord2f(r[1],s[1]);
- glVertex3f(p[1].x,p[1].z,p[1].y);
- glTexCoord2f(r[2],s[2]);
- glVertex3f(p[2].x,p[2].z,p[2].y);
- glNormal3f(vect[4].x,vect[4].z,vect[4].y);
- glTexCoord2f(r[0],s[0]);
- glVertex3f(p[0].x,p[0].z,p[0].y);
- glTexCoord2f(r[2],s[2]);
- glVertex3f(p[2].x,p[2].z,p[2].y);
- glTexCoord2f(r[3],s[3]);
- glVertex3f(p[3].x,p[3].z,p[3].y);
- }
- }
- glEnd();
- glEndList();
- return true;
- }
- BOOL LiFeng_DEM::LiFeng_DisplayDem()
- {
- glCallList(1);
- return true;
- }