OpenGL Entry Program for MFC

Source: Internet
Author: User

I have browsed a lot of OpenGL entry-level programs on the internet, either in the console or in Windows C programming, and I feel very inconvenient. So here I will give you an OpenGL entry-level program :)

Note: I will not talk about the OpenGL programming principles in windows. Refer to related books: D.

I know that those console programs and Windows C programs create very simple windows.

Here we will first describe the creation steps:

1. Create an MFC wizard named glframe, single document, and print support. By default, an OpenGL framework is generated.

2. Get a simple window style.

Add the precreatewindow code to cmianframe:

Bool cmainframe: precreatewindow (createstruct & CS)
{
If (! Cframewnd: precreatewindow (CS ))
Return false;

CS. Cx = 500;
CS. Cy = 400;
CS. lpszname = _ T ("OpenGL Framework Program ");
CS. Style & = ~ Fws_addtotitle; // do not add the title to the title bar
Return true;
}

3. Added OpenGL support:

Add the header file in cglframeview. h as follows:

# Include "Gl/Gl. H"
# Include "Gl/Glu. H"
# Include "Gl/Glaux. H"

In the project-> Settings-> Project Settings dialog box, select the link tab and add Glaux. Lib glu32.lib opengl32.lib to the object module library.

(Tip: Of course, you can also select "project"> "add to project"> "add FILE command". In the "Insert Files into project" dialog box that appears, convert it to the "vc98/lib" directory, select Glaux. lib, glu32.lib, and opengl32.lib files. Press OK to add them to the project file .)

(Write it here first, there is no time today, and I will write it tomorrow)

Today

The following operations are performed in cglframeview:

4. Add an RC handle and a public variable to the view.

Class cglframeview: Public cview

{

Public:

... // Other variables

Hglrc;

... // Other variables

}

5. Add message processing to the View:

Use classwizard to add the processing functions of wm_create, wm_destroy, and wm_size messages.

6. Modify the precreatewindow () function of cgframeview as follows:

Bool cglframeview: precreatewindow (createstruct & CS)
{
// Todo: Modify the window class or styles here by modifying
// The createstruct CS

CS. Style | = ws_clipsiblings | ws_clipchildren;
Return cview: precreatewindow (CS );
}

7. Modify the oncreate () function of cgframeview as follows:

Int cglframeview: oncreate (maid)
{
If (cview: oncreate (lpcreatestruct) =-1)
Return-1;
// ============================================
Pixelformatdescriptor PFD =
{
Sizeof (pixelformatdescriptor ),
1,
Pfd_draw_to_window | pfd_support_opengl |
Pfd_doublebuffer | pfd_support_gdi,
Pfd_type_rgba,
24,
0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0,
32,
0,
0,
Pfd_main_plane,
0,
0, 0
};

Cclientdc clientdc (this );
Int pF = choosepixelformat (clientdc. m_hdc, & PFD );
Bool RT = setpixelformat (clientdc. m_hdc, PF, & PFD );
Hglrc = wglcreatecontext (clientdc. m_hdc );
// ================================================ ========================
Return 0;
}
8. Modify the ondestroy () function of cgframeview as follows:

Void cglframeview: ondestroy ()
{
Cview: ondestroy ();
 
// Todo: add your message handler code here
If (wglgetcurrentcontext ()! = NULL)
Wglmakecurrent (null, null );
If (hglrc! = NULL)
{
Wgldeletecontext (hglrc );
Hglrc = NULL;
}
}

9. Modify the onsize () function of cgframeview as follows:

Void cglframeview: onsize (uint ntype, int CX, int CY)
{
Cview: onsize (ntype, CX, CY );

Glsizei W = Cx;
Glsizei H = Cy;
If (! H)
Return;
Cclientdc clientdc (this );
Wglmakecurrent (clientdc. m_hdc, hglrc );
Glviewport (0, 0, W, H );
Wglmakecurrent (null, null );
/**/
 
}

The above is the basic work that must be done well in OpenGL programming ^ _*

Add the line and teapot part tomorrow to complete this example.

 

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.