头文件OpenGL.h

/*
使用方法:
    1.在需要显示OpenGL的窗体中的OnCreateInitDialog之类函数中执行EnableOpenGL()
    2.OnDestroy之类函数中执行DisableOpenGL()
    3.在自己需要的地方如OnPaint中执行任意OpenGL函数,如果与显示有关,则在最后执行SwapBuffers()
*/
 
#pragma once
#include <Windows.h>
 
#include <GL/gl.h>
#pragma comment(lib,"opengl32.lib")
 
#include <GL/glu.h>
#pragma comment( lib, "glu32.lib")
 
#include <GL/glaux.h>
#pragma comment( lib, "glaux.lib")
 
class COpenGL
{  
private:
    HDC _m_hDC;
    HGLRC _m_hGLRC;
    HWND _m_hWnd;
public:
    //返回值等于0,表示启用OpenGL成功
    int EnableOpenGL(IN HWND hWnd);
   
    //结束OpenGL的使用
    void DisableOpenGL();
 
    //交换缓冲区
    void SwapBuffers();
};
 
源文件OpenGL.cpp

#include "StdAfx.h"
#include "OpenGL.h"
 
int COpenGL::EnableOpenGL(IN HWND hWnd)
{
    ASSERT(hWnd!=NULL && IsWindow(hWnd));
    _m_hWnd=hWnd;
    _m_hDC=::GetDC(_m_hWnd);
 
    PIXELFORMATDESCRIPTOR pfd ;
    memset(&pfd, 0, sizeof(pfd));
    pfd.nSize      = sizeof(pfd);
    pfd.nVersion   = 1 ;
    pfd.dwFlags    = PFD_SUPPORT_OPENGL|PFD_DRAW_TO_WINDOW|PFD_DOUBLEBUFFER;
    pfd.cColorBits = 24 ;
    pfd.cDepthBits = 32 ;
    pfd.iLayerType = PFD_MAIN_PLANE ;
 
    int nPixelFormat=ChoosePixelFormat(_m_hDC, &pfd);
    if (!SetPixelFormat(_m_hDC,nPixelFormat,&pfd)){
        return -1;
    }
 
    _m_hGLRC = wglCreateContext(_m_hDC);
    if (_m_hGLRC==NULL){
        return -2;
    }
 
    if (!wglMakeCurrent(_m_hDC, _m_hGLRC)){
        return -3;
    }
 
    return 0;
}
 
void COpenGL::DisableOpenGL()
{
    wglMakeCurrent(_m_hDC, NULL);
    wglDeleteContext(_m_hGLRC);
    ::ReleaseDC(_m_hWnd,_m_hDC);
};
 
void COpenGL::SwapBuffers()
{
    ::SwapBuffers(_m_hDC);
}

本文链接地址: OpenGL.5 Win32下的OpenGL基本框架之更简单的封装
https://blog.qingfengju.com/index.asp?id=182

上一篇: OpenGL.4 Win32下的OpenGL基本框架:对OpenGL简单的封装,使用wgl
下一篇: 用DPInst.exe/devcon.exe从命令行自动安装驱动程序

分类:Win32/C++ 查看次数:6547 发布时间:2009/12/9 17:30:40