1 void RegisterWinDowClass(HINSTANCE hInstance,std::string className,WNDPROC proc) 2 { 3 WNDCLASS wndClass; 4 wndClass.cbClsExtra = 0; 5 wndClass.cbWndExtra = 0; 6 wndClass.hbrBackground = (HBRUSH)::GetStockObject(GRAY_BRUSH); 7 wndClass.hCursor = ::LoadCursor(NULL,IDC_ARROW); 8 wndClass.hIcon = ::LoadIcon(NULL,IDI_APPLICATION); 9 wndClass.hInstance = hInstance; 10 wndClass.lpfnWndProc = proc; 11 wndClass.lpszClassName = className.c_str(); 12 wndClass.lpszMenuName = NULL; 13 wndClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS; 14 ::RegisterClass(&wndClass); 15 }
1 bool InitGlewLib(HINSTANCE hInstance) 2 { 3 RegisterWinDowClass(hInstance,"Fake",MainWinProc); 4 5 HWND hWnd = ::CreateWindow("Fake","OpenGL",WS_OVERLAPPEDWINDOW, 6 CW_USEDEFAULT,CW_USEDEFAULT,width,heigtht, 7 NULL,NULL,hInstance,NULL); 8 9 HDC hDC = ::GetDC(hWnd); 10 HGLRC hRc; 11 ::PIXELFORMATDESCRIPTOR pfd; 12 //memset(&pfd,0,sizeof(pfd)); 13 14 //pfd.nVersion = 1; 15 //pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR); 16 //pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW; 17 //pfd.iPixelType = PFD_TYPE_RGBA; 18 //pfd.cColorBits = 32; 19 //pfd.cDepthBits = 32; 20 //pfd.iLayerType = PFD_MAIN_PLANE; 21 22 //int iPixelFormat = ::ChoosePixelFormat(hDC,&pfd); 23 //if (iPixelFormat == 0) 24 //{ 25 // DestroyWindow(hWnd); 26 // return false; 27 //} 28 /************************************************************************** 29 30 1,注册fake窗口的class,如果和主窗口是同名的class,会使初始化失败 31 2,选择1号格式索引作为临时索引 32 3,根据设备上下文创建渲染上下文 33 4,绑定到当前上下文 34 5,初始化glew 35 6,解除上下文绑定,销毁窗口 36 **************************************************************************/ 37 38 int iPixelFormat = 1; 39 if (::SetPixelFormat(hDC,iPixelFormat,&pfd)) 40 { 41 hRc = ::wglCreateContext(hDC); 42 if (::wglMakeCurrent(hDC,hRc)) 43 { 44 GLenum retVal = glewInit(); 45 wglMakeCurrent(NULL,NULL); 46 wglDeleteContext(hRc); 47 DestroyWindow(hWnd); 48 return retVal == GLEW_OK; 49 } 50 wglDeleteContext(hRc); 51 DestroyWindow(hWnd); 52 } 53 else 54 { 55 DestroyWindow(hWnd); 56 } 57 58 return false; 59 }
1 bool InitOpenGL(HWND hWnd,HINSTANCE hInstance) 2 { 3 if (!InitGlewLib(hInstance)) 4 { 5 ::MessageBox(NULL,"glew init Error",NULL,MB_OK); 6 return 0; 7 } 8 9 if(!WGLEW_ARB_create_context || !WGLEW_ARB_pixel_format) 10 return false; 11 HDC hDC = ::GetDC(hWnd); 12 PIXELFORMATDESCRIPTOR pfd; 13 //memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR)); 14 //pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR); 15 //pfd.nVersion = 1; 16 //pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW; 17 //pfd.iPixelType = PFD_TYPE_RGBA; 18 //pfd.cColorBits = 32; 19 //pfd.cDepthBits = 32; 20 //pfd.iLayerType = PFD_MAIN_PLANE; 21 22 /************************************************************************** 23 24 1,填充像素格式属性 (缓冲区的初始化) 25 2,填充上下文属性 (用于设置指定OpenGL版本) 26 3,根据像素格式属性选择设备上下文合适像素格式索引。wglChoosePixelFormatARB(...) 27 4,将得到的像素格式索引绑定到设备上下文。SetPixelFormat(...) 28 5,根据设备上下文,及上下文属性设置创建一个OpenGL渲染上下文。wglCreateContextAttribsARB(...) 29 6,将渲染上下文与设备上下文绑定 wglMakeCurrent(...) 30 31 **************************************************************************/ 32 33 const int iPixelFormatAttributeList[] = 34 { 35 WGL_DRAW_TO_WINDOW_ARB, GL_TRUE, // 绘制到窗口 36 WGL_SUPPORT_OPENGL_ARB,GL_TRUE, // 支持OpenGL 37 WGL_ACCELERATION_ARB ,WGL_FULL_ACCELERATION_ARB , // 硬件加速 38 WGL_DOUBLE_BUFFER_ARB,GL_TRUE, // 双缓冲 39 WGL_PIXEL_TYPE_ARB,WGL_TYPE_RGBA_ARB, // RGBA 40 WGL_COLOR_BITS_ARB,32, // 颜色位数32 41 WGL_DEPTH_BITS_ARB,24, // 深度位数24 42 WGL_STENCIL_BITS_ARB,8, // 模板位数8 43 WGL_SWAP_METHOD_ARB, WGL_SWAP_EXCHANGE_ARB, // 双缓冲swap方式直接交换 44 WGL_SAMPLES_ARB, 4, // 4倍抗锯齿 45 0 46 }; 47 48 const int iContextAttributeList[] = 49 { 50 WGL_CONTEXT_MAJOR_VERSION_ARB,3, // 主版本号 51 WGL_CONTEXT_MINOR_VERSION_ARB,3, // 次版本号 52 WGL_CONTEXT_FLAGS_ARB,WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, 53 0 54 }; 55 56 int iPixelFormat,iNumFormat; 57 wglChoosePixelFormatARB(hDC,iPixelFormatAttributeList,NULL, 58 1,&iPixelFormat,(UINT *)&iNumFormat); 59 60 if (!SetPixelFormat(hDC,iPixelFormat,&pfd)) 61 { 62 //int error = ::GetLastError(); 63 return false; 64 } 65 HGLRC hRC = wglCreateContextAttribsARB(hDC,NULL,iContextAttributeList); 66 if (hRC) 67 { 68 ::wglMakeCurrent(hDC,hRC); 69 return true; 70 } 71 return false; 72 }