LiFengView.cpp
资源名称:虚拟地形建模.rar [点击查看]
上传用户:dfjhuyju
上传日期:2013-03-13
资源大小:11035k
文件大小:8k
源码类别:
OpenGL
开发平台:
Visual C++
- // LiFengView.cpp : implementation of the CLiFengView class
- //
- #include "stdafx.h"
- #include "LiFeng.h"
- #include "LiFengDoc.h"
- #include "LiFengView.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CLiFengView
- IMPLEMENT_DYNCREATE(CLiFengView, CView)
- BEGIN_MESSAGE_MAP(CLiFengView, CView)
- //{{AFX_MSG_MAP(CLiFengView)
- ON_WM_CREATE()
- ON_WM_DESTROY()
- ON_WM_SIZE()
- ON_WM_TIMER()
- ON_WM_LBUTTONDBLCLK()
- ON_WM_LBUTTONDOWN()
- ON_WM_LBUTTONUP()
- 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()
- /////////////////////////////////////////////////////////////////////////////
- // CLiFengView construction/destruction
- CLiFengView::CLiFengView()
- {
- }
- CLiFengView::~CLiFengView()
- {
- }
- BOOL CLiFengView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- return CView::PreCreateWindow(cs);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CLiFengView drawing
- void CLiFengView::OnDraw(CDC* pDC)
- {
- CLiFengDoc* pDoc = GetDocument();
- CRect rect;
- GetClientRect(&rect);
- Win_Height=rect.bottom-rect.top;
- Win_Width=rect.right-rect.left;
- RenderScene();
- ASSERT_VALID(pDoc);
- // TODO: add draw code for native data here
- }
- /////////////////////////////////////////////////////////////////////////////
- // CLiFengView printing
- BOOL CLiFengView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // default preparation
- return DoPreparePrinting(pInfo);
- }
- void CLiFengView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add extra initialization before printing
- }
- void CLiFengView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add cleanup after printing
- }
- /////////////////////////////////////////////////////////////////////////////
- // CLiFengView diagnostics
- #ifdef _DEBUG
- void CLiFengView::AssertValid() const
- {
- CView::AssertValid();
- }
- void CLiFengView::Dump(CDumpContext& dc) const
- {
- CView::Dump(dc);
- }
- CLiFengDoc* CLiFengView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CLiFengDoc)));
- return (CLiFengDoc*)m_pDocument;
- }
- void CLiFengView::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);
- }
- BOOL CLiFengView::InitializeOpenGL(CDC* pDC)
- {
- m_pDC = pDC;
- SetupPixelFormat();
- m_hrc = ::wglCreateContext(m_pDC->GetSafeHdc());
- ::wglMakeCurrent(m_pDC->GetSafeHdc(),m_hrc);
- InitScene();
- 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[] = {2, 4.0, 2.0, 0.0};
- 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);
- glEnable(GL_LIGHTING);
- glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
- glHint(GL_POLYGON_SMOOTH_HINT,GL_NICEST );
- glDepthFunc(GL_LESS);
- glEnable(GL_DEPTH_TEST);
- glShadeModel(GL_SMOOTH);
- glEnable(GL_NORMALIZE);
- glEnable(GL_COLOR_MATERIAL);
- return TRUE;
- }
- BOOL CLiFengView::SetupPixelFormat()
- {
- PIXELFORMATDESCRIPTOR pfd = {
- sizeof(PIXELFORMATDESCRIPTOR), // pfd结构的大小
- 1, // 版本号
- PFD_DRAW_TO_WINDOW | // 支持在窗口中绘图
- PFD_SUPPORT_OPENGL | // 支持 OpenGL
- PFD_DOUBLEBUFFER, // 双缓存模式
- PFD_TYPE_RGBA, // RGBA 颜色模式
- 36, // 24 位颜色深度
- 0, 0, 0, 0, 0, 0, // 忽略颜色位
- 0, // 没有非透明度缓存
- 0, // 忽略移位位
- 0, // 无累加缓存
- 0, 0, 0, 0, // 忽略累加位
- 24, // 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 CLiFengView::RenderScene()
- {
- wglMakeCurrent(m_pDC->GetSafeHdc(), m_hrc);
- glClearColor(0.0f,0.0f,0.0f,1.0f);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glPushMatrix();
- mycamera.Update(Win_Width,Win_Height,100);
- mycamera.Look();
- myskybox.DrawSkyBox();
- m_Dem.RenderDem();
- glPopMatrix();
- ::SwapBuffers(m_pDC->GetSafeHdc());
- return TRUE;
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CLiFengView message handlers
- int CLiFengView::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CView::OnCreate(lpCreateStruct) == -1)
- return -1;
- m_pDC = new CClientDC(this);
- SetTimer(1,20,NULL);
- InitializeOpenGL(m_pDC);
- return 0;
- }
- void CLiFengView::OnDestroy()
- {
- CView::OnDestroy();
- ::wglMakeCurrent(0,0);
- ::wglDeleteContext(m_hrc);
- if(m_hPalette)
- {
- DeleteObject(m_hPalette);
- }
- if(m_pDC)
- {
- delete m_pDC;
- }
- KillTimer(1);
- }
- void CLiFengView::OnSize(UINT nType, int cx, int cy)
- {
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(40.0,(GLdouble)cx/(GLdouble)cy,2,20000000.0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glViewport(0,0,cx,cy);
- CView::OnSize(nType, cx, cy);
- }
- void CLiFengView::OnTimer(UINT nIDEvent)
- {
- Invalidate(FALSE);
- CView::OnTimer(nIDEvent);
- }
- void CLiFengView::OnLButtonDblClk(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- CView::OnLButtonDblClk(nFlags, point);
- }
- void CLiFengView::OnLButtonDown(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- mycamera.Role_Flag = true;
- CView::OnLButtonDown(nFlags, point);
- }
- void CLiFengView::OnLButtonUp(UINT nFlags, CPoint point)
- {
- mycamera.Role_Flag = false;
- CView::OnLButtonUp(nFlags, point);
- }
- void CLiFengView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- switch (nChar)
- {
- case VK_ESCAPE: exit(0); //退出程序
- break;
- default:
- break;
- }
- CView::OnKeyDown(nChar, nRepCnt, nFlags);
- }
- void CLiFengView::InitScene()
- {
- BOOL t_Blend=false;
- m_Dem.ReadDem("terrain/085008.dem");
- m_Dem.CreateCompileList();
- float eyex;
- float eyey;
- float eyez;
- m_Dem.SetViewPosition(&eyex, &eyey, &eyez);
- mycamera.PositionCamera(eyex, eyey + 500, eyez, eyex, eyey, eyez + 10000, 0, 1, 0);
- myskybox.InitSkyBox(50000, 50000, 50000, eyex, eyey - 5000, eyez);
- }