Click here to Skip to main content
15,881,380 members
Articles / Desktop Programming / Win32
Article

Arcball OpenGL in C#

Rate me:
Please Sign up or sign in to vote.
3.88/5 (16 votes)
28 Mar 2008CPOL1 min read 99.2K   3.2K   44   10
An Arcball module using CsGL in C#.

Introduction

This 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.

Background

Arcball (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).

Screenshot - pic1.png

Using the code

Using this code is straightforward. Create a new form, create an instance of the Arcball class, and make if fill the form rectangle. All plots should be in the public class PlotGL, in OpenGL.cs. I am plotting a torus in this example.

C#
#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 Form1_SizeChanged, call PlotGL.

C#
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 Arcball class at all. Unless you plan to add more functions such as zoom/pan. Adding zoom/pan is also easy, but I didn't get a chance to add it here. Hope it comes useful to you.

History

This is release 1. Please let me know if you find bugs.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Kam
Engineer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generaldead lock Pin
wangtb11-Aug-10 17:22
wangtb11-Aug-10 17:22 
GeneralRe: dead lock [modified] Pin
Kam11-Aug-10 20:42
Kam11-Aug-10 20:42 
GeneralThank you .. but [modified] Pin
xian1233-Dec-08 7:16
xian1233-Dec-08 7:16 
GeneralRe: Thank you .. but [modified] Pin
Kam7-Dec-08 19:22
Kam7-Dec-08 19:22 
GeneralArticle suggestions Pin
Iain Clarke, Warrior Programmer12-Dec-07 2:11
Iain Clarke, Warrior Programmer12-Dec-07 2:11 
GeneralRe: Article suggestions Pin
Kam12-Dec-07 9:00
Kam12-Dec-07 9:00 
GeneralRe: Article suggestions Pin
Iain Clarke, Warrior Programmer12-Dec-07 22:20
Iain Clarke, Warrior Programmer12-Dec-07 22:20 
GeneralException that demanded the csgl.native.dll Pin
pita200011-Dec-07 20:39
pita200011-Dec-07 20:39 
GeneralRe: Exception that demanded the csgl.native.dll Pin
_Fandango_12-Dec-07 6:49
_Fandango_12-Dec-07 6:49 
Getting

System.TypeInitializationException was unhandled
Message="The type initializer for 'CsGL.OSLib' threw an exception."
Source="csgl"
TypeName="CsGL.OSLib"
StackTrace:
at CsGL.OSLib..ctor()
at CsGL.OpenGL.OpenGL..ctor()
at CsGL.OpenGL.OpenGL_Extension..ctor()
at CsGL.OpenGL.GLU..ctor()
at CsGL.OpenGL.GLUT..ctor()
at CsGL.OpenGL.GL..ctor()
at CsGL.OpenGL.OpenGLContext..ctor()
at CsGL.OpenGL.ControlGLContext..ctor(Control c)
at CsGL.OpenGL.OpenGLControl.CreateContext()
at CsGL.OpenGL.OpenGLControl.get_Context()
at CsGL.OpenGL.OpenGLControl.OnSizeChanged(EventArgs e)
at arcball.OGL.OnSizeChanged(EventArgs e) in \arcball\arcball\OpenGL.cs:line 83
at System.Windows.Forms.Control.UpdateBounds(Int32 x, Int32 y, Int32 width, Int32 height, Int32 clientWidth, Int32 clientHeight)
at System.Windows.Forms.Control.UpdateBounds(Int32 x, Int32 y, Int32 width, Int32 height)
at System.Windows.Forms.Control.SetBoundsCore(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified)
at System.Windows.Forms.Control.System.Windows.Forms.Layout.IArrangedElement.SetBounds(Rectangle bounds, BoundsSpecified specified)
at System.Windows.Forms.Layout.DefaultLayout.xLayoutDockedControl(IArrangedElement element, Rectangle newElementBounds, Boolean measureOnly, Size& preferredSize, Rectangle& remainingBounds)
at System.Windows.Forms.Layout.DefaultLayout.LayoutDockedControls(IArrangedElement container, Boolean measureOnly)
at System.Windows.Forms.Layout.DefaultLayout.xLayout(IArrangedElement container, Boolean measureOnly, Size& preferredSize)
at System.Windows.Forms.Layout.DefaultLayout.LayoutCore(IArrangedElement container, LayoutEventArgs args)
at System.Windows.Forms.Layout.LayoutEngine.Layout(Object container, LayoutEventArgs layoutEventArgs)
at System.Windows.Forms.Control.OnLayout(LayoutEventArgs levent)
at System.Windows.Forms.ScrollableControl.OnLayout(LayoutEventArgs levent)
at System.Windows.Forms.Form.OnLayout(LayoutEventArgs levent)
at System.Windows.Forms.Control.PerformLayout(LayoutEventArgs args)
at System.Windows.Forms.Control.PerformLayout()
at System.Windows.Forms.Control.ResumeLayout(Boolean performLayout)
at System.Windows.Forms.Layout.LayoutTransaction.Dispose()
at System.Windows.Forms.Layout.DefaultLayout.SetDock(IArrangedElement element, DockStyle value)
at System.Windows.Forms.Control.set_Dock(DockStyle value)
at arcball.Form1..ctor() in \arcball\arcball\Form1.cs:line 16
at arcball.Program.Main() in \arcball\arcball\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
GeneralRe: Exception that demanded the csgl.native.dll Pin
Kam12-Dec-07 8:58
Kam12-Dec-07 8:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.