.NET(C#) 中使用 CsGL-OpenGL .NET
2007-11-25 16:39
393 查看
最近才发现,很久以前 .NET 版本的 OpenGL 就出来了,并且版本已经升到了1.4.1 了。下载下来安装后写了第一个小程序。
csgl.native.dll 和 csgl.dll 两个文件拷贝到 %SystemRoot%\System32\ 中。
解决方案中添加 csgl.dll 的引用。
运行后屏幕截图为:
![](http://images.cnblogs.com/cnblogs_com/zxjay/2007112508335880.gif)
程序代码为:
从代码可以看出,Cs-OpenGL 作简单的二维图形不如直接用 GDI+ 简单直观,但其强项是做三维图形,这是 GDI+ 无可比拟的!
csgl.native.dll 和 csgl.dll 两个文件拷贝到 %SystemRoot%\System32\ 中。
解决方案中添加 csgl.dll 的引用。
运行后屏幕截图为:
![](http://images.cnblogs.com/cnblogs_com/zxjay/2007112508335880.gif)
程序代码为:
1 using System; 2 using System.Windows.Forms; 3 using System.Drawing; 4 using CsGL.OpenGL; 5 using CsGL.Util; 6 7 namespace TestOpenGL 8 { 9 /// <summary> 10 /// 11 /// ** .NET(C#) 中使用 CsGL-OpenGL .NET** 12 /// 13 /// File: FirstOpenGL.cs 14 /// 15 /// Author: 周振兴 (Zxjay 飘遥) 16 /// 17 /// E-Mail: tda7264@163.com 18 /// 19 /// Date: 07-05-23 20 /// 21 /// Blog: http://www.cnblogs.com/zxjay 22 /// 23 /// </summary> 24 public class FirstOpenGl : Form 25 { 26 /// <summary> 27 /// FirstOpenGl 的构造方法 28 /// </summary> 29 public FirstOpenGl() 30 { 31 this.Text = "First OpenGL!"; 32 this.MaximizeBox = false; 33 this.FormBorderStyle = FormBorderStyle.Fixed3D; 34 this.Size = new Size(400, 420); 35 } 36 37 /// <summary> 38 /// 初始化 Bitmap 39 /// </summary> 40 /// <returns> Bitmap </returns> 41 private Bitmap InitBitMap() 42 { 43 Bitmap bmp = new Bitmap(400, 400); 44 Graphics g = Graphics.FromImage(bmp); 45 GDIGLContext gdictxt = new GDIGLContext(g); 46 47 gdictxt.Create(new DisplayType(DisplayFlags.DRAW_TO_BITMAP, true), null); 48 gdictxt.Grab(); 49 50 GLTest gl = new GLTest(); 51 gl.Init(); 52 gl.Draw(); 53 GL.glFinish(); 54 55 gdictxt.Dispose(); 56 g.Dispose(); 57 58 return bmp; 59 } 60 61 /// <summary> 62 /// 重写 Form 的 OnPaint 方法,在其上绘制位图 63 /// </summary> 64 /// <param name="e"></param> 65 protected override void OnPaint(PaintEventArgs e) 66 { 67 Graphics g = e.Graphics; 68 g.DrawImage(InitBitMap(), new Rectangle(0, 0, 400, 400)); 69 base.OnPaint(e); 70 } 71 72 /// <summary> 73 /// 程序的入口 74 /// </summary> 75 public static void Main() 76 { 77 FirstOpenGl fog = new FirstOpenGl(); 78 Application.Run(fog); 79 } 80 } 81 82 83 /// <summary> 84 /// 继承自 System.Object/OSLib/OpenGL/OpenGL_Extension/GLU/GLUT/GL 85 /// </summary> 86 public class GLTest : GL 87 { 88 public void Init() 89 { 90 glMatrixMode(GL_PROJECTION); 91 gluOrtho2D(-10.0, 10.0, -10.0, 10.0); 92 glClearColor(1.0f, 1.0f, 1.0f, 1.0f); 93 glColor3f(1.0f, 0, 0); 94 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); 95 glClear(GL_COLOR_BUFFER_BIT); 96 glShadeModel(GL_SMOOTH); 97 } 98 99 /// <summary> 100 /// 绘制位图 101 /// </summary> 102 public void Draw() 103 { 104 const int NUMBER = 12; 105 const int RADIUS = 8; 106 double PI = 3.1415; 107 PointF[] pt = new PointF[NUMBER]; 108 109 for (int i = 0; i < NUMBER; i++) 110 { 111 pt[i].X = (float)(RADIUS * Math.Cos(PI / NUMBER + 2 * PI * i / NUMBER)); 112 pt[i].Y = (float)(RADIUS * Math.Sin(PI / NUMBER + 2 * PI * i / NUMBER)); 113 } 114 115 for (int i = 0; i < NUMBER; i++) 116 for (int j = i + 1; j < NUMBER; j++) 117 { 118 glBegin(GL_LINES); 119 glVertex2f(pt[i].X, pt[i].Y); 120 glVertex2f(pt[j].X, pt[j].Y); 121 glEnd(); 122 } 123 glFlush(); 124 } 125 126 } 127 }
从代码可以看出,Cs-OpenGL 作简单的二维图形不如直接用 GDI+ 简单直观,但其强项是做三维图形,这是 GDI+ 无可比拟的!
相关文章推荐
- .NET(C#) 中使用 CsGL-OpenGL .NET
- .NET(C#) 中使用 CsGL-OpenGL .NET
- .NET(C#) 中使用 CsGL-OpenGL .NET
- .net里面照样使用 escape 和 unescape (c# csharp vb.net jscript.net)
- 如何使用NET Reactor为您的.Net(C#,VB.Net) 源代码加密!
- ADO.NET .net core2.0添加json文件并转化成类注入控制器使用 简单了解 iTextSharp实现HTML to PDF ASP.NET MVC 中 Autofac依赖注入DI 控制反转IOC 了解一下 C# AutoMapper 了解一下
- .NET、C#和ASP.NET三者之间的区别 使用 IntraWeb (9) - JavaScript
- 使用 C# 和 C++.NET 开发的 .NET 应用程序实例列表
- 使用.NET命令行编译器编译项目(如ASP.NET、C#等)
- 同时使用 C# and VB.NET 在 .net 2.0 中
- 如何 ︰ 执行批量更新和插入使用.NET 提供程序在 C#.NET OpenXML
- .NET 水晶报表的使用实例 - [c#.net]
- 利用SQLite ADO.NET在.NET(C#)中使用SQLite数据库
- 使用.NET写Managed用户自定义函数(UDF)C#/VB.NET
- (引用)使用 C# 和 C++.NET 开发的 .NET 应用程序实例列表
- 使用.NET写Managed用户自定义函数(UDF)C#/VB.NET
- .Net平台 C# 程序, 使用DILE(Dotnet IL Editor) 进行动态调试
- Visual Studio 2017中使用正则修改部分内容 如何使用ILAsm与ILDasm修改.Net exe(dll)文件 C#学习-图解教程(1):格式化数字字符串 小程序开发之图片转Base64(C#、.Net) jquery遍历table为每一个单元格取值及赋值 。net加密解密相关方法 .net关于坐标之间一些简单操作
- [.NET]c#.net程序中使用ffmpeg.exe来处理视频并生成上传视频的截图
- NET 中,使用 GDI 来精确测量文本的实际绘出尺寸(C#)-.NET教程,C#语言