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

📄 childoglview.cpp

📁 visual c++ 实例编程
💻 CPP
字号:
// ChildOGLView.cpp : implementation file
//

#include "stdafx.h"
#include "Edit3DM.h"
#include "ChildOGLView.h"
#include "mainfrm.h"
#include "edit3dmview.h"

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

/////////////////////////////////////////////////////////////////////////////
// CChildOGLView

IMPLEMENT_DYNCREATE(CChildOGLView, CFormView)

CChildOGLView::CChildOGLView()
	: CFormView(CChildOGLView::IDD)
{
	//{{AFX_DATA_INIT(CChildOGLView)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_pDC = NULL;
	m_ViewID = 1;
}

CChildOGLView::~CChildOGLView()
{
}

void CChildOGLView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChildOGLView)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChildOGLView, CFormView)
	//{{AFX_MSG_MAP(CChildOGLView)
	ON_WM_DESTROY()
	ON_WM_CREATE()
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChildOGLView diagnostics

#ifdef _DEBUG
void CChildOGLView::AssertValid() const
{
	CFormView::AssertValid();
}

void CChildOGLView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChildOGLView message handlers


///////////////////////////////////////////////////////////////////////////
//OpenGL Setting///////////////////////////////////////////////////////////
bool CChildOGLView::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 CChildOGLView::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 CChildOGLView::OnDestroy() 
{
	CFormView::OnDestroy();
	
	// TODO: Add your message handler code here
////////////////////////////////////////////////////////
//Destroy Device////////////////////////////////////////
	wglDeleteContext(m_hRC);

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

int CChildOGLView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CFormView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	AddMetoDoc();
	init();
	
	return 0;
}


void CChildOGLView::OnSize(UINT nType, int cx, int cy) 
{
	CFormView::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);
	}
}

void CChildOGLView::DrawScene()
{
	ViewType currentviewtype;
	CMainFrame* pframe = (CMainFrame*)AfxGetMainWnd();
	if(m_ViewID == 1)
	{
		currentviewtype = pframe->m_ViewArrange.m_ChildViewAType;
	}
	else if(m_ViewID == 2)
	{
		currentviewtype = pframe->m_ViewArrange.m_ChildViewBType;
	}

	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());
}
void CChildOGLView::OnDraw(CDC* pDC) 
{
	// TODO: Add your specialized code here and/or call the base class
	wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC);
	DrawScene();
}

CEdit3DMDoc* CChildOGLView::GetDocument()
{
	CMainFrame* pframe = (CMainFrame*)AfxGetMainWnd();
	if(pframe != NULL)
	{
		return pframe->m_pMainView->GetDocument();
	}
	return NULL;
}

void CChildOGLView::AddMetoDoc()
{
	CEdit3DMDoc* pdoc = (CEdit3DMDoc*)GetDocument();
	if(pdoc != NULL)
	{
		pdoc->AddView(this);
	}
}

⌨️ 快捷键说明

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