C# 中的 OpenGL 位图
有人能帮助我吗?我有一个返回位图的函数 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用以下方法获取已渲染的像素:
其中
width
和height
是视口的尺寸。You can use the following method to get the pixels that have been rendered:
where
width
andheight
are the dimensions of your viewport.