Click here to Skip to main content
Licence CPOL
First Posted 11 Dec 2007
Views 53,145
Downloads 1,144
Bookmarked 39 times

Arcball OpenGL in C#

By | 28 Mar 2008 | Article
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.

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

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)

About the Author

Kam

Engineer

United States United States

Member

I currently work as a Computational Fluid Dynamics (CFD) consultant in an engineering company in Seattle. I hold a Ph.D. in mechanical engineering. Programming is essential in my "real" projects, but I still consider it a passion of mine. I have extensive experience in C++, FORTRAN, VB but still new to C#; though I like C# the most!

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Layout  Per page   
  Refresh
Generaldead lock Pinmemberwangtb17:22 11 Aug '10  
GeneralRe: dead lock [modified] PinmemberKam20:42 11 Aug '10  
GeneralThank you .. but [modified] Pinmemberxian1237:16 3 Dec '08  
GeneralRe: Thank you .. but [modified] PinmemberKam19:22 7 Dec '08  
GeneralArticle suggestions PinmemberIain Clarke2:11 12 Dec '07  
I don't know if you're shy, or it's because CP is messing up at the moment, but a Name would be nice - even if you make one up.
 
A screen shot of the arcball in action would be good too, and maybe a code snippet of how you insert it?
 
Iain.
GeneralRe: Article suggestions PinmemberMember 13603069:00 12 Dec '07  
GeneralRe: Article suggestions PinmemberIain Clarke22:20 12 Dec '07  
GeneralException that demanded the csgl.native.dll Pinmemberpita200020:39 11 Dec '07  
GeneralRe: Exception that demanded the csgl.native.dll Pinmember_Fandango_6:49 12 Dec '07  
GeneralRe: Exception that demanded the csgl.native.dll PinmemberMember 13603068:58 12 Dec '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 28 Mar 2008
Article Copyright 2007 by Kam
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid