使用GLEW在win环境下创建支持高版本OpenGL 上下文

BOOL Cgl::CreateMultiSample(HWND hwnd, BOOL bCreateZBuffer)/*by ZhouZhuo 2014.06.23 凹凸纹理 启动多采样抗锯齿 */
{
	m_hWnd =hwnd; //this->m_hWnd;
	m_hDC = ::GetDC(hwnd);
	DWORD depth_flag = PFD_DEPTH_DONTCARE;
	PIXELFORMATDESCRIPTOR pfd;
	if (bCreateZBuffer) depth_flag = 0;  //PFD_MAIN_PLANE
	memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
	pfd.nSize  = sizeof(PIXELFORMATDESCRIPTOR);
	pfd.nVersion   = 1;
	pfd.dwFlags    = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_SWAP_EXCHANGE | depth_flag;
	pfd.iPixelType = PFD_TYPE_RGBA;
	pfd.cColorBits = (BYTE)GetDeviceCaps(m_hDC, BITSPIXEL);
	pfd.cDepthBits = 24;//这里大多数机器只支持24
	pfd.iLayerType = PFD_MAIN_PLANE;
	
	int nPixelFormat = ChoosePixelFormat(m_hDC, &pfd);
	if (nPixelFormat == 0) return false;
	nPixelFormat = 56 ;//采用作弊办法获得了16采样下的值,
	BOOL bResult = SetPixelFormat (m_hDC, nPixelFormat, &pfd);

	if (!bResult) return false; 

	HGLRC tempContext = wglCreateContext(m_hDC);
	wglMakeCurrent(m_hDC, tempContext);

	GLenum err = glewInit();
	if (GLEW_OK != err)
	{
		AfxMessageBox(_T("GLEW is not initialized!"));
	}

	int attribs[] =
	{
		WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
		WGL_CONTEXT_MINOR_VERSION_ARB, 1,
		WGL_CONTEXT_FLAGS_ARB, 0,
		0
	};

	if(wglewIsSupported("WGL_ARB_create_context") == 1)
	{
		m_hRC = wglCreateContextAttribsARB(m_hDC,0, attribs);
		wglMakeCurrent(NULL,NULL);
		wglDeleteContext(tempContext);
		wglMakeCurrent(m_hDC, m_hRC);
	}
	else
	{       //It's not possible to make a GL 3.x context. Use the old style context (GL 2.1 and before)
		m_hRC = tempContext;
	}

	//Checking GL version
	const GLubyte *GLVersionString = glGetString(GL_VERSION);

	//Or better yet, use the GL3 way to get the version number
	int OpenGLVersion[2];
	glGetIntegerv(GL_MAJOR_VERSION, &OpenGLVersion[0]);
	glGetIntegerv(GL_MINOR_VERSION, &OpenGLVersion[1]);

	if (!m_hRC) return false;

	glewInit();
	if (glewIsSupported("GL_VERSION_4_0"))
		printf("Ready for OpenGL 2.0\n");
	else {
		printf("OpenGL 2.0 not supported\n");
		//exit(1);
	}
	RECT r;
	::GetClientRect(hwnd, &r);

	m_Width  = r.right  - r.left;
	m_Height = r.bottom - r.top;

	glViewport(0, 0, m_Width, m_Height);
	glDrawBuffer(GL_BACK);
	InitializeTexture();
	wglMakeCurrent(NULL, NULL);
	m_bInitialized = TRUE;
	return TRUE;
	return true;
}


转载于:https://my.oschina.net/syhgzz/blog/283997

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值