当先锋百科网

首页 1 2 3 4 5 6 7
 

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

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

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

运行后屏幕截图为:

程序代码为:

 
using System;
using System.Drawing;
using System.Windows.Forms;

using CsGL.OpenGL;
using CsGL.Util;

namespace CSGLDemo01
{
    public partial class FirstOpenGl : Form
    {
        public FirstOpenGl()
        {
            this.Text = "First OpenGL!"; 
            this.MaximizeBox = false; 
            this.FormBorderStyle = FormBorderStyle.Fixed3D; 
            this.Size = new Size(400, 420);
        }

        /// <summary> 
        /// 初始化 Bitmap 
        /// /// </summary>  
        /// <returns> Bitmap </returns>
        private Bitmap InitBitMap()
        { 
            Bitmap bmp = new Bitmap(400, 400); 
            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(0, 0, 400, 400)); 
            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.0, 10.0, -10.0, 10.0);
                glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
                glColor3f(1.0f, 0, 0);
                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+ 无可比拟的!