// called by AppThreadFunction
            public void ovrApp_HandleVrModeChanges()
            {
                //ConsoleExtensions.tracei("enter ovrApp_HandleVrModeChanges, FrameIndex: ", (int)FrameIndex);

                // 1432
                if (this.NativeWindow != null && this.Egl.MainSurface == egl.EGL_NO_SURFACE)
                {
                    ConsoleExtensions.trace("ovrApp_HandleVrModeChanges, ovrEgl_CreateSurface");
                    this.Egl.ovrEgl_CreateSurface(this.NativeWindow);
                }

                if (this.Resumed != false && this.NativeWindow != null)
                {
                    if (this.Ovr == null)
                    {
                        var parms = VrApi_Helpers.vrapi_DefaultModeParms(ref Java);
                        parms.CpuLevel      = 2;
                        parms.GpuLevel      = 3;
                        parms.MainThreadTid = unistd.gettid();
#if MULTI_THREADED
                        // Also set the renderer thread to SCHED_FIFO.
                        parms.RenderThreadTid = ovrRenderThread_GetTid(&app->RenderThread);
#endif

                        //ALOGV("        eglGetCurrentSurface( EGL_DRAW ) = %p", eglGetCurrentSurface(EGL_DRAW));

                        ConsoleExtensions.trace("ovrApp_HandleVrModeChanges, vrapi_EnterVrMode");
                        this.Ovr = VrApi.vrapi_EnterVrMode(ref parms);

                        //ALOGV("        vrapi_EnterVrMode()");
                        //ALOGV("        eglGetCurrentSurface( EGL_DRAW ) = %p", eglGetCurrentSurface(EGL_DRAW));
                    }
                }
                else
                {
                    if (this.Ovr != null)
                    {
#if MULTI_THREADED
                        // Make sure the renderer thread is no longer using the ovrMobile.
                        ovrRenderThread_Wait(&app->RenderThread);
#endif
                        //ALOGV("        eglGetCurrentSurface( EGL_DRAW ) = %p", eglGetCurrentSurface(EGL_DRAW));

                        ConsoleExtensions.trace("ovrApp_HandleVrModeChanges, vrapi_LeaveVrMode");
                        VrApi.vrapi_LeaveVrMode(this.Ovr);
                        this.Ovr = null;

                        //ALOGV("        vrapi_LeaveVrMode()");
                        //ALOGV("        eglGetCurrentSurface( EGL_DRAW ) = %p", eglGetCurrentSurface(EGL_DRAW));
                    }
                }

                if (this.NativeWindow == null && this.Egl.MainSurface != egl.EGL_NO_SURFACE)
                {
                    ConsoleExtensions.trace("ovrApp_HandleVrModeChanges, ovrEgl_DestroySurface");
                    this.Egl.ovrEgl_DestroySurface();
                }
            }
            //I/DEBUG   ( 2941): pid: 31621, tid: 31653, name: Thread-653  >>> OVRWindWheelActivity.Activities <<<
            //I/DEBUG   ( 2941): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x8
            //I/DEBUG   ( 2941):     r0 00000000  r1 00000000  r2 ffffffff  r3 3e86c40e
            //I/DEBUG   ( 2941):     r4 3e86c40e  r5 ff4fffc0  r6 00000000  r7 3f800000
            //I/DEBUG   ( 2941):     r8 e22fe448  r9 3f772ed9  sl e22fe488  fp 00000bb5
            //I/DEBUG   ( 2941):     ip f73a0710  sp e22fe310  lr f7380375  pc f40fab72  cpsr 800f0030
            //I/DEBUG   ( 2941):
            //I/DEBUG   ( 2941): backtrace:
            //I/DEBUG   ( 2941):     #00 pc 00008b72  /data/app/OVRWindWheelActivity.Activities-1/lib/arm/libmain.so (OVRWindWheelNDK___ovrMatrix4f_CreateRotation+99)
            //I/DEBUG   ( 2941):     #01 pc 00008e61  /data/app/OVRWindWheelActivity.Activities-1/lib/arm/libmain.so (OVRWindWheelNDK_VrCubeWorld_ovrRenderer_ovrRenderer_RenderFrame+376)
            //I/DEBUG   ( 2941):     #02 pc 000098eb  /data/app/OVRWindWheelActivity.Activities-1/lib/arm/libmain.so (OVRWindWheelNDK_VrCubeWorld_ovrAppThread_AppThreadFunction+810)


            void AppThreadFunction()
            {
                // 1778
                ConsoleExtensions.trace("enter pthread_create AppThreadFunction, call vrapi_DefaultInitParms");

                var java = default(ovrJava);

                java.Vm = this.JavaVm;
                java.Vm.AttachCurrentThread(java.Vm, out java.Env, null);
                java.ActivityObject = this.ActivityObject;
                // 1785

                var initParms = VrApi_Helpers.vrapi_DefaultInitParms(ref java);

                ConsoleExtensions.trace("AppThreadFunction, call vrapi_Initialize");
                VrApi.vrapi_Initialize(ref initParms);


                ConsoleExtensions.trace("AppThreadFunction, create ovrApp, call ovrEgl_CreateContext");

                this.appState = new ovrApp(ref java)
                {
                    AppThread = this
                };


                // 1794
                this.appState.Egl.ovrEgl_CreateContext(null);


                ConsoleExtensions.trace("AppThreadFunction, call vrapi_GetHmdInfo, then ovrRenderer_Create");
                var hmdInfo = VrApi.vrapi_GetHmdInfo(ref java);

                this.appState.Renderer.ovrRenderer_Create(ref hmdInfo);


                this.headModelParms = VrApi_Helpers.vrapi_DefaultHeadModelParms();


                ConsoleExtensions.trace("AppThreadFunction, enter loop, call ovrMessageQueue_GetNextMessage");
                bool destroyed = false;

                while (!destroyed)
                {
                    //appState.trace60("enter frame, ovrMessageQueue_GetNextMessage");

                    #region ovrMessageQueue_GetNextMessage
                    var ok = true;
                    while (ok)
                    {
                        ovrMessage message;
                        var        waitForMessages = appState.Ovr == null && !destroyed;

                        if (!this.MessageQueue.ovrMessageQueue_GetNextMessage(out message, waitForMessages))
                        {
                            break;
                        }
                        // 1812

                        // no switch for jsc?
                        if (message.Id == MESSAGE.MESSAGE_ON_CREATE)
                        {
                            //ConsoleExtensions.trace("AppThreadFunction, MESSAGE_ON_CREATE");
                        }
                        else if (message.Id == MESSAGE.MESSAGE_ON_START)
                        {
                        }
                        else if (message.Id == MESSAGE.MESSAGE_ON_RESUME)
                        {
                            appState.Resumed = true;
                            //ConsoleExtensions.trace("AppThreadFunction, MESSAGE_ON_RESUME");
                        }
                        else if (message.Id == MESSAGE.MESSAGE_ON_PAUSE)
                        {
                            appState.Resumed = false;
                        }
                        else if (message.Id == MESSAGE.MESSAGE_ON_STOP)
                        {
                        }
                        else if (message.Id == MESSAGE.MESSAGE_ON_DESTROY)
                        {
                            appState.NativeWindow = null; destroyed = true;
                        }
                        else if (message.Id == MESSAGE.MESSAGE_ON_SURFACE_CREATED)
                        {
                            //ConsoleExtensions.trace("AppThreadFunction, MESSAGE_ON_SURFACE_CREATED");
                            var m0 = message[0];
                            appState.NativeWindow = (native_window.ANativeWindow)m0.Pointer;
                        }
                        else if (message.Id == MESSAGE.MESSAGE_ON_SURFACE_DESTROYED)
                        {
                            appState.NativeWindow = null;
                        }
                        else if (message.Id == MESSAGE.MESSAGE_ON_KEY_EVENT)
                        {
                            appState.ovrApp_HandleKeyEvent((keycodes.AKEYCODE)(int) message[0], (input.AInputEventAction)(int) message[1]);
                        }
                        else if (message.Id == MESSAGE.MESSAGE_ON_TOUCH_EVENT)
                        {
                            //ConsoleExtensions.tracei("AppThreadFunction, MESSAGE_ON_TOUCH_EVENT");
                            appState.ovrApp_HandleTouchEvent(message[0], message[1], message[2]);
                        }

                        appState.ovrApp_HandleVrModeChanges();
                    }
                    #endregion

                    // ok
                    //appState.trace60("ovrMessageQueue_GetNextMessage done, ovrApp_BackButtonAction");

                    // ok
                    appState.ovrApp_BackButtonAction();

                    //appState.trace60("ovrApp_BackButtonAction done (ok), ovrApp_HandleSystemEvents (leak?)");

                    appState.ovrApp_HandleSystemEvents();

                    //appState.trace60("ovrApp_HandleSystemEvents done");

                    // not ready yet?
                    // set by vrapi_EnterVrMode
                    if (appState.Ovr == null)
                    {
                        appState.trace60("Ovr == null");
                        continue;
                    }


                    #region VRAPI_FRAME_INIT_LOADING_ICON_FLUSH
                    if (!appState.Scene.CreatedScene)
                    {
                        // need to keep the enum typename?

                        var parms = VrApi_Helpers.vrapi_DefaultFrameParms(ref appState.Java, ovrFrameInit.VRAPI_FRAME_INIT_LOADING_ICON_FLUSH, 0);
                        parms.FrameIndex = appState.FrameIndex;
                        ConsoleExtensions.trace("vrapi_SubmitFrame VRAPI_FRAME_INIT_LOADING_ICON_FLUSH");
                        appState.Ovr.vrapi_SubmitFrame(ref parms);

                        //unistd.usleep(1000);

                        appState.Scene.ovrScene_Create();

                        // keep the loader on for a moment...
                        //unistd.usleep(1000);
                    }
                    #endregion


                    if (xmallinfo() > GLES3JNILib.safemodeMemoryLimitMB * 1024 * 1024)
                    {
                        // I/xNativeActivity(24473): \VrCubeWorld.AppThread.cs:71 mallinfo    maximum total allocated space:  1825611032
                        // https://news.ycombinator.com/item?id=9179833

                        // https://www.youtube.com/watch?v=se2KMs5qrqY
                        ConsoleExtensions.tracei64("safe mode before sleep ", appState.FrameIndex);

                        // slow down VR thread...
                        System.Threading.Thread.Sleep(5000);
                        //unistd.usleep(2000 * 1000);

                        //ConsoleExtensions.tracei64("safe mode after sleep ", appState.FrameIndex);

                        //continue;
                    }



                    //appState.tracei60("AppThreadFunction, FrameIndex ", (int)appState.FrameIndex);

                    // 1862


                    appState.FrameIndex++;


                    //appState.trace60("who is eating our memory?");
                    //System.Threading.Thread.Sleep(1000 / 15);
                    //continue;


                    //ConsoleExtensions.tracei("AppThreadFunction, vrapi_GetPredictedDisplayTime");
                    var predictedDisplayTime = appState.Ovr.vrapi_GetPredictedDisplayTime(appState.FrameIndex);
                    //ConsoleExtensions.tracei("AppThreadFunction, vrapi_GetPredictedTracking");

                    this.trackingOld = this.tracking;
                    this.tracking    = appState.Ovr.vrapi_GetPredictedTracking(predictedDisplayTime);

                    // like step in physics?
                    appState.Simulation.ovrSimulation_AdvanceSimulation(predictedDisplayTime);

                    {
                        //var parms = appState.Renderer.ovrRenderer_RenderFrame(ref appState, ref tracking);
                        //var parms = appState.Renderer.ovrRenderer_RenderFrame(this, appState, ref tracking);
                        //var parms = this.appState.Renderer.ovrRenderer_RenderFrame(this, ref tracking);
                        var parms = this.appState.Renderer.ovrRenderer_RenderFrame(this);


                        //if (tracking.Status == trackingOld.Status)
                        //    appState.tracei60(" tracking.Status ", (int)tracking.Status);
                        //else
                        //    ConsoleExtensions.tracei(" tracking.Status ", (int)tracking.Status);

                        //appState.tracei60(" tracking.HeadPose.Pose.Orientation.x ", (int)(1000 * tracking.HeadPose.Pose.Orientation.x));
                        //appState.tracei60(" tracking.HeadPose.Pose.Orientation.y ", (int)(1000 * tracking.HeadPose.Pose.Orientation.y));
                        //appState.tracei60(" tracking.HeadPose.Pose.Orientation.z ", (int)(1000 * tracking.HeadPose.Pose.Orientation.z));
                        //appState.tracei60(" tracking.HeadPose.Pose.Orientation.w ", (int)(1000 * tracking.HeadPose.Pose.Orientation.w));
                        appState.Ovr.vrapi_SubmitFrame(ref parms);
                    }
                    // 1891
                }

                // 1896
                appState.Renderer.ovrRenderer_Destroy();

                // 1898
                appState.Scene.ovrScene_Destroy();
                appState.Egl.ovrEgl_DestroyContext();
                VrApi.vrapi_Shutdown();

                java.Vm.DetachCurrentThread(java.Vm);
            }
            // sent into vrapi_SubmitFrame
            // will use glMapBufferRange

            // no malloc for this method!
            public ovrFrameParms ovrRenderer_RenderFrame(ovrAppThread appThread)
            {
                // http://www.informit.com/articles/article.aspx?p=2033340&seqNum=2
                // X:\jsc.svn\examples\javascript\WebGL\WebGLSpadeWarrior\WebGLSpadeWarrior\Application.cs

                //ConsoleExtensions.tracei("enter ovrRenderer_RenderFrame, VRAPI_FRAME_INIT_DEFAULT");

                // can other processes/non ndk stream a surface to us?
                // local socket?
                // shared memory?
                // editn n continue?
                // 1049

                ovrFrameParms parms = VrApi_Helpers.vrapi_DefaultFrameParms(ref appThread.appState.Java, ovrFrameInit.VRAPI_FRAME_INIT_DEFAULT, 0u);

                parms.FrameIndex    = appThread.appState.FrameIndex;
                parms.MinimumVsyncs = appThread.appState.MinimumVsyncs;


                var scale0 = 1.5f;

                var scale = scale0 *
                            (1.0f + (float)Math.Sin(appThread.appState.FrameIndex * 0.05f) * 0.15f);



                // for gearvr we get do pre set all cubes?


                // Calculate the center view matrix.
                //ConsoleExtensions.tracei("ovrRenderer_RenderFrame, vrapi_DefaultHeadModelParms");


                //tracking.HeadPose.Pose.Orientation.y += appState.FrameIndex * 0.05f;

                //var tracking1 = appThread.tracking;

                // no thats not any better
                //tracking1.HeadPose.Pose.Orientation.x += GLES3JNILib.fields_mousey * 0.005f;

                //ConsoleExtensions.tracei("ovrRenderer_RenderFrame, vrapi_GetCenterEyeViewMatrix");

                // https://sites.google.com/a/jsc-solutions.net/work/knowledge-base/15-dualvr/20150703/mousex
                // https://sites.google.com/a/jsc-solutions.net/work/knowledge-base/15-dualvr/20150705

                // __mat4.rotate(pMatrix, pMatrix, degToRad(-mx), new float[] { 0, 1f, 0f });
                var centerEyeViewMatrix1 = __Matrix4x4.CreateRotation(0, GLES3JNILib.fields_mousex * 0.005f, 0);

                // ovrMatrix4f_Inverse
                //var centerEyeViewMatrix = VrApi_Helpers.vrapi_GetCenterEyeViewMatrix(ref appThread.headModelParms, ref appThread.tracking, ref centerEyeViewMatrix1);

                var centerEyeViewMatrix0 = VrApi_Helpers.vrapi_GetCenterEyeViewMatrix(ref appThread.headModelParms, ref appThread.tracking, input: null);


                //var centerEyeViewMatrix2 = __ovrMatrix4f.CreateTranslation(0, 0, (float)Math.Sin(appThread.appState.FrameIndex * 0.05f));
                //var centerEyeViewMatrix2 = __ovrMatrix4f.CreateTranslation(0, (float)Math.Sin(appThread.appState.FrameIndex * 0.01f), 0);
                var centerEyeViewMatrix2 = __Matrix4x4.CreateTranslation(

                    ovrScene.wasd_x0
                    ,
                    //(float)(0.1f * Math.Sin(appThread.appState.FrameIndex * 0.01f))
                    //+
                    ovrScene.wasd_z0

                    ,


                    ovrScene.wasd_y0
                    );

                //var centerEyeViewMatrix = __ovrMatrix4f.Multiply(centerEyeViewMatrix0, centerEyeViewMatrix1, centerEyeViewMatrix2);
                var centerEyeViewMatrix = __Matrix4x4.Multiply(
                    __Matrix4x4.Multiply(centerEyeViewMatrix0, centerEyeViewMatrix1),
                    centerEyeViewMatrix2);

                // 1077

                // NUM_EYES is length of RenderTextures
                for (int eye = 0; eye < NUM_EYES; eye++)
                {
                    fixed(ovrRenderTexture *RenderTexture = &appThread.appState.Renderer.RenderTextures[appThread.appState.Renderer.BufferIndex, eye])
                    {
                        // jni/OVRWindWheelNDK.dll.c:1095:197: error: invalid type argument of '->' (have 'struct tag_Java_com_oculus_gles3jni_GLES3JNILib_ByteArrayWithLength')

                        var loc_vertexTransform = GLES3JNILib.fields_vertexTransform;

                        //// 1085
                        //appState.tracei60("ovrRenderer_RenderFrame, ovrRenderTexture_SetCurrent BufferIndex ", appState.Renderer.BufferIndex);
                        //appState.tracei60("ovrRenderer_RenderFrame, ovrRenderTexture_SetCurrent eye ", eye);
                        //appThread.appState.tracei60("ovrRenderer_RenderFrame, vertexTransform.Length: ", loc_vertexTransform.BufferLength);

                        // using?
                        gl3.glBindFramebuffer(gl3.GL_FRAMEBUFFER, RenderTexture->FrameBuffer);


                        gl3.glEnable(gl3.GL_SCISSOR_TEST);
                        gl3.glDepthMask(true);
                        gl3.glEnable(gl3.GL_DEPTH_TEST);
                        gl3.glDepthFunc(gl3.GL_LEQUAL);
                        gl3.glViewport(0, 0, RenderTexture->Width, RenderTexture->Height);
                        gl3.glScissor(0, 0, RenderTexture->Width, RenderTexture->Height);
                        //gl3.glClearColor(0.125f, 0.0f, 0.125f, 1.0f);
                        //gl3.glClearColor(0.9f, 0.0f, 0.125f, 1.0f);

                        //
                        //gl3.glClearColor(0.0f, 0.0f, 1.0f, 0.1f);
                        gl3.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
                        //gl3.glClearColor(0.0f, 0.0f, Math.Abs(appThread.tracking.HeadPose.Pose.Orientation.z + 0.7f) / 1.4f, 0.0f);
                        //gl3.glClearColor(0.0f, 0.0f, Math.Abs(appThread.tracking.HeadPose.Pose.Orientation.x + 0.7f) / 1.4f, 0.0f);
                        //gl3.glClearColor(0.0f, 0.0f, Math.Abs(appThread.tracking.HeadPose.Pose.Orientation.x) / 1.00f, 0.0f);
                        gl3.glClear(gl3.GL_COLOR_BUFFER_BIT | gl3.GL_DEPTH_BUFFER_BIT);

                        // using
                        gl3.glUseProgram(appThread.appState.Scene.Program.Program);

                        // 1094


                        // thisone is pretty static.
                        //  uniform mat4 ProjectionMatrix;
                        //gl3.glUniformMatrix4fv(appThread.appState.Scene.Program.Uniforms[(int)ovrUniform_index.UNIFORM_PROJECTION_MATRIX], 1, true, (float*)ref_ProjectionMatrix);
                        gl3.glUniformMatrix4fv(appThread.appState.Scene.Program.Uniforms[(int)ovrUniform_index.UNIFORM_PROJECTION_MATRIX], 1, true, ref appThread.appState.Renderer.ProjectionMatrix);



                        // https://sites.google.com/a/jsc-solutions.net/work/knowledge-base/15-dualvr/20150618/ovrmatrix4f
                        var eyeViewMatrix = __Matrix4x4.GetEyeViewMatrix(ref appThread.headModelParms, ref centerEyeViewMatrix, eye);

                        //  uniform mat4 ViewMatrix;
                        gl3.glUniformMatrix4fv(appThread.appState.Scene.Program.Uniforms[(int)ovrUniform_index.UNIFORM_VIEW_MATRIX], 1, true, ref eyeViewMatrix);



                        // well at this point we should be able to select the type of cube we are about to draw...


                        var red_or_green = (appThread.appState.FrameIndex / 60) % 3;

                        // we are essentially reseting the tempplate here. udp could help us here.
                        if (red_or_green == 0)
                        {
                            appThread.appState.Scene.Cube.BindBufferData_red();
                        }
                        else if (red_or_green == 1)
                        {
                            appThread.appState.Scene.Cube.BindBufferData_green();
                        }
                        else
                        {
                            appThread.appState.Scene.Cube.BindBufferData_yellow();
                        }



                        #region InstanceTransformBuffer
                        var sizeof_ovrMatrix4f = sizeof(ovrMatrix4f);
                        gl3.glBindBuffer(gl3.GL_ARRAY_BUFFER, appThread.appState.Scene.InstanceTransformBuffer0);

                        var gpuInstanceTransformBuffer = gl3.glMapBufferRange <ovrMatrix4f>(
                            gl3.GL_ARRAY_BUFFER, 0,
                            // do we need marshal.getsize?
                            NUM_INSTANCES * sizeof_ovrMatrix4f,

                            // the first gl3 members, the other are gl2 apis
                            gl3.GL_MAP_WRITE_BIT | gl3.GL_MAP_INVALIDATE_BUFFER_BIT
                            );

                        // 1057

                        // what if we frame update only half of em?
                        //for (int i = 0; i < NUM_INSTANCES; i++)

                        // they dissapear!

                        //var dynamiccount = NUM_INSTANCES / 2 + (appThread.appState.FrameIndex / (60 * 5)) % (NUM_INSTANCES / 2);
                        var dynamiccount = NUM_INSTANCES;

                        for (int i = 0; i < dynamiccount; i++)
                        {
                            //ConsoleExtensions.tracei("ovrRenderer_RenderFrame, ovrMatrix4f_CreateRotation i ", i);


                            //ConsoleExtensions.tracei("ovrRenderer_RenderFrame, ovrMatrix4f_CreateTranslation i ", i);

                            // VR thread wants to know where our stuff is. lowframe thread changes data it may look choppy.
                            //var translation = VrApi_Helpers.ovrMatrix4f_CreateTranslation(
                            //    appState.Scene.CubePositions[i].x,
                            //    appState.Scene.CubePositions[i].y,
                            //    appState.Scene.CubePositions[i].z
                            //);


                            var rfloor = (i % floors);



                            //ConsoleExtensions.tracei("ovrRenderer_RenderFrame, ovrMatrix4f_Multiply i ", i);
                            //var transform = VrApi_Helpers.ovrMatrix4f_Multiply(ref translation, ref rotation);

                            //var scale2 = default(ovrMatrix4f);

                            //fixed (float* ff = scale2.M)

                            //scale2.m
                            //{
                            //    transform.M[0 + 4 * 0] = 2;

                            // glsl program not set up do do the scaling?
                            //translation.M[1 + 4 * 1] = 0.002f;
                            //translation.M[2 + 4 * 2] = 0.002f;
                            //translation.M[3 + 4 * 3] = 0.002f;
                            //}

                            if (rfloor == 0)
                            {
                                var translation = __Matrix4x4.CreateTranslation(
                                    appThread.appState.Scene.CubePositions[i].x,
                                    appThread.appState.Scene.CubePositions[i].y,
                                    appThread.appState.Scene.CubePositions[i].z,

                                    //scale: 1.0f
                                    //scale: 0.15f
                                    scale: scale0
                                    );

                                gpuInstanceTransformBuffer[i] = __Matrix4x4.Transpose(&translation);
                            }
                            else
                            {
                                var translation = __Matrix4x4.CreateTranslation(
                                    appThread.appState.Scene.CubePositions[i].x,
                                    appThread.appState.Scene.CubePositions[i].y

                                    + GLES3JNILib.fields_mousewheel * 0.1f

                                    //* (1.0f + (GLES3JNILib.fields_mousewheel * 0.5f))

                                    ,

                                    //  moves left to right
                                    appThread.appState.Scene.CubePositions[i].z,
                                    //appThread.appState.Scene.CubePositions[i].z + GLES3JNILib.fields_mousewheel,

                                    scale:
                                    scale
                                    //* (1.0f + (GLES3JNILib.fields_mousewheel * 0.1f))

                                    );

                                var rotation = __Matrix4x4.CreateRotation(
                                    0,
                                    (float)Math.Cos(appThread.appState.FrameIndex * 0.05f)
                                    ,
                                    0
                                    //appState.Scene.CubeRotations[i].x * appState.Simulation.CurrentRotation.x,
                                    //appState.Scene.CubeRotations[i].y * appState.Simulation.CurrentRotation.y,
                                    //appState.Scene.CubeRotations[i].z * appState.Simulation.CurrentRotation.z
                                    );

                                var transform2 = __Matrix4x4.Multiply(&translation, &rotation);

                                // what does it do??um. it messes up the display otherwise?
                                //var transpose = VrApi_Helpers.ovrMatrix4f_Transpose(ref translation);
                                var transpose = __Matrix4x4.Transpose(&transform2);

                                //transpose.M[0 + 4 * 0] = 2;
                                //transpose.M[1 + 4 * 1] = 2;
                                //transpose.M[2 + 4 * 2] = 2;

                                //ConsoleExtensions.tracei("ovrRenderer_RenderFrame, ubeTransforms[i] = transpose ", i);

                                // cuz landscape/portrait???
                                gpuInstanceTransformBuffer[i] = transpose;
                            }
                            //gpuInstanceTransformBuffer[i] = translation;
                        }

                        // 1070
                        gl3.glUnmapBuffer(gl3.GL_ARRAY_BUFFER);

                        // whats the next buffer to be bound?
                        gl3.glBindBuffer(gl3.GL_ARRAY_BUFFER, 0);
                        #endregion


                        gl3.glBindVertexArray(appThread.appState.Scene.Cube.VertexArrayObject0);
                        gl3.glDrawElementsInstanced(gl3.GL_TRIANGLES, appThread.appState.Scene.Cube.IndexCount, gl3.GL_UNSIGNED_SHORT, null, NUM_INSTANCES);
                        // gl3.glDrawElementsInstanced(gl3.GL_TRIANGLES, appThread.appState.Scene.Cube.IndexCount, gl3.GL_UNSIGNED_SHORT, null, (int)dynamiccount);
                        gl3.glBindVertexArray(0);
                        // landscape done...



                        //var mvMatrixStack = default(mat4stack256);



                        //Action<>

                        // whats the memory going to do?
                        #region specialDraw
                        Action_ffff specialDraw = (x, y, z, xyzscale) =>
                        {
                            #region special draw
                            appThread.appState.Scene.Cube.BindBufferData_yellow();

                            //gl3.glBindBuffer(gl3.GL_ARRAY_BUFFER, appThread.appState.Scene.InstanceTransformBuffer1);
                            gl3.glBindBuffer(gl3.GL_ARRAY_BUFFER, appThread.appState.Scene.InstanceTransformBuffer0);


                            var gpuInstanceTransformBuffer1 = gl3.glMapBufferRange <ovrMatrix4f>(
                                gl3.GL_ARRAY_BUFFER, 0,
                                // do we need marshal.getsize?
                                NUM_INSTANCES1 * sizeof_ovrMatrix4f,

                                // the first gl3 members, the other are gl2 apis
                                gl3.GL_MAP_WRITE_BIT | gl3.GL_MAP_INVALIDATE_BUFFER_BIT
                                );

                            var translation1 = __Matrix4x4.CreateTranslation(
                                x: x,
                                y: y,
                                z: z,
                                scale: xyzscale
                                );

                            gpuInstanceTransformBuffer1[0] = __Matrix4x4.Transpose(&translation1);


                            gl3.glUnmapBuffer(gl3.GL_ARRAY_BUFFER);


                            // would we know how to prep and use multiple distinct VertexArrayObjects ?
                            gl3.glBindVertexArray(appThread.appState.Scene.Cube.VertexArrayObject0);
                            gl3.glDrawElementsInstanced(gl3.GL_TRIANGLES, appThread.appState.Scene.Cube.IndexCount, gl3.GL_UNSIGNED_SHORT, null, (int)1);
                            gl3.glBindVertexArray(0);


                            // whats the next buffer to be bound?
                            gl3.glBindBuffer(gl3.GL_ARRAY_BUFFER, 0);
                            #endregion
                        };
                        #endregion



                        // would be cool if we could use generics someday. jsc rewriter would need to bake em?
                        specialDraw(10, 0, 20, 8f * 8);


                        // https://twitter.com/sfnet_ops/status/621859945487581184
                        // sf.net is down. yikes.
                        specialDraw(-4, 0, 4, scale);
                        specialDraw(-4, 1, 4, scale);
                        specialDraw(-4 - .4f, 1, 4, scale);
                        specialDraw(-4 + .4f, 1, 4, scale);



                        #region UDPDraw
                        Action UDPDraw = () =>
                        {
                            #region special draw
                            appThread.appState.Scene.Cube.BindBufferData_yellow();
                            //appThread.appState.Scene.Cube.BindBufferData_red();

                            //gl3.glBindBuffer(gl3.GL_ARRAY_BUFFER, appThread.appState.Scene.InstanceTransformBuffer1);
                            gl3.glBindBuffer(gl3.GL_ARRAY_BUFFER, appThread.appState.Scene.InstanceTransformBuffer0);



                            var UDPMatrixCount = loc_vertexTransform.BufferLength / 64;

                            //appThread.appState.tracei60("ovrRenderer_RenderFrame, vertexTransform UDPMatrixCount: ", UDPMatrixCount);

                            var gpuInstanceTransformBuffer1 = gl3.glMapBufferRange <ovrMatrix4f>(
                                gl3.GL_ARRAY_BUFFER, 0,
                                // do we need marshal.getsize?
                                UDPMatrixCount * sizeof_ovrMatrix4f,

                                // the first gl3 members, the other are gl2 apis
                                gl3.GL_MAP_WRITE_BIT | gl3.GL_MAP_INVALIDATE_BUFFER_BIT
                                );


                            // implictop from stack
                            //ovrMatrix4f current = mvMatrixStack;
                            //var current = mvMatrixStack.Peek();



                            for (int UDPMatrixIndex = 0; UDPMatrixIndex < UDPMatrixCount; UDPMatrixIndex++)
                            {
                                //appThread.appState.tracei60("ovrRenderer_RenderFrame, vertexTransform UDPMatrixIndex: ", UDPMatrixIndex);
                                //ConsoleExtensions.tracei("ovrRenderer_RenderFrame, vertexTransform UDPMatrixIndex: ", UDPMatrixIndex);


                                //var xyz = __Matrix4x4.CreateTranslation(
                                //  x: -4 + .4f + (appThread.appState.FrameIndex % 60) * 0.01f,

                                //  y: 0,

                                //  // can we make it float?
                                //  z: 3,

                                //  // or animate its size?
                                //    scale: 1.0f
                                //);

                                var xyz = default(ovrMatrix4f);

                                __Matrix4x4.Set(ref xyz, loc_vertexTransform.Buffer, UDPMatrixIndex);

                                gpuInstanceTransformBuffer1[UDPMatrixIndex] = __Matrix4x4.Transpose(&xyz);
                            }

                            //appThread.appState.tracei60("ovrRenderer_RenderFrame UDPDraw lets use loc_vertexTransform");
                            //ConsoleExtensions.trace("ovrRenderer_RenderFrame UDPDraw lets use loc_vertexTransform");

                            gl3.glUnmapBuffer(gl3.GL_ARRAY_BUFFER);

                            // would we know how to prep and use multiple distinct VertexArrayObjects ?
                            gl3.glBindVertexArray(appThread.appState.Scene.Cube.VertexArrayObject0);
                            //gl3.glDrawElementsInstanced(gl3.GL_LINES, appThread.appState.Scene.Cube.IndexCount, gl3.GL_UNSIGNED_SHORT, null, (int)UDPMatrixCount);
                            //gl3.glDrawElementsInstanced(gl3.GL_LINE_LOOP, appThread.appState.Scene.Cube.IndexCount, gl3.GL_UNSIGNED_SHORT, null, (int)UDPMatrixCount);
                            gl3.glDrawElementsInstanced(gl3.GL_TRIANGLES, appThread.appState.Scene.Cube.IndexCount, gl3.GL_UNSIGNED_SHORT, null, (int)UDPMatrixCount);
                            gl3.glBindVertexArray(0);


                            // whats the next buffer to be bound?
                            gl3.glBindBuffer(gl3.GL_ARRAY_BUFFER, 0);
                            #endregion
                        };
                        #endregion


                        //// using?

                        ////var mvMatrix0 = __Matrix4x4.CreateTranslation(
                        ////  x: -4 + .4f + (appThread.appState.FrameIndex % 60) * 0.01f,

                        ////  y: 0,

                        ////  // can we make it float?
                        ////  z: 3

                        ////  // or animate its size?
                        ////    //scale: 0.1f
                        ////);


                        ////// [1]=mvMatrix
                        ////mvMatrixStack.Push(mvMatrix0);

                        UDPDraw();
                        //mvMatrixStackDraw(-0, 1.4f, 0, 0.12f);
                        //mvMatrixStackDraw(-0, 1.8f, 0, 0.13f);
                        //mvMatrixStackDraw(-0, 2.2f, 0, 0.14f);
                        //mvMatrixStackDraw(0, 1, 1, 0.1f);
                        //mvMatrixStackDraw(0, 1, -1, 0.1f);

                        //mvMatrixStack.Pop();



                        gl3.glUseProgram(0);

                        // 1104

                        // what happens if we dont?

                        #region Explicitly clear the border texels to black because OpenGL-ES does not support GL_CLAMP_TO_BORDER.
                        {
                            // Clear to fully opaque black.
                            gl3.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
                            // bottom
                            gl3.glScissor(0, 0, RenderTexture->Width, 1);
                            gl3.glClear(gl3.GL_COLOR_BUFFER_BIT);
                            // top
                            gl3.glScissor(0, RenderTexture->Height - 1, RenderTexture->Width, 1);
                            gl3.glClear(gl3.GL_COLOR_BUFFER_BIT);
                            // left
                            gl3.glScissor(0, 0, 1, RenderTexture->Height);
                            gl3.glClear(gl3.GL_COLOR_BUFFER_BIT);
                            // right
                            gl3.glScissor(RenderTexture->Width - 1, 0, 1, RenderTexture->Height);
                            gl3.glClear(gl3.GL_COLOR_BUFFER_BIT);
                        }
                        #endregion

                        //// 1119
                        #region ovrRenderTexture_Resolve
                        // 763
                        // Discard the depth buffer, so the tiler won't need to write it back out to memory.
                        gl3.glInvalidateFramebuffer(gl3.GL_FRAMEBUFFER, 1, depthAttachment);
                        // Flush this frame worth of commands.
                        gl3.glFlush();
                        #endregion



                        parms.Layers[(int)ovrFrameLayerType.VRAPI_FRAME_LAYER_TYPE_WORLD].Images[eye].TexId = RenderTexture->FrameBufferColorTexture;
                        parms.Layers[(int)ovrFrameLayerType.VRAPI_FRAME_LAYER_TYPE_WORLD].Images[eye].TexCoordsFromTanAngles = appThread.appState.Renderer.TanAngleMatrix;
                        parms.Layers[(int)ovrFrameLayerType.VRAPI_FRAME_LAYER_TYPE_WORLD].Images[eye].HeadPose = appThread.tracking.HeadPose;
                    }
                }

                gl3.glBindFramebuffer(gl3.GL_FRAMEBUFFER, 0);

                appThread.appState.Renderer.BufferIndex = (appThread.appState.Renderer.BufferIndex + 1) % NUM_BUFFERS;

                // 1130
                //appState.tracei60("exit ovrRenderer_RenderFrame BufferIndex", appState.Renderer.BufferIndex);
                //ConsoleExtensions.tracei("exit ovrRenderer_RenderFrame");
                return(parms);
            }