Click here to Skip to main content
Licence CPOL
First Posted 27 Dec 2007
Views 58,355
Downloads 3,237
Bookmarked 42 times

Arcball Module in C# - Tao.OpenGL

By | 11 Jan 2008 | Article
Arcball module using Tao.OpenGL in C#.

Introduction

This article is the continuation on my Arcball module for C#. Previously, I had posted a code using CsGL. In this version, I have utilized the Tao.OpenGL wrapper and I have added Rotation/Zoom/Pan functions. I have also made use of Display Lists to speed up plot rendering. The Arcball module written here is general and not limited to OpenGL, and can be used in GDI+ and DirectX as well. Please note that my main focus in this tutorial is on the Arcball module, not on OpenGL application.

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 and you can Google for it. Here are few good links to educate yourself:

The code here is a C# source code implementing an Arcball in OpenGL (Tap.OpenGL).

Screenshot - pic1.png

Here is the same object but zoomed using the mouse middle button:

Screenshot - pic2.png

And, here is when it is panned to the left using the mouse right button:

Screenshot - pic3.png

Using the code

In this version, I am using Display List, Lighting, and Blending functions. I am not going to go into the details of OpenGL programming for the sake of brevity. Create a new form, create an instance of the Arcball class, and make if fill the form rectangle. All plots should go in the public function PlotGL in Form1.cs. I am plotting a torus and a backward torus in this example.

public void PlotGL()
{
    try

    {
        lock (matrixLock)
        {
            ThisTransformation.get_Renamed(matrix);
        }

        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

        #region plot something
        Gl.glPolygonMode(Gl.GL_FRONT, Gl.GL_FILL);
        Gl.glColor3f(0.8f, 0.3f, 0.1f);
        Gl.glCallList(plot_glList1); // plot using display list 

        //Gl.glPolygonMode(Gl.GL_FRONT, Gl.GL_LINE);
        Gl.glColor3f(0.5f, 0.5f, 0.9f);
        Gl.glCallList(plot_glList2); // plot using display list 

        #endregion plot something

        Gl.glPopMatrix(); // NEW: Unapply Dynamic Transform
        Gl.glFlush();     // Flush the GL Rendering Pipeline

        this.simpleOpenGlControl1.Invalidate();


    }
    catch
    {
        return;
    }
}

I have defined the transformation matrices in here (Arcball.cs):

public class Matrix4f
{

...


    public Quat4f Rotation
    {
        set
        {
            float n, s;
            float xs, ys, zs;
            float wx, wy, wz;
            float xx, xy, xz;
            float yy, yz, zz;

            M = new float[4, 4];

            n = (value.x * value.x) + (value.y * value.y) + 
                (value.z * value.z) + (value.w * value.w);
            s = (n > 0.0f) ? 2.0f / n : 0.0f;

            xs = value.x * s;
            ys = value.y * s;
            zs = value.z * s;
            wx = value.w * xs;
            wy = value.w * ys;
            wz = value.w * zs;
            xx = value.x * xs;
            xy = value.x * ys;
            xz = value.x * zs;
            yy = value.y * ys;
            yz = value.y * zs;
            zz = value.z * zs;

            // rotation
            M[0, 0] = 1.0f - (yy + zz);
            M[0, 1] = xy - wz;
            M[0, 2] = xz + wy;

            M[1, 0] = xy + wz;
            M[1, 1] = 1.0f - (xx + zz);
            M[1, 2] = yz - wx;

            M[2, 0] = xz - wy;
            M[2, 1] = yz + wx;
            M[2, 2] = 1.0f - (xx + yy);

            M[3, 3] = 1.0f;

            // translation (pan)
            M[0, 3] = pan.x;
            M[1, 3] = pan.y;

            // scale (zoom)
            for (int i = 0; i < 3; i++)
                for (int j = 0; j < 3; j++)
                    M[i, j] *= scl;


        }
    }

    public float Scale
    {
        set { scl = value; }

    }

    public Vector3f Pan
    {
        set { pan = value; }

    }

}

You don't need to change anything else and the code should work. I have included comments as much as possible, but feel free to contact me if you have questions.

Note: For almost any code, you do not need to modify the Arcball class at all.

History

  • This is release 1.01. Please let me know if you find any 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
QuestionThank you very much , but do you know how to do this kind of graphics Pinmemberderek091921:12 25 Nov '11  
QuestionThank you PinmemberMember 819926721:18 12 Sep '11  
GeneralThanks! Keeping Y axis vertical PinmembernorrisMiou3:07 27 Feb '11  
GeneralRe: Thanks! Keeping Y axis vertical PinmemberKam10:23 3 Mar '11  
GeneralRotate camera Pinmembersajmon440:30 23 Sep '10  
GeneralThanks! PinmemberLimeMan8:01 26 May '10  
GeneralPan is reversed PinmemberRajeev Lochan0:06 15 Apr '10  
GeneralRe: Pan is reversed PinmemberKam20:11 26 Apr '10  
GeneralZoom and Viewing directions [modified] PinmemberM.Siyamalan1:22 19 Mar '10  
GeneralRe: Zoom and Viewing directions PinmemberKam9:57 30 Mar '10  
GeneralInverse Transformation PinmemberMember 266410819:17 25 Nov '09  
GeneralRe: Inverse Transformation PinmemberKam19:34 27 Nov '09  
Questionhow to implement this in DirectX PinmemberSodrohu22:43 1 Oct '09  
AnswerRe: how to implement this in DirectX PinmemberKam19:35 27 Nov '09  
QuestionStandard views Pinmembersamjoe19:32 30 Apr '09  
AnswerRe: Standard views PinmemberKam20:14 11 Jun '09  
GeneralExcelent work PinmemberTiagoRibeiro15:27 23 Apr '09  
GeneralRe: Excelent work PinmemberKam18:24 28 Apr '09  
GeneralRe: Excelent work PinmemberFransiscus Herry20:13 10 Mar '10  
QuestionHow can I change the BackColor? PinmemberGerber Samuel0:02 27 Mar '09  
AnswerRe: How can I change the BackColor? PinmemberGerber Samuel1:07 27 Mar '09  
GeneralGood Pinmemberuaterelelli2:52 9 Jan '09  
GeneralRe: Good PinmemberKam14:04 21 Jan '09  
GeneralRe: Good PinmemberTom Reesbeck10:36 8 May '10  
GeneralThank you Pinmemberbingqilin5:42 1 Dec '08  

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
Web02 | 2.5.120517.1 | Last Updated 11 Jan 2008
Article Copyright 2007 by Kam
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid