A simple example of using OpenGL under the MFC

zhaozj2021-02-16  55

Recently, I started to prepare for OpenGL, and I'm hard to start, I really feel very deep, it is simply difficult. Sometimes you send each post to the Internet, keep refreshing, I hope to have a high-end pointing, but every harvest is not too big. Perhaps each programmer has a painful process, I certainly can't exceed it. However, there is a biggest happiness after pain.

Because of this reason, when I spent the first difficult time, my heart is cool; however, I think about my own pain experience, it is really going to look back; I will continue to learn OpenGL, I hope I will write it out. Things can reduce their pain experience.

In the MFC, an OpenGL program is first set, first set the environment:

First, under XXView.h, join the header file

#include "gl / gl.h" #include "gl / glu.h" #include "gl / glaux.h"

Then add the following files into your own project.

VC location: / Program Files / Microsoft Visual Studio / VC98 / LIB / OPENGL32.LIB

VC location: / Program Files / Microsoft Visual Studio / VC98 / LIB / Glu32.lib.lib

VC location: / Program Files / Microsoft Visual Studio / VC98 / LIB / GLAUX.LIB.LIB

Some of you can not use it, but there is no relationship, and you will use it if you use it.

The following is the main four parts:

4-1:

void CMySunView :: OnInitialUpdate () {CView :: OnInitialUpdate (); // m_pDC = new CClientDC (this); m_hDC = m_pDC-> GetSafeHdc (); static PIXELFORMATDESCRIPTOR pfd = {sizeof (PIXELFORMATDESCRIPTOR), // size of this pfd 1, // version number PFD_DRAW_TO_WINDOW | // support window PFD_SUPPORT_OPENGL | // support OpenGL PFD_DOUBLEBUFFER, // double buffered PFD_TYPE_RGBA, // RGBA type 24, // 24-bit color depth 0, 0, 0, 0, 0, 0 , // color bits ignored 0, // no alpha buffer 0, // Shift bit ignored 0, // no account ignore 0, 0, 0, 0, // Accum bits ignored 16, // 16-bit z-buffer 0, // no steecil buffer 0, // no auxiliary buffer PFD_MAIN_PLANE, // main layer 0, // reserved 0, 0, 0 // layer masks ignored}; int m_PixelFormat; m_PixelFormat = :: ChoosePixelFormat (m_hDC, & pfd); :: SetPixelFormat (m_hDC, m_PixelFormat, & pfd); // m_hrc = :: wglcreatecontext (m_hdc); WGLmakecurrent (m_hdc, m_hrc);}

4-2:

void CMySunView :: OnDraw (CDC * pDC) {CMySunDoc * pDoc = GetDocument (); ASSERT_VALID (pDoc); // :: glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); float size = 100.0f; glLineWidth (3.0f); // / / AXIS - X Glcolor3f (1, 0, 0); Glbegin (GL_Lines); Glvertex3f (-Size, 0, 0); GlvertEX3F (Size, 0, 0); glend (); // axis - y Glcolor3f ( 0, 1, 0); Glbegin (GL_LINES); Glvertex3f (0, -Size, 0); GlvertEX3F (0, size, 0); glend (); // axis - z Glcolor3f (0, 0, 1); Glbegin (GL_LINES); GLVERTEX3F (0, 0, -Size); GlvertEX3F (0, Size); glend (); // GLLINEWIDTH (1.0F); // ssj :: GLFINISH (); swapbuffers (m_hdc); } 4-3

Void CMYSUNVIEW :: Onsize (uint ntype, int cx, int CY) {cview :: Onsize (ntype, cx, cy); // if (0> = cx || 0> = cy) return; :: GlclearColor (0.0 F, 0.0F, 0.0F, 255.0F); // Specify the back of the buffer as clear depth :: glcleardepth (1.0f); // enable defth Testing :: Glenable (GL_Depth_test) ;:: GLVIEWPORT (0, 0 , CX, CY); :: GLMATRIXMODE (GL_Projection); :: GLLoadident (); :: glortidentity (); :: glortho (xorg - xmax, xorg xmax, yorg - ymax, yorg ymax, zorg-zmax, zorg zmax); :: GLROTATEF (XROTATE, 1.0F, 0.0F, 0.0F); :: Glrotatef (YROTATE, 0.0F, 1.0F, 0.0F); :: Glrotatef (Zrotate, 0.0F, 0.0F, 1.0F); :: GLmatrixmode GL_MODELVIEW); :: GLloadIdentity (); invalidate ();} 4-4

Bool Cmysunview :: OneRaseBkgnd (CDC * PDC) {Return True;}

Void cmysunview :: ONDESTROY () {cview :: overdestroy (); wgldeletecontext (m_hrc);

CMYSUNVIEW :: CMYSUNVIEW () {xrotate = 45.0f; yrotate = 0.0f; zrotate = -15.0F; xorg = 0, xmax = 42; yorg = 0, YMAX = 70; zorg = 0, zmax = 40;

}

Class CMYSUNVIEW: PUBLIC CVIEW {..........

Public: hglrc m_hrc; hdc m_hdc; cdc * m_pdc; int width, height; float xrotate; float yrotate; float zrotate; float xorg, xmax; float yorg, ymax; float zorg, zmax; .......

}

The above is the main step, of course, other personalized things can be added, this is the most basic thing that minimizes, may also have a place, but it is already possible to see something.

I don't want to make too much explanation, if I am interested, continue to discuss.

转载请注明原文地址:https://www.9cbs.com/read-27129.html

New Post(0)