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

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

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

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

运行后屏幕截图为:

.

程序代码为:

using  System;
using  System.Windows.Forms;
using  System.Drawing;
using  CsGL.OpenGL;
using  CsGL.Util;

namespace  TestOpenGL
{
    
/// <summary>
    
/// 
    
/// ** .NET(C#) 中使用 CsGL-OpenGL .NET**
    
/// 
    
///  File: FirstOpenGL.cs
    
/// 
    
///  Author: 周振兴 (Zxjay 飘遥)
    
/// 
    
///  E-Mail: tda7264@163.com
    
/// 
    
///  Date: 07-05-23
    
/// 
    
///  Blog: http://blog.csdn.net/zxjay
    
/// 
    
/// </summary>

    public class FirstOpenGl : Form
    
{
        
/// <summary>
        
///  FirstOpenGl 的构造方法
        
/// </summary>

        public FirstOpenGl()
        
{
            
this.Text = "First OpenGL!";
            
this.MaximizeBox = false;
            
this.FormBorderStyle = FormBorderStyle.Fixed3D;
            
this.Size = new Size(400420);
        }


        
/// <summary>
        
///  初始化 Bitmap 
        
/// </summary>
        
/// <returns> Bitmap </returns>

        private Bitmap InitBitMap()
        
{
            Bitmap bmp 
= new Bitmap(400400);
            Graphics g 
= Graphics.FromImage(bmp);
            GDIGLContext gdictxt 
= new GDIGLContext(g);

            gdictxt.Create(
new DisplayType(DisplayFlags.DRAW_TO_BITMAP, true), null);
            gdictxt.Grab();

            GLTest gl 
= new GLTest();
            gl.Init();
            gl.Draw();
            GL.glFinish();

            gdictxt.Dispose();
            g.Dispose();

            
return bmp;
        }


        
/// <summary>
        
///  重写 Form 的 OnPaint 方法,在其上绘制位图
        
/// </summary>
        
/// <param name="e"></param>

        protected override void OnPaint(PaintEventArgs e)
        
{
            Graphics g 
= e.Graphics;            
            g.DrawImage(InitBitMap(), 
new Rectangle(00400400));
            
base.OnPaint(e);
        }


        
/// <summary>
        
///  程序的入口
        
/// </summary>

        public static void Main()
        
{
            FirstOpenGl fog 
= new FirstOpenGl();
            Application.Run(fog);
        }

    }



    
/// <summary>
    
///  继承自 System.Object/OSLib/OpenGL/OpenGL_Extension/GLU/GLUT/GL
    
/// </summary>

    public class GLTest : GL
    
{
        
public void Init()
        
{
            glMatrixMode(GL_PROJECTION);
            gluOrtho2D(
-10.010.0-10.010.0);
            glClearColor(
1.0f1.0f1.0f1.0f);
            glColor3f(
1.0f00);
            glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
            glClear(GL_COLOR_BUFFER_BIT);
            glShadeModel(GL_SMOOTH);
        }


        
/// <summary>
        
///  绘制位图
        
/// </summary>

        public void Draw()
        
{
            
const int NUMBER = 12;
            
const int RADIUS = 8;
            
double PI = 3.1415;
            PointF[] pt 
= new PointF[NUMBER];           

            
for (int i = 0; i < NUMBER; i++)
            
{
                pt[i].X 
= (float)(RADIUS * Math.Cos(PI / NUMBER + 2 * PI * i / NUMBER));
                pt[i].Y 
= (float)(RADIUS * Math.Sin(PI / NUMBER + 2 * PI * i / NUMBER));
            }


            
for (int i = 0; i < NUMBER; i++)
                
for (int j = i + 1; j < NUMBER; j++)
                
{
                    glBegin(GL_LINES);
                        glVertex2f(pt[i].X, pt[i].Y);
                        glVertex2f(pt[j].X, pt[j].Y);
                    glEnd();
                }

            glFlush();
        }


    }

}

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值