Created
March 2, 2012 01:33
-
-
Save KonajuGames/1954646 to your computer and use it in GitHub Desktop.
Android OpenGL background context
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using OpenTK; | |
| using OpenTK.Graphics; | |
| using OpenTK.Graphics.ES20; | |
| using OpenTK.Platform; | |
| using OpenTK.Platform.Android; | |
| using Android.Views; | |
| using Android.Content; | |
| using System.Text; | |
| using System.Threading; | |
| using Javax.Microedition.Khronos.Egl; | |
| using Android.Runtime; | |
| namespace OpenGLApplication1 | |
| { | |
| class GLView1 : AndroidGameView | |
| { | |
| bool threadLaunched; | |
| string glslCode = "precision mediump float;\n" + | |
| "precision mediump int;\n" + | |
| "uniform vec4 vs_uniforms_vec4[4];\n" + | |
| "uniform vec4 posFixup;\n" + | |
| "#define vs_c0 vs_uniforms_vec4[0]\n" + | |
| "#define vs_c1 vs_uniforms_vec4[1]\n" + | |
| "#define vs_c2 vs_uniforms_vec4[2]\n" + | |
| "#define vs_c3 vs_uniforms_vec4[3]\n" + | |
| "attribute vec4 vs_v0;\n" + | |
| "#define vs_oPos gl_Position\n" + | |
| "varying vec4 vFrontColor;\n" + | |
| "#define vs_oD0 vFrontColor\n" + | |
| "varying vec4 vTexCoord0;\n" + | |
| "#define vs_oT0 vTexCoord0\n" + | |
| "attribute vec4 vs_v1;\n" + | |
| "attribute vec4 vs_v2;\n" + | |
| "\n" + | |
| "void main()\n" + | |
| "{\n" + | |
| " vs_oPos.x = dot(vs_v2, vs_c0);\n" + | |
| " vs_oPos.y = dot(vs_v2, vs_c1);\n" + | |
| " vs_oPos.z = dot(vs_v2, vs_c2);\n" + | |
| " vs_oPos.w = dot(vs_v2, vs_c3);\n" + | |
| " vs_oD0 = vs_v0;\n" + | |
| " vs_oT0.xy = vs_v1.xy;\n" + | |
| " gl_Position.y = gl_Position.y * posFixup.y;\n" + | |
| " gl_Position.xy += posFixup.zw * gl_Position.ww;\n" + | |
| "}"; | |
| IEGL10 egl; | |
| EGLDisplay eglDisplay; | |
| EGLSurface eglSurface; | |
| EGLContext eglContext; | |
| public GLView1(Context context) | |
| : base(context) | |
| { | |
| } | |
| // This gets called when the drawing surface is ready | |
| protected override void OnLoad(EventArgs e) | |
| { | |
| base.OnLoad(e); | |
| MakeCurrent(); | |
| egl = EGLContext.EGL as IEGL10; | |
| eglDisplay = egl.EglGetCurrentDisplay(); | |
| //eglSurface = egl.EglGetCurrentSurface(EGL10.EglRead); | |
| eglSurface = Java.Lang.Object.GetObject<EGLSurface>(Holder.Handle, JniHandleOwnership.DoNotTransfer); | |
| int EglOpenGLES2Bit = 4; | |
| int[] configSpec = new int[] { | |
| EGL10.EglDepthSize, 4, | |
| EGL10.EglRenderableType, EglOpenGLES2Bit, | |
| EGL10.EglNone | |
| }; | |
| EGLConfig[] configs = new EGLConfig[1]; | |
| int[] num_config = new int[1]; | |
| egl.EglChooseConfig(eglDisplay, configSpec, configs, 1, num_config); | |
| eglContext = egl.EglCreateContext(eglDisplay, configs[0], egl.EglGetCurrentContext(), configSpec); | |
| // Run the render loop | |
| Run(); | |
| } | |
| protected override void CreateFrameBuffer() | |
| { | |
| GLContextVersion = OpenTK.Graphics.GLContextVersion.Gles2_0; | |
| base.CreateFrameBuffer(); | |
| } | |
| void Worker() | |
| { | |
| egl.EglMakeCurrent(eglDisplay, EGL10.EglNoSurface, EGL10.EglNoSurface, EGL10.EglNoContext); | |
| egl.EglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext); | |
| int shader = GL.CreateShader(All.VertexShader); | |
| GL.ShaderSource(shader, 1, new string[] { glslCode }, (int[])null); | |
| GL.CompileShader(shader); | |
| int compiled = 0; | |
| GL.GetShader(shader, All.CompileStatus, ref compiled); | |
| if (compiled == (int)All.False) | |
| { | |
| string log = ""; | |
| int length = 0; | |
| GL.GetShader(shader, All.InfoLogLength, ref length); | |
| if (length > 0) | |
| { | |
| var logBuilder = new StringBuilder(length); | |
| GL.GetShaderInfoLog(shader, length, ref length, logBuilder); | |
| log = logBuilder.ToString(); | |
| } | |
| Console.WriteLine(log); | |
| GL.DeleteShader(shader); | |
| throw new InvalidOperationException("Shader Compilation Failed"); | |
| } | |
| } | |
| protected override void OnUpdateFrame(FrameEventArgs e) | |
| { | |
| base.OnUpdateFrame(e); | |
| if (!threadLaunched) | |
| { | |
| ThreadPool.QueueUserWorkItem(w => Worker()); | |
| threadLaunched = true; | |
| } | |
| } | |
| // This gets called on each frame render | |
| protected override void OnRenderFrame(FrameEventArgs e) | |
| { | |
| base.OnRenderFrame(e); | |
| GL.ClearColor(0.5f, 0.5f, 0.5f, 1.0f); | |
| GL.Clear((uint)All.ColorBufferBit); | |
| SwapBuffers(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment