SetPixelFormat 出错,请帮忙?

feng007lhf 2009-09-27 05:40:06
各位大侠,我使用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都搭建不起来, 有知道的,帮帮我啊,谢谢啦。

...全文
970 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
hxl6983 2011-07-26
  • 打赏
  • 举报
回复
还是楼主靠的是自己呀
xxxxxzelda 2009-11-21
  • 打赏
  • 举报
回复
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
16, // 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, // 32-bit z-buffer
0, // no stencil buffer
0, // no auxiliary buffer
PFD_MAIN_PLANE, // main layer
0, // reserved
0, 0, 0 // layer masks ignored
};
feng007lhf 2009-09-29
  • 打赏
  • 举报
回复
问题找到了,原来是我参数设置的不对,具体设置如下:

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

pix.dwFlags |= PFD_DRAW_TO_BITMAP;
pix.dwFlags |= PFD_DRAW_TO_WINDOW;
pix.dwFlags |= PFD_SUPPORT_GDI;
pix.iPixelType = PFD_TYPE_RGBA;
pix.cColorBits = 32;
pix.cRedBits = 8;
pix.cGreenBits = 8;
pix.cBlueBits = 8;
pix.cAlphaBits = 0;
pix.cAccumBits = 64;
pix.cAccumRedBits = 16;
pix.cAccumGreenBits = 16;
pix.cAccumBlueBits = 16;
pix.cAccumAlphaBits = 0;
pix.cDepthBits = 16;
pix.cStencilBits = 8;
pix.cAuxBuffers = 0;
//pix.iLayerType = 0;//(byte)PFD_MAIN_PLANE;
pix.bReserved = 0; // Reserved
pix.dwLayerMask = 0; // Layer Masks Ignored
pix.dwVisibleMask = 0;
pix.dwDamageMask = 0;


不过我还有一个疑问,既然是渲染到bitmap, 为什么还有设置PFD_DRAW_TO_WINDOW ?
ncjcz 2009-09-29
  • 打赏
  • 举报
回复
能弄出来就好了
gisyellow 2009-09-29
  • 打赏
  • 举报
回复
渲染到图片,也是在窗口中绘制啊。。
feng007lhf 2009-09-28
  • 打赏
  • 举报
回复
SetPixelForma() 返回false, 使用GetLastError()返回1, 表示函数不正确,应该如何解决啊
happyboyxq1985 2009-09-28
  • 打赏
  • 举报
回复
帮你顶一下
feng007lhf 2009-09-28
  • 打赏
  • 举报
回复
没有人知道吗,
feng007lhf 2009-09-27
  • 打赏
  • 举报
回复
自己顶一下

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧