bool GLRenderer::initialize(EGLNativeWindowType window) #endif { #ifndef IOS Log("Initializing OpenGL"); if(!InitEGL(window)) { Log("[GlesCube] GlesCube can run on systems which supports OpenGL ES(R) 2.0."); Log("[GlesCube] When GlesCube does not correctly execute, there are a few reasons."); Log("[GlesCube] 1. The current device(real-target or simulator) does not support OpenGL ES(R) 2.0. Check the Release Notes."); Log("[GlesCube] 2. The system running on simulator cannot support OpenGL(R) 2.1 or later. Try with other system."); Log("[GlesCube] 3. The system running on simulator does not maintain the latest graphics driver. Update the graphics driver."); DestroyGL(); return false; } #endif if(!InitGL()) { return false; } return true; }
// 创建opengl窗口 BOOL GLWindow::CreateGlWnd(const char* title, int x, int y, int width, int height) { m_width = width; m_height = height; RECT windowRect = { 0, 0, width, height }; DWORD windowStyle = WS_OVERLAPPEDWINDOW; // 预留做全屏 DWORD windowExStyle = WS_EX_APPWINDOW; // 扩展样式 // 全屏处理 - 预留 // 窗口样式 //windowStyle = WS_POPUP; //windowExStyle |= WS_EX_TOPMOST; ::AdjustWindowRectEx(&windowRect, windowStyle, 0, windowExStyle); // 调整窗口 CreateEx(0, "OpenGL", title, WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, x, y, windowRect.right - windowRect.left, windowRect.bottom - windowRect.top, NULL, NULL); if (!(m_hDc = GetDC(m_hWnd))) // 获取DC { DestroyGL(); // 销毁 return FALSE; // 不能获取DC } m_timerFrame = 1000; // 初始化 GLuint PixelFormat; static PIXELFORMATDESCRIPTOR pdf = { sizeof(PIXELFORMATDESCRIPTOR), // 结构体大小 1, // 版本号 PFD_DRAW_TO_WINDOW | // 支持窗口 PFD_SUPPORT_OPENGL | // 支持opengl PFD_DOUBLEBUFFER, // 支持双缓冲 PFD_TYPE_RGBA, // 申请RGBA格式 32, // 色彩深度 0, 0, 0, 0, 0, 0, // 忽略的色彩位 0, // 无alpha缓存 0, // 无shift Bit 0, // 无累加缓存 0, 0, 0, 0, // 忽略聚集位 24, // 16位Z-缓存 8, // 无蒙版缓存 0, // 无辅助缓存 PFD_MAIN_PLANE, // 主绘图层 0, // Reserved 0, 0, 0 // 忽略层遮罩 }; if (!(PixelFormat = ChoosePixelFormat(m_hDc, &pdf))) // 寻找相应像素格式 { DestroyGL(); // 销毁 // printf("1====error choose===="); return FALSE; } if (!SetPixelFormat(m_hDc, PixelFormat, &pdf)) { DestroyGL(); // printf("1====error choose===="); return FALSE; // 不能设置像素格式 } HGLRC tempContext; if (!(tempContext = wglCreateContext(m_hDc))) { DestroyGL(); // printf("2====error create context===="); return FALSE; // 不能获得着色描述表 } if (!wglMakeCurrent(m_hDc, tempContext)) // 开启低版本opengl { DestroyGL(); // printf("3========"); return FALSE; // 不能激活当前opengl渲染描述表 } if (GLEW_OK != glewInit()) { DestroyGL(); return FALSE; // glew初始化失败 } // 开启 opengl 4.3 支持 GLint attribs[] = { WGL_CONTEXT_MAJOR_VERSION_ARB, 4, // 主版本4 WGL_CONTEXT_MINOR_VERSION_ARB, 3, // 次版本号3 WGL_CONTEXT_PROFILE_MASK_ARB,WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,//要求返回兼容模式环境,如果不指定或指定为WGL_CONTEXT_CORE_PROFILE_BIT_ARB会返回只包含核心功能的环境 0 }; if (wglewIsSupported("WGL_ARB_create_context") == 1) { m_hRc = wglCreateContextAttribsARB(m_hDc, 0, attribs); wglMakeCurrent(NULL, NULL); wglDeleteContext(tempContext); int result = wglMakeCurrent(m_hDc, m_hRc); // 设置opengl 4.3 if (result != 1) { return FALSE; } } else { // 不支持opengl4.X 还原为opengl 2.1 m_hRc = tempContext; } //RECT rect; // 客户区大小 //::GetClientRect(m_hWnd, &rect); //ResizeGLScene(rect.right - rect.left, rect.bottom - rect.top); // 设置GL屏幕 (注意,这里只使用客户区计算) ResizeGLScene(width, height); if (!initGL()) // 初始化opengl { DestroyGL(); return FALSE; } // 设定计时器 每秒刷新60次 SetTimer(m_hWnd, m_timerFrame, 1000 / 60, NULL); return TRUE; }
LRESULT GLWindow::OnDestroy(WPARAM wParam, LPARAM lParam) { DestroyGL(); // 退出程序前销毁opengl PostQuitMessage(0); return 0; }
void GLRenderer::Cleanup() { DestroyGL(); }
GLRenderer::~GLRenderer() { DestroyGL(); }
bool GLRenderer::InitEGL(EGLNativeWindowType window) { EGLint numConfigs = 1; EGLint eglConfigList[] = { EGL_RED_SIZE, 5, EGL_GREEN_SIZE, 6, EGL_BLUE_SIZE, 5, EGL_ALPHA_SIZE, 0, EGL_DEPTH_SIZE, 8, EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_NONE }; EGLint eglContextList[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; eglBindAPI(EGL_OPENGL_ES_API); if(m_eglDisplay) { DestroyGL(); } m_eglDisplay = eglGetDisplay((EGLNativeDisplayType)EGL_DEFAULT_DISPLAY); if (EGL_NO_DISPLAY == m_eglDisplay) { Log("[GlesCube] eglGetDisplay() is failed. [0x%x]\n", eglGetError()); DestroyGL(); return false; } if (EGL_FALSE == eglInitialize(m_eglDisplay, null, null) || EGL_SUCCESS != eglGetError()) { Log("[GlesCube] eglInitialize() has been failed. [0x%x]\n", eglGetError()); DestroyGL(); return false; } if (EGL_FALSE == eglChooseConfig(m_eglDisplay, eglConfigList, &m_eglConfig, 1, &numConfigs) || EGL_SUCCESS != eglGetError()) { Log("[GlesCube] eglChooseConfig() has been failed. [0x%x]\n", eglGetError()); DestroyGL(); return false; } if (!numConfigs) { Log("[GlesCube] eglChooseConfig() has been failed. because of matching config doesn't exist \n"); DestroyGL(); return false; } m_eglSurface = eglCreateWindowSurface(m_eglDisplay, m_eglConfig, window, null); if (EGL_NO_SURFACE == m_eglSurface || EGL_SUCCESS != eglGetError()) { Log("[GlesCube] eglCreateWindowSurface() has been failed. EGL_NO_SURFACE [0x%x]\n", eglGetError()); DestroyGL(); return false; } m_eglContext = eglCreateContext(m_eglDisplay, m_eglConfig, EGL_NO_CONTEXT, eglContextList); if (EGL_NO_CONTEXT == m_eglContext || EGL_SUCCESS != eglGetError()) { Log("[GlesCube] eglCreateContext() has been failed. [0x%x]\n", eglGetError()); DestroyGL(); return false; } if (EGL_FALSE == eglMakeCurrent(m_eglDisplay, m_eglSurface, m_eglSurface, m_eglContext) || EGL_SUCCESS != eglGetError()) { Log("[GlesCube] eglMakeCurrent() has been failed. [0x%x]\n", eglGetError()); DestroyGL(); return false; } return true; }
void CIdleOpenGLWnd::OnDestroy() { DestroyGL(m_hrc); MSG_FORWARD_WM_DESTROY(COpenGLWnd); }