Exemple #1
0
static bool gl_init_extensions(HDC hdc)
{
	if (!gladLoadWGL(hdc)) {
		blog(LOG_ERROR, "Failed to load WGL entry functions.");
		return false;
	}

	if (!GLAD_WGL_ARB_pixel_format) {
		required_extension_error("ARB_pixel_format");
		return false;
	}

	if (!GLAD_WGL_ARB_create_context) {
		required_extension_error("ARB_create_context");
		return false;
	}

	if (!GLAD_WGL_ARB_create_context_profile) {
		required_extension_error("ARB_create_context_profile");
		return false;
	}

	return true;
}
	void OpenGLWindow::Impl::createContext()
	{
		int pixel_format;
		HGLRC tmp_context;

		PIXELFORMATDESCRIPTOR descriptor;
		memset(&descriptor, 0, sizeof(PIXELFORMATDESCRIPTOR));
		descriptor.nSize = sizeof(PIXELFORMATDESCRIPTOR);
		descriptor.nVersion = 1;

		//TODO: Make these parameters custom
		descriptor.cColorBits = 32;
		descriptor.cAlphaBits = 8; // 8 if 32-bits color
		descriptor.cDepthBits = 24;
		descriptor.cStencilBits = 0;
		descriptor.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
		descriptor.iPixelType = PFD_TYPE_RGBA;

		m_context.hdc = GetDC(m_handle);

		if (!m_context.hdc)
			ATEMA_ERROR("Invalid device context.");

		pixel_format = ChoosePixelFormat(m_context.hdc, &descriptor);

		if (!pixel_format)
			ATEMA_ERROR("Pixel format choice failed.");

		if (!SetPixelFormat(m_context.hdc, pixel_format, &descriptor))
			ATEMA_ERROR("Pixel format choice failed.");

		tmp_context = wglCreateContext(m_context.hdc);
		wglMakeCurrent(m_context.hdc, tmp_context);

		if (!gladLoadWGL(m_context.hdc))
			ATEMA_ERROR("Loading of WGL extensions failed.");

		wglMakeCurrent(nullptr, nullptr);
		wglDeleteContext(tmp_context);

		//Update of context settings about pixel format (needed?)
		/*
		if (DescribePixelFormat(m_context.hdc, pixel_format, sizeof(PIXELFORMATDESCRIPTOR), &descriptor) != 0)
		{
		settings.bitsPerPixel = descriptor.cColorBits + descriptor.cAlphaBits;
		settings.depthBits = descriptor.cDepthBits;
		settings.stencilBits = descriptor.cDepthBits;
		}//*/

		HGLRC targetContext = nullptr; // Context to share

		if (wglCreateContextAttribsARB)
		{
			// Atema needs at least OpenGL 3.3
			int attributes[] =
			{
				WGL_CONTEXT_MAJOR_VERSION_ARB,	3,
				WGL_CONTEXT_MINOR_VERSION_ARB,	3,
				WGL_CONTEXT_PROFILE_MASK_ARB,	WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
				0 // End of attributes
			};

			m_context.hglrc = wglCreateContextAttribsARB(m_context.hdc, targetContext, attributes);
		}

		// The creation failed. We try another method
		if (!m_context.hglrc)
		{
			m_context.hglrc = wglCreateContext(m_context.hdc);

			if (targetContext)
			{
				static std::mutex mutex;
				std::lock_guard<std::mutex> locker(mutex);

				if (!wglShareLists(targetContext, m_context.hglrc))
					ATEMA_ERROR("Sharing OpenGL context failed.");
			}
		}

		if (!m_context.hglrc)
			ATEMA_ERROR("OpenGL context creation failed.");

		wglMakeCurrent(m_context.hdc, m_context.hglrc);
	}