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

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

转载自:http://blog.csdn.net/ewyetc/article/details/6665403

 

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

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

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

运行后屏幕截图为:

程序代码为:

 
 
[csharp] view plain copy print ?
  1. using System;  
  2. using System.Drawing;  
  3. using System.Windows.Forms;  
  4.   
  5. using CsGL.OpenGL;  
  6. using CsGL.Util;  
  7.   
  8. namespace CSGLDemo01  
  9. {  
  10.     public partial class FirstOpenGl : Form  
  11.     {  
  12.         public FirstOpenGl()  
  13.         {  
  14.             this.Text = "First OpenGL!";   
  15.             this.MaximizeBox = false;   
  16.             this.FormBorderStyle = FormBorderStyle.Fixed3D;   
  17.             this.Size = new Size(400, 420);  
  18.         }  
  19.   
  20.         /// <summary>    
  21.         /// 初始化 Bitmap    
  22.         /// /// </summary>     
  23.         /// <returns> Bitmap </returns>   
  24.         private Bitmap InitBitMap()  
  25.         {   
  26.             Bitmap bmp = new Bitmap(400, 400);   
  27.             Graphics g = Graphics.FromImage(bmp);   
  28.             GDIGLContext gdictxt = new GDIGLContext(g);   
  29.             gdictxt.Create(new DisplayType(DisplayFlags.DRAW_TO_BITMAP, true), null);   
  30.             gdictxt.Grab();   
  31.             GLTest gl = new GLTest();   
  32.             gl.Init();  
  33.             gl.Draw();   
  34.             GL.glFinish();   
  35.             gdictxt.Dispose();   
  36.             g.Dispose();   
  37.             return bmp;   
  38.         }  
  39.           
  40.         /// <summary>   
  41.         /// 重写 Form 的 OnPaint 方法,在其上绘制位图   
  42.         /// </summary>    
  43.         /// <param name="e"></param>     
  44.         protected override void OnPaint(PaintEventArgs e)   
  45.         {  
  46.             Graphics g = e.Graphics;   
  47.             g.DrawImage(InitBitMap(), new Rectangle(0, 0, 400, 400));   
  48.             base.OnPaint(e);   
  49.         }  
  50.    
  51.         /// <summary>   
  52.         /// 程序的入口    
  53.         /// </summary>   
  54.         public static void Main()   
  55.         {   
  56.             FirstOpenGl fog = new FirstOpenGl();   
  57.             Application.Run(fog);   
  58.         }   
  59.           
  60.         /// <summary>    
  61.         /// 继承自 System.Object/OSLib/OpenGL/OpenGL_Extension/GLU/GLUT/GL   
  62.         /// </summary>   
  63.         public class GLTest : GL  
  64.         {  
  65.             public void Init()  
  66.             {  
  67.                 glMatrixMode(GL_PROJECTION);  
  68.                 gluOrtho2D(-10.0, 10.0, -10.0, 10.0);  
  69.                 glClearColor(1.0f, 1.0f, 1.0f, 1.0f);  
  70.                 glColor3f(1.0f, 0, 0);  
  71.                 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);  
  72.                 glClear(GL_COLOR_BUFFER_BIT);  
  73.                 glShadeModel(GL_SMOOTH);  
  74.             }  
  75.   
  76.             /// <summary>    
  77.             /// 绘制位图        
  78.             /// </summary>    
  79.             public void Draw()  
  80.             {  
  81.                 const int NUMBER = 12;  
  82.                 const int RADIUS = 8;  
  83.                 double PI = 3.1415;  
  84.                 PointF[] pt = new PointF[NUMBER];  
  85.                 for (int i = 0; i < NUMBER; i++)  
  86.                 {  
  87.                     pt[i].X = (float)(RADIUS * Math.Cos(PI / NUMBER + 2 * PI * i / NUMBER));  
  88.                     pt[i].Y = (float)(RADIUS * Math.Sin(PI / NUMBER + 2 * PI * i / NUMBER));  
  89.                 }  
  90.                 for (int i = 0; i < NUMBER; i++)  
  91.                 {  
  92.                     for (int j = i + 1; j < NUMBER; j++)  
  93.                     {  
  94.                         glBegin(GL_LINES);  
  95.                         glVertex2f(pt[i].X, pt[i].Y);  
  96.                         glVertex2f(pt[j].X, pt[j].Y);  
  97.                         glEnd();  
  98.                     }  
  99.                 }  
  100.   
  101.                 glFlush();  
  102.             }  
  103.   
  104.         }  
  105.       
  106.       
  107.     }  
  108. }  

从代码可以看出,Cs-OpenGL 作简单的二维图形不如直接用 GDI+ 简单直观,但其强项是做三维图形,这是 GDI+ 无可比拟的!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值