Click here to Skip to main content
Click here to Skip to main content

Create OpenGL Mono For Android Application

, 21 Feb 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Create OpenGL Application in Visual Studio

Introduction

Let us understand how to create an OpenGL application for Android phone from Visual Studio 2010.

System Requirements

  • Android SDK
  • Mono
  • Visual Studio 2010

Background

Using the Code

As you click New Project, if all required software are properly installed, then you are able to see Mono for Android in your Visual Studio, or else install it.

Please see below to understand the use of each and every file.

namespace OpenGLApplication1
{    
    public partial class Resource
    {        
        public partial class Attribute
        {
            private Attribute()
            {
            }
        }
        
        public partial class Drawable
        {
            // aapt resource value: 0x7f020000
            public const int Icon = 2130837504;
            
            private Drawable()
            {
            }
        }
        
        public partial class Layout
        {
            // aapt resource value: 0x7f030000
            public const int Main = 2130903040;
            
            private Layout()
            {
            }
        }
        
        public partial class String
        {
            // aapt resource value: 0x7f040001
            public const int ApplicationName = 2130968577;
            
            // aapt resource value: 0x7f040000
            public const int Hello = 2130968576;
            
            private String()
            {
            }
        }
    }
}

Activity1.cs

This class is standing first in the hierarchy of execution. It is because of the attribute. And from this call, the event is executed:

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace OpenGLApplication1
{
    [Activity(Label = "OpenGLApplication1", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Create our OpenGL view, and display it
            GLView1 view = new GLView1(this);
            SetContentView(view);
        }
    }
} 

GLView1.cs

using System;

using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.ES11;
using OpenTK.Platform;
using OpenTK.Platform.Android;

using Android.Views;
using Android.Content;

namespace OpenGLApplication1
{
    class GLView1 : AndroidGameView
    {
        public GLView1(Context context)
            : base(context)
        {
        }

        // This gets called when the drawing surface is ready
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            // Run the render loop
            Run();
        }

        // This gets called on each frame render
        protected override void OnRenderFrame(FrameEventArgs e)
        {
            base.OnRenderFrame(e);
            MakeCurrent();
            GL.MatrixMode(All.Projection);
            GL.LoadIdentity();
            GL.Ortho(-1.0f, 1.0f, -1.5f, 1.5f, -1.0f, 1.0f);
            GL.MatrixMode(All.Modelview);
            GL.Rotate(3.0f, 0.0f, 0.0f, 1.0f);

            GL.ClearColor(0.5f, 0.5f, 0.5f, 1.0f);
            GL.Clear((uint)All.ColorBufferBit);

            GL.VertexPointer(2, All.Float, 0, square_vertices);
            GL.EnableClientState(All.VertexArray);
            GL.ColorPointer(4, All.UnsignedByte, 0, square_colors);
            GL.EnableClientState(All.ColorArray);
            GL.DrawArrays(All.TriangleStrip, 0, 4);

            SwapBuffers();
        }

        float[] square_vertices = {
            -0.5f, -0.5f,
            0.5f, -0.5f,    -0.5f, 0.5f, 
            0.5f, 0.5f,    };
        byte[] square_colors = {
            255, 255,   0, 255,
            0,   255, 255, 255,
            0,     0,    0,  0,
            255,   0,  255, 255,
        };
    }
}

Then select any emulator and press OK.

Then the execution process gets started and the package is created. As the process of execution completes, you are able to see the screen of Android phone with rotating rectangle as per the code.

History

  • 21st February, 2012: Initial version

License

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

Share

About the Author

amitprabhu
Web Developer
India India
Name : Amit Prabhu.
IT Exp: 8 yrs.
Lang : C#, Asp.Net, Mono - Android
Loc : Pune.

Comments and Discussions

 
QuestionThanks, it's work fine, but i can't set this as live wallpaper PinmemberMember 93733145-Jan-13 1:26 
AnswerRe: Thanks, it's work fine, but i can't set this as live wallpaper Pinmembercjb11010-Mar-14 21:46 
QuestionMono for Android is free for using. PinmemberMeteOS313-May-12 6:15 

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.

| Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 21 Feb 2012
Article Copyright 2012 by amitprabhu
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid