/* * This Code Was Created By Jeff Molofee 2000 * A HUGE Thanks To Fredric Echols For Cleaning Up * And Optimizing This Code, Making It More Flexible! * If You've Found This Code Useful, Please Let Me Know. * Visit My Site At nehe.gamedev.net */ #include <windows.h> // Windows的头文件 #include <gl/gl.h> // OpenGL32库的头文件 #include <gl/glu.h> // GLu32库的头文件 #include <gl/glaux.h> // GLaux库的头文件 HDC hDC=NULL; // Private GDI Device Context/设备描述表 HGLRC hRC=NULL; // Permanent Rendering Context/着色描述表 HWND hWnd=NULL; // Holds Our Window Handle/窗口指派的句柄 HINSTANCE hInstance; // Holds The Instance Of The Application/实例 bool keys[256]; // Array Used For The Keyboard Routine bool active=TRUE; // Window Active Flag Set To TRUE By Default bool fullscreen=TRUE; // Fullscreen Flag Set To Fullscreen Mode By Default LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window { if (height==0) // Prevent A Divide By Zero By { height=1; // Making Height Equal One } glViewport(0,0,width,height); // 重置当前的视口 glMatrixMode(GL_PROJECTION); // 投影矩阵 glLoadIdentity(); // Reset The Projection Matrix // Calculate The Aspect Ratio Of The Window gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f); glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix/模型观察矩阵 glLoadIdentity(); // Reset The Modelview Matrix } int InitGL(GLvoid) //初始化 { glShadeModel(GL_SMOOTH); // Enable Smooth Shading glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // 背景颜色 glClearDepth(1.0f); // 深度缓存 glEnable(GL_DEPTH_TEST); // Enables Depth Testing glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // 透视修正 return TRUE; // Initialization Went OK } int DrawGLScene(GLvoid) // 开始进行所有的绘制 { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // 清除屏幕和深度缓存 glLoadIdentity(); // 重置当前的模型观察矩阵 return TRUE; // Everything Went OK } GLvoid KillGLWindow(GLvoid) // 正常销毁窗口 { if (fullscreen) // Are We In Fullscreen Mode? { ChangeDisplaySettings(NULL,0); // If So Switch Back To The Desktop ShowCursor(TRUE); // Show Mouse Pointer } if (hRC) // Do We Have A Rendering Context? { if (!wglMakeCurrent(NULL,NULL)) // 释放RC { MessageBox(NULL,"无法释放着色描述表.","关闭失败",MB_OK | MB_ICONINFORMATION); } if (!wglDeleteContext(hRC)) // 删除RC { MessageBox(NULL,"无法删除着色描述表.","关闭失败",MB_OK | MB_ICONINFORMATION); } hRC=NULL; // Set RC To NULL } if (hDC && !ReleaseDC(hWnd,hDC)) // 释放DC { MessageBox(NULL,"无法释放设备描述表.","关闭失败",MB_OK | MB_ICONINFORMATION); hDC=NULL; // Set DC To NULL } if (hWnd && !DestroyWindow(hWnd)) // 销毁窗口 { MessageBox(NULL,"无法释放窗体.","关闭失败",MB_OK | MB_ICONINFORMATION); hWnd=NULL; // Set hWnd To NULL } if (!UnregisterClass("OpenGL",hInstance)) // Are We Able To Unregister Class { MessageBox(NULL,"无法注销类.","关闭失败",MB_OK | MB_ICONINFORMATION); hInstance=NULL; // Set hInstance To NULL } } /* This Code Creates Our OpenGL Window. Parameters Are: * * title - Title To Appear At The Top Of The Window * * width - Width Of The GL Window Or Fullscreen Mode * * height - Height Of The GL Window Or Fullscreen Mode * * bits - Number Of Bits To Use For Color (8/16/24/32) * * fullscreenflag - Use Fullscreen Mode (TRUE) Or Windowed Mode (FALSE) */ BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag) { GLuint PixelFormat; // 象素格式 WNDCLASS wc; // 窗口类 DWORD dwExStyle; // Window Extended Style DWORD dwStyle; // 窗口风格 RECT WindowRect; // Grabs Rectangle Upper Left / Lower Right Values WindowRect.left=(long)0; // Set Left Value To 0 WindowRect.right=(long)width; // Set Right Value To Requested Width WindowRect.top=(long)0; // Set Top Value To 0 WindowRect.bottom=(long)height; // Set Bottom Value To Requested Height fullscreen=fullscreenflag; // Set The Global Fullscreen Flag hInstance = GetModuleHandle(NULL); // 取得窗口实例 wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // 移动时重画,并创建私有DC wc.lpfnWndProc = (WNDPROC) WndProc; // WndProc 消息处理 wc.cbClsExtra = 0; // No Extra Window Data wc.cbWndExtra = 0; // No Extra Window Data 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; // Return FALSE } if (fullscreen) // 全屏模式 { DEVMODE dmScreenSettings; // Device Mode memset(&dmScreenSettings,0,sizeof(dmScreenSettings)); // Makes Sure Memory's Cleared dmScreenSettings.dmSize=sizeof(dmScreenSettings); // Size Of The Devmode Structure dmScreenSettings.dmPelsWidth = width; // Selected Screen Width dmScreenSettings.dmPelsHeight = height; // Selected Screen Height dmScreenSettings.dmBitsPerPel = bits; // Selected Bits Per Pixel dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT; // Try To Set Selected Mode And Get Results. NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar. if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL) { // If The Mode Fails, Offer Two Options. Quit Or Use Windowed Mode. if (MessageBox(NULL,"无法启用全屏模式,是否使用窗体模式?","警告",MB_YESNO|MB_ICONEXCLAMATION)==IDYES) { fullscreen=FALSE; // Windowed Mode Selected. Fullscreen = FALSE } else { // Pop Up A Message Box Letting User Know The Program Is Closing. MessageBox(NULL,"程序遇到问题需要关闭.","错误",MB_OK|MB_ICONSTOP); return FALSE; // Return FALSE } } } if (fullscreen) // 全屏模式 { dwExStyle=WS_EX_APPWINDOW; // Window Extended Style dwStyle=WS_POPUP; // Windows Style ShowCursor(FALSE); // 隐藏鼠标指针 } else { dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended Style dwStyle=WS_OVERLAPPEDWINDOW; // Windows Style } AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle); // Adjust Window To True Requested Size // Create The Window if (!(hWnd=CreateWindowEx( dwExStyle, // Extended Style For The Window "OpenGL", // 类名称 title, // 窗口标题 dwStyle | // Defined Window Style WS_CLIPSIBLINGS | // Required Window Style WS_CLIPCHILDREN, // Required Window Style 0, 0, // 窗口位置 WindowRect.right-WindowRect.left, // 宽 WindowRect.bottom-WindowRect.top, // 高 NULL, // 父窗口 NULL, // 菜单 hInstance, // 实例 NULL))) // Dont Pass Anything To WM_CREATE { KillGLWindow(); // Reset The Display MessageBox(NULL,"窗体创建失败.","错误",MB_OK|MB_ICONEXCLAMATION); return FALSE; // Return FALSE } static PIXELFORMATDESCRIPTOR pfd= // 象素格式 { sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor 1, // Version Number PFD_DRAW_TO_WINDOW | // Format Must Support Window PFD_SUPPORT_OPENGL | // 支持OpenGL PFD_DOUBLEBUFFER, // 双缓冲 PFD_TYPE_RGBA, // Request An RGBA Format bits, // 色彩深度 0, 0, 0, 0, 0, 0, // Color Bits Ignored 0, // No Alpha Buffer 0, // Shift Bit Ignored 0, // 聚集缓存 0, 0, 0, 0, // Accumulation Bits Ignored 16, // 16Bit Z-Buffer (Depth Buffer) 0, // 模板缓存 0, // 辅助缓存 PFD_MAIN_PLANE, // 主绘图层 0, // Reserved 0, 0, 0 // 层遮罩 }; if (!(hDC=GetDC(hWnd))) // 取得OpenGL设备描述表 { KillGLWindow(); // Reset The Display MessageBox(NULL,"无法取得设备描述表.","错误",MB_OK|MB_ICONEXCLAMATION); return FALSE; // Return FALSE } if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) // 象素格式 { KillGLWindow(); // Reset The Display MessageBox(NULL,"无法获得象素格式.","错误",MB_OK|MB_ICONEXCLAMATION); return FALSE; // Return FALSE } if(!SetPixelFormat(hDC,PixelFormat,&pfd)) // 设置象素格式 { KillGLWindow(); // Reset The Display MessageBox(NULL,"无法设置象素格式.","错误",MB_OK|MB_ICONEXCLAMATION); return FALSE; // Return FALSE } if (!(hRC=wglCreateContext(hDC))) // 着色描述表 { KillGLWindow(); // Reset The Display MessageBox(NULL,"无法取得着色描述表.","错误",MB_OK|MB_ICONEXCLAMATION); return FALSE; // Return FALSE } if(!wglMakeCurrent(hDC,hRC)) // 激活着色描述表 { KillGLWindow(); // Reset The Display MessageBox(NULL,"无法激活着色描述表.","错误",MB_OK|MB_ICONEXCLAMATION); return FALSE; // Return FALSE } ShowWindow(hWnd,SW_SHOW); // 显示窗口 SetForegroundWindow(hWnd); // Slightly Higher Priority SetFocus(hWnd); // 键盘焦点 ReSizeGLScene(width, height); // Set Up Our Perspective GL Screen if (!InitGL()) // 初始化 { KillGLWindow(); // Reset The Display MessageBox(NULL,"初始化失败.","错误",MB_OK|MB_ICONEXCLAMATION); return FALSE; // Return FALSE } return TRUE; // Success } LRESULT CALLBACK WndProc( // 窗口消息处理 HWND hWnd, // 句柄 UINT uMsg, // M消息 WPARAM wParam, // 附加的消息内容 LPARAM lParam) // 附加的消息内容 { switch (uMsg) // 检查Windows消息 { case WM_ACTIVATE: // 窗口激活消息 { if (!HIWORD(wParam)) // 最小化状态 { active=TRUE; // Program Is Active } else { active=FALSE; // Program Is No Longer Active } return 0; // Return To The Message Loop } case WM_SYSCOMMAND: // 系统命令 { switch (wParam) // Check System Calls { case SC_SCREENSAVE: // 屏幕保护 case SC_MONITORPOWER: // 节电模式 return 0; // Prevent From Happening } break; // Exit } case WM_CLOSE: // Close消息 { PostQuitMessage(0); // Send A Quit Message return 0; // Jump Back } case WM_KEYDOWN: // 按键消息 { keys[wParam] = TRUE; // If So, Mark It As TRUE return 0; // Jump Back } case WM_KEYUP: // 按键放开消息 { keys[wParam] = FALSE; // If So, Mark It As FALSE return 0; // Jump Back } case WM_SIZE: // Resize消息 { ReSizeGLScene(LOWORD(lParam),HIWORD(lParam)); // LoWord=Width, HiWord=Height return 0; // Jump Back } } // 无关的消息被传递给DefWindowProc 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,L"是否使用全屏?", L"启动全屏?",MB_YESNO|MB_ICONQUESTION)==IDNO) //{ fullscreen=FALSE; // Windowed Mode //} // Create Our OpenGL Window if (!CreateGLWindow("OpenGL 程序设计",640,480,16,fullscreen)) { return 0; // Quit If Window Was Not Created } while(!done) // 消息循环 { if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is There A Message Waiting? { if (msg.message==WM_QUIT) // Have We Received A Quit Message? { done=TRUE; // If So done=TRUE } else // If Not, Deal With Window Messages { TranslateMessage(&msg); // 翻译消息 DispatchMessage(&msg); // 发送消息 } } else // If There Are No Messages { // Draw The Scene. Watch For ESC Key And Quit Messages From DrawGLScene() if (active) // Program Active? { if (keys[VK_ESCAPE]) // Was ESC Pressed? { done=TRUE; // ESC Signalled A Quit } else // Not Time To Quit, Update Screen { DrawGLScene(); // 绘制场景 SwapBuffers(hDC); // 交换缓存 (双缓存) } } if (keys[VK_F1]) // Is F1 Being Pressed? { keys[VK_F1]=FALSE; // If So Make Key FALSE KillGLWindow(); // 销毁当前的窗口 fullscreen=!fullscreen; // 切换 全屏 / 窗口 模式 // 重建 OpenGL 窗口 if (!CreateGLWindow("OpenGL 程序设计",640,480,16,fullscreen)) { return 0; // Quit If Window Was Not Created } } } } // 关闭程序 KillGLWindow(); // Kill The Window return (msg.wParam); // Exit The Program } /* * This Code Was Created By Jeff Molofee 2000 * A HUGE Thanks To Fredric Echols For Cleaning Up * And Optimizing This Code, Making It More Flexible! * If You've Found This Code Useful, Please Let Me Know. * Visit My Site At nehe.gamedev.net */ #include <windows.h> // Windows的头文件 #include <gl/gl.h> // OpenGL32库的头文件 #include <gl/glu.h> // GLu32库的头文件 #include <gl/glaux.h> // GLaux库的头文件 HDC hDC=NULL; // Private GDI Device Context/设备描述表 HGLRC hRC=NULL; // Permanent Rendering Context/着色描述表 HWND hWnd=NULL; // Holds Our Window Handle/窗口指派的句柄 HINSTANCE hInstance; // Holds The Instance Of The Application/实例 bool keys[256]; // Array Used For The Keyboard Routine bool active=TRUE; // Window Active Flag Set To TRUE By Default bool fullscreen=TRUE; // Fullscreen Flag Set To Fullscreen Mode By Default LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window { if (height==0) // Prevent A Divide By Zero By { height=1; // Making Height Equal One } glViewport(0,0,width,height); // 重置当前的视口 glMatrixMode(GL_PROJECTION); // 投影矩阵 glLoadIdentity(); // Reset The Projection Matrix // Calculate The Aspect Ratio Of The Window gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f); glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix/模型观察矩阵 glLoadIdentity(); // Reset The Modelview Matrix } int InitGL(GLvoid) //初始化 { glShadeModel(GL_SMOOTH); // Enable Smooth Shading glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // 背景颜色 glClearDepth(1.0f); // 深度缓存 glEnable(GL_DEPTH_TEST); // Enables Depth Testing glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // 透视修正 return TRUE; // Initialization Went OK } int DrawGLScene(GLvoid) // 开始进行所有的绘制 { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // 清除屏幕和深度缓存 glLoadIdentity(); // 重置当前的模型观察矩阵 return TRUE; // Everything Went OK } GLvoid KillGLWindow(GLvoid) // 正常销毁窗口 { if (fullscreen) // Are We In Fullscreen Mode? { ChangeDisplaySettings(NULL,0); // If So Switch Back To The Desktop ShowCursor(TRUE); // Show Mouse Pointer } if (hRC) // Do We Have A Rendering Context? { if (!wglMakeCurrent(NULL,NULL)) // 释放RC { MessageBox(NULL,"无法释放着色描述表.","关闭失败",MB_OK | MB_ICONINFORMATION); } if (!wglDeleteContext(hRC)) // 删除RC { MessageBox(NULL,"无法删除着色描述表.","关闭失败",MB_OK | MB_ICONINFORMATION); } hRC=NULL; // Set RC To NULL } if (hDC && !ReleaseDC(hWnd,hDC)) // 释放DC { MessageBox(NULL,"无法释放设备描述表.","关闭失败",MB_OK | MB_ICONINFORMATION); hDC=NULL; // Set DC To NULL } if (hWnd && !DestroyWindow(hWnd)) // 销毁窗口 { MessageBox(NULL,"无法释放窗体.","关闭失败",MB_OK | MB_ICONINFORMATION); hWnd=NULL; // Set hWnd To NULL } if (!UnregisterClass("OpenGL",hInstance)) // Are We Able To Unregister Class { MessageBox(NULL,"无法注销类.","关闭失败",MB_OK | MB_ICONINFORMATION); hInstance=NULL; // Set hInstance To NULL } } /* This Code Creates Our OpenGL Window. Parameters Are: * * title - Title To Appear At The Top Of The Window * * width - Width Of The GL Window Or Fullscreen Mode * * height - Height Of The GL Window Or Fullscreen Mode * * bits - Number Of Bits To Use For Color (8/16/24/32) * * fullscreenflag - Use Fullscreen Mode (TRUE) Or Windowed Mode (FALSE) */ BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag) { GLuint PixelFormat; // 象素格式 WNDCLASS wc; // 窗口类 DWORD dwExStyle; // Window Extended Style DWORD dwStyle; // 窗口风格 RECT WindowRect; // Grabs Rectangle Upper Left / Lower Right Values WindowRect.left=(long)0; // Set Left Value To 0 WindowRect.right=(long)width; // Set Right Value To Requested Width WindowRect.top=(long)0; // Set Top Value To 0 WindowRect.bottom=(long)height; // Set Bottom Value To Requested Height fullscreen=fullscreenflag; // Set The Global Fullscreen Flag hInstance = GetModuleHandle(NULL); // 取得窗口实例 wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // 移动时重画,并创建私有DC wc.lpfnWndProc = (WNDPROC) WndProc; // WndProc 消息处理 wc.cbClsExtra = 0; // No Extra Window Data wc.cbWndExtra = 0; // No Extra Window Data 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; // Return FALSE } if (fullscreen) // 全屏模式 { DEVMODE dmScreenSettings; // Device Mode memset(&dmScreenSettings,0,sizeof(dmScreenSettings)); // Makes Sure Memory's Cleared dmScreenSettings.dmSize=sizeof(dmScreenSettings); // Size Of The Devmode Structure dmScreenSettings.dmPelsWidth = width; // Selected Screen Width dmScreenSettings.dmPelsHeight = height; // Selected Screen Height dmScreenSettings.dmBitsPerPel = bits; // Selected Bits Per Pixel dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT; // Try To Set Selected Mode And Get Results. NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar. if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL) { // If The Mode Fails, Offer Two Options. Quit Or Use Windowed Mode. if (MessageBox(NULL,"无法启用全屏模式,是否使用窗体模式?","警告",MB_YESNO|MB_ICONEXCLAMATION)==IDYES) { fullscreen=FALSE; // Windowed Mode Selected. Fullscreen = FALSE } else { // Pop Up A Message Box Letting User Know The Program Is Closing. MessageBox(NULL,"程序遇到问题需要关闭.","错误",MB_OK|MB_ICONSTOP); return FALSE; // Return FALSE } } } if (fullscreen) // 全屏模式 { dwExStyle=WS_EX_APPWINDOW; // Window Extended Style dwStyle=WS_POPUP; // Windows Style ShowCursor(FALSE); // 隐藏鼠标指针 } else { dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended Style dwStyle=WS_OVERLAPPEDWINDOW; // Windows Style } AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle); // Adjust Window To True Requested Size // Create The Window if (!(hWnd=CreateWindowEx( dwExStyle, // Extended Style For The Window "OpenGL", // 类名称 title, // 窗口标题 dwStyle | // Defined Window Style WS_CLIPSIBLINGS | // Required Window Style WS_CLIPCHILDREN, // Required Window Style 0, 0, // 窗口位置 WindowRect.right-WindowRect.left, // 宽 WindowRect.bottom-WindowRect.top, // 高 NULL, // 父窗口 NULL, // 菜单 hInstance, // 实例 NULL))) // Dont Pass Anything To WM_CREATE { KillGLWindow(); // Reset The Display MessageBox(NULL,"窗体创建失败.","错误",MB_OK|MB_ICONEXCLAMATION); return FALSE; // Return FALSE } static PIXELFORMATDESCRIPTOR pfd= // 象素格式 { sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor 1, // Version Number PFD_DRAW_TO_WINDOW | // Format Must Support Window PFD_SUPPORT_OPENGL | // 支持OpenGL PFD_DOUBLEBUFFER, // 双缓冲 PFD_TYPE_RGBA, // Request An RGBA Format bits, // 色彩深度 0, 0, 0, 0, 0, 0, // Color Bits Ignored 0, // No Alpha Buffer 0, // Shift Bit Ignored 0, // 聚集缓存 0, 0, 0, 0, // Accumulation Bits Ignored 16, // 16Bit Z-Buffer (Depth Buffer) 0, // 模板缓存 0, // 辅助缓存 PFD_MAIN_PLANE, // 主绘图层 0, // Reserved 0, 0, 0 // 层遮罩 }; if (!(hDC=GetDC(hWnd))) // 取得OpenGL设备描述表 { KillGLWindow(); // Reset The Display MessageBox(NULL,"无法取得设备描述表.","错误",MB_OK|MB_ICONEXCLAMATION); return FALSE; // Return FALSE } if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) // 象素格式 { KillGLWindow(); // Reset The Display MessageBox(NULL,"无法获得象素格式.","错误",MB_OK|MB_ICONEXCLAMATION); return FALSE; // Return FALSE } if(!SetPixelFormat(hDC,PixelFormat,&pfd)) // 设置象素格式 { KillGLWindow(); // Reset The Display MessageBox(NULL,"无法设置象素格式.","错误",MB_OK|MB_ICONEXCLAMATION); return FALSE; // Return FALSE } if (!(hRC=wglCreateContext(hDC))) // 着色描述表 { KillGLWindow(); // Reset The Display MessageBox(NULL,"无法取得着色描述表.","错误",MB_OK|MB_ICONEXCLAMATION); return FALSE; // Return FALSE } if(!wglMakeCurrent(hDC,hRC)) // 激活着色描述表 { KillGLWindow(); // Reset The Display MessageBox(NULL,"无法激活着色描述表.","错误",MB_OK|MB_ICONEXCLAMATION); return FALSE; // Return FALSE } ShowWindow(hWnd,SW_SHOW); // 显示窗口 SetForegroundWindow(hWnd); // Slightly Higher Priority SetFocus(hWnd); // 键盘焦点 ReSizeGLScene(width, height); // Set Up Our Perspective GL Screen if (!InitGL()) // 初始化 { KillGLWindow(); // Reset The Display MessageBox(NULL,"初始化失败.","错误",MB_OK|MB_ICONEXCLAMATION); return FALSE; // Return FALSE } return TRUE; // Success } LRESULT CALLBACK WndProc( // 窗口消息处理 HWND hWnd, // 句柄 UINT uMsg, // M消息 WPARAM wParam, // 附加的消息内容 LPARAM lParam) // 附加的消息内容 { switch (uMsg) // 检查Windows消息 { case WM_ACTIVATE: // 窗口激活消息 { if (!HIWORD(wParam)) // 最小化状态 { active=TRUE; // Program Is Active } else { active=FALSE; // Program Is No Longer Active } return 0; // Return To The Message Loop } case WM_SYSCOMMAND: // 系统命令 { switch (wParam) // Check System Calls { case SC_SCREENSAVE: // 屏幕保护 case SC_MONITORPOWER: // 节电模式 return 0; // Prevent From Happening } break; // Exit } case WM_CLOSE: // Close消息 { PostQuitMessage(0); // Send A Quit Message return 0; // Jump Back } case WM_KEYDOWN: // 按键消息 { keys[wParam] = TRUE; // If So, Mark It As TRUE return 0; // Jump Back } case WM_KEYUP: // 按键放开消息 { keys[wParam] = FALSE; // If So, Mark It As FALSE return 0; // Jump Back } case WM_SIZE: // Resize消息 { ReSizeGLScene(LOWORD(lParam),HIWORD(lParam)); // LoWord=Width, HiWord=Height return 0; // Jump Back } } // 无关的消息被传递给DefWindowProc 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,L"是否使用全屏?", L"启动全屏?",MB_YESNO|MB_ICONQUESTION)==IDNO) //{ fullscreen=FALSE; // Windowed Mode //} // Create Our OpenGL Window if (!CreateGLWindow("OpenGL 程序设计",640,480,16,fullscreen)) { return 0; // Quit If Window Was Not Created } while(!done) // 消息循环 { if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is There A Message Waiting? { if (msg.message==WM_QUIT) // Have We Received A Quit Message? { done=TRUE; // If So done=TRUE } else // If Not, Deal With Window Messages { TranslateMessage(&msg); // 翻译消息 DispatchMessage(&msg); // 发送消息 } } else // If There Are No Messages { // Draw The Scene. Watch For ESC Key And Quit Messages From DrawGLScene() if (active) // Program Active? { if (keys[VK_ESCAPE]) // Was ESC Pressed? { done=TRUE; // ESC Signalled A Quit } else // Not Time To Quit, Update Screen { DrawGLScene(); // 绘制场景 SwapBuffers(hDC); // 交换缓存 (双缓存) } } if (keys[VK_F1]) // Is F1 Being Pressed? { keys[VK_F1]=FALSE; // If So Make Key FALSE KillGLWindow(); // 销毁当前的窗口 fullscreen=!fullscreen; // 切换 全屏 / 窗口 模式 // 重建 OpenGL 窗口 if (!CreateGLWindow("OpenGL 程序设计",640,480,16,fullscreen)) { return 0; // Quit If Window Was Not Created } } } } // 关闭程序 KillGLWindow(); // Kill The Window return (msg.wParam); // Exit The Program }