《基于MFC的OpenGL编程》Part 2 Setting up OpenGL on Windows(1)

本文源代码下载:OpenGL_ch2.rar
 
WGL – Windows OpenGL 扩展层
The WGL extension consists of a set of functions (wglCreateContext, wglDeleteContext etc.) and structures (such as PIXELFORMATDESCRIPTOR, GLYPHMETRICSFLOAT) etc. Thus every OpenGL implementation has a platform-specific portion which has to be set up and used according to the particular platform.
设备上下文
The Windows Graphical Device Interface (GDI) is capable of drawing to screen, to memory, to printers or to any other device that provides a GDI interface layer and that can process GDI calls. GDI accomplishes this by a rendering handle to the currently selected device, which is called the device context, or DC.
绘制上下文
A rendering context is the OpenGL equivalent of the GDI DC. All OpenGL calls are rendered to the device through a RC. The rendering context maintains OpenGL state variables such as current background color, current color etc. just as the DC maintains GDI state variables such as current pen, current brush etc.
像素格式
Pixel formats are the translation layer between OpenGL calls and the rendering operation that Windows performs.
举个例子,若像素格式只支持很少一部分颜色值,则 OpenGL 在用 RGB (128,120,135) 绘制一个像素时,就可能使用转换后的值 (128,128,128) 来绘制 .
The pixel format selected essentially describes such things as how colors are displayed, depth of field resolution and what additional capabilities are supported by the rendering context created.
第一个基于MFCOpenGL应用程
开发环境:VC6.0
1, 首先下载需要的 GLUT 头文件 ,DLL Lib 文件,下载链接 : glutdlls37beta.zip (149 kilobytes), 解压缩后把 gltu.h 放到 "VC98/Include/GL" 下,把 glut.lib glut32.lib 放到 "VC9/Lib" 下 glut32.dll glut.dll 放到你创建的应用程序的运行目录下
2, 创建一个 MFC SDI 应用程序,在项目属性中加入所需要链接的库文件
《基于MFC的OpenGL编程》Part 2 Setting up OpenGL on Windows(1)_第1张图片
1, stdafx.h中加入下列语句: 2, 打开ClassWizard,选择CCY457OpenGLView类,为下述消息加入消息处理函数:WM_CREATE (for OnCreate), WM_DESTROY (for OnDestroy), WM_SIZE (for OnSize), WM_ERASEBACKGROUND (for OnEraseBkground).
//OpenGL Headers

#include
<gl/gl.h>

#include
<gl/glu.h>

#include
<gl/glut.h>

#include
<gl/glaux.h>
 
3,在窗口创建之前我们必须设置窗口风格包含WS_CLIPCHILDREN WS_CLIPSIBLINGS,从而避免OpenGL绘制到其他窗口中去。这些应该放在PreCreateWindow()中。
BOOL CCY457OpenGLView::PreCreateWindow(CREATESTRUCT& cs)
{
    
//
TODO: Modify the Window class or styles here by modifying
    
//
   the CREATESTRUCT cs
    
//An OpenGL Window must be created with the following flags

     cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
    
return
CView::PreCreateWindow(cs);
}
4, CCY457OpenGLView.h 中加入如下语句:
     HGLRC m_hRC;    //Rendering Context
     CDC* m_pDC;        //Device Context
     BOOL InitializeOpenGL();    //Initialize OpenGL
     BOOL SetupPixelFormat();    //Set up the Pixel Format
    void RenderScene();            //Render the Scene
 
 

你可能感兴趣的:(windows)