Dem.cpp
资源名称:虚拟地形建模.rar [点击查看]
上传用户:dfjhuyju
上传日期:2013-03-13
资源大小:11035k
文件大小:7k
源码类别:
OpenGL
开发平台:
Visual C++
- // Dem.cpp: implementation of the CDem class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "LiFeng.h"
- #include "Dem.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CDem::CDem()
- {
- m_fHeight = NULL;
- }
- CDem::~CDem()
- {
- if(m_fHeight!=NULL)
- {
- delete [] m_fHeight;
- }
- }
- void CDem::ReadDem(char *demfile)
- {
- CFile findem;
- findem.Open(demfile,CFile::modeRead);
- findem.Read(&m_fLeftDownX,sizeof(double));
- findem.Read(&m_fLeftDownY,sizeof(double));
- findem.Read(&m_fRightUpX,sizeof(double));
- findem.Read(&m_fRightUpY,sizeof(double));
- findem.Read(&m_iCountX,sizeof(int));
- findem.Read(&m_iCountY,sizeof(int));
- float ss = m_iCountX;
- float ll= m_iCountY;
- findem.Read(&m_fIntervalX,sizeof(double));
- findem.Read(&m_fIntervalY,sizeof(double));
- findem.Read(&m_fRealDistance,sizeof(double));
- findem.Read(&m_fMax_Height,sizeof(float));
- findem.Read(&m_fMin_Height,sizeof(float));
- m_fHeight = new float [m_iCountX*m_iCountY];
- findem.Read(m_fHeight,m_iCountX*m_iCountY*sizeof(float));
- m_fMax_Height = 0;
- m_fMin_Height = 10000;
- for(int i=0; i<m_iCountX*m_iCountY; i++)
- {
- if(m_fHeight[i] > m_fMax_Height)
- {
- m_fMax_Height = m_fHeight[i];
- }
- if(m_fHeight[i] < m_fMin_Height)
- {
- m_fMin_Height = m_fHeight[i];
- }
- }
- m_fMin_Height = 200;
- findem.Close();
- m_fIntervalX = m_fIntervalX * m_fRealDistance * 50;
- m_fIntervalY = m_fIntervalY * m_fRealDistance * 50;
- }
- void CDem::ReadTextDem(char *demfile)
- {
- FILE *fp = NULL;
- fp = fopen(demfile, "r");
- fscanf(fp, "%lf %lf", &m_fLeftDownX, &m_fLeftDownY);
- fscanf(fp, "%d %d", &m_iCountX, &m_iCountY);
- m_fHeight = new float [m_iCountX * m_iCountY];
- fscanf(fp, "%lf %lf", &m_fIntervalX, &m_fIntervalY);
- m_fMax_Height = 0;
- m_fMin_Height = 10000;
- for(int i=0; i<m_iCountX*m_iCountY; i++)
- {
- fscanf(fp, "%f", &m_fHeight[i]);
- if(m_fHeight[i] > m_fMax_Height)
- {
- m_fMax_Height = m_fHeight[i];
- }
- if(m_fHeight[i] < m_fMin_Height)
- {
- m_fMin_Height = m_fHeight[i];
- }
- }
- m_fRealDistance = 20;
- m_fIntervalX = 50;
- m_fIntervalY = 50;
- fclose(fp);
- }
- void CDem::CreateCompileList()
- {
- m_tTexure = CreateTexture("terrain/085008.bmp");
- Cvpoint p[4];
- float r[4];
- float s[4];
- float dx = m_fIntervalX * m_iCountX;
- float dy = m_fIntervalY * m_iCountY;
- m_iList = glGenLists(1);
- glNewList(m_iList,GL_COMPILE);
- glBegin(GL_TRIANGLES);
- for(int j=0; j<m_iCountY-1; j++)
- {
- for(int i=0; i<m_iCountX-1; i++)
- {
- p[0].x = i*m_fIntervalX;
- p[0].y = m_fHeight[j*m_iCountX + i];
- p[0].z = j*m_fIntervalY;
- p[1].x=(i+1)*m_fIntervalX;
- p[1].y=m_fHeight[j*m_iCountX+i+1];
- p[1].z=j*m_fIntervalY;
- p[2].x=(i+1)*m_fIntervalX;
- p[2].y=m_fHeight[(j+1)*m_iCountX+i+1];
- p[2].z=(j+1)*m_fIntervalY;
- p[3].x=i*m_fIntervalX;
- p[3].y=m_fHeight[(j+1)*m_iCountX+i];
- p[3].z=(j+1)*m_fIntervalY;
- 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].z/dy;
- s[1]=p[1].z/dy;
- s[2]=p[2].z/dy;
- s[3]=p[3].z/dy;
- SetNormal( i,j);
- SetColor(p[0].y);
- glTexCoord2f(r[0],s[0]);
- glVertex3f(p[0].x,p[0].y,p[0].z);
- SetNormal( i+1,j);
- SetColor(p[1].y);
- glTexCoord2f(r[1],s[1]);
- glVertex3f(p[1].x,p[1].y,p[1].z);
- SetNormal( i+1,j+1);
- SetColor(p[2].y);
- glTexCoord2f(r[2],s[2]);
- glVertex3f(p[2].x,p[2].y,p[2].z);
- SetNormal( i,j);
- SetColor(p[0].y);
- glTexCoord2f(r[0],s[0]);
- glVertex3f(p[0].x,p[0].y,p[0].z);
- SetNormal( i+1,j+1);
- SetColor(p[2].y);
- glTexCoord2f(r[2],s[2]);
- glVertex3f(p[2].x,p[2].y,p[2].z);
- SetNormal( i,j+1);
- SetColor(p[3].y);
- glTexCoord2f(r[3],s[3]);
- glVertex3f(p[3].x,p[3].y,p[3].z);
- }
- }
- glEnd();
- glEndList();
- }
- GLuint CDem::CreateTexture( CString filename )
- {
- GLuint texture;
- _AUX_RGBImageRec *Image;
- Image = auxDIBImageLoadA( (const char*) filename );
- glGenTextures(1, &texture);
- glBindTexture(GL_TEXTURE_2D, texture);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_CLAMP);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_CLAMP);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
- glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
- gluBuild2DMipmaps(GL_TEXTURE_2D, 3, Image->sizeX,
- Image->sizeY, GL_RGB,
- GL_UNSIGNED_BYTE, Image->data);
- delete Image;
- return texture;
- }
- void CDem::RenderDem()
- {
- glBindTexture(GL_TEXTURE_2D, m_tTexure);
- glEnable(GL_TEXTURE_2D);
- // glDisable(GL_TEXTURE_2D);
- glCallList(m_iList);
- }
- void CDem::SetColor(float height)
- {
- float r,g,b;
- float rL=80,rH=200;
- float gL=128,gH=130;
- float bL=62,bH=70;
- r=(rL+(rH-rL)*(height-m_fMin_Height)/
- (m_fMax_Height-m_fMin_Height))/255;
- g=(gL+(gH-gL)*(height-m_fMin_Height)/
- (m_fMax_Height-m_fMin_Height))/255;
- b=(bL+(bH-bL)*(height-m_fMin_Height)/
- (m_fMax_Height-m_fMin_Height))/255;
- glColor3f(r,g,b);
- }
- void CDem::SetViewPosition(float *eye_x, float *eye_y, float *eye_z)
- {
- Cvpoint viewpoint;
- viewpoint.x=((m_iCountX/2)*m_fIntervalX);
- viewpoint.y=((m_iCountY/2)*m_fIntervalY);
- viewpoint.z=m_fHeight[(m_iCountY/2)*m_iCountX
- +(m_iCountX/2)];
- *eye_x = viewpoint.x;
- *eye_y = viewpoint.z;
- *eye_z = viewpoint.y;
- }
- void CDem::SetNormal(int i, int j)
- {
- CVector3 VecNormal;
- if(i==0||i==m_iCountX||j==0||j==m_iCountY)
- {
- VecNormal.x = 0;
- VecNormal.y = 1;
- VecNormal.z = 0;
- }
- else
- {
- CVector3 n1((i-1)*m_fIntervalX, m_fHeight[(j-1)*m_iCountX+i-1], (j-1)*m_fIntervalY);
- CVector3 n2(i*m_fIntervalX, m_fHeight[(j-1)*m_iCountX+i], (j-1)*m_fIntervalY);
- CVector3 n3((i+1)*m_fIntervalX, m_fHeight[(j-1)*m_iCountX+i+1], (j-1)*m_fIntervalY);
- CVector3 n4((i-1)*m_fIntervalX, m_fHeight[j*m_iCountX+i-1], j*m_fIntervalY);
- CVector3 n5(i*m_fIntervalX, m_fHeight[j*m_iCountX+i], j*m_fIntervalY);
- CVector3 n6((i+1)*m_fIntervalX, m_fHeight[j*m_iCountX+i+1], j*m_fIntervalY);
- CVector3 n7((i-1)*m_fIntervalX, m_fHeight[(j+1)*m_iCountX+i-1], (j+1)*m_fIntervalY);
- CVector3 n8(i*m_fIntervalX, m_fHeight[(j+1)*m_iCountX+i], (j+1)*m_fIntervalY);
- CVector3 n9((i+1)*m_fIntervalX, m_fHeight[(j+1)*m_iCountX+i+1], (j+1)*m_fIntervalY);
- VecNormal = (CalNormal(n1,n2,n5) + CalNormal(n2,n3,n5)
- + CalNormal(n3,n6,n5) + CalNormal(n6,n9,n5)
- + CalNormal(n9,n8,n5) + CalNormal(n8,n7,n5)
- + CalNormal(n7,n4,n5) + CalNormal(n4,n1,n5))/8.0;
- Normalize(VecNormal);
- glNormal3f(-VecNormal.x,-VecNormal.y,-VecNormal.z);
- }
- }
- CVector3 CDem::CalNormal(CVector3 p1,CVector3 p2,CVector3 p3)
- {
- CVector3 vec1=p1-p2;
- CVector3 vec2=p3-p2;
- CVector3 vec=Cross(vec2,vec1);
- return vec;
- }