⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dialogdlg.cpp

📁 这是树上的代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// 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;i<height-1;i++) //循环遍历每个地形采样点
	
	for(j=0;j<width-1;j++)
	
			{
			glBegin(GL_TRIANGLE_STRIP);//通过绘制三角带,构造地形的曲面

				glColor3f(colour[i][j].r, colour[i][j].g, colour[i][j].b);//设置颜色
				glTexCoord2f(j/7.00,i/7.00);//施加纹理
				GetVertexNormal(i,j,normal);//设置法向量
				glNormal3fv(normal);
				glVertex3f(x[j],y[i],h[i*width+j]);//设置点坐标
							
				glColor3f(colour[i+1][j].r, colour[i+1][j].g, colour[i+1][j].b);
				glTexCoord2f(j/7.00,(i+1)/7.00);
				GetVertexNormal(i+1,j,normal);
				glNormal3fv(normal);
				glVertex3f(x[j],y[i+1],h[(i+1)*width+j]);

				glColor3f(colour[i][j+1].r, colour[i][j+1].g, colour[i][j+1].b);
				glTexCoord2f((j+1)/7.00,i/7.00);				
				GetVertexNormal(i,j+1,normal);
				glNormal3fv(normal);
				glVertex3f(x[j+1],y[i],h[i*width+j+1]);
				
				glColor3f(colour[i+1][j+1].r, colour[i+1][j+1].g, colour[i+1][j+1].b);
				glTexCoord2f((j+1)/7.00,(i+1)/7.00);
				GetVertexNormal(i+1,j+1,normal);
				glNormal3fv(normal);
                glVertex3f(x[j+1],y[i+1],h[(i+1)*width+j+1]);
				
				glEnd(); 
			}
    //因为我采用的地形文件很小,只有120x120的大小,
	//所以在这个地方做了一个基于y轴的场景镜像,用于扩大显示的场景    
	for(i=0;i<width;i++)			
           x[i]=-1*x[i];
		for(i=0;i<119;i++)
			for(j=0;j<119;j++)
			{
			glBegin(GL_TRIANGLE_STRIP);
			    glColor3f(colour[i][j].r, colour[i][j].g, colour[i][j].b);
				glTexCoord2f(j/7.00,i/7.00);
				GetVertexNormal(i,j,normal);
				glNormal3fv(normal);
				glVertex3f(x[j],y[i],h[i*width+j]);
							
				glColor3f(colour[i+1][j].r, colour[i+1][j].g, colour[i+1][j].b);
				glTexCoord2f(j/7.00,(i+1)/7.00);
				GetVertexNormal(i+1,j,normal);
				glNormal3fv(normal);
				glVertex3f(x[j],y[i+1],h[(i+1)*width+j]);

				glColor3f(colour[i][j+1].r, colour[i][j+1].g, colour[i][j+1].b);
				glTexCoord2f((j+1)/7.00,i/7.00);				
				GetVertexNormal(i,j+1,normal);
				glNormal3fv(normal);
				glVertex3f(x[j+1],y[i],h[i*width+j+1]);
				
				glColor3f(colour[i+1][j+1].r, colour[i+1][j+1].g, colour[i+1][j+1].b);
				glTexCoord2f((j+1)/7.00,(i+1)/7.00);
				GetVertexNormal(i+1,j+1,normal);
				glNormal3fv(normal);
                glVertex3f(x[j+1],y[i+1],h[(i+1)*width+j+1]);
				
			glEnd(); 
			}
			glDisable(GL_TEXTURE_2D);
   glMatrixMode(GL_PROJECTION);
	GLsizei	nWidth=1000;
	GLsizei nHeight=700;
	GLdouble dAspect=(GLdouble)nWidth/(GLdouble)nHeight;
	
	glLoadIdentity();
	gluPerspective(60,dAspect,1.0,500.0);
	glViewport(0,0,nWidth,nHeight);

	    glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
		glTranslated(0,0,20);
		gluLookAt(0,50,0,0,0,0,0,0,1);
		glPushMatrix();
			glEnable(GL_LIGHTING);
		    glEnable(GL_LIGHT0);

			glTranslatef(0.0f, 0.0f, 1.0f);
            glTranslatef(2.0f, 0.0f, 0.0f);
			glTranslatef(0.0f, 17.0f, 0.0f);
			//glRotatef(-29.0f, 0.0, 1.0, 0.0);
			glRotatef(40.0f, 0.0f, 0.0f, 1.0f);
			glRotatef(5.1f, 1.0f, 0.0f, .0f);
			glScalef(1.7f, 1.7f, 1.7f);
			draw("dh10.obj","dh10.bmp");//绘制巡航导弹模型
			glDisable(GL_LIGHTING);
		glDisable(GL_TEXTURE_2D);
		glPopMatrix();
	glEndList();
   	
	   
    glNewList(no3,GL_COMPILE);//绘制天空背景
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
	glPixelStorei(GL_UNPACK_ALIGNMENT, 8);
		glTexImage2D(GL_TEXTURE_2D, 0, 3, 512, 512, 0,GL_BGR_EXT,
							GL_UNSIGNED_BYTE, &(GLubyte)m_pTexyun[0][0][0]);
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
    	glEnable(GL_TEXTURE_2D);
	glDisable(GL_DEPTH_TEST);
	glMatrixMode(GL_MODELVIEW);
    glViewport(0,0,1024,768);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glBegin(GL_QUADS);
		glTexCoord2f(0,0);
		glVertex2f(-1.0,-1.0);
		glTexCoord2f(0,1);
		glVertex2f(-1.0,1.0);
		glTexCoord2f(1,1);
		glVertex2f(1.0,1.0);
		glTexCoord2f(1,0);
		glVertex2f(1.0,-1.0);
		glEnd();
		glEndList();
 
	
//通过刷新场景,启用绘图的代码
CDialogDlg::OnPaint();	
}

//设置像素格式,这部分代码是用于设置opengl绘图环境的,属于固定的代码,
//一般情况下不用修改
void CDialogDlg::SetThePixelFormat(CDC* pDC)
{
    static PIXELFORMATDESCRIPTOR pfd = 
	{
        sizeof(PIXELFORMATDESCRIPTOR),  // size of this pfd
        1,                              // version number
        PFD_DRAW_TO_WINDOW |            // support window
          PFD_SUPPORT_OPENGL |          // support OpenGL
          PFD_DOUBLEBUFFER,             // double buffered
        PFD_TYPE_RGBA,                  // RGBA type
        24,                             // 24-bit color depth
        0, 0, 0, 0, 0, 0,               // color bits ignored

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -