www.pudn.com > Map_OpenGL.rar > Map_OpenGLView.cpp


// Map_OpenGLView.cpp : implementation of the CMap_OpenGLView class 
// 
 
#include "stdafx.h" 
#include "Map_OpenGL.h" 
#include "Map_OpenGLDoc.h" 
#include "Map_OpenGLView.h" 
#include "Map_Tool.h" 
 
#include "math.h" 
#define GL_PI 3.1415 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CMap_OpenGLView 
 
IMPLEMENT_DYNCREATE(CMap_OpenGLView, CGLEnabledView) 
 
BEGIN_MESSAGE_MAP(CMap_OpenGLView, CGLEnabledView) 
	//{{AFX_MSG_MAP(CMap_OpenGLView) 
	ON_WM_LBUTTONDOWN() 
	ON_WM_MOUSEMOVE() 
	ON_WM_LBUTTONUP() 
	ON_WM_KEYDOWN() 
	ON_WM_TIMER() 
	ON_COMMAND(ID_TRIANGLE, OnTriangle) 
	ON_UPDATE_COMMAND_UI(ID_TRIANGLE, OnUpdateTriangle) 
	ON_COMMAND(ID_COLOR, OnColor) 
	ON_UPDATE_COMMAND_UI(ID_COLOR, OnUpdateColor) 
	ON_COMMAND(ID_BITMAP, OnBitmap) 
	ON_UPDATE_COMMAND_UI(ID_BITMAP, OnUpdateBitmap) 
	//}}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() 
 
///////////////////////////////////////////////////////////////////////////// 
// CMap_OpenGLView construction/destruction 
 
ShowMap CMap_OpenGLView::m_showmap=nonemap; 
 
CMap_OpenGLView::CMap_OpenGLView() 
{ 
	// TODO: add construction code here 
	x_angle=0; 
	y_angle=0; 
	Key_x=Key_z=Key_y=0; 
	m_showmap=triangle; 
} 
 
CMap_OpenGLView::~CMap_OpenGLView() 
{ 
} 
 
 
 
/* 
BOOL CMap_OpenGLView::PreCreateWindow(CREATESTRUCT& cs) 
{ 
	// TODO: Modify the Window class or styles here by modifying 
	//  the CREATESTRUCT cs 
 
	return CView::PreCreateWindow(cs); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMap_OpenGLView drawing 
 
void CMap_OpenGLView::OnDraw(CDC* pDC) 
{ 
	CMap_OpenGLDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc); 
	// TODO: add draw code for native data here 
} 
*/ 
 
 
///////////////////////////////////////////////////////////////////////////// 
// CMap_OpenGLView printing 
 
BOOL CMap_OpenGLView::OnPreparePrinting(CPrintInfo* pInfo) 
{ 
	// default preparation 
	return DoPreparePrinting(pInfo); 
} 
 
void CMap_OpenGLView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add extra initialization before printing 
} 
 
void CMap_OpenGLView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add cleanup after printing 
} 
 
void CMap_OpenGLView::OnDrawGL(void) 
{ 
	CMap_OpenGLDoc* pDoc=GetDocument(); 
	ASSERT(pDoc); 
 
 
	if(CMap_Tool::map_select==Map_binarydem) 
	{ 
		float midx,midz,midy; 
		pDoc->m_binarydem.GetMidDate(midx,midz,midy); 
		glTranslated(-midx,-midy,-midz); 
		glTranslated(Key_x,Key_y,Key_z); 
		pDoc->m_binarydem.showtriagDem(); 
	} 
 
 
	if(CMap_Tool::map_select==Map_texttin) 
	{	 
		float tinmidx,tinmidy,tinmidz; 
		pDoc->m_tindate.GetMidDate(tinmidx,tinmidy,tinmidz); 
		glTranslated(-tinmidx,-tinmidy,-tinmidz); 
		glTranslated(Key_x,Key_y,Key_z); 
		pDoc->m_tindate.Show3DMode(); 
	} 
 
} 
 
 
void CMap_OpenGLView::SetRC(void) 
{ 
	glMatrixMode(GL_MODELVIEW); 
	glLoadIdentity(); 
 
	glEnable(GL_DEPTH_TEST); 
	glEnable(GL_NORMALIZE); 
 
	if(m_showmap==triangle) 
	{ 
		glPolygonMode(GL_FRONT,GL_LINE); 
	} 
	if(m_showmap==color) 
	{ 
		glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); 
	} 
	glEnable(GL_CULL_FACE); 
		 
	//高置明暗处理是平直还是光滑的 
	glShadeModel(GL_SMOOTH); 
 
	//本函数为删除隐藏表面而设置深度缓冲区 
	glDepthFunc(GL_LESS); 
 
	 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMap_OpenGLView diagnostics 
 
#ifdef _DEBUG 
void CMap_OpenGLView::AssertValid() const 
{ 
	CView::AssertValid(); 
} 
 
void CMap_OpenGLView::Dump(CDumpContext& dc) const 
{ 
	CView::Dump(dc); 
} 
 
CMap_OpenGLDoc* CMap_OpenGLView::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMap_OpenGLDoc))); 
	return (CMap_OpenGLDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CMap_OpenGLView message handlers 
 
void CMap_OpenGLView::OnLButtonDown(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	mousedownpoint=point; 
	SetCapture(); 
 
	 
	CGLEnabledView::OnLButtonDown(nFlags, point); 
} 
 
void CMap_OpenGLView::OnMouseMove(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	if(GetCapture()==this) 
	{ 
	y_angle=double(point.x-mousedownpoint.x)/3.6; 
	x_angle=double(point.y-mousedownpoint.y)/3.6; 
 
	Invalidate(TRUE); 
	mousedownpoint=point; 
	} 
	 
	CGLEnabledView::OnMouseMove(nFlags, point); 
} 
 
void CMap_OpenGLView::OnLButtonUp(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	mousedownpoint=CPoint(0,0); 
	ReleaseCapture(); 
 
	 
	CGLEnabledView::OnLButtonUp(nFlags, point); 
} 
 
void CMap_OpenGLView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)  
{ 
	// TODO: Add your message handler code here and/or call default 
	if(nChar=='W' || nChar==VK_UP) 
	{ 
		Key_z=Key_z+100; 
	} 
	if(nChar=='A' || nChar==VK_LEFT) 
	{ 
		Key_x=Key_x+100; 
	} 
	if(nChar=='D' || nChar==VK_RIGHT) 
	{ 
		Key_x=Key_x-100; 
	} 
	if(nChar=='S' || nChar==VK_DOWN) 
	{ 
		Key_z=Key_z-100; 
	} 
	if(nChar=='Z') 
	{ 
		Key_y=Key_y+50; 
	} 
	if(nChar=='X') 
	{ 
		Key_y=Key_y-50; 
	} 
	CGLEnabledView::OnKeyDown(nChar, nRepCnt, nFlags); 
} 
 
void CMap_OpenGLView::OnTimer(UINT nIDEvent)  
{ 
	// TODO: Add your message handler code here and/or call default 
	CRect m_maprc; 
	GetClientRect(m_maprc); 
	InvalidateRect(m_maprc); 
	 
	CGLEnabledView::OnTimer(nIDEvent); 
} 
 
 
 
//这个为显示三角形格网 
void CMap_OpenGLView::OnTriangle()  
{ 
	// TODO: Add your command handler code here 
	m_showmap=triangle; 
	 
} 
 
void CMap_OpenGLView::OnUpdateTriangle(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
	pCmdUI->SetCheck(m_showmap==triangle); 
	 
} 
 
 
//渲染 
void CMap_OpenGLView::OnColor()  
{ 
	// TODO: Add your command handler code here 
	m_showmap=color; 
	 
} 
 
void CMap_OpenGLView::OnUpdateColor(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
	pCmdUI->SetCheck(m_showmap==color); 
	 
} 
 
 
//贴图 
void CMap_OpenGLView::OnBitmap()  
{ 
	// TODO: Add your command handler code here 
	m_showmap=bit; 
	 
} 
 
void CMap_OpenGLView::OnUpdateBitmap(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
	pCmdUI->SetCheck(m_showmap==bit); 
	 
}