www.pudn.com > 3dpipe.zip > Tridimensional PipeView.cpp, change:2006-01-21,size:13491b


// Tridimensional PipeView.cpp : implementation of the CTridimensionalPipeView class 
// 
 
#include "stdafx.h" 
#include "Tridimensional Pipe.h" 
 
#include "Tridimensional PipeDoc.h" 
#include "Tridimensional PipeView.h" 
 
#include "PipelineDatabase.h" 
#include "PipeLineObject.h" 
#include "math.h" 
 
//加入包含有DXFToFile.DLL中导出信息的头文件 
#include "ReadDXF.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CTridimensionalPipeView 
 
IMPLEMENT_DYNCREATE(CTridimensionalPipeView, CView) 
 
BEGIN_MESSAGE_MAP(CTridimensionalPipeView, CView) 
	//{{AFX_MSG_MAP(CTridimensionalPipeView) 
	ON_COMMAND(ID_VIEW_BROWSE_MOVE, OnViewBrowseMove) 
	ON_WM_SIZE() 
	ON_WM_CREATE() 
	ON_WM_DESTROY() 
	ON_WM_ERASEBKGND() 
	ON_WM_KEYDOWN() 
	ON_WM_LBUTTONDBLCLK() 
	ON_WM_MOUSEMOVE() 
	ON_COMMAND(ID_FILE_SAVE, OnFileSave) 
	//}}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() 
 
///////////////////////////////////////////////////////////////////////////// 
// CTridimensionalPipeView construction/destruction 
 
CTridimensionalPipeView::CTridimensionalPipeView() 
{ 
//	CTridimensionalPipeDoc* pDoc = GetDocument(); 
//	ptCentre3DArray.SetSize(5000,500); 
	 
	rotate=0.0; 
	Near=1.0f;angle=30.0f; 
	oldmx=oldmy=0.0f; 
 
//	centerx = (pDoc->MapFile.boxBoundary.maxX - pDoc->MapFile.boxBoundary.minX) / 2; 
//	centery = (pDoc->MapFile.boxBoundary.maxY - pDoc->MapFile.boxBoundary.minY) / 2; 
//	centerz = 0.0; 
 
//	eyex = centerx;		 
//	eyey = centery - 10; 
//	eyez = 0.0; 
 
//	eyex = 8500.0f; eyey = 1000.0f; eyez =2000.0f; 
//	centerx = 8500.0f;	centery = 3251.0f;	centerz =0.0f;	 
//	eyex = 100.0f; eyey = 100.0f; eyez =1000.0f; 
//	centerx = 60000.0f;	centery = 75000.0f;	centerz =0.0f;	 
	eyex = 300.0f; eyey = 100.0f; eyez =10.0f; 
	centerx = 300.0f;	centery = 150.0f;	centerz =0.0f;	 
	 
	 
} 
 
CTridimensionalPipeView::~CTridimensionalPipeView() 
{ 
	glDisable(GL_TEXTURE_2D);	 
	glDisable(GL_DEPTH_TEST); 
	glMatrixMode(GL_MODELVIEW); 
	glLoadIdentity(); 
} 
 
BOOL CTridimensionalPipeView::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); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CTridimensionalPipeView drawing 
 
void CTridimensionalPipeView::OnDraw(CDC* pDC) 
{ 
	CTridimensionalPipeDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc); 
 
	wglMakeCurrent(pDC->m_hDC,hglrc); 
 
	Drawing(); 
	if (SwapBuffers(pDC->m_hDC)==FALSE) 
	{ 
			DWORD errorCode = GetLastError(); 
			CString errorStr; 
			errorStr.Format("SwapBuffers returned error code %d.", errorCode); 
			AfxMessageBox(errorStr); 
			return; 
	} 
	wglMakeCurrent(pDC->m_hDC,NULL); 
	// TODO: add draw code for native data here 
} 
 
void CTridimensionalPipeView::OnSize(UINT nType, int cx, int cy)  
{ 
	CView::OnSize(nType, cx, cy); 
	 
	// TODO: Add your message handler code here 
	int width=cx; 
	int height=cy; 
	CClientDC clientDC(this); 
	wglMakeCurrent(clientDC.m_hDC,hglrc); 
	glViewport(0,0,width,height); 
	glMatrixMode(GL_PROJECTION); 
	glLoadIdentity(); 
	gluPerspective(45.0,(GLfloat)width/(GLfloat)height,0.1,20000.0); 
 
	glMatrixMode(GL_MODELVIEW); 
	glLoadIdentity(); 
 
	glDrawBuffer(GL_BACK); 
	 
} 
 
int CTridimensionalPipeView::OnCreate(LPCREATESTRUCT lpCreateStruct)  
{ 
	if (CView::OnCreate(lpCreateStruct) == -1) 
		return -1; 
	 
	// TODO: Add your specialized creation code here 
		PIXELFORMATDESCRIPTOR pfd={ 
		sizeof(PIXELFORMATDESCRIPTOR), 
			1, 
			PFD_DRAW_TO_WINDOW| 
			PFD_SUPPORT_OPENGL| 
			PFD_DOUBLEBUFFER, 
			PFD_TYPE_RGBA, 
			24, 
			0,0,0,0,0,0, 
			0, 
			0, 
			0, 
			0,0,0,0, 
			32, 
			0, 
			PFD_MAIN_PLANE, 
			0, 
			0,0,0 
	}; 
	CClientDC clientdc(this); 
	int pf=ChoosePixelFormat(clientdc.m_hDC,&pfd); 
	BOOL rt=SetPixelFormat(clientdc.m_hDC,pf,&pfd); 
	hglrc=wglCreateContext(clientdc.m_hDC); 
	 
	return 0; 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CTridimensionalPipeView printing 
 
BOOL CTridimensionalPipeView::OnPreparePrinting(CPrintInfo* pInfo) 
{ 
	// default preparation 
	return DoPreparePrinting(pInfo); 
} 
 
void CTridimensionalPipeView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add extra initialization before printing 
} 
 
void CTridimensionalPipeView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add cleanup after printing 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CTridimensionalPipeView diagnostics 
 
#ifdef _DEBUG 
void CTridimensionalPipeView::AssertValid() const 
{ 
	CView::AssertValid(); 
} 
 
void CTridimensionalPipeView::Dump(CDumpContext& dc) const 
{ 
	CView::Dump(dc); 
} 
 
CTridimensionalPipeDoc* CTridimensionalPipeView::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTridimensionalPipeDoc))); 
	return (CTridimensionalPipeDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CTridimensionalPipeView message handlers 
 
void CTridimensionalPipeView::OnViewBrowseMove()  
{ 
	// TODO: Add your command handler code here 
	 
} 
 
void CTridimensionalPipeView::OnDestroy()  
{ 
	if(wglGetCurrentContext()!=NULL)  
	{ 
		// make the rendering context not current 
		wglMakeCurrent(NULL, NULL) ; 
	} 
	 
	if (hglrc!=NULL) 
	{ 
		wglDeleteContext(hglrc); 
		hglrc = NULL; 
	} 
 
	CView::OnDestroy(); 
	 
	// TODO: Add your message handler code here 
	 
} 
 
BOOL CTridimensionalPipeView::OnEraseBkgnd(CDC* pDC)  
{ 
	// TODO: Add your message handler code here and/or call default 
	 
	return TRUE; 
} 
 
void CTridimensionalPipeView::MoveEye(int type, GLfloat amount, int update) 
{ 
	GLfloat r,deltax,deltay,angle; 
	switch(type) 
	{ 
	case FORWARD: 
		r=(float)sqrt((centerx-eyex)*(centerx-eyex)+(centery-eyey)*(centery-eyey)); 
		deltax=amount*(centerx-eyex)/r*10; 
		deltay=amount*(centery-eyey)/r*10; 
		eyex+=deltax; 
		eyey+=deltay; 
		centerx+=deltax; 
		centery+=deltay; 
		break; 
	case TURNLEFT: 
		angle=5*amount/360.0f; 
		centerx=(float)(centerx-eyex)*cos(angle)+(centery-eyey)*sin(angle)+eyex; 
		centery=(float)(centery-eyey)*cos(angle)-(centerx-eyex)*sin(angle)+eyey; 
		break; 
	case UPDOWN: 
		eyez+=amount; 
		centerz+=amount; 
		break; 
	case LEFTRIGHT: 
		r=(float)sqrt((centerx-eyex)*(centerx-eyex)+(centery-eyey)*(centery-eyey)); 
		deltax=amount*(centerx-eyex)/r*5; 
		deltay=-amount*(centery-eyey)/r*5; 
		eyex+=deltay; 
		eyey+=deltax; 
		centerx+=deltay; 
		centery+=deltax; 
		break; 
	case LOOKUP: 
		centerz += amount; 
		break; 
	case LOOKDOWN: 
		centerz += amount; 
		break; 
	case ROTATE1: 
		rotate+=5.0; 
		break; 
	case ROTATE2: 
		rotate-=5.0; 
		break; 
	} 
/*	if(update) 
	{ 
		glMatrixMode(GL_MODELVIEW); 
		glLoadIdentity(); 
		gluLookAt(eyex,eyey,eyez,centerx,centery,centerz,0.0f,0.0f,1.0f); 
	}*/ 
} 
 
void CTridimensionalPipeView::OnMouseMove(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	switch(nFlags) 
	{ 
	case(MK_LBUTTON): 
		MoveEye(FORWARD,(GLfloat)(oldmy-point.y)/15.0,1); 
			 
//		MoveEye(FORWARD,(GLfloat)100,1); 
		break; 
	case(MK_RBUTTON): 
		MoveEye(TURNLEFT,(GLfloat)(oldmx-point.x)/10.0,1); 
		break; 
	} 
	oldmy=point.y; 
	oldmx=point.x; 
 
	Invalidate(FALSE); 
	 
	CView::OnMouseMove(nFlags, point); 
} 
 
void CTridimensionalPipeView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)  
{ 
	// TODO: Add your message handler code here and/or call default 
	GLfloat updown,leftright,rotate,lookupdown; 
	updown=leftright=rotate=lookupdown=0.0; 
	CWnd::OnKeyDown(nChar, nRepCnt, nFlags); 
 
	switch(nChar){ 
	 
	case VK_UP: 
		updown=updown+10.0; 
		MoveEye(UPDOWN,(GLfloat)updown,1); 
		break; 
	case VK_DOWN: 
		updown=updown-10.0; 
		MoveEye(UPDOWN,(GLfloat)updown,1); 
		break; 
	case VK_RIGHT: 
		leftright-=4; 
		MoveEye(LEFTRIGHT,(GLfloat)leftright,1); 
		break; 
	case VK_LEFT: 
		leftright+=4; 
		MoveEye(LEFTRIGHT,(GLfloat)leftright,1); 
		break; 
 
	case VK_INSERT: 
//		rotate+=20.0; 
		MoveEye(ROTATE1,(GLfloat)rotate,1); 
		break; 
	case VK_DELETE: 
//		rotate-=20.0; 
		MoveEye(ROTATE2,(GLfloat)rotate,1); 
		break; 
 
	case VK_HOME: 
		lookupdown += 2; 
		MoveEye(LOOKUP,(GLfloat)lookupdown,1); 
		break; 
 
	case VK_END: 
		lookupdown -= 2; 
		MoveEye(LOOKDOWN,(GLfloat)lookupdown,1); 
		break; 
	 
	} 
	InvalidateRect(NULL, FALSE); 
} 
 
 
void CTridimensionalPipeView::OnLButtonDblClk(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
//	GLuint selectBuf[BUFSIZE]; 
	GLint viewport[4]; 
//	GLint hits; 
	int x,y; 
	//************************* 
	CClientDC clientDC(this); 
	wglMakeCurrent(clientDC.m_hDC,hglrc); 
	//************************* 
	x=point.x; 
	y=point.y; 
	//get current viewport size 
	glGetIntegerv(GL_VIEWPORT,viewport); 
	//difine select buffer 
	glSelectBuffer(BUFSIZE,selectBuf); 
	//enter select mode 
	(void)glRenderMode(GL_SELECT); 
	//initialize name stack 
	glInitNames(); 
	glPushName((unsigned)-1); 
	glMatrixMode(GL_PROJECTION); 
	glPushMatrix(); 
	glLoadIdentity(); 
	//define pick matrix 
	gluPickMatrix((GLdouble)x,(GLdouble)(viewport[3]-y),1.0,1.0,viewport); 
	//define projection transformation 
	gluPerspective(45.0,viewport[2]/viewport[3],1.0,20000.0); 
	//draw in select mode 
//	draw(); 
	glPopMatrix(); 
	//flush drawing comands 
	glFlush(); 
	//exit select mode    
	//**************************** 
	wglMakeCurrent(NULL,NULL); 
	//**************************** 
		 
	CView::OnLButtonDblClk(nFlags, point); 
	Invalidate(); 
} 
 
void CTridimensionalPipeView::OnFileSave()  
{ 
	// TODO: Add your command handler code here 
	CTridimensionalPipeDoc* pDoc = GetDocument(); 
	CString str; 
	CFile file; 
 
	CFileDialog dlg(FALSE,"txt",NULL, 
		OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, 
		"文本文件(*.txt)|*.txt|所有文件(*.*)|*.*||",NULL); 
	 
	if(dlg.DoModal() == IDCANCEL) 
		return; 
	 
	CString filename = dlg.GetPathName(); 
	if(filename.IsEmpty()) 
		filename = pDoc->GetTitle() + ".txt"; 
	else 
		filename = filename.Left(filename.GetLength() - 3) + "txt"; 
 
	if(file.Open(filename,CFile::modeCreate| 
		CFile::modeNoTruncate|CFile::modeWrite) == 0) 
	{ 
		str = "保存文件" + filename + "失败!"; 
		AfxMessageBox(str); 
		return; 
	} 
 
	file.Write(&pDoc->MapFile.boxBoundary, 4*sizeof(double)); 
	file.Write(&pDoc->MapFile.pMapName, 20*sizeof(char)); 
	file.Write(&pDoc->MapFile.nPipePointSum, sizeof(unsigned int)); 
	for(UINT i = 0; i  pDoc->MapFile.nPipePointSum; i++) 
		file.Write(&pDoc->MapFile.arrPointFile[i], sizeof(unsigned int)+sizeof(float)*3); 
	file.Write(&pDoc->MapFile.nPipeLineSum, sizeof(unsigned int)); 
	for(i = 0; i  pDoc->MapFile.nPipeLineSum; i++) 
	{ 
		file.Write(&pDoc->MapFile.arrLineFile[i].ID,sizeof(unsigned int)); 
		file.Write(&pDoc->MapFile.arrLineFile[i].nPointNUM,sizeof(unsigned int)); 
		for(UINT j = 0; j  pDoc->MapFile.arrLineFile[i].nPointNUM; j++) 
			file.Write(&pDoc->MapFile.arrLineFile[i].arrPointArray[j],sizeof(unsigned int)); 
	}	 
} 
 
 
void CTridimensionalPipeView::Drawing() 
{ 
	CTridimensionalPipeDoc* pDoc = GetDocument(); 
 
	pDoc->PipeLine.GetGraphCenter(pMapCenter); 
	//clear window 
	glClearColor(0.0,0.0,0.0,0.0); 
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 
	glEnable(GL_DEPTH_TEST); 
	glDepthFunc(GL_LEQUAL); 
	 
	glMatrixMode(GL_MODELVIEW); 
	glLoadIdentity(); 
 
//	glPushMatrix(); 
	gluLookAt(eyex,eyey,eyez,centerx,centery,centerz,0.0f,0.0f,1.0f); 
	glPushMatrix(); 
	glTranslated(pMapCenter.x,pMapCenter.y,pMapCenter.z); 
	glRotatef((GLfloat)rotate,0.0,0.0,1.0); 
	glTranslated(-pMapCenter.x,-pMapCenter.y,pMapCenter.z); 
	LightInit(); 
	pDoc->PipeLine.DrawPipeSection(); 
	 
	glPopMatrix(); 
//	glPopMatrix(); 
	//flush drawing commands 
	glFlush(); 
} 
 
void CTridimensionalPipeView::LightInit() 
{ 
//	GLfloat mat_amb_diff[] = {0.1,0.5,0.8,1.0}; 
	GLfloat local_model_ambient[] = {0.2,0.2,0.2,1.0}; 
 
//	GLfloat mat_ambient[] = {0.2,0.2,0.2,1.0}; 
//	GLfloat mat_diffuse[] = {0.8,0.8,0.8,1.0}; 
//	GLfloat mat_specular[] = {1.0,1.0,1.0,1.0}; 
//	GLfloat mat_shininess[] = {50.0}; 
 
	GLfloat light0_ambient[] = {0.5,0.5,0.5,1.0}; 
	GLfloat light0_diffuse[] = {1.0,1.0,1.0,1.0}; 
	GLfloat light0_position[] = {-20.0,-20.0,10.0,1.0}; 
 
	GLfloat light1_ambient[] = {0.2,0.2,0.2,1.0}; 
	GLfloat light1_diffuse[] = {1.0,1.0,1.0,1.0}; 
	GLfloat light1_position[] = {1.0,1.0,1.0,0.0}; 
 
//	glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,mat_amb_diff); 
	glLightModelfv(GL_LIGHT_MODEL_AMBIENT,local_model_ambient); 
	glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,GL_TRUE); 
 
	glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE); 
 
	glLightfv(GL_LIGHT0,GL_AMBIENT,light0_ambient); 
	glLightfv(GL_LIGHT0,GL_DIFFUSE,light0_diffuse); 
	glLightfv(GL_LIGHT0,GL_POSITION,light0_position); 
 
	glLightfv(GL_LIGHT1,GL_AMBIENT,light0_ambient); 
	glLightfv(GL_LIGHT1,GL_DIFFUSE,light0_diffuse); 
	glLightfv(GL_LIGHT1,GL_POSITION,light0_position); 
	 
	glEnable(GL_COLOR_MATERIAL);	 
	glEnable(GL_LIGHTING); 
	glEnable(GL_LIGHT0); 
//	glEnable(GL_LIGHT1); 
}