头文件:

/* 简单封装的OpenGL显示窗体,本程序显示一个旋转的3D茶壶。
 * 使用方法:把控件关联到一个COpenGLWnd类型的变量即可。
 * 参考网站:
   http://code.google.com/p/jtianling/source/browse/2009-9-28/RotateRect/RotateRect.cpp?repo=blog-sample-code
http://blog.csdn.net/vagrxie/archive/2009/09/28/4602961.aspx
 
 * 参考书目:
   OpenGL超级宝典(第二版)》潇湘工作室译
 */
 
#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 COpenGLWnd : public CWnd
{
    DECLARE_DYNAMIC(COpenGLWnd)
 
public:
    COpenGLWnd();
    virtual ~COpenGLWnd();
private:
    //设备环境句柄
    HDC _m_hDC;
 
    //OpenGL渲染环境句柄
    HGLRC _m_hGLRC;
private:
    void OpenGLEnable();
    void OpenGLDisable();
    void OpenGLInit();
    void OpenGLRender();
 
public:
    afx_msg void OnDestroy();
    afx_msg void OnPaint();
    afx_msg void OnTimer(UINT nIDEvent);
 
protected:
    virtual void PreSubclassWindow();
   
protected:
    DECLARE_MESSAGE_MAP()
};
 
源文件:

#include "stdafx.h"
#include "OpenGLWnd.h"
 
 
IMPLEMENT_DYNAMIC(COpenGLWnd, CWnd)
COpenGLWnd::COpenGLWnd()
{
    _m_hDC=NULL;
}
 
COpenGLWnd::~COpenGLWnd()
{
}
 
 
BEGIN_MESSAGE_MAP(COpenGLWnd, CWnd)
    ON_WM_DESTROY()
    ON_WM_PAINT()
    ON_WM_TIMER()
END_MESSAGE_MAP()
 
 
void COpenGLWnd::OnDestroy()
{
    CWnd::OnDestroy();
   
    //4.第四步
    OpenGLDisable();
}
 
void COpenGLWnd::OpenGLDisable()
{
    //设置当前线程的渲染环境为NULL
    wglMakeCurrent(_m_hDC, NULL);
 
    //删除应用程序不再需要的渲染环境
    wglDeleteContext(_m_hGLRC);
 
    ::ReleaseDC(m_hWnd,_m_hDC);
}
 
void COpenGLWnd::OpenGLEnable()
{
    _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.iLayerType = PFD_MAIN_PLANE ;
 
    //此函数为当前设备环境选择最匹配的像素格式
    int nPixelFormat=ChoosePixelFormat(_m_hDC, &pfd);
 
    //设置像素格式
    SetPixelFormat(_m_hDC,nPixelFormat,&pfd);
 
    //创建OpenGL渲染环境
    _m_hGLRC = wglCreateContext(_m_hDC); 
 
    //设置当前线程的渲染环境,并与设备环境关联
    wglMakeCurrent(_m_hDC, _m_hGLRC);  
}
 
 
void COpenGLWnd::OnPaint()
{
    CPaintDC dc(this);
 
    //3.第三步
    OpenGLRender();
 
    //把缓冲区中的内容复制到前景中
    SwapBuffers(_m_hDC);
}
 
void COpenGLWnd::PreSubclassWindow()
{
    //1.第一步
    OpenGLEnable();
 
    //2.第二步
    OpenGLInit();
 
    SetTimer(1,5,NULL);
    CWnd::PreSubclassWindow();
}
 
void COpenGLWnd::OpenGLRender()
{
    //Todo:有代码,往这里写
    //
   
    //清空缓存
    glClear(GL_COLOR_BUFFER_BIT);
 
    //每次旋转2
    glRotatef(2.0,0.0,1.0,1.0);
 
    //著名的大茶壶
    auxWireTeapot(0.55);//线条版
    //auxSolidTeapot(0.55);//实心版
 
    //刷新命令队列和缓冲区
    glFlush();
}
 
void COpenGLWnd::OpenGLInit()
{
    //清空缓存
    glClear(GL_COLOR_BUFFER_BIT);
 
    //清除颜色缓冲区时所用的颜色
    glClearColor(0.0,0.0,0.0,0.0);
 
    //指定当前颜色
    glColor3f(1.0,1.0,1.0);
}
 
void COpenGLWnd::OnTimer(UINT nIDEvent)
{
    RedrawWindow();
    CWnd::OnTimer(nIDEvent);
}


本文链接地址: OpenGL.4 Win32下的OpenGL基本框架:对OpenGL简单的封装,使用wgl
https://blog.qingfengju.com/index.asp?id=181

上一篇: 几个实用的注册表设置数据(右键进入命令行,Windows2000模式的搜索,)
下一篇: OpenGL.5 Win32下的OpenGL基本框架之更简单的封装

分类:Win32/C++ 查看次数:7124 发布时间:2009/12/9 14:27:31