使用OpenGL直接绘制一个.NET位图 [英] Using OpenGL to draw directly to a .NET Bitmap

查看:130
本文介绍了使用OpenGL直接绘制一个.NET位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用OpenGL直接绘制一个.NET System :: Drawing :: Bitmap(C ++ / CLI)。它似乎应该这样工作:

I'd like to use OpenGL to draw directly to a .NET System::Drawing::Bitmap (C++/CLI). It seems like it should work like this:

char buf[48];
ZeroMemory( &buf, sizeof(buf));
System::Drawing::Bitmap bmp(
    4,
    4,
    12,
    Imaging::PixelFormat::Format24bppRgb,
    (System::IntPtr)buf);
Graphics^ g = Graphics::FromImage(%bmp);
HDC local_hdc = (HDC)((void*)g->GetHdc());
HGLRC local_hrc = wglCreateContext( local_hdc );
if(!wglMakeCurrent( local_hdc, local_hrc ))
    ShowError();

//Draw something with OpenGL ...

g->ReleaseHdc((IntPtr)((void*)local_hdc));

//Do something with the bitmap ...

但是wglMakeCurrent失败像素格式无效。

But wglMakeCurrent fails with "The pixel format is invalid".

当然!我认为。 我只需要设置位图的像素格式,使OpenGL可以渲染它!

"Of course!" I think. "I just need to set the pixel format of the bitmap in such a way that OpenGL can render to it!"

很遗憾,如果我使用上面的Bitmap构造函数,我只限于一个枚举的罐头像素格式,而不是一个结构,包含一堆属性,我可以OR在一起。我不认为可以在事后设置像素格式。

Sadly, if I use the above Bitmap constructor, I'm limited to an enum of canned pixelformats, rather than a struct holding a bunch of attributes that I can OR together. And I don't think it's possible to set the pixel format after the fact.

无论如何,这种做事的方式比使用一堆Win32调用更漂亮使得设备无关的位图,但是固定像素格式似乎省略任何类型的GL启用的任何东西,我只是失败。你能成功吗?

Anyway, this way of doing things would be prettier than using a bunch of Win32 calls to make a Device-Independent Bitmap, but the canned pixel formats seem to omit any sort of GL-enabled anything, and I just fail. Can you succeed?

推荐答案

你可能不想直接绘制到缓冲区,这是OpenGL的优势开始。而是:

You probably don't want to draw directly into your buffer anyway, you'd lose all hardware acceleration which is the advantage of OpenGL to begin with. Instead:


  • 使用视频随机存储器设置OpenGL上下文。


  • 调用Bitmap :: LockBits以获取指向原始像素内存的指针。
  • $
    < b $ b
  • 使用glCopyImage将视频RAM中的结果读入位图。

  • Set up an OpenGL context as usual, using video RAM.
  • Draw into it, fully accelerated.
  • Create a Bitmap.
  • Call Bitmap::LockBits to get the pointer to the raw pixel memory.
  • Use glCopyImage to read the results from video RAM into the Bitmap.

PixelFormat传递给LockBits和glCopyImage最后两个步骤当然需要匹配。

The PixelFormat passed to LockBits and glCopyImage in the last two steps need to match of course.

这篇关于使用OpenGL直接绘制一个.NET位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆