Document directory
- 1. Add a link library.
- 2. Include the header file.
- 3. Set the window display style.
- 4. Set the window pixel format
- 5. generate an RC and set it to the current RC.
- 6. Set the viewport
- 7. Draw a scenario (all the Code related to drawing with OpenGL is here !!)
- 8. Some final work.
Index of this blog's computer graphics series:
Address: Index of related articles in computer graphics series (continuous update)
I. Introduction
Windows GDI uses device context (device description table) ("DC") to draw, while OpenGL needs to draw the environment (rendering context ), (RC "). Each GDI Command needs to be passed to a DC, but unlike GDI, OpenGL uses the current rendering environment (RC ). However, RC cannot directly complete the drawing. It can only be associated with a specific DC to complete the specific drawing. Once a current RC is specified in a thread, all subsequent OpenGL commands in this thread use the same current RC. Although multiple RC can be used in a Single Window, there is only one current RC in a single thread. Next I will first generate an OpenGL
RC and make it the current RC. There are three steps:
1. Set the pixel format of the window;
2. RC generation;
3. Set it to the current RC.
The following figure shows the information that needs to be set in each function:
Ii. Build the basic OpenGL framework in MFC
This document uses a single-document program as an example. Create a new single document named vcopengl2.
1. Add a link library.
Choose project> Properties> Configuration Properties> linker> input> Add opengl32.lib glu32.lib Glaux. lib to the additional dependency,
If you do not need to add a link library in this way, you can write the following code to achieve the same effect:
#pragma comment( lib, "opengl32.lib" ) #pragma comment( lib, "glu32.lib" ) #pragma comment( lib, "glut32.lib" )#pragma comment( lib, "glaux.lib" )
2. Include the header file.
Add the OpenGL header file in stdafx (you can also add it to other files, for example, the drawing is generally in the view, you can include the header file in the xxxview. cpp file ). The following code is used:
#include <GL\glaux.h>#include <GL\glut.h>
There are several notes:
2.1 When glut. H is included, both Gl. h and Glu. h are included. Open glut. h and you can see that, as shown in, GL. h and Glu. h are included.
2.2. Let's take a look at my other article on the introduction of these libraries in OpenGL. The address is as follows:
Http://blog.csdn.net/zhangkaihang/article/details/7459629
3. Set the window display style.
Before creating a window, we must set the window style to include
Ws_clipchildren (the Windows style used to create the parent window, used to crop the area covered by the Child Window during repainting) and ws_clipsiblings (the Windows style used to create the child window, used to crop the areas covered by other subwindows during repainting ),
This prevents OpenGL from being drawn to other windows. These should be placed inPrecreatewindow (). The Code is as follows:
4. Set the window pixel format
First, add several protected member variables and common member functions to the vcopengl2view class. As follows:
Hglrc m_hrc; // rendering context coloring description table cclientdc * m_pdc; // device context device description table bool initializeopengl (); // initialize openglbool setuppixelformat (); // set the pixel format void renderscene (); // draw the scenario
Do not forget to set m_hrc = NULL; m_pdc = NULL in the constructor of vcopengl2view;
The first step to generate an RC is to define the pixel format of the window. The pixel format determines how the displayed image is displayed in the memory. Parameters controlled by the pixel format include: color depth, buffer mode, and supported painting interfaces. These parameters are set in the setuppixelformat () function. The Code is as follows:
Bool cvcopengl2view: setuppixelformat (void) {static pixelformatdescriptor PFD = {sizeof (pixelformatdescriptor), // PFD structure size 1, // version pfd_draw_to_window | // supports drawing pfd_support_opengl in the window | // supports OpenGL pfd_doublebuffer, // Double Cache mode pfd_type_rgba, // rgba color mode 24, // 24-bit color depth 0, 0, 0, 0, 0, 0, // ignore color bit 0, // no non-transparency cache 0, // ignore shift bit 0, // No cumulative cache 0, 0, 0, 0, // ignore cumulative 32 bits, // 32 bits depth cache 0, // no template cache 0, // no secondary cache pfd_main_plane, // main layer 0, // retain 0, 0, 0 // ignore layer, visibility and damage mask}; int pixelformat; // obtain the most matched pixel format for the device description table if (pixelformat = choosepixelformat (m_pdc-> getsafehdc (), & PFD) = 0) {MessageBox (_ T ("choosepixelformat failed"); Return false ;} // set the most matched pixel format to the current pixel format if (setpixelformat (m_pdc-> getsafehdc (), pixelformat, & PFD) = false) {MessageBox (_ T ("setpixelformat failed"); Return false;} return true ;}
5. generate an RC and set it to the current RC.
Now the pixel format has been set. Our next step is to generate the rendering environment (RC) and make it the current drawing environment, that is, compile the initializeopengl () function. The Code is as follows:
Bool cvcopengl2view: initializeopengl (void) {pixelformatdescriptor PFD; int N; m_pdc = new cclientdc (this); Assert (m_pdc! = NULL); // set the current drawing pixel format if (! Setuppixelformat () {return false;} n =: getpixelformat (m_pdc-> getsafehdc ();: describepixelformat (m_pdc-> getsafehdc (), N, sizeof (PFD), & PFD); // create a Drawing description table m_hrc = wglcreatecontext (m_pdc-> getsafehdc (); If (m_hrc = NULL) {return false ;} // set the Drawing description table to the current Drawing description table of the current calling process. If (wglmakecurrent (m_pdc-> getsafehdc (), m_hrc) = false) {return false ;} glcleardepth (1.0f); glable (gl_depth_test); Return true ;}
Do not forget to call the initializeopengl () function in the oncreate () function. As follows:
Int cvcopengl2view: oncreate (maid) {If (cview: oncreate (maid) =-1) Return-1; // todo: add your dedicated creation code here if (initializeopengl () {return 0 ;}return 0 ;}
6. Set the viewport
In onsize (), it is generally used to set the views and cones, because these are related to the window size. The Code is as follows:
Void cvcopengl2view: onsize (uint ntype, int CX, int CY) {cview: onsize (ntype, CX, CY); // todo: here, add the message processing program code m_wide = Cx; // m_wide to m_heigth = cy, the member variable indicating the width of the view added in the cvcopengl2view class; // m_height is the member variable that represents the height of the view added in the cvcopengl2view class. // avoid division 0if (m_heigth = 0) {m_heigth = 1 ;} // set the size of the viewport and window glviewport (0, 0, m_wide, m_heigth );}
7. Draw a scenario (all the Code related to drawing with OpenGL is here !!)
This article uses a three-dimensional cube as an example. The Code is as follows:
Void cvcopengl2view: renderscene (void) {// set the clear screen color to black glclearcolor (0.0f, 0.0f, 0.0f, 0.0f ); // clear the color buffer and the depth buffer glclear (gl_color_buffer_bit | gl_depth_buffer_bit); // create the shear body under the orthogonal transformation if (W
Don't forget to call it in the ondraw () function !!!!! As follows:
Void cvcopengl2view: ondraw (CDC */* PDC */) {cvcopengl2doc * pdoc = getdocument (); assert_valid (pdoc); If (! Pdoc) return; // todo: add the drawing code renderscene () for the local data here ();}
8. Some final work.
8.1 In order to make the window much flickering, do some operations in onerasebkgnd () to Prevent Windows from refreshing and flashing. The onerasebkgnd () function must be rewritten. As follows:
Bool cvcopengl2view: onerasebkgnd (CDC * PDC) {// todo: add the message processing program code here and/or call the default value return true; // return cview :: onerasebkgnd (PDC );}
8.2 To avoid Memory leakage, add the following code to the ondestroy () function:
Void cvcopengl2view: ondestroy () {cview: ondestroy (); // todo: add the message processing program code m_hrc =: wglgetcurrentcontext (); If (:: wglmakecurrent (0, 0) = false) {MessageBox (_ T ("cocould not make RC non-current");} If (m_hrc) {If (: wgldeletecontext (m_hrc) = false) {MessageBox (_ T ("cocould not delete RC") ;}}if (m_pdc) {Delete m_pdc;} m_pdc = NULL ;}
Now the single-document Framework is ready. The following is a picture of the cube, for example: