LiFengView.cpp
上传用户:dfjhuyju
上传日期:2013-03-13
资源大小:11035k
文件大小:8k
源码类别:

OpenGL

开发平台:

Visual C++

  1. // LiFengView.cpp : implementation of the CLiFengView class
  2. //
  3. #include "stdafx.h"
  4. #include "LiFeng.h"
  5. #include "LiFengDoc.h"
  6. #include "LiFengView.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CLiFengView
  14. IMPLEMENT_DYNCREATE(CLiFengView, CView)
  15. BEGIN_MESSAGE_MAP(CLiFengView, CView)
  16. //{{AFX_MSG_MAP(CLiFengView)
  17. ON_WM_CREATE()
  18. ON_WM_DESTROY()
  19. ON_WM_SIZE()
  20. ON_WM_TIMER()
  21. ON_WM_LBUTTONDBLCLK()
  22. ON_WM_LBUTTONDOWN()
  23. ON_WM_LBUTTONUP()
  24. ON_WM_KEYDOWN()
  25. //}}AFX_MSG_MAP
  26. // Standard printing commands
  27. ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  28. ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  29. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  30. END_MESSAGE_MAP()
  31. /////////////////////////////////////////////////////////////////////////////
  32. // CLiFengView construction/destruction
  33. CLiFengView::CLiFengView()
  34. {
  35. }
  36. CLiFengView::~CLiFengView()
  37. {
  38. }
  39. BOOL CLiFengView::PreCreateWindow(CREATESTRUCT& cs)
  40. {
  41. // TODO: Modify the Window class or styles here by modifying
  42. //  the CREATESTRUCT cs
  43. return CView::PreCreateWindow(cs);
  44. }
  45. /////////////////////////////////////////////////////////////////////////////
  46. // CLiFengView drawing
  47. void CLiFengView::OnDraw(CDC* pDC)
  48. {
  49. CLiFengDoc* pDoc = GetDocument();
  50. CRect rect;
  51. GetClientRect(&rect);
  52. Win_Height=rect.bottom-rect.top;
  53. Win_Width=rect.right-rect.left;
  54. RenderScene();
  55. ASSERT_VALID(pDoc);
  56. // TODO: add draw code for native data here
  57. }
  58. /////////////////////////////////////////////////////////////////////////////
  59. // CLiFengView printing
  60. BOOL CLiFengView::OnPreparePrinting(CPrintInfo* pInfo)
  61. {
  62. // default preparation
  63. return DoPreparePrinting(pInfo);
  64. }
  65. void CLiFengView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  66. {
  67. // TODO: add extra initialization before printing
  68. }
  69. void CLiFengView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  70. {
  71. // TODO: add cleanup after printing
  72. }
  73. /////////////////////////////////////////////////////////////////////////////
  74. // CLiFengView diagnostics
  75. #ifdef _DEBUG
  76. void CLiFengView::AssertValid() const
  77. {
  78. CView::AssertValid();
  79. }
  80. void CLiFengView::Dump(CDumpContext& dc) const
  81. {
  82. CView::Dump(dc);
  83. }
  84. CLiFengDoc* CLiFengView::GetDocument() // non-debug version is inline
  85. {
  86. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CLiFengDoc)));
  87. return (CLiFengDoc*)m_pDocument;
  88. }
  89. void CLiFengView::SetLogicalPalette(void)
  90. {
  91. struct
  92. {
  93. WORD Version;
  94. WORD NumberOfEntries;
  95. PALETTEENTRY aEntries[256];
  96. }logicalPalette = {0x300,256};
  97. BYTE reds[] = {0,36,72,109,145,182,218,255};
  98. BYTE greens[]={0,36,72,109,145,182,218,255};
  99. BYTE blues[]={0,85,170,255};
  100. for (int colorNum=0; colorNum<256; ++colorNum)
  101.     {
  102.         logicalPalette.aEntries[colorNum].peRed =
  103.             reds[colorNum & 0x07];
  104.         logicalPalette.aEntries[colorNum].peGreen =
  105.             greens[(colorNum >> 0x03) & 0x07];
  106.         logicalPalette.aEntries[colorNum].peBlue =
  107.             blues[(colorNum >> 0x06) & 0x03];
  108.         logicalPalette.aEntries[colorNum].peFlags = 0;
  109.     }
  110.     m_hPalette = CreatePalette ((LOGPALETTE*)&logicalPalette);
  111. }
  112. BOOL CLiFengView::InitializeOpenGL(CDC* pDC)
  113. {
  114. m_pDC = pDC;
  115. SetupPixelFormat();
  116. m_hrc = ::wglCreateContext(m_pDC->GetSafeHdc());
  117. ::wglMakeCurrent(m_pDC->GetSafeHdc(),m_hrc);
  118. InitScene();
  119. GLfloat light_ambient[] = {0.0, 0.0, 0.0, 1.0};
  120. GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0};
  121. GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0};
  122. GLfloat light_position[] = {2, 4.0, 2.0, 0.0};
  123. glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  124. glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  125. glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  126. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  127. glEnable(GL_LIGHT0);
  128. glEnable(GL_LIGHTING);
  129. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
  130. glHint(GL_POLYGON_SMOOTH_HINT,GL_NICEST ); 
  131. glDepthFunc(GL_LESS);
  132. glEnable(GL_DEPTH_TEST);
  133. glShadeModel(GL_SMOOTH);
  134. glEnable(GL_NORMALIZE);
  135. glEnable(GL_COLOR_MATERIAL);
  136. return TRUE;
  137. }
  138. BOOL CLiFengView::SetupPixelFormat()
  139. {
  140. PIXELFORMATDESCRIPTOR pfd = { 
  141.     sizeof(PIXELFORMATDESCRIPTOR),    // pfd结构的大小 
  142.     1,                                // 版本号 
  143.     PFD_DRAW_TO_WINDOW |              // 支持在窗口中绘图 
  144.     PFD_SUPPORT_OPENGL |              // 支持 OpenGL 
  145.     PFD_DOUBLEBUFFER,                 // 双缓存模式 
  146.     PFD_TYPE_RGBA,                    // RGBA 颜色模式 
  147.     36,                               // 24 位颜色深度 
  148.     0, 0, 0, 0, 0, 0,                 // 忽略颜色位 
  149.     0,                                // 没有非透明度缓存 
  150.     0,                                // 忽略移位位 
  151.     0,                                // 无累加缓存 
  152.     0, 0, 0, 0,                       // 忽略累加位 
  153.     24,                               // 32 位深度缓存     
  154.     0,                                // 无模板缓存 
  155.     0,                                // 无辅助缓存 
  156.     PFD_MAIN_PLANE,                   // 主层 
  157.     0,                                // 保留 
  158.     0, 0, 0                           // 忽略层,可见性和损毁掩模 
  159. }; 
  160. int pixelformat;
  161. pixelformat = ::ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd);//选择像素格式
  162. ::SetPixelFormat(m_pDC->GetSafeHdc(), pixelformat, &pfd); //设置像素格式
  163. if(pfd.dwFlags & PFD_NEED_PALETTE)
  164. SetLogicalPalette(); //设置逻辑调色板
  165. return TRUE;
  166. }
  167. BOOL CLiFengView::RenderScene()
  168. {
  169. wglMakeCurrent(m_pDC->GetSafeHdc(), m_hrc);
  170. glClearColor(0.0f,0.0f,0.0f,1.0f);
  171. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  172. glPushMatrix();
  173. mycamera.Update(Win_Width,Win_Height,100);
  174. mycamera.Look();
  175. myskybox.DrawSkyBox();
  176. m_Dem.RenderDem();
  177. glPopMatrix();
  178. ::SwapBuffers(m_pDC->GetSafeHdc());
  179. return TRUE;
  180. }
  181. #endif //_DEBUG
  182. /////////////////////////////////////////////////////////////////////////////
  183. // CLiFengView message handlers
  184. int CLiFengView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  185. {
  186. if (CView::OnCreate(lpCreateStruct) == -1)
  187. return -1;
  188. m_pDC = new CClientDC(this);
  189. SetTimer(1,20,NULL);
  190. InitializeOpenGL(m_pDC);
  191. return 0;
  192. }
  193. void CLiFengView::OnDestroy() 
  194. {
  195. CView::OnDestroy();
  196. ::wglMakeCurrent(0,0);
  197. ::wglDeleteContext(m_hrc);
  198. if(m_hPalette)
  199. {
  200. DeleteObject(m_hPalette);
  201. }
  202. if(m_pDC)
  203. {
  204. delete m_pDC;
  205. }
  206. KillTimer(1);
  207. }
  208. void CLiFengView::OnSize(UINT nType, int cx, int cy) 
  209. {
  210. glMatrixMode(GL_PROJECTION);  
  211. glLoadIdentity();
  212. gluPerspective(40.0,(GLdouble)cx/(GLdouble)cy,2,20000000.0);
  213. glMatrixMode(GL_MODELVIEW);
  214. glLoadIdentity();
  215. glViewport(0,0,cx,cy);
  216. CView::OnSize(nType, cx, cy);
  217. }
  218. void CLiFengView::OnTimer(UINT nIDEvent) 
  219. {
  220. Invalidate(FALSE);
  221. CView::OnTimer(nIDEvent);
  222. }
  223. void CLiFengView::OnLButtonDblClk(UINT nFlags, CPoint point) 
  224. {
  225. // TODO: Add your message handler code here and/or call default
  226. CView::OnLButtonDblClk(nFlags, point);
  227. }
  228. void CLiFengView::OnLButtonDown(UINT nFlags, CPoint point) 
  229. {
  230. // TODO: Add your message handler code here and/or call default
  231. mycamera.Role_Flag = true;
  232. CView::OnLButtonDown(nFlags, point);
  233. }
  234. void CLiFengView::OnLButtonUp(UINT nFlags, CPoint point) 
  235. {
  236. mycamera.Role_Flag = false;
  237. CView::OnLButtonUp(nFlags, point);
  238. }
  239. void CLiFengView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
  240. {
  241. switch (nChar)
  242. {
  243.    
  244.     case VK_ESCAPE: exit(0); //退出程序
  245. break;
  246. default:
  247. break;
  248. }
  249. CView::OnKeyDown(nChar, nRepCnt, nFlags);
  250. }
  251. void CLiFengView::InitScene()
  252. {
  253. BOOL t_Blend=false;
  254. m_Dem.ReadDem("terrain/085008.dem");
  255. m_Dem.CreateCompileList();
  256. float eyex;
  257. float eyey;
  258. float eyez;
  259. m_Dem.SetViewPosition(&eyex, &eyey, &eyez);
  260. mycamera.PositionCamera(eyex, eyey + 500, eyez, eyex, eyey, eyez + 10000, 0, 1, 0);
  261. myskybox.InitSkyBox(50000, 50000, 50000, eyex, eyey - 5000, eyez);
  262. }