SetPixelFormat 出错,请帮忙?
各位大侠,我使用Tao opengl 开一个项目,其中要使用opengl的 off-screen(渲染到一张位图)渲染,但是我在搭建opengl context的时候遇到如题的错误, 即我不能设置像素格式,具体代码如下: 其中SetPixelFormat返回FALSE, 使用GetLastError返回1: 表示无效功能或者函数,但是我应该如何做哪?
System.Drawing.Image img = new System.Drawing.Bitmap(width, height);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(img);
CreateGLContext(width, height, 16);
//define CreateGLContext
private bool CreateGLContext (int width, int height, int bits)
{
int pixelFormat;
Gdi.PIXELFORMATDESCRIPTOR pfd = new Gdi.PIXELFORMATDESCRIPTOR ();
pfd.nSize = (short) Marshal.SizeOf ( pfd );
pfd.nVersion = 1;
pfd.dwFlags = Gdi.PFD_DRAW_TO_BITMAP |
Gdi.PFD_SUPPORT_OPENGL |
Gdi.PFD_SUPPORT_GDI;
pfd.iPixelType = (byte) Gdi.PFD_TYPE_RGBA;
pfd.cColorBits = (byte) bits;
pfd.cRedBits = 0;
pfd.cRedShift = 0;
pfd.cGreenBits = 0;
pfd.cGreenShift = 0;
pfd.cBlueBits = 0;
pfd.cBlueShift = 0;
pfd.cAlphaBits = 0;
pfd.cAlphaShift = 0;
pfd.cAccumBits = 0;
pfd.cAccumRedBits = 0;
pfd.cAccumGreenBits = 0;
pfd.cAccumBlueBits = 0;
pfd.cAccumAlphaBits = 0;
pfd.cDepthBits = 16;
pfd.cStencilBits = 0;
pfd.cAuxBuffers = 0;
pfd.iLayerType = (byte) Gdi.PFD_MAIN_PLANE;
pfd.bReserved = 0;
pfd.dwLayerMask = 0;
pfd.dwVisibleMask = 0;
pfd.dwDamageMask = 0;
if ( hDC == IntPtr.Zero )
{
KillGLContext ();
MessageBox.Show ( "Can't Create A GL Device Context.", "ERROR 1",
MessageBoxButtons.OK, MessageBoxIcon.Error );
return false;
}
pixelFormat = Gdi.ChoosePixelFormat ( hDC, ref pfd );
if ( pixelFormat == 0 )
{
KillGLContext ();
MessageBox.Show ( "Can't Find A Suitable PixelFormat.", "ERROR 2",
MessageBoxButtons.OK, MessageBoxIcon.Error );
return false;
}
try
{
if ( !Gdi.SetPixelFormat ( hDC, pixelFormat, ref pfd ) )
{
MessageBox.Show ( Gl.glGetError ().ToString () );
MessageBox.Show ( Marshal.GetLastWin32Error ().ToString () );
KillGLContext ();
MessageBox.Show ( "Can't Set The PixelFormat.", "ERROR 3",
MessageBoxButtons.OK, MessageBoxIcon.Error );
//return false;
}
}
catch ( Exception e )
{
MessageBox.Show ( e.Message );
}
hRC = Wgl.wglCreateContext ( hDC );
if ( hRC == IntPtr.Zero )
{
KillGLContext ();
MessageBox.Show ( "Can't Create A GL Rendering Context.", "ERROR 4",
MessageBoxButtons.OK, MessageBoxIcon.Error );
return false;
}
if ( !Wgl.wglMakeCurrent ( hDC, hRC ) )
{
KillGLContext ();
MessageBox.Show ( "Can't Activate The GL Rendering Context.", "ERROR 5",
MessageBoxButtons.OK, MessageBoxIcon.Error );
return false;
}
ReSizeGLScene ( width, height );
if ( !InitGL () )
{
KillGLContext ();
MessageBox.Show ( "Initialization Failed.", "ERROR 6",
MessageBoxButtons.OK, MessageBoxIcon.Error );
return false;
}
return true;
}
private bool InitGL ()
private void KillGLContext ()
但是我在CSGL中 使用如下代码是可以创建off-screen的渲染环境的,
System.Drawing.Image img = new System.Drawing.Bitmap((int)(pj.ScreenSize.Width), (int)(pj.ScreenSize.Height));
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(img);
GDIGLContext gdictxt = new GDIGLContext(g);//将opengl和Graphics绑定
gdictxt.Create(new DisplayType(DisplayFlags.DRAW_TO_BITMAP, true), null);//创建GDIGLContext容器
gdictxt.Grab();//使该context为当前激活的context.
但是由于我要是用VBO,而 CSGL不支持,或者不知能如何自己产生这些扩展,所以转向了Tao OpenGL, 不过现在我连他的render context都搭建不起来, 有知道的,帮帮我啊,谢谢啦。