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

📄 edit3dmview.cpp

📁 visual c++ 实例编程
💻 CPP
字号:
// Edit3DMView.cpp : implementation of the CEdit3DMView class
//

#include "stdafx.h"
#include "Edit3DM.h"

#include "Edit3DMDoc.h"
#include "Edit3DMView.h"
#include "mainfrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CEdit3DMView

IMPLEMENT_DYNCREATE(CEdit3DMView, CView)

BEGIN_MESSAGE_MAP(CEdit3DMView, CView)
	//{{AFX_MSG_MAP(CEdit3DMView)
	ON_WM_DESTROY()
	ON_WM_SIZE()
	ON_WM_CREATE()
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CEdit3DMView construction/destruction

CEdit3DMView::CEdit3DMView()
{
	// TODO: add construction code here
	m_pDC = NULL;

}

CEdit3DMView::~CEdit3DMView()
{
}

BOOL CEdit3DMView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CEdit3DMView drawing

void CEdit3DMView::OnDraw(CDC* pDC)
{
	CEdit3DMDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	//set current device
	wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC);
	DrawScene();
}

/////////////////////////////////////////////////////////////////////////////
// CEdit3DMView printing

BOOL CEdit3DMView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CEdit3DMView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CEdit3DMView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CEdit3DMView diagnostics

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

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

CEdit3DMDoc* CEdit3DMView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEdit3DMDoc)));
	return (CEdit3DMDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CEdit3DMView message handlers


///////////////////////////////////////////////////////////////////////////
//OpenGL Setting///////////////////////////////////////////////////////////
bool CEdit3DMView::bSetupPixelFormat()
{
	static 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,
		0,
		PFD_MAIN_PLANE,
		0,
		0,0,0
	};
	int pixelformat;

	if((pixelformat=ChoosePixelFormat(m_pDC->GetSafeHdc(),&pfd))==0)
	{
		MessageBox("ChoosePixelFormat failed");
		return false;
	}
	if(SetPixelFormat(m_pDC->GetSafeHdc(),pixelformat,&pfd)==false)
	{
		MessageBox("SetPixelFormat failed");
		return false;
	}
	return true;
}

void CEdit3DMView::init()
{
	PIXELFORMATDESCRIPTOR pfd;
	int n;
	//HGLRC hrc;

	m_pDC = new CClientDC(this);
	ASSERT(m_pDC!=NULL);
	if(!bSetupPixelFormat()) return;

	n=::GetPixelFormat(m_pDC->GetSafeHdc());
	::DescribePixelFormat(m_pDC->GetSafeHdc(),n,sizeof(pfd),&pfd);
	//hrc=wglCreateContext(m_pDC->GetSafeHdc());
	m_hRC = wglCreateContext(m_pDC->GetSafeHdc());
	//wglMakeCurrent(m_pDC->GetSafeHdc(), hrc);
	wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC); 

	GetClientRect(&m_oldRect);
	glClearDepth(1.0f);
	glEnable(GL_DEPTH_TEST);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	//glEnable(GL_TEXTURE_2D);	//启动纹理
	//glEnable(GL_NORMALIZE);

	//if u have many device(windows) for OpenGL, u can add this sentence
	//to make the current device to be NULL.
	//wglMakeCurrent(m_pDC->GetSafeHdc(), NULL);

}
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////

void CEdit3DMView::OnDestroy() 
{
	CView::OnDestroy();
	
	// TODO: Add your message handler code here

////////////////////////////////////////////////////////
//Destroy Device////////////////////////////////////////
	wglDeleteContext(m_hRC);

	if(m_pDC)
		delete m_pDC;
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
}

void CEdit3DMView::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
	if(cy>0)
	{
		if((m_oldRect.right > cx)||(m_oldRect.bottom > cy))
			RedrawWindow();
		m_oldRect.right=cx;
		m_oldRect.bottom=cy;
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		glMatrixMode(GL_MODELVIEW);
		glViewport(0,0,cx,cy);
	}
}

int CEdit3DMView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	init();

	//past the pointer pointing the mainview to mainframe
	CMainFrame* pframe = (CMainFrame*)GetParentFrame();
	if(pframe != NULL)
	{
		pframe->m_pMainView = this;
	}

	return 0;
}

void CEdit3DMView::DrawScene()
{
	//To get the current view type
	ViewType currentviewtype;
	CMainFrame* pframe = (CMainFrame*)GetParentFrame();
	currentviewtype = pframe->m_ViewArrange.m_MainViewType;

	glViewport(0, 0, m_oldRect.right, m_oldRect.bottom);

	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	
	CEdit3DMDoc* pdoc = GetDocument();
	if(pdoc != NULL)
	{
		pdoc->m_3DMani.SetProjection(currentviewtype, 0);
		pdoc->m_3DMani.DrawBound(currentviewtype);
	}

	glFinish();
	SwapBuffers(wglGetCurrentDC());
}

⌨️ 快捷键说明

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