Implementation of OpenGL Multi-view in floating form

Source: Internet
Author: User
Because in the work need to combine the floating form to achieve OpenGL multi-view, used to get three-dimensional solid view observation effect, through reference to other materials, design a program framework, on this basis you can expand according to their own needs, to achieve the required functions.

Program Implementation Effect Diagram



Key Technology Implementation Introduction:

   I. Implementation of OPENGL multi-View

Most of our programs usually build an OpenGL device context, but in this program you need to set up a multi-OpenGL device context and switch when needed, because you want to implement multi-view viewing of three-dimensional entities.

As with the general OpenGL program, we have defined the device description context for each view in each view class and created it at the time of view creation.

Add in the header file of view class
Public
cclientdc* M_PDC;
HGLRC M_HRC;
Add in the init () function of view class
M_HRC = Wglcreatecontext (M_pdc->getsafehdc ());

Then, when a view needs to be updated (typically in the OnDraw () function of each view), the device context of the view is set to the OpenGL current render context (OpenGL Rendering context)

Add in the OnDraw () function of view class
Set Current device
Wglmakecurrent (M_pdc->getsafehdc (), M_HRC);

   second, the switch of the view type and the judgment of the current view type

Because the same view may have different functions depending on the user's needs at different times, there is a need to switch functionality between several views. This program selects one view as the primary, and the other two as a child view. There are several classes of views: top, bottom view, front, rear view, left, and right view. An enumeration type variable is designed for this purpose to indicate the type of view. To ensure effective switching between different view types, which does not produce duplicate types, a class is designed for type switching between operation views.

To reduce the burden of programming, all child views share a view class, and the current type of each view is stored outside of this view class, so you need to determine your own type when drawing each view. This is the case where you cannot judge your current view type in the child view class. For this reason, a member variable is added to the child view class to record its own type.

Add in the header file of a child view class
View ID, it'll be assigned by parentframe if this program begin
[Childviewa id = 1; CHILDVIEWB id = 2]
int M_viewid

Then, at the beginning of the program run, after the floating form is created, set the type of child views that each floating form contains.

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;
}
}
}
}

We see that we need to retrieve all the child Views (Cchildoglview) from the document class, so we need to add ourselves to the document when the child view is created.

Add in the OnCreate () function of a child view class
Addmetodoc ();

The definition of Addmetodoc ()
void Cchildoglview::addmetodoc ()
{
cedit3dmdoc* pdoc = (cedit3dmdoc*) getdocument ();
if (pdoc! = NULL)
{
Pdoc->addview (this);
}
}

At this point, the key functions of this program have been implemented, in the main view and sub-views of the Drawing function section can be judged by the current view of the type of different content drawing.

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 a 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 ());

Readers can then implement different functions based on their needs. It should be noted that the program contains the three-dimensional entity model class that we need in the system development, you can modify it or replace it with your own data structure, welcome to the article to make suggestions, look forward to working with you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.