您的位置:首页 > 运维架构

在多文档应用程序中使用OpenGL绘图

2012-01-20 15:59 381 查看
 
1、在视图类的头文件中添加两个成员变量:

[code]     [code] public:


int m_GLPixelIndex;


HGLRC m_hGLContext;

[/code]
[/code]

2、在视图类的构造函数中对这两个成员进行初始化:

[code]
[code] m_GLPixelIndex = 0;


m_hGLContext = NULL;

[/code]
[/code]

3、在视图类声明中添加两个成员函数:

[code]
[code] BOOL SetWindowPixelFormat(HDC hDC);


BOOL CreateViewGLContext(HDC hDC);

[/code]
[/code]

4、在视图类的实现文件中添加这两个成员函数的实现:

[code]
[code] BOOL CXXXView::CreateViewGLContext(HDC hDC)


{


m_hGLContext = wglCreateContext(hDC);


 


if(m_hGLContext==NULL)


return FALSE;


 


if(wglMakeCurrent(hDC,m_hGLContext)==FALSE)


return FALSE;


 


return TRUE;


}

[/code]
[/code]

以及

[code]
[code] BOOL CXXXView::SetWindowPixelFormat(HDC hDC)


{


PIXELFORMATDESCRIPTOR pixelDesc;


 


pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR);


pixelDesc.nVersion = 1;


 


pixelDesc.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |


PFD_DOUBLEBUFFER | PFD_STEREO_DONTCARE;


 


pixelDesc.iPixelType = PFD_TYPE_RGBA;


pixelDesc.cColorBits = 32;


pixelDesc.cRedBits = 8;


pixelDesc.cRedShift = 16;


pixelDesc.cGreenBits = 8;


pixelDesc.cGreenShift = 8;


pixelDesc.cBlueBits = 8;


pixelDesc.cBlueShift = 0;


pixelDesc.cAlphaBits = 0;


pixelDesc.cAlphaShift = 0;


pixelDesc.cAccumBits = 64;


pixelDesc.cAccumRedBits = 16;


pixelDesc.cAccumGreenBits = 16;


pixelDesc.cAccumBlueBits = 16;


pixelDesc.cAccumAlphaBits = 0;


pixelDesc.cDepthBits = 32;


pixelDesc.cStencilBits = 8;


pixelDesc.cAuxBuffers = 0;


pixelDesc.iLayerType = PFD_MAIN_PLANE;


pixelDesc.bReserved = 0;


pixelDesc.dwLayerMask = 0;


pixelDesc.dwVisibleMask = 0;


pixelDesc.dwDamageMask = 0;


 


m_GLPixelIndex = ChoosePixelFormat(hDC,&pixelDesc);


if(m_GLPixelIndex == 0) // Choose default


  {


m_GLPixelIndex = 1;


if(DescribePixelFormat(hDC,m_GLPixelIndex,


sizeof(PIXELFORMATDESCRIPTOR),&pixelDesc)==0)


    return FALSE;


  }


 


if(!SetPixelFormat(hDC,m_GLPixelIndex,&pixelDesc))


return FALSE;


 


return TRUE;


}

[/code]
[/code]

在视图类的OnCreate成员函数中添加:

[code]
[code] HWND hWnd = GetSafeHwnd();


HDC hDC = ::GetDC(hWnd);


 


if(SetWindowPixelFormat(hDC)==FALSE)


return -1;


 


if(CreateViewGLContext(hDC)==FALSE)


return -1;

[/code]
[/code]

添加OnPaint消息响应函数,在函数中加入:

[code]
[code] HWND hWnd = GetSafeHwnd();


HDC hDC = ::GetDC(hWnd);


wglMakeCurrent(dc.m_ps.hdc,m_hGLContext);


 


....


 


// Double buffer


SwapBuffers(dc.m_ps.hdc);


glFlush();


 


// release


::ReleaseDC(hWnd,hDC);

[/code]
[/code]

添加OnSize消息响应函数。这里的内容可以根据需要进行修改:

[code]
[code] glViewport(0,0,(GLsizei)cx,(GLsizei)cy);


glMatrixMode(GL_PROJECTION);


glLoadIdentity();


gluPerspective(60.0,(GLfloat)cx/(GLfloat)cy,0.1,100.0);


glMatrixMode(GL_MODELVIEW);


glLoadIdentity();

[/code]
[/code]

添加虚函数OnActivateView,在其中添加:

[code]
[code] HWND hWnd = GetSafeHwnd();


HDC hDC = ::GetDC(hWnd);


wglMakeCurrent(hDC,m_hGLContext);


::ReleaseDC(hWnd,hDC);

[/code]
[/code]

添加WM_DESTROY消息响应函数,在其中添加:

[code]
[code] if(wglGetCurrentContext() != NULL)


wglMakeCurrent(NULL,NULL);


 


if(m_hGLContext != NULL)


{


wglDeleteContext(m_hGLContext);


    m_hGLContext = NULL;


}

[/code]
[/code]

Over。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐