#include <gl/opengl.h>
HGLRC hRc = NULL;
HDC hDc = NULL;
HWND hWnd = NULL;
HINSTANCE hInstance = NULL;
BOOL keys[256];
BOOL active = TRUE;
BOOL fullscreen = TRUE;
LRESULT CALLBACK WindowProc( HWND, UINT, WPARAM, LPARAM);
GLvoid ReSizeGLScene(GLsizei width, GLsizei height){
if (height == 0) height = 1;
glViewport(0,0,width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)width/(GLfloat)height,0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int InitGL(GLvoid){
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
return TRUE;
}
BOOL DrawGLScene(GLvoid){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
return TRUE;
}
GLvoid KillGLWindow(GLvoid){
if (fullscreen){
ChangeDisplaySettings(NULL, 0);
ShowCursor(TRUE);
}
if (hRc){
if (!wglMakeCurrent(NULL, NULL)){
MessageBox(NULL, "释放DC或RC失败。","关闭错误",MB_OK|MB_CANCELTRYCONTINUE);
}
if (!wglDeleteContext(hRc)){
MessageBox(NULL, "释放RC失败。","关闭错误",MB_OK|MB_CANCELTRYCONTINUE);
}
hRc = NULL;
}
if (hDc && !ReleaseDC(hWnd, hDc)){
MessageBox(NULL, "释放DC失败。","关闭错误",MB_OK|MB_CANCELTRYCONTINUE);
hDc = NULL;
}
if (hWnd && !DestroyWindow(hWnd)){
MessageBox(NULL, "释放窗口句柄失败。","关闭错误",MB_OK|MB_CANCELTRYCONTINUE);
hWnd = NULL;
}
if (!UnregisterClass("opengl", hInstance)){
MessageBox(NULL, "不能注销窗口类。","关闭错误",MB_OK|MB_CANCELTRYCONTINUE);
hInstance = NULL;
}
}
BOOL CreateGLWindow(char* title, int width, int height, int bits, BOOL fullscreenflag){
GLuint PixelFormat;
WNDCLASS wc;
DWORD dwExStyle;
DWORD dwStyle;
RECT WindowRect;
WindowRect.left = static_cast<long>(0);
WindowRect.right = static_cast<long>(width);
WindowRect.top = static_cast<long>(0);
WindowRect.bottom = static_cast<long>(height);
fullscreen = fullscreenflag;
hInstance = GetModuleHandle(NULL);
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = "opengl";
if (!RegisterClass(&wc)){
MessageBox(NULL, "注册窗口失败", "错误", MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
if (fullscreen){
DEVMODE dmScreenSetting;
memset(&dmScreenSetting, 0, sizeof(dmScreenSetting));
dmScreenSetting.dmSize = sizeof(dmScreenSetting);
dmScreenSetting.dmPelsHeight = height;
dmScreenSetting.dmPelsWidth = width;
dmScreenSetting.dmBitsPerPel = bits;
dmScreenSetting.dmFields = DM_BITSPERPEL|DM_PELSHEIGHT|DM_PELSWIDTH;
if (ChangeDisplaySettings(&dmScreenSetting, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL){
if (MessageBox(NULL, "全屏模式在当前显卡上设置失败!
使用窗口模式?","NeHe G",MB_YESNO|MB_ICONEXCLAMATION) == IDYES){
fullscreen = FALSE;
}
else{
MessageBox(NULL, "程序将被关闭", "错误", MB_OK|MB_ICONSTOP);
return FALSE;
}
}
}
if (fullscreen){
dwExStyle = WS_EX_APPWINDOW;
dwStyle = WS_POPUP;
ShowCursor(FALSE);
}
else{
dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
dwStyle = WS_OVERLAPPEDWINDOW;
}
AdjustWindowRectEx(&WindowRect,dwStyle,FALSE, dwExStyle);
if (! (hWnd = CreateWindowEx(dwExStyle,
"opengl",
title,
WS_CLIPSIBLINGS|
WS_CLIPCHILDREN|
dwStyle,
0, 0,
WindowRect.right-WindowRect.left,
WindowRect.bottom-WindowRect.top,
NULL,
NULL,
hInstance,
NULL))){
KillGLWindow();
MessageBox(NULL, "不能创建一个窗口设备描述表", "错误", MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
static PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL|
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
bits,
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
16,
0,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0
};
if (!(hDc = GetDC(hWnd))){
KillGLWindow();
MessageBox(NULL, "不能创建一种相匹配的像素格式", "错误", MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
if (!(PixelFormat = ChoosePixelFormat(hDc, &pfd))){
KillGLWindow();
MessageBox(hWnd, "不能设置像素格式", "错误", MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
if (!SetPixelFormat(hDc, PixelFormat, &pfd)){
KillGLWindow();
MessageBox(hWnd, "不能设置像素格式", "错误", MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
if (!(hRc = wglCreateContext(hDc))){
KillGLWindow();
MessageBox(NULL, "不能创建openGL渲染描述表","错误",MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
if (!(wglMakeCurrent(hDc, hRc))){
KillGLWindow();
MessageBox(NULL, "不能激活当前的openGL渲染描述表", "错误", MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
ShowWindow(hWnd, SW_SHOW);
SetForegroundWindow(hWnd);
SetFocus(hWnd);
ReSizeGLScene(width, height);
if (!InitGL()){
KillGLWindow();
MessageBox(NULL, "Initialization Failed.","error",MB_OK|MB_ICONEXCLAMATION );
return FALSE;
}
return TRUE;
}
LRESULT CALLBACK WindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
switch(uMsg){
case WM_ACTIVATE:
{
if (!HIWORD(wParam))
active = TRUE;
else
active = FALSE;
return 0;
}
case WM_SYSCOMMAND:
{
switch(wParam){
case SC_SCREENSAVE:
case SC_MONITORPOWER:
return 0;
}
break;
}
case WM_CLOSE:
{
PostQuitMessage(0);
return 0;
}
case WM_KEYUP:
{
keys[wParam] = TRUE;
return 0;
}
case WM_KEYDOWN:
{
keys[wParam] = FALSE;
return 0;
}
case WM_SIZE:
{
ReSizeGLScene(LOWORD(lParam), HIWORD(lParam));
return 0;
}
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ){
MSG msg;
BOOL done=FALSE;
if (MessageBox(NULL, "你想在全屏模式下运行吗?", "设置全屏模式", MB_YESNO|MB_ICONEXCLAMATION) == IDNO)
fullscreen = FALSE;
if (!CreateGLWindow("Nehe's openGL 程序框架", 640, 480, 16, fullscreen))
return 0;
while (!done){
if (PeekMessage(&msg, NULL, 0,0,PM_REMOVE)){
if (msg.message == WM_QUIT){
done = TRUE;
}
else{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else{
if (active){
if (keys[VK_ESCAPE]){
done = TRUE;
}
else{
DrawGLScene();
SwapBuffers(hDc);
}
if (keys[VK_F1]){
keys[VK_F1] =FALSE;
KillGLWindow();
fullscreen = !fullscreen;
if (!CreateGLWindow("Nehe's openGL 程序框架", 640, 480, 16, fullscreen))
return 0;
}
}
}
}
KillGLWindow();
return (msg.wParam);
}