当前位置:文档之家› opengl简介及实例

opengl简介及实例

C# 实例OpenGL是图形硬件的一个软件接口,是一种快速、高质量的3D图形软件。

它提供了近120个绘制点、线点多边形等3D图形的命令,可以完成绘制物体、变换、光照处理、着色、反走样、融合、雾化、位图和图像、纹理映射、动画等基本操作,通过把这一系列基本操作进行组合,可以构造更复杂的3D物体和描绘丰富多彩、千变万化的客观世界。

C#是以运行库为基础的一种编程语言,它几乎集中了所有关于软件开发和软件工程研究的最新成果,如面向对象、类型安全等,并被寄希望成为微软发布的用于企业编写基于COM+和视窗系统的程序语言中的最好的一种[2]。

与C++相比,C#的语法更加简洁,调试更加容易,且应用程序开发更加快速。

把C#和OpenGL结合起来开发3D应用程序和软件,将显著提高开发效率。

在C#中,程序间的依赖项通过符号而不是文本来控制,因而不使用头文件,而且opengl32.dll以及opengl32.lib等文件也不能像在C++中那样进行部署和引用,所以,无法直接使用OpenGL所提供的图形库。

在C#中通过调用OpenGL 动态链接库文件:csgl.dll和csgl.native.dll实现OpenGL所提供的强大的图形功能。

这2个文件可以从网页上获取。

csgl.dll中定义了4个名称空间,即CsGL,CsGL.OpenGL,CsGL.Pointers,CsGL.Util,其中,CsGL.OpenGL定义的4个类OpenGL、GL、GLU、GLUT中封装了几乎所有的OpenGL函数、用户库函数、辅助库函数和实用库函数及常量;类OpenGLControl中定义了OpenGL场景绘制函数,如场景的初始化、场景的绘制函数等;类OpenGLContext中定义了OpenGL环境控制命令,如像素格式、调色板的创建等命令。

CsGL.Util定义了键盘、鼠标事件及异常处理等。

为了能够使用这2个文件,先将这2个文件拷贝到系统文件夹%systemroot%╲system32中,然后在项目的属性页对话框中将"引用路径"设置为系统文件夹%systemroot%╲system32,这样C#就可以找到运行/调试应用程序所需要的库文件。

下面是如何在VS2005环境下实现OpneGL建模C# 实例11、新建windows应用程序项目,将csgl.dll和csgl.native.dll两个文件拷贝到.....\bin\debug\文件夹中(可在/projects/csgl/files/下载),增加引用csgl.dll,新增类xzqOpenGLClass类。

2、xzqOpenGLClass.cs文件改为:using System;using System.Collections.Generic;using System.Text;using CsGL.OpenGL;//引用CsGL.OpenGL命名空间namespace OpenGL03{class xzqOpenGLClass : OpenGLControl{public double xzq_T, xzq_eyeX, xzq_eyeY, xzq_eyeZ;public xzqOpenGLClass(){}protected override void OnSizeChanged(EventArgs e){double aspect_ratio = (double)Size.Width / (double)Size.Height;GL.glViewport(0, 0, Size.Width, Size.Height);//视口大小GL.glMatrixMode(GL.GL_PROJECTION);GL.glLoadIdentity();GL.gluPerspective(10.0f, aspect_ratio, 0.1f, 100.0f);//等价与glFrustum,创建一个视景体GL.glMatrixMode(GL.GL_MODELVIEW);GL.glLoadIdentity();}/// <summary>/// OnPaint方法处理Paint事件/// </summary>/// <param name="pevent"></param>protected override void OnPaint(System.Windows.Forms.PaintEventArgs pevent){base.OnPaint(pevent);//ToDo:可加入自己的设计代码}/************************************************************************//* OpenGL初始化*//************************************************************************/protected override void InitGLContext(){base.InitGLContext();GL.glShadeModel(GL.GL_SMOOTH);GL.glClearColor(1.0f, 1.0f, 1.0f, 0.5f);GL.glClearDepth(1.0f);GL.glEnable(GL.GL_DEPTH_TEST);GL.glDepthFunc(GL.GL_LEQUAL);GL.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);double aspect_ratio = (double)Size.Width / (double)Size.Height;GL.glViewport(0, 0, Size.Width, Size.Height);//视口大小GL.glMatrixMode(GL.GL_PROJECTION);GL.glLoadIdentity();GL.gluPerspective(10.0f, aspect_ratio, 0.1f, 100.0f);//等价与glFrustum,创建一个视景体GL.glMatrixMode(GL.GL_MODELVIEW);GL.glLoadIdentity();}public override void glDraw(){GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); // 清除屏幕及深度缓存GL.glLoadIdentity();GLLight();//设置灯光GL.gluLookAt(xzq_eyeX, xzq_eyeY, xzq_eyeZ, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);//设置视图变换// GL.glColor3f(0.5f, 0.0f, 0.0f);//DrawSphere();DrawCylinder();//DrawCube();GL.glFlush(); // Flush The GL Pipeline}public void GLLight(){float[] ambient = { 1.0f, 1.0f, 0.0f, 1.0f };float[] diffuse = { 1.0f, 1.0f, 1.0f, 1.0f };float[] specular = { 1.0f, 1.0f, 1.0f, 1.0f };float[] position = { 1.0f, 1.0f, 1.0f, 0.0f };GL.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, ambient);GL.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, diffuse); GL.glLightfv(GL.GL_LIGHT0, GL.GL_SPECULAR, specular);GL.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, position); GL.glEnable(GL.GL_LIGHT0); GL.glEnable(GL.GL_LIGHTING);}public void DrawCylinder(){GLUquadric hdc0 = GL.gluNewQuadric();GL.gluSphere(hdc0, 0.2, 38, 38);GL.gluDeleteQuadric(hdc0);GLUquadric hdc = GL.gluNewQuadric();GL.gluCylinder(hdc, 0.1, 0.1, 1.0, 16, 16);GL.gluDeleteQuadric(hdc);GL.glTranslatef(0.0f, 0.0f, 1.0f);GLUquadric hdc1 = GL.gluNewQuadric();GL.gluCylinder(hdc1, 0.2, 0.0, 0.4, 16, 16);GL.gluDeleteQuadric(hdc1);GL.glTranslatef(0.0f, 0.0f, -1.0f);GL.glRotatef(-90, 1.0f, 0.0f, 0.0f);GLUquadric hdc2 = GL.gluNewQuadric();GL.gluCylinder(hdc2, 0.1, 0.1, 1.0, 16, 16);GL.gluDeleteQuadric(hdc2);GL.glTranslatef(0.0f, 0.0f, 1.0f);GLUquadric hdc3 = GL.gluNewQuadric();GL.gluCylinder(hdc3, 0.2, 0.0, 0.4, 16, 16);GL.gluDeleteQuadric(hdc3);GL.glTranslatef(0.0f, 0.0f, -1.0f);GL.glRotatef(-90, 0.0f, 1.0f, 0.0f);GLUquadric hdc4 = GL.gluNewQuadric();GL.gluCylinder(hdc4, 0.1, 0.1, 1.0, 16, 16);GL.gluDeleteQuadric(hdc4);GL.glTranslatef(0.0f, 0.0f, 1.0f);GLUquadric hdc5 = GL.gluNewQuadric();GL.gluCylinder(hdc5, 0.2, 0.0, 0.4, 16, 16);GL.gluDeleteQuadric(hdc5);GL.glTranslatef(0.0f, -1.0f, 0.0f);}}}3、Form1.cs文件改为:using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace OpenGL03{public partial class Form1 : Form{xzqOpenGLClass xzqGLView = new xzqOpenGLClass();public Form1(){InitializeComponent();this.ClientSize = new System.Drawing.Size(500, 500);xzqGLView.xzq_T = -0.0f;xzqGLView.xzq_eyeX = 20.0f;xzqGLView.xzq_eyeY = 20.0f;xzqGLView.xzq_eyeZ = 20.0f;xzqGLView.Parent = this;xzqGLView.BringToFront();xzqGLView.Dock = DockStyle.Fill;this.Controls.Add(xzqGLView);}}}4、编译运行效果C# 实例2开始的步骤基本上是一样的,只说正文了哈!1using System;2using System.Windows.Forms;3using System.Drawing;4using CsGL.OpenGL;5using CsGL.Util;67namespace TestOpenGL8 {9///<summary>10///11/// ** .NET(C#) 中使用 CsGL-OpenGL .NET** 12///13/// File: FirstOpenGL.cs14///15/// Author: 周振兴 (Zxjay 飘遥)16///17/// E-Mail: tda7264@18///19/// Date: 07-05-2320///21/// Blog: 22///23///</summary>24public class FirstOpenGl : Form25 {26///<summary>27/// FirstOpenGl 的构造方法28///</summary>29public FirstOpenGl()30 {31this.Text = "First OpenGL!";32this.MaximizeBox = false;33this.FormBorderStyle = FormBorderStyle.Fixed3D;34this.Size = new Size(400, 420);35 }3637///<summary>38///初始化 Bitmap39///</summary>40///<returns> Bitmap </returns>41private Bitmap InitBitMap()42 {43 Bitmap bmp = new Bitmap(400, 400);44 Graphics g = Graphics.FromImage(bmp);45 GDIGLContext gdictxt = new GDIGLContext(g);4647 gdictxt.Create(new DisplayType(DisplayFlags.DRAW_TO_B ITMAP, true), null);48 gdictxt.Grab();4950 GLTest gl = new GLTest();51 gl.Init();52 gl.Draw();53 GL.glFinish();5455 gdictxt.Dispose();56 g.Dispose();5758return bmp;59 }6061///<summary>62///重写 Form 的 OnPaint 方法,在其上绘制位图63///</summary>64///<param name="e"></param>65protected override void OnPaint(PaintEventArgs e)66 {67 Graphics g = e.Graphics;68 g.DrawImage(InitBitMap(), new Rectangle(0, 0, 400, 40 0));69base.OnPaint(e);70 }7172///<summary>73///程序的入口74///</summary>75public static void Main()76 {77 FirstOpenGl fog = new FirstOpenGl();78 Application.Run(fog);79 }80 }818283///<summary>84///继承自 System.Object/OSLib/OpenGL/OpenGL_Extension/GLU/GL UT/GL85///</summary>86public class GLTest : GL87 {88public void Init()89 {90 glMatrixMode(GL_PROJECTION);91 gluOrtho2D(-10.0, 10.0, -10.0, 10.0);92 glClearColor(1.0f, 1.0f, 1.0f, 1.0f);93 glColor3f(1.0f, 0, 0);94 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);95 glClear(GL_COLOR_BUFFER_BIT);96 glShadeModel(GL_SMOOTH);97 }9899///<summary>100///绘制位图101///</summary>102public void Draw()103 {104const int NUMBER = 12;105const int RADIUS = 8;106double PI = 3.1415;107 PointF[] pt = new PointF[NUMBER];108109for (int i = 0; i < NUMBER; i++)110 {111 pt[i].X = (float)(RADIUS * Math.Cos(PI / NUMBER + 2 * PI * i / NUMBER));112 pt[i].Y = (float)(RADIUS * Math.Sin(PI / NUMBER + 2 * PI * i / NUMBER));113 }114115for (int i = 0; i < NUMBER; i++)116for (int j = i + 1; j < NUMBER; j++)117 {118 glBegin(GL_LINES);119 glVertex2f(pt[i].X, pt[i].Y);120 glVertex2f(pt[j].X, pt[j].Y);121 glEnd();122 }123 glFlush();124 }。

相关主题