Need to improve resolution of screenshot from OpenGL

旧城冷巷雨未停 提交于 2020-01-16 13:18:33

问题


I want to get a screen shot from current scene in OpenTK with good resolution. I am using GL.ReadPixels to get a photo for the scene.If I save the photo to disk; I found it with low resolution/quality.

following C# code is used to get a photo for the scene :

Bitmap bmp = null;

    if (GraphicsContext.CurrentContext != null)
    {
        glControl_window.Invalidate();
        int w = glControl_window.ClientSize.Width;
        int h = glControl_window.ClientSize.Height;
        bmp = new Bitmap(w, h);
        System.Drawing.Imaging.BitmapData data =
        bmp.LockBits(glControl_window.ClientRectangle, 
        System.Drawing.Imaging.ImageLockMode.ReadWrite, 
        System.Drawing.Imaging.PixelFormat.Format24bppRgb);


        GL.ReadPixels(0, 0, w, h, OpenTK.Graphics.OpenGL.PixelFormat.Bgr, 
        PixelType.UnsignedByte, data.Scan0);
        bmp.UnlockBits(data);

        bmp.RotateFlip(RotateFlipType.RotateNoneFlipY);

    }

when Initialize OpenTK use :

         this.glControl_window = new OpenTK.GLControl(new 
         OpenTK.Graphics.GraphicsMode(new OpenTK.Graphics.ColorFormat(32), 
         24,8,4,new OpenTK.Graphics.ColorFormat(0),2,false));

with resize event of OpenTK I use :

            GL.Viewport(0, 0, Width, Height);

with paint event of OpenTK I use (to enable zoom in/out by mouse) :

              float aspectRatio;
            if (Height == 0 || Width == 0) aspectRatio = 1f; else 
               aspectRatio = Width / (float)Height;

            double W = 20 * aspectRatio / CameraZoomScale, H = 20 / 
               CameraZoomScale;
            GL.Ortho(-0.5 * W, 0.5 * W, -0.5 * H, 0.5 * H, -600.0f, 600.0f);

I need to :

1.increase the resolution of the token screen shot during GL.ReadPixels.


回答1:


I assume you know that you are asking for two very different things here.

Part 1 - render more pixels

The number of pixels to be read from a frame buffer using GL.readpixels() is always going to be the number of pixels corresponding to the buffer size. i.e. 1280x720 or something like that.

The only way to be able to read more pixels would be to increase the size/resolution of the OpenGL frame buffer.

Look for the part of your OpenTK code that initializes the open gl window and increase the resolution of the window. Look for something like:

public MainWindow()
    : base(
           1280, // initial width
           720,  // initial height
           ...
    )
{
    // your code
}

and change the resolution as desired. You might also have to override the OnResize method in your GameWindow class as described here: How to change the viewport resolution in OpenTK

Part 2 - scaling an image

Once the bitmap has been saved to disk, any attempts to "increase" the resolution will involve upscaling the already existing image. This will ultimately result in a larger number of pixels, but also introduce upscaling artifacts depending on the image and which upscaling algoritm that is used. You can read more on different image scaling algoritms on wikipedia: Image scaling Algorithms



来源:https://stackoverflow.com/questions/49910682/need-to-improve-resolution-of-screenshot-from-opengl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!