opengl的配置

本文详细介绍如何在Windows环境下使用OpenGL进行图形渲染,包括设置像素格式、创建渲染上下文、初始化OpenGL库及视窗调整等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 应当包含opengl的头文件#include <glut.h>,有时候会通不过编译,没有识别XXX,这时要加上宏#pragma comment(lib,“glut.lib”)
  2. 在CxxView类中进行修改;
    • 在PreCreateWindow中添加cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN;原因是:http://apps.hi.baidu.com/share/detail/15392609
    • 在头文件中添加成员变量 HGLRC m_hRC;CDC* m_pDC;。其中m_hRC是RenderContext的句柄(着色表述表)。每一个OpenGL都被连接到一个着色描述表上。着色描述表将所有的OpenGL调用的命令连接到Device Context(设备描述表windows)上。而 m_pDC则是一个指向CDC类的指针,其中包含着HDC句柄。
    • 添加一些函数初始化以上两个变量。
      • 初始化m_pDC 用 CClientDC(this);
      • 设置像素格式。像素格式结构PIXELFORMATDESCRIPTOR,用于选择颜色,alpha通道,双缓冲格式等。定义完像素格式后PixelFormat=ChoosePixelFormat(hDC,&pfd)其中PixelFormat为适合的像素格式索引,pfd则是我们自己定义的像素格式,windows会帮我们选择最适合的像素格式。然后调用SetPixelFormat(hDC,PixelFormat,&pfd)设置像素格式。其中hDC为设备上下文的句柄。
      • 初始化m_hDC,用wglCreateContext与wglMakeCurrent函数。其中wglCreatContext用于创建rendering context(The wglCreateContext function creates a new OpenGL rendering context, which is suitable for drawing on the device referenced by hdc. The rendering context has the same pixel format as the device context.)。wglMakeCurrent函数用于指定一个当前的rendering context用于渲染。
      • 代码如下:
        BOOL COpenGL01View::SetupPixelFormat()
        {
        	GLuint		PixelFormat;
        	static PIXELFORMATDESCRIPTOR pfd = 
            {
                sizeof(PIXELFORMATDESCRIPTOR),  // size of this pfd
                1,                              // version number
                PFD_DRAW_TO_WINDOW |            // support window
                PFD_SUPPORT_OPENGL |            // support OpenGL
                PFD_DOUBLEBUFFER,                // double buffered
                PFD_TYPE_RGBA,                  // RGBA type
                24,                             // 24-bit color depth
                0, 0, 0, 0, 0, 0,               // color bits ignored
                0,                              // no alpha buffer
                0,                              // shift bit ignored
                0,                              // no accumulation buffer
                0, 0, 0, 0,                     // accum bits ignored
                16,                             // 16-bit z-buffer
                0,                              // no stencil buffer
                0,                              // no auxiliary buffer
                PFD_MAIN_PLANE,                 // main layer
                0,                              // reserved
                0, 0, 0                         // layer masks ignored
            };
        	if(!(PixelFormat = ChoosePixelFormat(m_pDC->GetSafeHdc(),&pfd)))
        	{
        		MessageBox(TEXT("不能设置像素格式"),TEXT("错误"),MB_OK|MB_ICONEXCLAMATION);
        		return FALSE;	
        	}
        	if(!SetPixelFormat(m_pDC->GetSafeHdc(),PixelFormat,&pfd))
        	{
        		MessageBox(TEXT("不能设置像素格式"),TEXT("错误"),MB_OK|MB_ICONEXCLAMATION);
        		return FALSE;	
        	}
        	return TRUE;
        }
        BOOL COpenGL01View::CreateOpenGLRC()
        {
        	m_pDC = new CClientDC(this);
        
        	if(m_pDC == NULL)
        	{
        		MessageBox(TEXT("Error Obtaining DC"));
        		return FALSE;
        	}
        	if(!SetupPixelFormat())
        		return FALSE;
        	if(!(m_hRC = ::wglCreateContext(m_pDC->GetSafeHdc())))
        	{
        		MessageBox(TEXT("不能创建OpenGL渲染描述表"),TEXT("错误"),MB_OK|MB_ICONEXCLAMATION);
        		return FALSE;	
        	}
        
        	if(!wglMakeCurrent(m_pDC->GetSafeHdc(),m_hRC))						// 尝试激活着色描述表
        	{
        		MessageBox(TEXT("不能激活当前的OpenGL渲然描述表"),TEXT("错误"),MB_OK|MB_ICONEXCLAMATION);
        		return FALSE;							// 返回 FALSE
        	}
        
        	return TRUE;
        }

  3. 初始化OpenGL:
    BOOL COpenGL01View::InitGL()
    {
    	glShadeModel(GL_SMOOTH);
    	glClearColor(0.0f,0.0f,0.0f,0.0f);
    
    	glClearDepth(1.0f);
    	glEnable(GL_DEPTH_TEST);
    	glDepthFunc(GL_LEQUAL);
    
    	glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
    
    	return TRUE;
    }
    
  4. 之后就可以在rc上渲染内容了,并在ondraw中调用
  5. 当窗口大小改变时,需要改变opengl的映射关系。
    void COpenGL01View::OnSize(UINT nType, int cx, int cy)
    {
    	CView::OnSize(nType, cx, cy);
    
    	// TODO: Add your message handler code here
    	GLdouble aspect_ratio; // width/height ratio
        
        if ( 0 >= cx || 0 >= cy )
        {
            return;
        }
        // select the full client area
        ::glViewport(0, 0, cx, cy);
        // compute the aspect ratio
        // this will keep all dimension scales equal
        aspect_ratio = (GLdouble)cx/(GLdouble)cy;
        // select the projection matrix and clear it
        ::glMatrixMode(GL_PROJECTION);
        ::glLoadIdentity();
        // select the viewing volume
        ::gluPerspective(45.0f, aspect_ratio, .01f, 200.0f);
        
        // switch back to the modelview matrix and clear it
        ::glMatrixMode(GL_MODELVIEW);
        ::glLoadIdentity();
    }

  6. 当窗口销毁时需要释放资源
    void COpenGL01View::OnDestroy()
    {
    	CView::OnDestroy();
    
    	// TODO: Add your message handler code here
    	if(m_hRC)
    	{
    		if(!wglMakeCurrent(NULL,NULL))
    			MessageBox(TEXT("断开 DC与RC关系失败 "),TEXT("关闭错误"),MB_OK | MB_ICONINFORMATION);
    		if(!wglDeleteContext(m_hRC))
    			MessageBox(TEXT("释放RC失败。"),TEXT("关闭错误"),MB_OK | MB_ICONINFORMATION);
    		m_hRC = NULL;
    	}
    
    	if(m_pDC && !ReleaseDC(m_pDC))
    	{
    		MessageBox(TEXT("释放DC失败。"),TEXT("关闭错误"),MB_OK | MB_ICONINFORMATION);
    		delete m_pDC;
    		m_pDC = NULL;
    	}
        //Set it to NULL
        m_pDC = NULL;
    }
  7. 试试改变窗口的大小,你会看到很严重的闪烁,并且关闭程序后会报告内存泄露,因此我们这就来解决这两个问题吧。发生闪烁的原因是Windows先绘制背景,然后再是OpenGL绘制,因为我们已经让OpenGL负责清空背景色,因此我们不需要Windows去清空背景了

    BOOL COpenGL01View::OnEraseBkgnd(CDC* pDC)
    {
    	// TODO: Add your message handler code here and/or call default
    
    	return TRUE;
    }
    点击打开链接
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值