Windows XP Tablet Ed.Portable .NET嵌入式MonoOpenGLWindows Vista.NET 1.0Windows 2003Win32.NET 1.1.NET 3.0Windows 2000Windows XP.NET 2.0.NET 3.5C# 2.0C# 3.0中级Windows.NETC#
C# 中的 Arcball OpenGL
一个在C#中使用CsGL的Arcball模块。
引言
这是一个最初在NeHe教程系列中以C++发布的Arcball模块。它已经被转换并修改为C#。默认情况下,它使用CsGL包装器,但也可以替换为Tao版本。
背景
Arcball (也称为滚球) 可能是查看三维对象最直观的方法。Arcball的原理是基于在对象周围创建一个球体,并允许用户点击球体上的一个点并将其拖动到不同的位置。当然,其中涉及一些数学知识,你可以用谷歌搜索它。这里的代码是一个C#源代码,实现了OpenGL中的Arcball (准确地说是CsGL)。
使用代码
使用这段代码非常简单。创建一个新的窗体,创建Arcball
类的一个实例,并使其填充窗体矩形。所有的绘图都应该在OpenGL.cs的公共类PlotGL
中。在这个例子中,我绘制了一个圆环。
#region CsGL - Plot Here
public void PlotGL()
{
try
{
lock (matrixLock)
{
ThisRot.get_Renamed(matrix);
}
// Clear screen and DepthBuffer
GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
GL.glLoadIdentity();
GL.glPushMatrix(); // NEW: Prepare Dynamic Transform
GL.glMultMatrixf(matrix); // NEW: Apply Dynamic Transform
GL.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINE);
#region plot something
GL.glColor3f(0.8f, 0.3f, 0.1f);
this.torus(0.3f, 0.5f);
#endregion plot something
GL.glPopMatrix(); // NEW: Unapply Dynamic Transform
GL.glFlush(); // Flush the GL Rendering Pipeline
this.Invalidate();
}
catch
{
return;
}
}
鼠标左键单击动态旋转对象。鼠标右键单击将其重置为原始方向。你也可以轻松控制鼠标按钮。
在Form1_SizeChanged
中,调用PlotGL
。
public partial class Form1 : Form
{
private OGL glWindow = new OGL();
public Form1()
{
InitializeComponent();
this.glWindow.Parent = this;
this.glWindow.Dock = DockStyle.Fill; // fill the parent form
this.glWindow.MouseMove += new
System.Windows.Forms.MouseEventHandler(this.glWindow.glOnMouseMove);
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
this.glWindow.PlotGL();
}
private void Form1_Load(object sender, EventArgs e)
{
this.glWindow.PlotGL();
}
}
你不需要更改任何其他内容,代码应该可以工作。你也可以根据你的需要扩展键盘控制。我在这里编程了Escape和R键。
注意:对于几乎任何代码,你完全不需要修改Arcball
类。除非你计划添加更多功能,如缩放/平移。添加缩放/平移也很容易,但我没有机会在这里添加它。希望它对你有用。
历史
这是版本1。如果你发现错误,请告诉我。