渲染结果截图
OpenGL渲染的结果通常是3D图像,如果我们想把3D图像转换成2D图像保存下来呢?较为复杂的方法是利用帧缓存技术(FBO)来实现,我这里总结一种自用的较为简便的方式。仍然以WPF开发为例介绍,如下的代码实现了点击一个Button,就把渲染结果存为一副24bit的bmp图像。
private void save_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
OpenGL gl = openGLControl.OpenGL
int width = 680
int height = 665
byte[] pixels = new byte[(int)(3 * width * height)]
gl.ReadPixels(0, 0, width, height, OpenGL.GL_RGB, OpenGL.GL_UNSIGNED_BYTE, pixels)
System.Drawing.Rectangle retc = new System.Drawing.Rectangle()
retc.Width = width
retc.Height = height
Bitmap bmp = new Bitmap(width, height)
System.Drawing.Imaging.BitmapData data = bmp.LockBits(retc, System.Drawing.Imaging.ImageLockMode.WriteOnly,
System.Drawing.Imaging.PixelFormat.Format24bppRgb)
gl.ReadPixels(0, 0, width, height, OpenGL.GL_BGR, OpenGL.GL_UNSIGNED_BYTE, data.Scan0)
bmp.UnlockBits(data)
bmp.RotateFlip(RotateFlipType.RotateNoneFlipY)
bmp.Save(@"Snapshot\head.bmp")
}