本文介绍了将OpenGL场景保存到TBitmap-glReadPixels返回空数据吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将OpenGL创建的场景另存为TBitmap.我遇到的问题是glReadPixels返回所有空数据(全0).关于为什么会发生这种情况的想法?我正在尝试捕获当前的OpenGL上下文. (屏幕上的显示一切正常).这是Windows下的Delphi 7.

I'm attempting to save an OpenGL created scene as a TBitmap. The problem I'm having is glReadPixels is returning all empty data (all 0's). Thoughts on why this would occur? I'm attempting to capture the currentl OpenGL context. (display on screen is all working fine). This is with Delphi 7 under Windows.

var
   pbuf: pointer;
   y: integer;
   bmp: TBitmap;
   p1, p2: pointer;
begin
   GetMem( pbuf, pnScene.Width * pnScene.Height * 4);
   glReadPixels( 0, 0, pnScene.Width, pnScene.Height, GL_RGBA, GL_UNSIGNED_BYTE, pbuf);
   //<------  pbuf now contains all 0's  ---------->

   bmp := TBitmap.Create;
   bmp.PixelFormat := pf32bit;
   bmp.Width := pnScene.Width;
   bmp.Height := pnScene.height;

   for y := 0 to (pnScene.Height -1) do
   begin
      p1 := bmp.ScanLine[y];
      p2 := pointer( integer(pbuf)+ (y * bmp.Width * 4));
      CopyMemory( p1, p2, bmp.Width * 4);
   end;

   bmp.SaveToFile( 'c:\test\temp.bmp');
   bmp.Free;
   FreeMem( pbuf);
end;

非常欢迎您提出建议/想法!

And suggestions/thoughts are greatly welcome!

附录:

最终计划是在渲染调用之后立即放置glReadPixels()调用,以记录视频.当我这样做时,它可以正常工作,所以最终这不是问题.

Ultimately the plan was to place the glReadPixels() call right after the rendering calls for purposes of recording video. When I did that, it worked ok, so this ended up being something of a non-problem.

推荐答案

请确保您从与创建GL上下文所在的线程相同的线程进行此操作.如果这是您提供给第三方库的回调函数,那么它可能会在另一个线程中执行而您甚至没有意识到.这一次发生在我身上,就像你一样,我得到的都是零.

Make sure you are doing this from the same thread as where you created the GL context. If this is a callback function you supply to a third-party library then it may execute in another thread without you even realizing it. This happened to me once and all I got was zeroes just like you.

这篇关于将OpenGL场景保存到TBitmap-glReadPixels返回空数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 23:49