|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThis is an Arcball module originally published in the NeHe tutorial series in C++. It has been converted and modified for C#. It utilizes the CsGL wrapper by default, but can be replaced with the Tao version as well. BackgroundArcball (also know as Rollerball) is probably the most intuitive method to view three dimensional objects. The principle of the Arcball is based on creating a sphere around the object and letting users to click a point on the sphere and drag it to a different location. There is a bit of math involved, of course, and you can Google for it. The code here is a C# source code implementing an Arcball in OpenGL (CsGL to be exact).
Using the codeUsing this code is straightforward. Create a new form, create an instance of the #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;
}
}
Mouse left click rotates the object dynamically. Mouse right click resets it to its original orientation. You can easily control the mouse button too. In 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();
}
}
You don't need to change anything else and the code should work. You can also extend the keyboard control based on your needs. I have the Escape and R keys programmed here. Note: For almost any code, you do not need to modify the HistoryThis is release 1. Please let me know if you find bugs.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||