www.pudn.com > 3DShow-v.rar > 3DShowView.cpp, change:2007-05-20,size:9330b


// 3DShowView.cpp : implementation of the CMy3DShowView class 
// 
 
#include "stdafx.h" 
#include "3DShow.h" 
 
#include "3DShowDoc.h" 
#include "3DShowView.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CMy3DShowView 
 
IMPLEMENT_DYNCREATE(CMy3DShowView, CView) 
 
BEGIN_MESSAGE_MAP(CMy3DShowView, CView) 
	//{{AFX_MSG_MAP(CMy3DShowView) 
	ON_WM_CREATE() 
	ON_WM_LBUTTONDOWN() 
	ON_WM_LBUTTONUP() 
	ON_WM_MOUSEMOVE() 
	ON_WM_RBUTTONDOWN() 
	ON_WM_RBUTTONUP() 
	ON_WM_SIZE() 
	ON_COMMAND(ID_DISPLAY_XYZ, OnDisplayXyz) 
	//}}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() 
 
///////////////////////////////////////////////////////////////////////////// 
// CMy3DShowView construction/destruction 
 
CMy3DShowView::CMy3DShowView() 
{ 
	// TODO: add construction code here 
	m_xTranslation = 0; 
	m_yTranslation = 0; 
	m_zTranslation = -300; 
	m_xRotation = 0; 
	m_yRotation = 0; 
	m_zRotation = 0; 
	m_xScaling = m_yScaling = m_zScaling = 1; 
 
	bLight = false; 
	bXYZ = true; 
	 
	m_LeftDownPos = 0; 
	m_RightDownPos = 0; 
	m_LeftButtonDown = false; 
	m_RightButtonDown = false; 
 
} 
 
CMy3DShowView::~CMy3DShowView() 
{ 
} 
 
BOOL CMy3DShowView::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); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMy3DShowView drawing 
 
void CMy3DShowView::OnDraw(CDC* pDC) 
{ 
	CMy3DShowDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc); 
	// TODO: add draw code for native data here 
	if(pDoc->ver_num == 0) 
		return; 
	wglMakeCurrent(m_pDC->GetSafeHdc(),hglrc); 
	myinit(); 
	light(); 
 
	glEnable(GL_LIGHTING); 
	glEnable(GL_LIGHT0); 
	 
	glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);       //仅绘出边缘线 
 
	Draw(); 
	glFlush(); 
 
	glFinish(); 
	 
	SwapBuffers(m_pDC->GetSafeHdc()); 
 
	wglMakeCurrent(NULL,NULL); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMy3DShowView printing 
 
BOOL CMy3DShowView::OnPreparePrinting(CPrintInfo* pInfo) 
{ 
	// default preparation 
	return DoPreparePrinting(pInfo); 
} 
 
void CMy3DShowView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add extra initialization before printing 
} 
 
void CMy3DShowView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add cleanup after printing 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMy3DShowView diagnostics 
 
#ifdef _DEBUG 
void CMy3DShowView::AssertValid() const 
{ 
	CView::AssertValid(); 
} 
 
void CMy3DShowView::Dump(CDumpContext& dc) const 
{ 
	CView::Dump(dc); 
} 
 
CMy3DShowDoc* CMy3DShowView::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMy3DShowDoc))); 
	return (CMy3DShowDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CMy3DShowView message handlers 
 
void CMy3DShowView::Draw() 
{ 
	CMy3DShowDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc); 
 
	glTranslatef(m_xTranslation,m_yTranslation,m_zTranslation);    //平移 
	glRotatef(m_xRotation,1.0, 0.0, 0.0); 
	glRotatef(m_yRotation,0.0, 1.0, 0.0); 
	glRotatef(m_zRotation,0.0, 0.0, 1.0);                          //旋转 
	glScalef(m_xScaling,m_yScaling,m_zScaling);                    //缩放 
 
	if(bXYZ) 
	{ 
		glBegin(GL_LINES); 
		glColor3f(1.0f, 0.0f, 0.0f); 
		glVertex3f(0.0f, 0.0f, 0.0f); 
		glVertex3f(20.0f, 0.0f, 0.0f); 
 
		glColor3f(0.0f, 1.0f, 0.0f); 
		glVertex3f(0.0f, 0.0f, 0.0f); 
		glVertex3f(0.0f, 20.0f, 0.0f); 
 
		glColor3f(0.0f, 0.0f, 1.0f); 
		glVertex3f(0.0f, 0.0f, 0.0f); 
		glVertex3f(0.0f, 0.0f, 20.0f); 
		glEnd(); 
 
	} 
	//开始绘点 
	glPointSize(1.0); 
	glColor3f(1,1,0); 
	glBegin(GL_POINTS); 
	for(int i=0;i<pDoc->ver_num;i++) 
 
		glVertex3d(pDoc->vertex[i].x,pDoc->vertex[i].y,pDoc->vertex[i].z); 
	glEnd(); 
 
} 
 
void CMy3DShowView::myinit() 
{ 
	//置黑背景 
	glClearColor(0.0,0.0,0.0,0.0); 
 
	glClearDepth(1.0f);         //clear z-buffer 
	glEnable(GL_DEPTH_TEST);    //Depth test 
 
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 
	glEnable(GL_NORMALIZE); 
	glShadeModel(GL_SMOOTH); 
 
	glMatrixMode(GL_MODELVIEW); 
	glLoadIdentity(); 
 
 
} 
 
void CMy3DShowView::light() 
{ 
	GLfloat ambient[]  = {0.7f, 0.7f, 0.7f, 1.0f}; 
	GLfloat diffuse[]  = {0.4f, 0.4f, 0.4f, 1.0f}; 
	GLfloat position[] = {1.0f, 0.0f, 1.0f, 1.0f}; 
	glLightfv( GL_LIGHT0, GL_POSITION, position); 
	glLightfv( GL_LIGHT0, GL_AMBIENT, ambient); 
	glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuse); 
	glLightModelf( GL_LIGHT_MODEL_TWO_SIDE, 1.0); 
	glPolygonMode( GL_FRONT_AND_BACK, GL_FILL); 
	//定义材质 
	glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,diffuse); 
	glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,ambient); 
 
	bLight = true; 
} 
 
int CMy3DShowView::OnCreate(LPCREATESTRUCT lpCreateStruct)  
{ 
	if (CView::OnCreate(lpCreateStruct) == -1) 
		return -1; 
	 
	// TODO: Add your specialized creation code here 
	//初始化象素格式以及选取合适的格式来创建RC 
	PIXELFORMATDESCRIPTOR  pixelDesc; 
	 
	pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR); 
	pixelDesc.nVersion = 1; 
 
	pixelDesc.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_STEREO; 
	 
	pixelDesc.iPixelType = PFD_TYPE_RGBA; 
	pixelDesc.cColorBits = 32; 
	pixelDesc.cRedBits = 8; 
	pixelDesc.cRedShift = 16; 
	pixelDesc.cGreenBits = 8; 
	pixelDesc.cGreenShift = 8; 
	pixelDesc.cBlueBits = 8; 
	pixelDesc.cBlueShift = 0; 
	pixelDesc.cAlphaBits = 0; 
	pixelDesc.cAlphaShift = 0; 
	pixelDesc.cAccumBits = 64; 
	pixelDesc.cAccumRedBits = 16; 
	pixelDesc.cAccumGreenBits = 16; 
	pixelDesc.cAccumBlueBits = 16; 
	pixelDesc.cAccumAlphaBits = 0; 
	pixelDesc.cDepthBits = 32; 
	pixelDesc.cStencilBits = 8; 
	pixelDesc.cAuxBuffers = 0; 
	pixelDesc.iLayerType = PFD_MAIN_PLANE; 
	pixelDesc.bReserved = 0; 
	pixelDesc.dwLayerMask = 0; 
	pixelDesc.dwVisibleMask = 0; 
	pixelDesc.dwDamageMask = 0; 
 
	m_pDC = new CClientDC(this); 
	int m_GLPixelIndex = ChoosePixelFormat(m_pDC->GetSafeHdc(),&pixelDesc); 
	if(m_GLPixelIndex == 0) 
	{ 
		AfxMessageBox("no matched pixelformat!"); 
		m_GLPixelIndex = 1; 
		if(DescribePixelFormat(m_pDC->GetSafeHdc(),m_GLPixelIndex,sizeof(PIXELFORMATDESCRIPTOR),&pixelDesc) == 0) 
			return FALSE; 
 
	} 
	if(!SetPixelFormat(m_pDC->GetSafeHdc(),m_GLPixelIndex,&pixelDesc)) 
		return FALSE; 
	hglrc = wglCreateContext(m_pDC->GetSafeHdc()); 
 
 
	return 0; 
} 
 
void CMy3DShowView::OnLButtonDown(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	m_LeftButtonDown = TRUE; 
	m_LeftDownPos = point; 
	SetCapture(); 
	 
 
 
//	CMy3DShowDoc* pDoc = GetDocument(); 
//	ASSERT_VALID(pDoc); 
//	CString str; 
//	str.Format("%lf %lf %lf",pDoc->vertex[0].x,pDoc->vertex[0].y,pDoc->vertex[0].z); 
//	AfxMessageBox(str); 
//	Invalidate(); 
	CView::OnLButtonDown(nFlags, point); 
} 
 
void CMy3DShowView::OnLButtonUp(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	m_LeftButtonDown = FALSE; 
	ReleaseCapture(); 
	CView::OnLButtonUp(nFlags, point); 
} 
 
void CMy3DShowView::OnMouseMove(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	if(m_RightButtonDown&&m_LeftButtonDown) 
	{ 
		m_xRotation += (float)((point.y - m_LeftDownPos.y)/3.0); 
		m_yRotation += (float)((point.x - m_LeftDownPos.x)/3.0); 
		m_LeftDownPos = point; 
		InvalidateRect(NULL,FALSE); 
	} 
	if(m_LeftButtonDown&&!m_RightButtonDown) 
	{ 
		m_xTranslation += (float)((point.y - m_LeftDownPos.y)*0.1); 
		m_yTranslation += (float)((point.x - m_LeftDownPos.x)*0.1); 
		m_LeftDownPos = point; 
		InvalidateRect(NULL,FALSE); 
	} 
	if(!m_LeftButtonDown&&m_RightButtonDown) 
	{ 
		m_zTranslation += (float)(point.y - m_RightDownPos.y); 
		m_RightDownPos = point; 
		InvalidateRect(NULL,FALSE); 
	} 
	CView::OnMouseMove(nFlags, point); 
} 
 
void CMy3DShowView::OnRButtonDown(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	m_RightButtonDown = true; 
	m_RightDownPos = point; 
	SetCapture(); 
	CView::OnRButtonDown(nFlags, point); 
} 
 
void CMy3DShowView::OnRButtonUp(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	m_RightButtonDown = false; 
	ReleaseCapture(); 
	CView::OnRButtonUp(nFlags, point); 
} 
 
void CMy3DShowView::OnSize(UINT nType, int cx, int cy)  
{ 
	CView::OnSize(nType, cx, cy); 
	 
	// TODO: Add your message handler code here 
	wglMakeCurrent(m_pDC->GetSafeHdc(),hglrc); 
 
	glViewport(0,0,cx,cy); 
 
	glMatrixMode(GL_PROJECTION); 
	glLoadIdentity(); 
	gluPerspective(45.0f,(GLfloat)cx/(GLfloat)cy,0.1f,10000.0f); 
	glMatrixMode(GL_MODELVIEW); 
	glLoadIdentity(); 
 
	wglMakeCurrent(m_pDC->GetSafeHdc(),NULL); 
} 
 
void CMy3DShowView::OnDisplayXyz()  
{ 
	// TODO: Add your command handler code here 
	bXYZ=!bXYZ; 
	Invalidate();	 
 
}