1

有谁能帮助我吗?我有一个返回位图的函数 Renderframe,但我想在 c# 中使用 OpenGL(TK) 以 3D 形式绘制一些东西 - 是否可以返回旋转后的立方体?或者我应该怎么做才能获得有效的代码?

非常感谢每一个想法或帮助:)

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);


}
4

1 回答 1

2

您可以使用以下方法获取已渲染的像素:

    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;
    }

wherewidthheight是视口的尺寸。

于 2012-01-06T11:44:31.213 回答