OpenGL ES Wrapper: Your First Application

OpenGL ES Wrapper: Your First Application

In my previous post on Getting Started with OpenGL on Windows Mobile I talked about taking the existing OpenGL ES wrapper by Koushik Dutta and extending it to make it easier to work with.  In this, and subsequent posts, I’ll show you how you can take this wrapper and start building out an application.

To get started create a new Smart Device project in Visual Studio 2008.

image image

Note that I’ve created this against the Windows Mobile 5 SDK – if you can find a legacy device still running WM5 which supports OpenGL then this application will still run. I only bother building against the later SDKs if I particularly need one of the few features that were added since WM5.

Next, copy the two OpenGL projects from the this link into your solution folder and add them to your solution within Visual Studio. Also, go ahead and add a project reference from your application to both OpenGL projects.

image

You won’t be needing the designer support for your form but you do need some of the initialization values set in the Form1.designer.cs file. It’s up to you whether you move the InitializeComponent method from your Form1.designer.cs file into your Form1.cs, and then delete Form1.designer.cs, or just leave the designer.cs file there and ignore it.

At this stage I’d recommend renaming Form1 to something more descriptive. In this case my application is only going to have a single form so I have used MainForm. In the MainForm.cs file I’ve modified the class to inherit from ApplicationForm, rather than just Form – this gives me all the OpenGL initialization I talked about previously, as well as some virtual methods that can be implemented to setup, draw and update the current scene:

public partial class MainForm : ApplicationForm
    {
        public MainForm()
        {
            InitializeComponent();
        }

        protected override void SetupScene()
        {
            base.SetupScene();
        }

        protected override void DrawScene()
        {
            base.DrawScene();
        }

        protected override void UpdateScene(float secondsSinceLastUpdate)
        {
            base.UpdateScene(secondsSinceLastUpdate);
        }
    }

In this case we’re going to simply implement Hello World by creating a Font and GlyphRun in the SetupScene method:

OpenGLFont font;
GlyphRun title;
protected override void SetupScene()
{
    base.SetupScene();

    font = new OpenGLFont(new Font(FontFamily.GenericSerif, 12, FontStyle.Regular));
    title = new GlyphRun(font, “Hello World!”, new Size(int.MaxValue, int.MaxValue), OpenGLTextAlignment.Left, true);
}

Then to draw the text on the screen, simply update the Draw method:

protected override void DrawScene()
        {
            base.DrawScene();

            title.Draw();
        }

 

 

image

Not the worlds greatest demo, but surprisingly simple for an OpenGL application. In the next post we’ll start to investigate what you can really do with OpenGL that you can’t do easily with traditional Windows Forms application.

Note: In the InitializeOpenGL function within ApplicationForm you need to include the following call otherwise your text will appear as a solid white rectangle:

gl.BlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA);

Leave a Reply

Your email address will not be published. Required fields are marked *