Search code examples
c#openglbitmapopentk

Bitmap with OpenGL in c#


is there anyone who is able to help me? I have a function Renderframe which returns a bitmap but I want to paint there something in 3D with OpenGL(TK) in c# - is it possible to return the rotated cube? Or how should I do it to get the valid code?

Many thanks for every idea or help :)

public Animation ()
{
}

public void Draw3D()
{

    GL.Begin(BeginMode.Quads);

    GL.Color3(0.0f, 1.0f, 0.0f);          // Set The Color To Green
    GL.Vertex3(1.0f, 1.0f, -1.0f);        // Top Right Of The Quad (Top)
    GL.Vertex3(-1.0f, 1.0f, -1.0f);       // Top Left Of The Quad (Top)
    GL.Vertex3(-1.0f, 1.0f, 1.0f);        // Bottom Left Of The Quad (Top)
    GL.Vertex3(1.0f, 1.0f, 1.0f);         // Bottom Right Of The Quad (Top)

    GL.Color3(1.0f, 0.5f, 0.0f);          // Set The Color To Orange
    GL.Vertex3(1.0f, -1.0f, 1.0f);        // Top Right Of The Quad (Bottom)
    GL.Vertex3(-1.0f, -1.0f, 1.0f);       // Top Left Of The Quad (Bottom)
    GL.Vertex3(-1.0f, -1.0f, -1.0f);      // Bottom Left Of The Quad (Bottom)
    GL.Vertex3(1.0f, -1.0f, -1.0f);       // Bottom Right Of The Quad (Bottom)

    GL.Color3(1.0f, 0.0f, 0.0f);          // Set The Color To Red
    GL.Vertex3(1.0f, 1.0f, 1.0f);         // Top Right Of The Quad (Front)
    GL.Vertex3(-1.0f, 1.0f, 1.0f);        // Top Left Of The Quad (Front)
    GL.Vertex3(-1.0f, -1.0f, 1.0f);       // Bottom Left Of The Quad (Front)
    GL.Vertex3(1.0f, -1.0f, 1.0f);        // Bottom Right Of The Quad (Front)

    GL.Color3(1.0f, 1.0f, 0.0f);          // Set The Color To Yellow
    GL.Vertex3(1.0f, -1.0f, -1.0f);       // Bottom Left Of The Quad (Back)
    GL.Vertex3(-1.0f, -1.0f, -1.0f);      // Bottom Right Of The Quad (Back)
    GL.Vertex3(-1.0f, 1.0f, -1.0f);       // Top Right Of The Quad (Back)
    GL.Vertex3(1.0f, 1.0f, -1.0f);        // Top Left Of The Quad (Back)

    GL.Color3(0.0f, 0.0f, 1.0f);          // Set The Color To Blue
    GL.Vertex3(-1.0f, 1.0f, 1.0f);        // Top Right Of The Quad (Left)
    GL.Vertex3(-1.0f, 1.0f, -1.0f);       // Top Left Of The Quad (Left)
    GL.Vertex3(-1.0f, -1.0f, -1.0f);      // Bottom Left Of The Quad (Left)
    GL.Vertex3(-1.0f, -1.0f, 1.0f);       // Bottom Right Of The Quad (Left)

    GL.Color3(1.0f, 0.0f, 1.0f);          // Set The Color To Violet
    GL.Vertex3(1.0f, 1.0f, -1.0f);        // Top Right Of The Quad (Right)
    GL.Vertex3(1.0f, 1.0f, 1.0f);         // Top Left Of The Quad (Right)
    GL.Vertex3(1.0f, -1.0f, 1.0f);        // Bottom Left Of The Quad (Right)
    GL.Vertex3(1.0f, -1.0f, -1.0f);       // Bottom Right Of The Quad (Right)

    GL.End();

    GL.Rotate(50, 1.0, 0, 0);
}


static Bitmap GetSnapShot(int width, int height)
{
    var snapShotBmp = new Bitmap(width, height);
    BitmapData bmpData = snapShotBmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
    GL.ReadPixels(0, 0, width, height, OpenTK.Graphics.OpenGL.PixelFormat.Bgr, PixelType.UnsignedByte, bmpData.Scan0);
    snapShotBmp.UnlockBits(bmpData);
    return snapShotBmp;
}

public Bitmap RenderFrame ( int width, int height, int currentFrame, int totalFrames )
{



  Draw3D();

  return GetSnapShot(width, height);


}

Solution

  • You can use the following method to get the pixels that have been rendered:

        static Bitmap GetSnapShot(int width, int height)
        {
            var snapShotBmp = new Bitmap(width, height);
            BitmapData bmpData = snapShotBmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly,
                                                      PixelFormat.Format24bppRgb);
            GL.ReadPixels(0, 0, width, height, OpenTK.Graphics.OpenGL.PixelFormat.Bgr, PixelType.UnsignedByte,
                          bmpData.Scan0);
            snapShotBmp.UnlockBits(bmpData);
            return snapShotBmp;
        }
    

    where width and height are the dimensions of your viewport.