ShareIdeas

本博客不再更新,欢迎访问我的github,https://github.com/sunke-github/

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

        1.因为作四轴项目的需要(在上位机显示飞行器姿态),找了点时间学习了一下OpenGl,什么是OpenGL,OpenGL是一个功能强大的

开放图形Open Graphics Library。其前身是SGI公司为其图形工作站开发的IRIS GL。为使其能够更加容易地移植到不同的硬

件和操作系统,SGI开发了OpenGL从程序开发人员的角度来看,OpenGL是一组绘图命令的API集合。利用这些API能够方便地描述

二维和三维几何物体,并控制这些物体按某种方式绘制到显示缓冲区中。OpenGLAPI集提供了物体描述、平移、旋转、缩放、光照、

纹理、材质、像素、位图、文字、交互以及提高显示性能等方面的功能,基本涵盖了开发二、三维图形程序所需的各个方面。

      2.我这里用到的是CsGl框架,CsGLopenGL.NET下的包装。可以在网上下载:目前关于CsGL的开发基本上已经停止,官方主页上的

说法是认为CsGL对大多数应用而言已经足够稳定了,目前CsGL开发工作基本上集中在Example程序的撰写.主要文件列表如下:

 GLViewer.1.1.src.tgz
 GLViewer.1.1.bin.tgz
 CsGLExamples.0.9.1.zip
 csgl.1.4.1.doc.zip
 csgl.1.4.1.src.zip
 csgl.1.4.1.dll.zip
其中我用过以下两个文件:csgl.1.4.1.dll.zip(转到CSDN下载)为动态链接库文件,将其下载引用即可进行OpenGL 3D程CsGLExamples.0.9.1.zip(可从CSDN论坛下载)为一些示例的源代码和可执行文件,其中包括Nehe的CsGL源代码,不过是在GUN平台下建立的,通过一些简单操作,可以加入到Visual studio 来, 我这里使用的是VS2008.目前大概有两种导入方法,我用的是继承CsGL中的OpenGLControl类的方法 .步骤如下:
      1 新建Form工程,添加一个新建的空类(本例为OpenGLBase),引用名称空间using CsGL.OpenGL;用于继承CsGL中的OpenGLControl类,继承后的OpenGLBase类就可以当OpenGL控件使用了。(这中做法的目的是因为C#中不允许多重继承,Form工程的主窗口继承了Form类就不能够再继承OpenGLBase类了).最后添加dll文件的引用.
注意: csgl.native.dll  这个程序是运行所需要的,需要添加到你 与编译生成的  .exe文件在同一个目录 .
 
 


 
     
添加csgl.dll引用
 

    2. 在Form 的构造函数加入下面代码
 //捆绑到一个Form窗体上显示
   OpenGLBase myGLView = new OpenGLBase();
    myGLView.Dock = DockStyle.Fill            myGLView.Location = new Point(0, 0);
    myGLView.Name = "OpenGLView1";
    myGLView.Visible = true;
    Controls.Add(myGLView);
    ResumeLayout(false);
//代码具体作用 ,VS中有详细说明
 
 
编程实例
 
 
转动的几何体:
 

 

代码说明:这里主要是编写OpenGLBase 这个类的代码:
    
/// <summary>
    /// 新建一个类,因为C#不允许多重继承
    /// </summary>
    class OpenGLBase : OpenGLControl
    {
    }
 
OpenGLBase类中主要包括下面一些方法:
 1.创建屏幕刷新计时器回调函数,每10ms刷新一次OpenGL窗口
    private void Timer_GLupdate_Tick(object sender, EventArgs e){
        this.Invalidate();
     }
2.创建按键响应计时器回调函数,(按照个人需求添加),响应按键信息,可以按需要修改.
    private void OpenGLBase_KeyDown(object sender, KeyEventArgs e){}
3.窗口大小改变时发生(窗体加载的时候会运行一次)
   protected override void OnSizeChanged(EventArgs e) {}
 
 
4. OpenGL初始化场景
    protected override void InitGLContext()
     {}
5. OpenGL绘制函数,绘图代码都在这里完成
    public override void glDraw()
     {}
 

 

 下面是完整OpenGLBase类的原码,代码来自NEHE教程,紧供学习(

作者信息:

NeHe Lessons
NeHe Productions
http://nehe.gamedev.net

Contains ports of the lessons developed by Jeff Molofee (NeHe)
and contributors to his site.

)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CsGL.OpenGL;
using System.Windows.Forms;

namespace CsGl_Tri_squ
{
    /// <summary>
    /// 新建一个类,因为C#不允许多重继承
    /// </summary>
    class OpenGLBase : OpenGLControl
    {
        //Timer Timer_GLupdate = new Timer();窗口重绘计时器
        Timer Timer_GLupdate = new Timer();//  窗口重绘计时器
        private static float rtri = 0;                                                    // Angle For The Triangle
        private static float rquad = 0;                                                    // Angle For The Quad

        public OpenGLBase()
        {
            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 Timer_GLupdate_Tick(object sender, EventArgs e)
        {
            this.Invalidate();
        }
        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;
            }
        }
        /// <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:可加入自己的设计代码
        }
        /// <summary>
        /// OpenGL初始化场景
        /// </summary>
        protected override void InitGLContext()
        {
            base.InitGLContext();
            GL.glEnable(GL.GL_DEPTH_TEST);                        // Enables Depth Testing
           // GL.glDepthFunc(GL.GL_LEQUAL);                        // The Type Of Depth Testing To Do
           // GL.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);    // Really Nice Perspective Calculations
            GL.glMatrixMode(GL.GL_PROJECTION);
            GL.glLoadIdentity();/
            GL.gluPerspective(50.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()
        {

            // Here's Where We Do All The Drawing
           GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);                            // Clear Screen And Depth Buffer
            GL.glLoadIdentity();                                                            // Reset The Current Modelview Matrix,单位化
         
            GL.glTranslatef(-1.5f, 0.0f, -6.0f);                                            // Move Left 1.5 Units And Into The Screen 

            GL.glRotatef(rtri, 0.0f, 1.0f, 0.0f);                                            // 绕Y轴旋转金字塔  rtri是角度
            GL.glBegin(GL.GL_TRIANGLES);                                                        // Drawing Using Triangles
            //正对的面
            GL.glColor3f(1.0f, 0.0f, 0.0f);                                            // Red
            GL.glVertex3f(0.0f, 1.0f, 0.0f);                                            // Top Of Triangle (Front)
            GL.glColor3f(0.0f, 1.0f, 0.0f);                                            // Green
            GL.glVertex3f(-1.0f, -1.0f, 1.0f);                                            // Left Of Triangle (Front)
            GL.glColor3f(0.0f, 0.0f, 1.0f);                                            // Blue
            GL.glVertex3f(1.0f, -1.0f, 1.0f);                                            // Right Of Triangle (Front)
            //2
            GL.glColor3f(1.0f, 0.0f, 0.0f);                                        // Red
            GL.glVertex3f(0.0f, 1.0f, 0.0f);                                        // Top Of Triangle (Right)
            GL.glColor3f(0.0f, 0.0f, 1.0f);                                        // Blue
            GL.glVertex3f(1.0f, -1.0f, 1.0f);                                        // Left Of Triangle (Right)
            GL.glColor3f(0.0f, 1.0f, 0.0f);                                        // Green
            GL.glVertex3f(1.0f, -1.0f, -1.0f);                                        // Right Of Triangle (Right)
            //3
            GL.glColor3f(1.0f, 0.0f, 0.0f);                                        // Red
            GL.glVertex3f(0.0f, 1.0f, 0.0f);                                        // Top Of Triangle (Back)
            GL.glColor3f(0.0f, 1.0f, 0.0f);                                        // Green
            GL.glVertex3f(1.0f, -1.0f, -1.0f);                                        // Left Of Triangle (Back)
            GL.glColor3f(0.0f, 0.0f, 1.0f);                                        // Blue
            GL.glVertex3f(-1.0f, -1.0f, -1.0f);                                        // Right Of Triangle (Back)
            //4
            GL.glColor3f(1.0f, 0.0f, 0.0f);                                        // Red
            GL.glVertex3f(0.0f, 1.0f, 0.0f);                                        // Top Of Triangle (Left)
            GL.glColor3f(0.0f, 0.0f, 1.0f);                                        // Blue
            GL.glVertex3f(-1.0f, -1.0f, -1.0f);                                        // Left Of Triangle (Left)
            GL.glColor3f(0.0f, 1.0f, 0.0f);                                        // Green
            GL.glVertex3f(-1.0f, -1.0f, 1.0f);                                        // Right Of Triangle (Left)
            GL.glEnd();                                                                    // Finished Drawing The Pyramid

            GL.glLoadIdentity();                                                            // Reset The Current Modelview Matrix,回到中心位置
            GL.glTranslatef(1.5f, 0.0f, -7.0f);                                            // Move Right 1.5 Units And Into The Screen 7.0

            GL.glRotatef(rquad, 1.0f, 1.0f, 1.0f);                                            // Rotate The Quad On The X, Y, And Z Axes
            GL.glBegin(GL.GL_QUADS);                                                            // Draw A Quad
            //1
            GL.glColor3f(0.0f, 1.0f, 0.0f);                                            // Set The Color To Green
            GL.glVertex3f(1.0f, 1.0f, -1.0f);                                            // Top Right Of The Quad (Top)
            GL.glVertex3f(-1.0f, 1.0f, -1.0f);                                            // Top Left Of The Quad (Top)
            GL.glVertex3f(-1.0f, 1.0f, 1.0f);                                            // Bottom Left Of The Quad (Top)
            GL.glVertex3f(1.0f, 1.0f, 1.0f);                                            // Bottom Right Of The Quad (Top)
            //2
            GL.glColor3f(1.0f, 0.5f, 0.0f);                                        // Set The Color To Orange
            GL.glVertex3f(1.0f, -1.0f, 1.0f);                                        // Top Right Of The Quad (Bottom)
            GL.glVertex3f(-1.0f, -1.0f, 1.0f);                                        // Top Left Of The Quad (Bottom)
            GL.glVertex3f(-1.0f, -1.0f, -1.0f);                                        // Bottom Left Of The Quad (Bottom)
            GL.glVertex3f(1.0f, -1.0f, -1.0f);                                        // Bottom Right Of The Quad (Bottom)
            //3
            GL.glColor3f(1.0f, 0.0f, 0.0f);                                            // Set The Color To Red
            GL.glVertex3f(1.0f, 1.0f, 1.0f);                                            // Top Right Of The Quad (Front)
            GL.glVertex3f(-1.0f, 1.0f, 1.0f);                                            // Top Left Of The Quad (Front)
            GL.glVertex3f(-1.0f, -1.0f, 1.0f);                                            // Bottom Left Of The Quad (Front)
            GL.glVertex3f(1.0f, -1.0f, 1.0f);                                            // Bottom Right Of The Quad (Front)
            //4
            GL.glColor3f(1.0f, 1.0f, 0.0f);                                        // Set The Color To Yellow
            GL.glVertex3f(1.0f, -1.0f, -1.0f);                                        // Bottom Left Of The Quad (Back)
            GL.glVertex3f(-1.0f, -1.0f, -1.0f);                                        // Bottom Right Of The Quad (Back)
            GL.glVertex3f(-1.0f, 1.0f, -1.0f);                                        // Top Right Of The Quad (Back)
            GL.glVertex3f(1.0f, 1.0f, -1.0f);                                        // Top Left Of The Quad (Back)
            //5
            GL.glColor3f(0.0f, 0.0f, 1.0f);                                        // Set The Color To Blue
            GL.glVertex3f(-1.0f, 1.0f, 1.0f);                                        // Top Right Of The Quad (Left)
            GL.glVertex3f(-1.0f, 1.0f, -1.0f);                                        // Top Left Of The Quad (Left)
            GL.glVertex3f(-1.0f, -1.0f, -1.0f);                                        // Bottom Left Of The Quad (Left)
            GL.glVertex3f(-1.0f, -1.0f, 1.0f);                                        // Bottom Right Of The Quad (Left)
            //6
            GL.glColor3f(1.0f, 0.0f, 1.0f);                                                    // Set The Color To Violet
            GL.glVertex3f(1.0f, 1.0f, -1.0f);                                            // Top Right Of The Quad (Right)
            GL.glVertex3f(1.0f, 1.0f, 1.0f);                                            // Top Left Of The Quad (Right)
            GL.glVertex3f(1.0f, -1.0f, 1.0f);                                            // Bottom Left Of The Quad (Right)
            GL.glVertex3f(1.0f, -1.0f, -1.0f);                                            // Bottom Right Of The Quad (Right)
            GL.glEnd();                // Done Drawing The Cube
            rtri += 0.6f;                     // Increase The Rotation Variable For The Triangle
            rquad += 0.6f;            // Decrease The Rotation Variable For The Quad,单位是角度
          
           

        }

    }
}

 

 
 
 
 
posted on 2012-10-31 15:56  ShareIdeas  阅读(5880)  评论(0编辑  收藏  举报