www.pudn.com > Dynamicdisplayofreal-time3Dterrain.rar > dialogDlg.cpp
// dialogDlg.cpp : implementation file
//
#include "stdafx.h"
#include "dialog.h"
#include "dialogDlg.h"
#include "gl/gl.h"
#include "gl/glu.h"
#include "gl/glaux.h"
#include "math.h"
#include "dibapi.h"
#include "draw.h"
#include "splash.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#pragma warning (disable:4305)
#pragma warning (disable :4244)
int* m_pDemX; //记录地形数据x方向采样点的坐标
int* m_pDemY;//记录地形数据y方向采样点的坐标
int* m_pDemH;//记录地形数据z方向采样点的坐标
int m_iDemWidth;//记录地形数据x方向采样点的个数
int m_iDemHeight;//记录地形数据x方向采样点的个数
BYTE m_pTexForest0[128][128][3];//存放地形纹理图像
BYTE m_pTexyun[512][512][3];//存放天空背景图像
//定义三个显示列表编号常量
const int no1=1;
const int no2=2;
const int no3=3;
//定义颜色的类型
typedef struct COLOUR
{
GLfloat r, g, b;
}COLOUR;
//用于存放地形采样点颜色的二维数组,120,120含义是地形采样点的个数横向不超过120个点,纵向不超过120个点
//也可以动态生成,解除个数的限制,我们这里使用的是静态分配
COLOUR colour [120][120];
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
//Cdocument pDoc;
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDialogDlg dialog
CDialogDlg::CDialogDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDialogDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDialogDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CDialogDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDialogDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDialogDlg, CDialog)
//{{AFX_MSG_MAP(CDialogDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON9, exit_system)
ON_BN_CLICKED(IDC_BUTTON2, create_display_list)
ON_BN_CLICKED(IDC_BUTTON3, move_scence)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_BUTTON4, stop_move)
ON_WM_CREATE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDialogDlg message handlers
BOOL CDialogDlg::OnInitDialog()
{
CDialog::OnInitDialog();//初始化对话框
px=py=pz=0; //三个绝对坐标设为0
//定义眼睛的位置
lookx=-10000;
looky=9990;
lookz=870;
//读取地形相关数据
readdem();
return TRUE; // return TRUE unless you set the focus to a control
}
void CDialogDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CDialogDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
CWnd* mywnd;
mywnd=GetDlgItem(IDC_BUTTON1);
CDC* pDC=mywnd->GetDC();
wglMakeCurrent(pDC->GetSafeHdc(),m_hrc);//使用opengl的绘制上下文
draw_scence();//三维场景的绘制函数
SwapBuffers(pDC->GetSafeHdc());//缓存提交,更新场景
ReleaseDC(pDC);//释放上下文
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CDialogDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CDialogDlg::exit_system()
{
// TODO: Add your control notification handler code here
wglDeleteContext(m_hrc);//删除绘图上下文
AfxGetMainWnd()->PostMessage(WM_CLOSE);//关闭程序
}
void CDialogDlg::create_display_list() //创建显示列表,并启动三维场景绘制
{
// TODO: Add your control notification handler code here
CWnd* mywnd;
mywnd=GetDlgItem(IDC_BUTTON3);
mywnd->EnableWindow(true);
mywnd=GetDlgItem(IDC_BUTTON2);
mywnd->EnableWindow(false);
mywnd=GetDlgItem(IDC_BUTTON1);
mywnd->ShowWindow(SW_SHOW);
CDC* pDC=mywnd->GetDC();
SetThePixelFormat(pDC);//设置像素格式
m_hrc=wglCreateContext(pDC->GetSafeHdc());//创建opengl要使用的上下文;
wglMakeCurrent(pDC->GetSafeHdc(),m_hrc);
//上面的代码就是准备绘图环境,将来会在IDC_BUTTON1上面绘制三维图形
//下面定义了三个显示列表,编号为no1,no2,no3
glNewList(no1,GL_COMPILE);//设置缺省的绘图条件
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHTING);
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER ,1);
glLightModelf (GL_LIGHT_MODEL_TWO_SIDE,1.0f);
GLfloat vflPosition[]={50.0f,50.0f,50.0f,1.0f};
GLfloat vflAmbient[]={1.f,1.f,1.f,1.0f};
GLfloat vflDiffuse[]={1.f,1.f,1.f,1.0f};
GLfloat vflSpecular[]={1.0f,1.0f,1.0f,1.0f};
glLightfv(GL_LIGHT0,GL_POSITION,vflPosition);
glLightfv(GL_LIGHT0,GL_AMBIENT,vflAmbient);
glLightfv(GL_LIGHT0,GL_DIFFUSE,vflDiffuse);
glLightfv(GL_LIGHT0,GL_SPECULAR,vflSpecular);
glEnable(GL_LIGHT0);//设置缺省的光照
static GLfloat vfmAmbient[]={1.0f,1.0f,1.0f,1.0f};
static GLfloat vfmSpecular[]={0.1f,0.1f,0.1f,1.0f};
static GLfloat shine[]={10};
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,vfmAmbient);
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,vfmSpecular);
glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,shine);
//设置缺省的材质
GLfloat bordercolor[]={.5f,.5f,.5f,0.0f};
glPixelStorei(GL_UNPACK_ALIGNMENT, 8);
//设置缺省的纹理
glTexImage2D(GL_TEXTURE_2D, 0, 4, 128, 128, 0,GL_RGB,
GL_UNSIGNED_BYTE, &(GLubyte)m_pTexForest0[0][0][0]);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, bordercolor);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);
glEnable(GL_TEXTURE_2D);
glEndList();
glNewList(no2,GL_COMPILE); //三维地形核心代码
float normal[3];
int width=m_iDemWidth;
int height=m_iDemHeight;
int i,j;
GLint* x=m_pDemX;
GLint* y=m_pDemY;
GLint* h=m_pDemH;
glDisable(GL_LIGHTING);
for(i=0;iGetSafeHdc(), &pfd)) == 0 )
{ MessageBox("ChoosePixelFormat failed");
return;
}
if (SetPixelFormat(pDC->GetSafeHdc(), pixelformat, &pfd) == FALSE)
{ MessageBox("SetPixelFormat failed");
return;
}
return;
}
//三维场景的绘制函数
void CDialogDlg::draw_scence()
{
glCallList(no3);//第一步:绘制天空背景
glCallList(no1);//第二步:回到缺省的绘制条件下
glMatrixMode(GL_PROJECTION);
GLsizei nWidth=800;
GLsizei nHeight=600;
GLdouble dAspect=(GLdouble)nWidth/(GLdouble)nHeight;
glLoadIdentity();
gluPerspective(60,dAspect,1.0,20000.0);
glViewport(0,0,nWidth,nHeight);
glMatrixMode(GL_MODELVIEW);
gluLookAt(lookx,looky,lookz,0,9990,860,0,0,1);
glTranslated(px,py,pz);
glCallList(no2);//第三步:绘制三维地形
glFinish();
}
//计算点的规一化法向量,计算方法参见计算机图形学相关书籍
void CDialogDlg::GetVertexNormal(int i,int j, float normal[3])
{
float normal1[3],normal2[3],normal3[3],normal4[3];
GLint x[4],y[4],z[4];
int width,height;
width=m_iDemWidth;
height=m_iDemHeight;
if ((i0)&&(j>0)&&(jEnableWindow(true);
mywnd=GetDlgItem(IDC_BUTTON3);
mywnd->EnableWindow(false);
}
void CDialogDlg::readdem()//读取地形文件数据,加载到内存
{
m_pDemH=NULL;
m_pDemX=NULL;
m_pDemY=NULL;
m_iDemWidth=0;
m_iDemHeight=0;
typedef struct tagDEMFILEHEADER{//地形文件的文件头,需要根据不同的地形文件进行相应的修改
int map[6];
int height;
int width;
float sx;
float sy;
float tap;
}DEMFILEHEADER;
DEMFILEHEADER DemHeader;
LPSTR lpstr=(LPSTR)::LockResource(LoadResource(NULL,
FindResource(NULL,MAKEINTRESOURCE(IDR_DEM1),"DEM")));
memcpy((LPSTR)&DemHeader,lpstr,sizeof(DEMFILEHEADER));
// 获取地形文件的相关信息,比如宽度,长度,间距等
m_iDemWidth=DemHeader.width;
m_iDemHeight=DemHeader.height;
m_pDemX = new int[m_iDemWidth];
m_pDemY = new int[m_iDemHeight];
m_pDemH = new int[m_iDemWidth*m_iDemHeight];
for(int i=0;i10)
{
if((g_fHeight[x*m_iDemWidth+z]-g_fHeight[x*m_iDemWidth+z+1])<12 && (g_fHeight[x*m_iDemWidth+z]-g_fHeight[x*m_iDemWidth+z+1])>-12 && (g_fHeight[x*m_iDemWidth+z]-g_fHeight[(x+1)*m_iDemWidth+z])<12 && (g_fHeight[x*m_iDemWidth+z]-g_fHeight[(x+1)*m_iDemWidth+z])>-12)
{
colour[x][z].g =gradient + 0.5;//0.6;
colour[x][z].r = colour[x][z].g*0.2;
colour[x][z].b = colour[x][z].g*0.2;
}
else
{
colour[x][z].r = gradient + 0.75;
colour[x][z].g = colour[x][z].r*0.8;
colour[x][z].b = colour[x][z].r*0.7;
}
}
else if(g_fHeight[x*m_iDemWidth+z]>0){
colour[x][z].g = gradient + 0.75;
colour[x][z].r = colour[x][z].g * 0.2;
colour[x][z].b = 0.2;
}
else {
colour[x][z].r = gradient + 1.0;
colour[x][z].g = colour[x][z].r * 0.75;
colour[x][z].b = colour[x][z].r * 0.5;
}
}
}
void CDialogDlg::OnTimer(UINT nIDEvent) //漫游时进行位置平移
{
// TODO: Add your message handler code here and/or call default
px-=10;
py+=8;
pz-=1;
OnPaint();
CDialog::OnTimer(nIDEvent);
}
void CDialogDlg::stop_move() //停止漫游,也就是关闭定时器
{
// TODO: Add your control notification handler code here
KillTimer(1);
CWnd* mywnd;
mywnd=GetDlgItem(IDC_BUTTON3);
mywnd->EnableWindow(true);
mywnd=GetDlgItem(IDC_BUTTON4);
mywnd->EnableWindow(false);
}
//计算法向量之---求矢量的叉乘
void CDialogDlg::GetShiCha(float x1, float y1, float h1, float x2, float y2, float h2,float normal[3])
{
normal[0]=y1*h2-y2*h1;
normal[1]=x2*h1-x1*h2;
normal[2]=x1*y2-x2*y1;
}
int CDialogDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
CSplashWnd::ShowSplashScreen(this);//程序运行开始出现闪现窗口
return 0;
}