⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 designview.cpp

📁 本文以数字图像处理、压缩技术和MATLAB应用为基础
💻 CPP
📖 第 1 页 / 共 3 页
字号:
void CDesignView::OnEditDelete() 
{
	// TODO: Add your command handler code here
	CDesignDoc* pDoc = GetDocument();
	if (pDoc->m_ElemSelected == -1)
		return;
	// Delete currently selected element
	if (m_Deletable == FALSE)
	{
		AfxMessageBox("Please delete associated components first.");
		return;
	}
	int Index = pDoc->m_ElemArray.GetUpperBound();
	if (Index > -1)
	{
		delete pDoc->m_ElemArray.GetAt(pDoc->m_ElemSelected);
		pDoc->m_ElemArray.RemoveAt(pDoc->m_ElemSelected);
	}
	pDoc->UpdateAllViews(0);
	pDoc->SetModifiedFlag();
	pDoc->m_sel_pt_a = (0,0);
	pDoc->m_sel_pt_b = (0,0);
	pDoc->m_ElemSelected = -1;
}

void CDesignView::OnUpdateEditDelete(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	CDesignDoc* pDoc = GetDocument();
	pCmdUI->Enable(pDoc->m_ElemSelected != -1);
}





/////////////////////////////////////////////////////////////////////////////
// CModelView

IMPLEMENT_DYNCREATE(CModelView, CView)

CModelView::CModelView()
{
	m_hGLContext = NULL;
	m_GLPixelIndex = 0;
	m_LButtonDown = FALSE;
	m_RButtonDown = FALSE;
	m_BuildList = FALSE;
}

CModelView::~CModelView()
{
}


BEGIN_MESSAGE_MAP(CModelView, CView)
	//{{AFX_MSG_MAP(CModelView)
	ON_WM_CREATE()
	ON_WM_DESTROY()
	ON_WM_SIZE()
	ON_WM_PAINT()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_WM_CHAR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CModelView drawing

void CModelView::OnDraw(CDC* pDC)
{
	CDesignDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code here
}

/////////////////////////////////////////////////////////////////////////////
// CModelView diagnostics

#ifdef _DEBUG
void CModelView::AssertValid() const
{
	CView::AssertValid();
}

void CModelView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CDesignDoc* CModelView::GetDocument()
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDesignDoc)));
	return (CDesignDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CModelView message handlers

BOOL CModelView::PreCreateWindow(CREATESTRUCT& cs) 
{
	// TODO: Add your specialized code here and/or call the base class
	cs.style |= (WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
	return CView::PreCreateWindow(cs);
}

BOOL CModelView::SetWindowPixelFormat(HDC hDC)
{
	PIXELFORMATDESCRIPTOR pixelDesc;

	pixelDesc.nSize			= sizeof (PIXELFORMATDESCRIPTOR);
	pixelDesc.nVersion		= 1;
	pixelDesc.dwFlags		=	PFD_DRAW_TO_WINDOW	|
								PFD_SUPPORT_OPENGL	|
								PFD_DOUBLEBUFFER	|
								PFD_STEREO_DONTCARE;
	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.cAuxBuffers	= 0;
	pixelDesc.iLayerType	= PFD_MAIN_PLANE;
	pixelDesc.bReserved		= 0;
	pixelDesc.dwLayerMask	= 0;
	pixelDesc.dwVisibleMask	= 0;
	pixelDesc.dwDamageMask	= 0;

	m_GLPixelIndex = ChoosePixelFormat(hDC, &pixelDesc);
	if (m_GLPixelIndex ==0)	//Let's choose a default index.
	{
		m_GLPixelIndex = 1;
		if (DescribePixelFormat(hDC, m_GLPixelIndex, sizeof(PIXELFORMATDESCRIPTOR), &pixelDesc)==0)
		{
			return FALSE;
		}
	}
	if (SetPixelFormat( hDC, m_GLPixelIndex, &pixelDesc)==FALSE)
	{
		return FALSE;
	}
	return TRUE;
}

int CModelView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	HWND hWnd = GetSafeHwnd();
	HDC hDC = ::GetDC(hWnd);

	if (SetWindowPixelFormat(hDC) == FALSE)
		return 0;

	if (CreateViewGLContext(hDC) == FALSE)
		return 0;

	return 0;
}

BOOL CModelView::CreateViewGLContext(HDC hDC)
{
	m_hGLContext = wglCreateContext(hDC);
	if (m_hGLContext == NULL)
	{
		return FALSE;
	}
	if (wglMakeCurrent(hDC, m_hGLContext) == FALSE)
	{
		return FALSE;
	}
	return TRUE;
}

void CModelView::OnDestroy() 
{
	if (wglGetCurrentContext()!=NULL)
	{
		// make the rendering context not current
		wglMakeCurrent(NULL, NULL);
	}
	if (m_hGLContext!=NULL)
	{
		wglDeleteContext(m_hGLContext);
		m_hGLContext = NULL;
	}

	// Now the associated DC can be released
	CView::OnDestroy();
}

void CModelView::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
	GLsizei	width, height;
	GLdouble aspect;

	width = cx;
	height = cy;

	if (cy == 0)
		aspect = (GLdouble)width;
	else
		aspect = (GLdouble)width/(GLdouble)height;

	glViewport(0, 0, width, height);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(30, aspect, 1, 1200);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	glDrawBuffer(GL_BACK);
	glEnable(GL_LIGHTING);
	glEnable(GL_DEPTH_TEST);

}

void CModelView::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	CDesignDoc* pDoc = GetDocument();

////add vertex list here//////////////////////
	if (m_BuildList == FALSE)
	{
		glNewList(DOOR, GL_COMPILE);
			glBegin(GL_QUADS);
				glNormal3d(  0.0,  0.0,  1.0);
				glVertex3d(  0.15/10.0,  0.0, 0.1/10.0);
				glVertex3d(  0.15/10.0,  56.0/10.0, 0.1/10.0);
				glVertex3d(  23.85/10.0, 56.0/10.0, 0.1/10.0);
				glVertex3d(  23.85/10.0, 0.0, 0.1/10.0);
			glEnd();
			glBegin(GL_QUADS);
				glNormal3d(  0.0,  0.0,  -1.0);
				glVertex3d(  0.15/10.0,  0.0, -0.1/10.0);
				glVertex3d(  0.15/10.0,  56.0/10.0, -0.1/10.0);
				glVertex3d(  23.85/10.0, 56.0/10.0, -0.1/10.0);
				glVertex3d(  23.85/10.0, 0.0, -0.1/10.0);
			glEnd();
		glEndList();

		glNewList(WINDOW, GL_COMPILE);
			glBegin(GL_QUADS);
				glNormal3d(  0.0,  0.0,  1.0);
				glVertex3d(  0.0,  0.0, 0.1/10.0);
				glVertex3d(  0.0,  32.0/10.0, 0.1/10.0);
				glVertex3d(  24/10.0, 32.0/10.0, 0.1/10.0);
				glVertex3d(  24/10.0, 0.0, 0.1/10.0);
			glEnd();
			glBegin(GL_QUADS);
				glNormal3d(  0.0,  0.0,  -1.0);
				glVertex3d(  0.0,  0.0, -0.1/10.0);
				glVertex3d(  0.0,  32.0/10.0, -0.1/10.0);
				glVertex3d(  24/10.0, 32.0/10.0, -0.1/10.0);
				glVertex3d(  24/10.0, 0.0, -0.1/10.0);
			glEnd();
		glEndList();

		m_BuildList = TRUE;
	}
//////////////////////////////////////////////
	pDoc->RenderScene();

	SwapBuffers(dc.m_ps.hdc);
	// Do not call CView::OnPaint() for painting messages
}

void CModelView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CDesignDoc* pDoc = GetDocument();
	if (pDoc->m_CurrentMode == ID_VIEW_NORMAL)
	{
		m_LButtonDown = TRUE;
		m_LDownPos = point;
		SetCapture();
		CRect ViewRect;
		GetWindowRect(&ViewRect);
		::ClipCursor(&ViewRect);
	}
	CView::OnLButtonDown(nFlags, point);
}

void CModelView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CDesignDoc* pDoc = GetDocument();
	if (pDoc->m_CurrentMode == ID_VIEW_NORMAL)
	{
		m_LButtonDown = FALSE;	
		::ReleaseCapture();
		::ClipCursor(NULL);
	}
	CView::OnLButtonUp(nFlags, point);
}

void CModelView::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CDesignDoc* pDoc = GetDocument();
	if (m_LButtonDown)
	{
		CSize rotate = m_LDownPos - point;
		m_LDownPos = point;
		pDoc->m_yRotate -= rotate.cx/2;
		if ((pDoc->m_xRotate - rotate.cy/2 < 89)&&(pDoc->m_xRotate - rotate.cy/2 > -89))
			pDoc->m_xRotate -= rotate.cy/2;
		InvalidateRect(NULL, FALSE);
	}
	CView::OnMouseMove(nFlags, point);
}

void CModelView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	CDesignDoc* pDoc = GetDocument();

	if (pDoc->m_CurrentMode == ID_VIEW_WALKTHROUGH)
	{
		double diffx = (pDoc->eyex - pDoc->centerx)/2;
		double diffy = (pDoc->eyey - pDoc->centery)/2;
		double diffz = (pDoc->eyez - pDoc->centerz)/2;

		switch (nChar)
		{
		case '5':
			pDoc->eyex = pDoc->eyex - diffx;
			pDoc->eyey = pDoc->eyey - diffy;
			pDoc->eyez = pDoc->eyez - diffz;
			pDoc->centerx = pDoc->centerx - diffx;
			pDoc->centery = pDoc->centery - diffy;
			pDoc->centerz = pDoc->centerz - diffz;
			break;
		case '0':
			pDoc->eyex = pDoc->eyex + diffx;
			pDoc->eyey = pDoc->eyey + diffy;
			pDoc->eyez = pDoc->eyez + diffz;
			pDoc->centerx = pDoc->centerx + diffx;
			pDoc->centery = pDoc->centery + diffy;
			pDoc->centerz = pDoc->centerz + diffz;
			break;
		case '8':
			pDoc->m_yz_angle = pDoc->m_yz_angle + 0.02;

			pDoc->centerx = pDoc->eyex + cos(pDoc->m_yz_angle)*sin(pDoc->m_xz_angle);
			pDoc->centery = pDoc->eyey + sin(pDoc->m_yz_angle);
			pDoc->centerz = pDoc->eyez - cos(pDoc->m_yz_angle)*cos(pDoc->m_xz_angle);
			break;
		case '2':
			pDoc->m_yz_angle = pDoc->m_yz_angle - 0.02;
			
			pDoc->centerx = pDoc->eyex + cos(pDoc->m_yz_angle)*sin(pDoc->m_xz_angle);
			pDoc->centery = pDoc->eyey + sin(pDoc->m_yz_angle);
			pDoc->centerz = pDoc->eyez - cos(pDoc->m_yz_angle)*cos(pDoc->m_xz_angle);
			break;
		case '4':
			pDoc->m_xz_angle = pDoc->m_xz_angle - 0.02;

			pDoc->centerx = pDoc->eyex + cos(pDoc->m_yz_angle)*sin(pDoc->m_xz_angle);
			pDoc->centery = pDoc->eyey + sin(pDoc->m_yz_angle);
			pDoc->centerz = pDoc->eyez - cos(pDoc->m_yz_angle)*cos(pDoc->m_xz_angle);
			break;
		case '6':
			pDoc->m_xz_angle = pDoc->m_xz_angle + 0.02;

			pDoc->centerx = pDoc->eyex + cos(pDoc->m_yz_angle)*sin(pDoc->m_xz_angle);
			pDoc->centery = pDoc->eyey + sin(pDoc->m_yz_angle);
			pDoc->centerz = pDoc->eyez - cos(pDoc->m_yz_angle)*cos(pDoc->m_xz_angle);
			break;
		}
		InvalidateRect(NULL, FALSE);
	}
	CView::OnChar(nChar, nRepCnt, nFlags);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -