飘遥的Blog

C/C++/.NET
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

.NET(C#) 中使用 CsGL-OpenGL .NET

Posted on 2007-11-25 16:39  Zzx飘遥  阅读(6280)  评论(4编辑  收藏  举报

最近才发现,很久以前 .NET 版本的 OpenGL 就出来了,并且版本已经升到了1.4.1 了。下载下来安装后写了第一个小程序。

csgl.native.dll 和 csgl.dll 两个文件拷贝到 %SystemRoot%\System32\ 中。

解决方案中添加 csgl.dll 的引用。

运行后屏幕截图为:


程序代码为:

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+ 无可比拟的!