浮动窗体中的OpenGL多视图的实现
2010-07-11 20:44:48 来源:WEB开发网然后在程序运行之初,浮动窗体创建之后,对每个浮动窗体所包含的子视图的类型进行设置。
//add in the OnCreate() function of CMainFrame class
AssignViewID();
//the definition of AssignViewID()
void CMainFrame::AssignViewID()
{
CEdit3DMDoc* pdoc = (CEdit3DMDoc*)m_pMainView->GetDocument();
if(pdoc)
{
POSITION pos = pdoc->GetFirstViewPosition();
CView* pview;
int tempid = 1;
while (pos != NULL)
{
pview = pdoc->GetNextView(pos);
if (pview->IsKindOf(RUNTIME_CLASS(CChildOGLView)))
{
CChildOGLView* pchildview = (CChildOGLView*)pview;
pchildview->m_ViewID = tempid;
tempid += 1;
}
}
}
}
我们看到这里需要从文档类中检索出所有子视图(CChildOGLView),因此,在子视图创建的时候需要把自己加入到文档中。
//add in the OnCreate() function of child view class
AddMetoDoc();
//the definition of AddMetoDoc()
void CChildOGLView::AddMetoDoc()
{
CEdit3DMDoc* pdoc = (CEdit3DMDoc*)GetDocument();
if(pdoc != NULL)
{
pdoc->AddView(this);
}
}
至此,本程序的关键功能已经实现,在主视图和子视图的绘制函数部分便可以通过判断当前视图的类型进行不同内容的绘制。
//in the DrawScene() function of main view
//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());
//in the DrawScene() function of child view
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());
各位读者可以在此基础之上根据各自的需要实现不同的功能。需要指出的是,程序中包含了我们在系统开发中所需要的三维实体模型类,你可以对它进行修改或者替换为自己的数据结构,欢迎大家对本文提出建议,期待与大家进行合作!
本文配套源码
更多精彩
赞助商链接