我这里用到的是CsGl框架,CsGL是openGL在.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 为动态链接库文件。
1.新建一个WPF项目。
2.添加以下引用到项目中:
System.Windows.Forms
WindowsFormsIntegration
csgl
3.在xaml中加入如下内容:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Title="MainWindow" Height="500" Width="500" Loaded="Window_Loaded">
<Grid Name="grid">
<WindowsFormsHost Width="500" Height="500" Name="chart" HorizontalAlignment="Right" />
</Grid>
</Window>`
4.在cs中添加如下引用
using System.Windows.Forms;
5创建窗体Load事件
private void Window_Loaded(object sender, RoutedEventArgs e)
{
OpenGLBase myGLView = new OpenGLBase();
myGLView.Dock = DockStyle.Fill;
myGLView.Location = new System.Drawing.Point(0, 0);
myGLView.Name = "OpenGLView1";
myGLView.Visible = true;
// Controls.Add(myGLView);
chart.Child = myGLView; //wpf中winform控件的使用
myGLView.ResumeLayout(false);
}
6.新建一个类OpenGLBase
7.OpenGLBase类里面添加如下引用
using CsGL.OpenGL;
using System.Windows.Forms;
8.继承 public class OpenGLBase:OpenGLControl
public class OpenGLBase:OpenGLControl
{
Timer Timer_GLupdate = new Timer();// 窗口重绘计时器
private static float rtri = 0; // Angle For The Triangle
private static float rquad = 0;
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);
}
/// <summary>
/// OpenGL绘制函数,绘图代码都在这里完成
/// </summary>
public override void glDraw()
{
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(3f, 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,单位是角度
}
}