您的位置:首页 > 编程语言

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

2010-11-23 03:20 337 查看
本文源代码下载: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应用程序,在项目属性中加入所需要链接的库文件



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_CLIPCHILDRENWS_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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: