C# 中的 OpenGL 位图

发布于 2024-12-25 09:26:14 字数 3103 浏览 1 评论 0原文

有人能帮助我吗?我有一个返回位图的函数 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);


}

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


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盗琴音 2025-01-01 09:26:14

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

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

其中 widthheight 是视口的尺寸。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文