CsGL使用的基本方法

来源:互联网 发布:骨朵网络影视详情页 编辑:程序博客网 时间:2024/05/16 15:20

闲来无事,想在sharpmap的基础上添加一个三维方面的功能。翻了一下网页发现一个C#版本的OpenGL库,即CsGL

CsGL的官方网站:http://csgl.sourceforge.net/

又在网上找了几篇博文,觉得很好就转载到自己博客了。

原文作者并没有把代码贴出来,但为了方便查看,我这里就直接上代码了

如果要下载源码可以点击原文的链接:

http://crazylove.blog.51cto.com/855385/181608

首先新建一个C# WindowsForm项目,命名为CsGL_Base

右击解决方案,添加一个名为OpenGLBase的组件类,该类继承自OpenGLControl类,记得添加CsGL的引用,代码如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using CsGL.OpenGL;using System.Windows.Forms;using System.Drawing;using System.Drawing.Imaging;namespace CsGL_Base{    class OpenGLBase : OpenGLControl    {       #region 数据成员        Timer Timer_GLupdate = new Timer();//窗口重绘计时器        #endregion       #region 构造函数及刷新GL事件初始化        public OpenGLBase()        {            /************************************************************************/            /* OpenGL屏幕刷新计时器                                                           */            /************************************************************************/            this.Timer_GLupdate.Tick += new EventHandler(Timer_GLupdate_Tick);            this.Timer_GLupdate.Interval = 10;            this.Timer_GLupdate.Start();            /************************************************************************/            /* 按键回调          */            /************************************************************************/            this.KeyDown += new KeyEventHandler(OpenGLBase_KeyDown);        }        private void OpenGLBase_KeyDown(object sender, KeyEventArgs e)        {            switch (e.KeyCode)            {                /***************************************************************/                /* 场景控制                                                              */                /************************************************************* */                case Keys.D:                    break;                case Keys.A:                    break;                case Keys.S:                    break;                case Keys.W:                    break;                default:                    break;            }        }        private void Timer_GLupdate_Tick(object sender, EventArgs e)        {            this.Invalidate();        }        #endregion        #region 窗体重绘         /// <summary>        /// 视口改变大小时重新设置视口和绘图         /// </summary>        /// <param name="e"></param>        protected override void OnSizeChanged(EventArgs e)        {            GL.glViewport(0, 0, this.Bounds.Width, this.Bounds.Height);            GL.glMatrixMode(GL.GL_PROJECTION);            GL.glLoadIdentity();            GL.gluPerspective(40.0f, (float)this.Bounds.Width / (float)this.Bounds.Height, 0.1f, 500.0f);            GL.glMatrixMode(GL.GL_MODELVIEW);        }        /// <summary>        /// OnPaint方法处理Paint事件         /// </summary>        /// <param name="pevent"></param>        protected override void OnPaint(System.Windows.Forms.PaintEventArgs pevent)        {            base.OnPaint(pevent);            //ToDo:可加入自己的设计代码         }        #endregion        /************************************************************************/        /* OpenGL初始化                                                                   */        /************************************************************************/        protected override void InitGLContext()        {          base.InitGLContext();          GL.glMatrixMode(GL.GL_PROJECTION); //选择投影矩阵            GL.glLoadIdentity(); //重设投影矩阵            GL.gluPerspective(40.0, ((double)(this.Width)/(double)(this.Height)), 1.0, 1000.0);//调整视口大小            GL.glMatrixMode(GL.GL_MODELVIEW); //选择模型观察矩阵            GL.glLoadIdentity(); //重置模型观察矩阵            GL.glShadeModel(GL.GL_SMOOTH); // 启用阴影平滑         }        public override void glDraw()        {          GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);//清除屏幕及深度缓存            GL.glLoadIdentity();          GL.glFlush(); // Flush The GL Pipeline        }        protected override void Dispose(bool disposing)        {            base.Dispose(disposing);        }    }}

在项目默认生成的窗体Form1中实例化上面的OpenGLBase组件类,代码如下:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace CsGL_Base{    public partial class Form1 : Form    {        OpenGLBase myGLView = new OpenGLBase();        public Form1()        {            InitializeComponent();            myGLView.Dock = DockStyle.Fill;            myGLView.Location = new Point(0, 0);            myGLView.Name = "OpenGLView1";            myGLView.Visible = true;            Controls.Add(myGLView);            ResumeLayout(false);        }    }}

运行一下,就可以看到一个自己定义的CsGL窗口了。