www.pudn.com > ReadDxf.rar > ReadDxfView.cpp


// ReadDxfView.cpp : implementation of the CReadDxfView class 
// 
 
#include "stdafx.h" 
#include "ReadDxf.h" 
 
#include "gl\gl.h" 
#include "gl\glu.h" 
#include "gl\glaux.h" 
 
#include "ReadDxfDoc.h" 
#include "ReadDxfView.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CReadDxfView 
 
IMPLEMENT_DYNCREATE(CReadDxfView, CView) 
 
BEGIN_MESSAGE_MAP(CReadDxfView, CView) 
	//{{AFX_MSG_MAP(CReadDxfView) 
	ON_WM_ERASEBKGND() 
	ON_WM_CREATE() 
	ON_WM_DESTROY() 
	ON_WM_MOUSEMOVE() 
	ON_WM_SIZE() 
	ON_WM_LBUTTONDOWN() 
	ON_WM_LBUTTONUP() 
	ON_COMMAND(ID_ZOOMIN, OnZoomin) 
	ON_COMMAND(ID_ZOOMOUT, OnZoomout) 
	//}}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() 
 
///////////////////////////////////////////////////////////////////////////// 
// CReadDxfView construction/destruction 
 
CReadDxfView::CReadDxfView(): 
X_Angle(0.0),Y_Angle(0.0) 
{ 
	// TODO: add construction code here 
	m_hDC = NULL; 
	m_hRC = NULL; 
	Scale=40; 
} 
 
CReadDxfView::~CReadDxfView() 
{ 
} 
 
BOOL CReadDxfView::PreCreateWindow(CREATESTRUCT& cs) 
{ 
	// TODO: Modify the Window class or styles here by modifying 
	//  the CREATESTRUCT cs 
 
	return CView::PreCreateWindow(cs); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CReadDxfView drawing 
 
void CReadDxfView::OnDraw(CDC* pDC) 
{ 
	wglMakeCurrent(m_hDC,m_hRC); 
	 
	RenderScene(); 
	 
	SwapBuffers(m_hDC); 
	 
	wglMakeCurrent(m_hDC,NULL); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CReadDxfView printing 
 
BOOL CReadDxfView::OnPreparePrinting(CPrintInfo* pInfo) 
{ 
	// default preparation 
	return DoPreparePrinting(pInfo); 
} 
 
void CReadDxfView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add extra initialization before printing 
} 
 
void CReadDxfView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add cleanup after printing 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CReadDxfView diagnostics 
 
#ifdef _DEBUG 
void CReadDxfView::AssertValid() const 
{ 
	CView::AssertValid(); 
} 
 
void CReadDxfView::Dump(CDumpContext& dc) const 
{ 
	CView::Dump(dc); 
} 
 
CReadDxfDoc* CReadDxfView::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CReadDxfDoc))); 
	return (CReadDxfDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CReadDxfView message handlers 
 
BOOL CReadDxfView::OnEraseBkgnd(CDC* pDC)  
{ 
	// TODO: Add your message handler code here and/or call default 
    return FALSE; 
	return CView::OnEraseBkgnd(pDC); 
} 
 
void CReadDxfView::GLInit() 
{ 
	m_hDC = ::GetDC(m_hWnd); // Get the Device context 
	 
	ASSERT(m_hDC); 
	 
	static	PIXELFORMATDESCRIPTOR pfdWnd = 
	{ 
		sizeof(PIXELFORMATDESCRIPTOR), // Structure size. 
			1,                             // Structure version number. 
			PFD_DRAW_TO_WINDOW |           // Property flags. 
			PFD_SUPPORT_OPENGL | 
			PFD_DOUBLEBUFFER, 
			PFD_TYPE_RGBA, 
			24,                            // 24-bit color. 
			0, 0, 0, 0, 0, 0,              // Not concerned with these. 
			0, 0, 0, 0, 0, 0, 0,           // No alpha or accum buffer. 
			32,                            // 32-bit depth buffer. 
			0, 0,                          // No stencil or aux buffer. 
			PFD_MAIN_PLANE,                // Main layer type. 
			0,                             // Reserved. 
			0, 0, 0                        // Unsupported. 
	}; 
	 
	int pixelformat; 
    pixelformat = ChoosePixelFormat(m_hDC, &pfdWnd); 
	 
    ASSERT(SetPixelFormat(m_hDC, pixelformat, &pfdWnd)); 
	 
	m_hRC=wglCreateContext(m_hDC); 
	 
	//make the rending context current, perform initialization, the 
	//deselect it 
	VERIFY(wglMakeCurrent(m_hDC,m_hRC)); 
	GLSetupRC(); 
	VERIFY(wglMakeCurrent(NULL,NULL)); 
} 
 
void CReadDxfView::GLSetupRC() 
{ 
	glEnable(GL_DEPTH_TEST);	// Hidden surface removal 
	glEnable(GL_COLOR_MATERIAL); 
	 
	// Lighting components 
	GLfloat  ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f }; 
	GLfloat  diffuseLight[] = { 0.6f, 0.6f, 0.6f, 1.0f }; 
	GLfloat  specular[] = { 1.0f, 1.0f, 1.0f, 1.0f}; 
	GLfloat	 lightPos[] = { 1.0f, 1.0f, -1.0f, 0.0f }; 
	 
	glEnable(GL_LIGHTING); 
	glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight); 
	glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight); 
	glLightfv(GL_LIGHT0,GL_SPECULAR,specular); 
	glLightfv(GL_LIGHT0,GL_POSITION,lightPos); 
	glEnable(GL_LIGHT0); 
	 
	 
	glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); 
	glMaterialfv(GL_FRONT, GL_SPECULAR,specular); 
	glMateriali(GL_FRONT,GL_SHININESS,100); 
	 
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f ); //background color 
	// default color 
	glColor3ub(0, 0, 255); 
} 
 
void CReadDxfView::GLRelease() 
{ 
	wglDeleteContext(m_hRC); 
	::ReleaseDC(m_hWnd,m_hDC); 
} 
 
void CReadDxfView::GLResize(int cx, int cy) 
{ 
	double nRange = 1200.0; 
	 
	// Prevent a divide by zero 
	if(cy == 0) 
		cy = 1; 
	 
	// Set Viewport to window dimensions 
    glViewport(0, 0, cx, cy); 
	 
	glMatrixMode(GL_PROJECTION); 
	 
	// Reset coordinate system 
	glLoadIdentity(); 
	 
	// Establish clipping volume (left, right, bottom, top, near, far) 
	if(cx <= cy) 
		glOrtho(-nRange,nRange,-nRange*cy/cx,nRange*cy/cx,nRange,-nRange); 
	else 
		glOrtho(-nRange*cx/cy,nRange*cx/cy,-nRange,nRange,nRange,-nRange); 
	 
	 
	glMatrixMode( GL_MODELVIEW ); 
	glLoadIdentity(); 
} 
 
int CReadDxfView::OnCreate(LPCREATESTRUCT lpCreateStruct)  
{ 
	if (CView::OnCreate(lpCreateStruct) == -1) 
		return -1; 
	 
	// TODO: Add your specialized creation code here 
	GLInit();	 
	return 0; 
} 
 
void CReadDxfView::OnDestroy()  
{ 
	CView::OnDestroy(); 
	 
	// TODO: Add your message handler code here 
	GLRelease(); 
} 
 
void CReadDxfView::RenderScene() 
{ 
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
	CReadDxfDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc);	 
	glDisable( GL_LIGHTING ); 
	DrawCoord(); 
	SetColor(RGB(255,255,255)); 
	glRotated(X_Angle,1.0,0.0,0.0); 
	glRotated(Y_Angle,0.0,1.0,0.0);  
	int i=0; 
	for(;iPointNum;i++) 
	{ 
		glBegin(GL_POINTS); 
		glVertex3f(pDoc->pPointList[i].x*Scale,pDoc->pPointList[i].y*Scale,pDoc->pPointList[i].z*Scale); 
		glEnd(); 
	} 
} 
 
void CReadDxfView::SetColor(COLORREF clr) 
{ 
	BYTE r,g,b; 
	r = GetRValue(clr); 
	g = GetGValue(clr); 
	b = GetBValue(clr); 
	glColor3ub(r,g,b); 
} 
 
void CReadDxfView::OnMouseMove(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	CSize rotate; 
	if (GetCapture()==this) 
	{ 
			rotate= MouseDownPoint - point; 
			X_Angle = rotate.cy; 
			Y_Angle = rotate.cx; 
			Invalidate(TRUE); 
	} 
	MouseDownPoint=point; 
	//CView::OnMouseMove(nFlags, point); 
} 
 
void CReadDxfView::DrawCoord() 
{ 
	SetColor(RGB(255,0,0)); 
	glBegin(GL_LINES); 
	glVertex3f(0,0,0); 
	glVertex3f(0,0,1000); 
	glEnd(); 
	SetColor(RGB(0,255,0)); 
	glBegin(GL_LINES); 
	glVertex3f(0,0,0); 
	glVertex3f(0,1000,0); 
	glEnd(); 
	SetColor(RGB(0,0,255)); 
	glBegin(GL_LINES); 
	glVertex3f(0,0,0); 
	glVertex3f(1000,0,0); 
	glEnd(); 
} 
 
void CReadDxfView::OnSize(UINT nType, int cx, int cy)  
{ 
	CView::OnSize(nType, cx, cy); 
	 
	// TODO: Add your message handler code here 
	VERIFY(wglMakeCurrent(m_hDC,m_hRC)); 
	GLResize(cx,cy); 
	VERIFY(wglMakeCurrent(NULL,NULL)); 
} 
 
void CReadDxfView::OnLButtonDown(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	MouseDownPoint=point; 
	SetCapture(); 
	//CView::OnLButtonDown(nFlags, point); 
} 
 
void CReadDxfView::OnLButtonUp(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	MouseDownPoint=CPoint(0,0); 
	ReleaseCapture(); 
	//CView::OnLButtonUp(nFlags, point); 
} 
 
void CReadDxfView::OnZoomin() 
{ 
	// TODO: Add your command handler code here 
	X_Angle=0.0; 
	Y_Angle=0.0; 
	if(Scale>1) 
		Scale-=5; 
	Invalidate(TRUE); 
} 
 
void CReadDxfView::OnZoomout() 
{ 
	// TODO: Add your command handler code here 
	X_Angle=0.0; 
	Y_Angle=0.0; 
	if(Scale<100) 
		Scale+=5; 
	Invalidate(TRUE); 
}