MyTerrain1View.cpp
上传用户:sz25923981
上传日期:2022-06-28
资源大小:3615k
文件大小:10k
- // MyTerrain1View.cpp : implementation of the CMyTerrain1View class
- //
- #include "stdafx.h"
- #include "MyTerrain1.h"
- #include "MyTerrain1Doc.h"
- #include "MyTerrain1View.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CMyTerrain1View
- IMPLEMENT_DYNCREATE(CMyTerrain1View, CView)
- BEGIN_MESSAGE_MAP(CMyTerrain1View, CView)
- //{{AFX_MSG_MAP(CMyTerrain1View)
- ON_WM_CREATE()
- ON_WM_DESTROY()
- ON_WM_SIZE()
- ON_WM_TIMER()
- ON_WM_KEYDOWN()
- //}}AFX_MSG_MAP
- // Standard printing commands
- ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CMyTerrain1View construction/destruction
- CMyTerrain1View::CMyTerrain1View()
- {
- // TODO: add construction code here
- }
- CMyTerrain1View::~CMyTerrain1View()
- {
- }
- BOOL CMyTerrain1View::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- ////////////////////////////////////////////////////////////////
- //设置窗口类型
- cs.style |=WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
- ////////////////////////////////////////////////////////////////
- return CView::PreCreateWindow(cs);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMyTerrain1View drawing
- void CMyTerrain1View::OnDraw(CDC* pDC)
- {
- CMyTerrain1Doc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- // TODO: add draw code for native data here
- //////////////////////////////////////////////////////////////////
- RenderScene(); //渲染场景
- //////////////////////////////////////////////////////////////////
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMyTerrain1View printing
- BOOL CMyTerrain1View::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // default preparation
- return DoPreparePrinting(pInfo);
- }
- void CMyTerrain1View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add extra initialization before printing
- }
- void CMyTerrain1View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add cleanup after printing
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMyTerrain1View diagnostics
- #ifdef _DEBUG
- void CMyTerrain1View::AssertValid() const
- {
- CView::AssertValid();
- }
- void CMyTerrain1View::Dump(CDumpContext& dc) const
- {
- CView::Dump(dc);
- }
- CMyTerrain1Doc* CMyTerrain1View::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyTerrain1Doc)));
- return (CMyTerrain1Doc*)m_pDocument;
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CMyTerrain1View message handlers
- int CMyTerrain1View::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CView::OnCreate(lpCreateStruct) == -1)
- return -1;
-
- // TODO: Add your specialized creation code here
- //////////////////////////////////////////////////////////////////
- //初始化OpenGL和设置定时器
- m_pDC = new CClientDC(this);
- SetTimer(1, 20, NULL);
- InitializeOpenGL(m_pDC);
- //////////////////////////////////////////////////////////////////
- Init();
- return 0;
- }
- void CMyTerrain1View::OnDestroy()
- {
- CView::OnDestroy();
-
- // TODO: Add your message handler code here
- /////////////////////////////////////////////////////////////////
- //删除调色板和渲染上下文、定时器
- ::wglMakeCurrent(0,0);
- ::wglDeleteContext( m_hRC);
- if (m_hPalette)
- DeleteObject(m_hPalette);
- if ( m_pDC )
- {
- delete m_pDC;
- }
- KillTimer(1);
- /////////////////////////////////////////////////////////////////
-
- }
- void CMyTerrain1View::OnSize(UINT nType, int cx, int cy)
- {
- CView::OnSize(nType, cx, cy);
-
- // TODO: Add your message handler code here
- /////////////////////////////////////////////////////////////////
- //添加窗口缩放时的图形变换函数
- glViewport(0,0,cx,cy);
- /////////////////////////////////////////////////////////////////
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glFrustum(-10, 10, -10, 10, 10, 500);
- glMatrixMode(GL_MODELVIEW);
- }
- void CMyTerrain1View::OnTimer(UINT nIDEvent)
- {
- // TODO: Add your message handler code here and/or call default
- /////////////////////////////////////////////////////////////////
- //添加定时器响应函数和场景更新函数
- Invalidate(FALSE);
- /////////////////////////////////////////////////////////////////
-
- CView::OnTimer(nIDEvent);
- }
- /////////////////////////////////////////////////////////////////////
- // 设置逻辑调色板
- //////////////////////////////////////////////////////////////////////
- void CMyTerrain1View::SetLogicalPalette(void)
- {
- struct
- {
- WORD Version;
- WORD NumberOfEntries;
- PALETTEENTRY aEntries[256];
- } logicalPalette = { 0x300, 256 };
- BYTE reds[] = {0, 36, 72, 109, 145, 182, 218, 255};
- BYTE greens[] = {0, 36, 72, 109, 145, 182, 218, 255};
- BYTE blues[] = {0, 85, 170, 255};
- for (int colorNum=0; colorNum<256; ++colorNum)
- {
- logicalPalette.aEntries[colorNum].peRed =
- reds[colorNum & 0x07];
- logicalPalette.aEntries[colorNum].peGreen =
- greens[(colorNum >> 0x03) & 0x07];
- logicalPalette.aEntries[colorNum].peBlue =
- blues[(colorNum >> 0x06) & 0x03];
- logicalPalette.aEntries[colorNum].peFlags = 0;
- }
- m_hPalette = CreatePalette ((LOGPALETTE*)&logicalPalette);
- }
- //////////////////////////////////////////////////////////
- // 初始化openGL场景
- //////////////////////////////////////////////////////////
- BOOL CMyTerrain1View::InitializeOpenGL(CDC* pDC)
- {
- m_pDC = pDC;
- SetupPixelFormat();
- //生成绘制描述表
- m_hRC = ::wglCreateContext(m_pDC->GetSafeHdc());
- //置当前绘制描述表
- ::wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC);
- return TRUE;
- }
- //////////////////////////////////////////////////////////
- // 设置像素格式
- //////////////////////////////////////////////////////////
- BOOL CMyTerrain1View::SetupPixelFormat()
- {
- PIXELFORMATDESCRIPTOR pfd = {
- sizeof(PIXELFORMATDESCRIPTOR), // pfd结构的大小
- 1, // 版本号
- PFD_DRAW_TO_WINDOW | // 支持在窗口中绘图
- PFD_SUPPORT_OPENGL | // 支持 OpenGL
- PFD_DOUBLEBUFFER, // 双缓存模式
- PFD_TYPE_RGBA, // RGBA 颜色模式
- 24, // 24 位颜色深度
- 0, 0, 0, 0, 0, 0, // 忽略颜色位
- 0, // 没有非透明度缓存
- 0, // 忽略移位位
- 0, // 无累加缓存
- 0, 0, 0, 0, // 忽略累加位
- 32, // 32 位深度缓存
- 0, // 无模板缓存
- 0, // 无辅助缓存
- PFD_MAIN_PLANE, // 主层
- 0, // 保留
- 0, 0, 0 // 忽略层,可见性和损毁掩模
- };
- int pixelformat;
- pixelformat = ::ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd);//选择像素格式
- ::SetPixelFormat(m_pDC->GetSafeHdc(), pixelformat, &pfd); //设置像素格式
- if(pfd.dwFlags & PFD_NEED_PALETTE)
- SetLogicalPalette(); //设置逻辑调色板
- return TRUE;
- }
- //////////////////////////////////////////////////////////
- // 场景绘制与渲染
- //////////////////////////////////////////////////////////
- BOOL CMyTerrain1View::RenderScene()
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
- glLoadIdentity();
- gluLookAt(100, 100, 100, 0, 0, 0, 0, 1, 0);
-
- glRotatef((float)angle, 0, 1, 0);
- glRotatef(90, 1, 0, 0);
- glTranslatef(-200, -200, 0);
- if (!stop) angle += 1;
- if (angle > 359) angle = 0;
- g.Draw(); // 绘制地形
- glFlush();
- ::SwapBuffers(m_pDC->GetSafeHdc()); //交互缓冲区
- return TRUE;
- }
- void CMyTerrain1View::Init(void)
- {
- int angle=0;
- int stop=0;
- GLfloat light_ambient[] = {0.0, 0.0, 0.0, 1.0};
- GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0};
- GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0};
- GLfloat light_position[] = {0.0, -100.0, -100.0, 0.0};
- glClearColor(0, 0, 0, 0);
- glColor3f(1, 1, 1);
-
- g.SetCellLength(40); // 网格之间的长度
- g.SetHeight(0, 40); // 地形高程的最小和最大值
- g.GenerateNewGrid(); // 生成新的地形
- g.SetInterpolationLevel(6); // 曲线插值阶数
- g.Compile();
- glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
- glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
- glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
- glEnable(GL_LIGHT0);
- glDepthFunc(GL_LESS);
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_LIGHTING); // 使用光照
- glEnable(GL_NORMALIZE);
- glShadeModel(GL_FLAT);
- glCullFace(GL_FRONT);
- glEnable(GL_CULL_FACE);
- glGenTextures(1, texture);
- LoadTexture("tr.rgb", 0);
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, texture[0]);
- }
- /////////////////////////////////////////////////////////////////////////////////
- //装入纹理
- void CMyTerrain1View::LoadTexture(char *fn, int t_num)
- {
- int texwid, texht;
- int texcomps;
-
- teximage = m_Tex->read_texture(fn, &texwid, &texht, &texcomps);
- if (!teximage)
- {
- MessageBox("没有找到图形文件tr.rgb","错误",MB_ICONQUESTION);
- exit(0);
- }
- glBindTexture(GL_TEXTURE_2D, texture[t_num]);
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- glTexImage2D(GL_TEXTURE_2D, 0, 3, texwid, texht, 0, GL_RGBA, GL_UNSIGNED_BYTE, teximage);
- gluBuild2DMipmaps(GL_TEXTURE_2D, 3, texwid, texht, GL_RGBA, GL_UNSIGNED_BYTE, teximage);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
- free(teximage);
- }
- void CMyTerrain1View::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- // TODO: Add your message handler code here and/or call default
- switch (nChar)
- {
- case VK_F12: g.GenerateNewGrid(); g.Compile(); // 生成新的地形
- break;
- case VK_SPACE: stop = !stop; // 停止动画
- break;
- case VK_ESCAPE: exit(0); //退出程序
- break;
- }
- CView::OnKeyDown(nChar, nRepCnt, nFlags);
- }