最近在学OpenGL,不想使用OpenGL的GLUT工具,自己写了一个初始化OpenGL的类,并在win32中使用,每次都要新建一个win32项目,然后将OpenGL初始化类拷贝到项目,然后进行各种初始化,通过自定义向导,我们可以很方便解决这个问题。
一、新建win32项目。
二、将OpenGL初始化类CUseOpenGL源文件和头文件拷贝到当前工程目录下。
UseOpenGL.h文件内容
#ifndef _USE_OPENGL_H_
#define _USE_OPENGL_H_
#include <Windows.h>
#include <gl/GL.h>
#include <gl/GLU.h>
class CUseOpenGL
{
public:
CUseOpenGL();
~CUseOpenGL();
public:
bool SetWindowPixelFormat(HDC hDC);
bool CreateViewGLContext(HDC hDC);
void DestoryGLContext();
virtual void ResizeGLView(int width, int height);
private:
int m_nGLPixelIndex;
HGLRC m_hGLContext;
};
#endif
UseOpenGL.cpp文件内容
#include "UseOpenGL.h"
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")
CUseOpenGL::CUseOpenGL()
{
m_nGLPixelIndex = 0;
m_hGLContext = NULL;
}
CUseOpenGL::~CUseOpenGL()
{
DestoryGLContext();
}
bool CUseOpenGL::SetWindowPixelFormat(HDC hDC)
{
bool bRet = true;
try
{
PIXELFORMATDESCRIPTOR pixelDesc = {sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER | PFD_SUPPORT_GDI,
PFD_TYPE_RGBA,
24,
0,0,0,0,0,0,
0,0,0,0,0,0,0,
32,
0, 0, PFD_MAIN_PLANE,
0, 0, 0, 0};
m_nGLPixelIndex = ChoosePixelFormat(hDC, &pixelDesc);
if (m_nGLPixelIndex == 0)
{
m_nGLPixelIndex = 1;
if (DescribePixelFormat(hDC, m_nGLPixelIndex, sizeof(PIXELFORMATDESCRIPTOR), &pixelDesc) == 0)
{
bRet = false;
throw bRet;
}
}
if (!SetPixelFormat(hDC, m_nGLPixelIndex, &pixelDesc))
{
bRet = false;
throw bRet;
}
}
catch (bool)
{
}
return bRet;
}
bool CUseOpenGL::CreateViewGLContext(HDC hDC)
{
bool bRet = true;
try
{
m_hGLContext = wglCreateContext(hDC);
if (!m_hGLContext)
{
bRet = false;
throw bRet;
}
if (!wglMakeCurrent(hDC, m_hGLContext))
{
bRet = false;
throw bRet;
}
}
catch (bool)
{
}
return bRet;
}
void CUseOpenGL::DestoryGLContext()
{
if (wglGetCurrentContext())
{
wglMakeCurrent(NULL, NULL);
}
if (m_hGLContext)
{
wglDeleteContext(m_hGLContext);
m_hGLContext = NULL;
}
}
void CUseOpenGL::ResizeGLView(int width, int height)
{
if (width < 1)
{
width = 1;
}
if (height < 1)
{
height = 1;
}
//视口原点在窗口左下角
glViewport(0, 0, width, height);
}
三、将UseOpenGL.h和UseOpenGL.cpp文件添加到现有工程
四、使用OpenGL,在Win32OpenGL.cpp中加入以下代码
#include "UseOpenGL.h"
voidDisplay();
class CMyUseOpenGL : public CUseOpenGL
{
public:
virtual void ResizeGLView(int width, int height);
};
CMyUseOpenGL g_useOpenGL;
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_CREATE:
{
hdc = GetDC(hWnd);
g_useOpenGL.SetWindowPixelFormat(hdc);
g_useOpenGL.CreateViewGLContext(hdc);
ReleaseDC(hWnd, hdc);
}
break;
case WM_SIZE:
{
RECT rect;
GetClientRect(hWnd, &rect);
g_useOpenGL.ResizeGLView(rect.right - rect.left, rect.bottom - rect.top);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
Display();
SwapBuffers(hdc);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
void CMyUseOpenGL::ResizeGLView(int width, int height)
{
CUseOpenGL::ResizeGLView(width, height);
}
void Display()
{
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
}
运行,如下图所示:
五、创建自定义向导
六、将Win32OpenGL下的.h/.ico/.cpp/.rc文件拷贝到Win32OpenGLWizard项目下的\Templates\2052目录,并添加到项目
模板文件上添加现有项
七、打开Templates.inf文件,将拷贝的文件名添加进去
readme.txt
Resource.h
small.ico
stdafx.cpp
stdafx.h
targetver.h
UseOpenGL.cpp
UseOpenGL.h
Win32OpenGL.cpp
Win32OpenGL.h
Win32OpenGL.ico
Win32OpenGL.rc
八、打开default.js文件,进行修改
function AddFilters(proj)
{
try
{
// 将文件夹添加到项目
// var strSrcFilter = wizard.FindSymbol('SOURCE_FILTER');
// var group = proj.Object.AddFilter('Source Files');
// group.Filter = strSrcFilter;
proj.Object.AddFilter('源文件').Filter = 'cpp;c';
proj.Object.AddFilter('头文件').Filter = 'hpp;h';
proj.Object.AddFilter('资源文件').Filter = 'ico;rc;bin';
}
catch(e)
{
throw e;
}
}
function GetTargetName(strName, strProjectName)
{
try
{
// TODO: 基于模板文件名设置呈现文件的名称
var strTarget = strName;
if (strName == 'readme.txt')
strTarget = 'ReadMe.txt';
if (strName == 'sample.txt')
strTarget = 'Sample.txt';
if (strName == 'Win32OpenGL.cpp')
strTarget = strProjectName + '.cpp';
if (strName == 'Win32OpenGL.h')
strTarget = strProjectName + '.h';
if (strName == 'Win32OpenGL.ico')
strTarget = strProjectName + '.ico';
if (strName == 'Win32OpenGL.rc')
strTarget = strProjectName + '.rc';
return strTarget;
}
catch(e)
{
throw e;
}
}
九、将所有老的项目修改为新的项目名
打开Win32OpenGL.cpp文件
1、将#include "Win32OpenGL.h"修改为
#include "[!output PROJECT_NAME].h"
打开Win32OpenGL.rc文件
2、将IDI_WIN32OPENGL ICON "Win32OpenGL.ico"修改为
IDI_WIN32OPENGL ICON "[!output PROJECT_NAME].ico"
3、将CAPTION "关于 Win32OpenGL"修改为
CAPTION "关于 [!output PROJECT_NAME]"
4、将 LTEXT "Win32OpenGL,1.0 版",IDC_STATIC,42,14,114,8,SS_NOPREFIX修改为
LTEXT "[!output PROJECT_NAME],1.0 版",IDC_STATIC,42,14,114,8,SS_NOPREFIX
5、将IDC_WIN32OPENGL "WIN32OPENGL"修改为
IDC_WIN32OPENGL "[!output PROJECT_NAME]"
6、将IDS_APP_TITLE "Win32OpenGL"修改为
IDS_APP_TITLE "[!output PROJECT_NAME]"
十、此时向导已经完成。
打开VS2010,新建项目,向导,Win32OpenGLWizard
我们可以看到Win32OpenGLWizard向导是在"向导"这个子项下,并且向导的说明也是默认的,我们可以修改向导的说明并且可以将Win32OpenGLWizard向导放在"Win32"子项下。
打开"我的文档",定位到“Documents\Visual Studio 2010\Wizards”目录,里面有三个文件Win32OpenGLWizard.ico、Win32OpenGLWizard.vsdir、Win32OpenGLWizard.vsz,如果将这三个文件删除后,那么Win32OpenGLWizard便会自动从向导中移除,现在我们将这三个文件移动到VS2010安装目录下的\Microsoft Visual Studio 10.0\VC\vcprojects目录,打开Win32OpenGLWizard.vsdir文件,将内容\Microsoft Visual Studio 10.0\VC\vcprojects\Win32目录下的win32.vsdir文件的末尾,内容如下
..\Win32Wiz.vsz|{1B027A40-8F43-11D0-8D11-00A0C91BC942}|#1171|1|#1172|{1B027A40-8F43-11D0-8D11-00A0C91BC942}|0|4096|#1154
..\Win32Console.vsz|{1B027A40-8F43-11D0-8D11-00A0C91BC942}|#1204|1|#1205|{1B027A40-8F43-11D0-8D11-00A0C91BC942}|0|4096|#1154
..\Win32OpenGLWizard.vsz| |Win32OpenGLWizard|1|Win32 OpenGL 项目| |6777| |Win32OpenGLWizard
其中"Win32 OpenGL 项目"便是Win32OpenGLWizard向导的说明
打开Win32OpenGLWizard.vsz文件
VSWIZARD 7.0
Wizard=VsWizard.VsWizardEngine.10.0
Param="WIZARD_NAME = Win32OpenGLWizard"
Param="ABSOLUTE_PATH = E:\桌面\Custom\example\Win32OpenGLWizard\Win32OpenGLWizard"
Param="FALLBACK_LCID = 2052"
Param="WIZARD_UI = FALSE"
Param="SOURCE_FILTER = txt"
"ABSOLUTE_PATH = E:\桌面\Custom\example\Win32OpenGLWizard\Win32OpenGLWizard"是向导文件的位置,即第五步创建自定义向导的位置,这个路径下的文件不能被删除,否则向导不能新建项目。
参考:http://www.rupeng.com/forum/thread-9243-1-1-uid54410.html