Example #1
0
        // Set up the texture objects.
        static bool arglSetupTextureObjects(ARGL_CONTEXT_SETTINGS contextSettings)
        {
            int textureWrapMode;

            // Delete previous textures, unless this is our first time here.
            if (contextSettings.textureObjectsHaveBeenSetup)
            {
                GL1.ActiveTexture(All1.Texture0);
                GL1.BindTexture(All1.Texture2D, 0);
                GL1.DeleteTextures(1, ref contextSettings.texture);
                contextSettings.textureObjectsHaveBeenSetup = false;
            }

            GL1.GenTextures(1, out contextSettings.texture);
            GL1.ActiveTexture(All1.Texture0);
            GL1.BindTexture(All1.Texture2D, contextSettings.texture);
            GL1.TexParameterx(All1.Texture2D, All1.TextureMinFilter, (int)All1.Linear);
            GL1.TexParameterx(All1.Texture2D, All1.TextureMagFilter, (int)All1.Linear);
            // Decide whether we can use GL_CLAMP_TO_EDGE.
            //if (arglGLCapabilityCheck(0x0120, (unsigned char *)"GL_SGIS_texture_edge_clamp")) {
            textureWrapMode = (int)All1.ClampToEdge;
            //} else {
            //    textureWrapMode = (int)All1.Repeat;
            //}
            GL1.TexParameterx(All1.Texture2D, All1.TextureWrapS, textureWrapMode);
            GL1.TexParameterx(All1.Texture2D, All1.TextureWrapT, textureWrapMode);

            contextSettings.textureObjectsHaveBeenSetup = true;
            return(true);
        }
Example #2
0
        static bool arglPixelBufferDataUpload(ARGL_CONTEXT_SETTINGS contextSettings, byte[] bufDataPtr)
        {
            if (contextSettings == null)
            {
                return(false);
            }
            if (contextSettings.textureObjectsHaveBeenSetup || contextSettings.textureGeometryHasBeenSetup || contextSettings.pixSize == 0)
            {
                return(false);
            }

            GL1.ActiveTexture(All1.Texture0);
            GL1.BindTexture(All1.Texture2D, contextSettings.texture);

            GL1.PixelStore(All1.UnpackAlignment, (((contextSettings.bufSizeX * contextSettings.pixSize) & 0x3) == 0 ? 4 : 1));

            if (contextSettings.bufSizeIsTextureSize)
            {
                GL1.TexImage2D(All1.Texture2D, 0, (OpenTK.Graphics.ES11.All)contextSettings.pixIntFormat, contextSettings.textureSizeX, contextSettings.textureSizeY, 0, (All1)contextSettings.pixFormat, (All1)contextSettings.pixType, bufDataPtr);
            }
            else
            {
                // Request OpenGL allocate memory internally for a power-of-two texture of the appropriate size.
                // Then send the NPOT-data as a subimage.
                GL1.TexImage2D(All1.Texture2D, 0, (OpenTK.Graphics.ES11.All)contextSettings.pixIntFormat, contextSettings.textureSizeX, contextSettings.textureSizeY, 0, (All1)contextSettings.pixFormat, (All1)contextSettings.pixType, IntPtr.Zero);
                GL1.TexSubImage2D(All1.Texture2D, 0, 0, 0, contextSettings.bufSizeX, contextSettings.bufSizeY, (All1)contextSettings.pixFormat, (All1)contextSettings.pixType, bufDataPtr);
            }

            contextSettings.textureDataReady = true;

            return(true);
        }
        public void DrawInRect(Rectangle rect)
        {
            float[] coordinates = { 0, _maxT, _maxS, _maxT, 0, 0, _maxS, 0 };
            float[] vertices    = { rect.Left, rect.Top, 0.0f, rect.Right, rect.Top, 0.0f, rect.Left, rect.Bottom, 0.0f, rect.Right, rect.Bottom, 0.0f };

            GL11.BindTexture(ALL11.Texture2D, _name);
            GL11.VertexPointer(3, ALL11.Float, 0, vertices);
            GL11.TexCoordPointer(2, ALL11.Float, 0, coordinates);
            GL11.DrawArrays(ALL11.TriangleStrip, 0, 4);
        }
        public void DrawAtPoint(Vector2 point)
        {
            float[] coordinates = { 0, _maxT, _maxS, _maxT, 0, 0, _maxS, 0 };
            float   width       = (float)_width * _maxS;
            float   height      = (float)_height * _maxT;

            float[] vertices = { -width / 2.0f + point.X, -height / 2.0f + point.Y, 0.0f,
                                 width / 2.0f + point.X,  -height / 2.0f + point.Y, 0.0f,
                                 -width / 2.0f + point.X, height / 2.0f + point.Y,  0.0f,
                                 width / 2.0f + point.X,  height / 2.0f + point.Y, 0.0f };

            GL11.BindTexture(ALL11.Texture2D, _name);
            GL11.VertexPointer(3, ALL11.Float, 0, vertices);
            GL11.TexCoordPointer(2, ALL11.Float, 0, coordinates);
            GL11.DrawArrays(ALL11.TriangleStrip, 0, 4);
        }
Example #5
0
        private void allocateOpenGLTexture()
        {
            // modeled after this
            // http://steinsoft.net/index.php?site=Programming/Code%20Snippets/OpenGL/no9

            // Allocate the space needed for the texture
            GL11.BindTexture(All11.Texture2D, this.textureId);

            // it seems like we do not need to allocate any buffer space
            //byte[] data = new byte[_width * _height * 4];
            // Use offset instead of pointer to indictate that we want to use data copied from a PBO
            //GL.TexImage2D (TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, _width, _height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, data);
            GL11.TexImage2D(All11.Texture2D, 0, (int)All11.Rgba, _width, _height, 0, All11.Rgba, All11.UnsignedByte, IntPtr.Zero);

            GL11.BindTexture(All11.Texture2D, 0);
            //data = null;
        }
Example #6
0
        static void arglCleanup(ARGL_CONTEXT_SETTINGS contextSettings)
        {
            if (contextSettings == null)
            {
                return;                          // Sanity check.
            }
            if (contextSettings.textureObjectsHaveBeenSetup)
            {
                GL1.ActiveTexture(All1.Texture0);
                GL1.BindTexture(All1.Texture2D, 0);
                GL1.DeleteTextures(1, ref contextSettings.texture);
            }

            if (contextSettings.textureGeometryHasBeenSetup)
            {
                GL1.DeleteBuffers(1, ref contextSettings.t2bo);
                GL1.DeleteBuffers(1, ref contextSettings.v2bo);
            }

            contextSettings = null;
        }
Example #7
0
        protected override void OnRenderFrame(FrameEventArgs e)
        {
            base.OnRenderFrame(e);

            MakeCurrent();

            SetupView();

            GL1.Clear((int)(All.ColorBufferBit | All.DepthBufferBit));          // Clear The Screen And The Depth Buffer
            GL1.LoadIdentity();                                                 // Reset The Current Modelview Matrix

            GL1.Translate(0.0f, 0.0f, z);                                       // Move Right And Into The Screen
            GL1.Rotate(xrot, 1.0f, 0.0f, 0.0f);                                 // Rotate On The X Axis By xrot
            GL1.Rotate(yrot, 0.0f, 1.0f, 0.0f);                                 // Rotate On The Y Axis By yrot

            GL1.BindTexture(All1.Texture2D, texture[filter]);                   // Select A Texture Based On filter


            float [] cubeVerticies =
            {
                -1.0f,  1.0f, 1.0f,                                             // Top Left Of The Quad (Front)
                -1.0f, -1.0f, 1.0f,                                             // Bottom Left Of The Quad (Front)
                1.0f,   1.0f, 1.0f,                                             // Top Right Of The Quad (Front)
                1.0f,  -1.0f, 1.0f,                                             // Bottom Right Of The Quad (Front)

                -1.0f, -1.0f, -1.0f,                                            // Bottom Left Of The Quad (Back)
                -1.0f,  1.0f, -1.0f,                                            // Top Left Of The Quad (Back)
                1.0f,  -1.0f, -1.0f,                                            // Bottom Right Of The Quad (Back)
                1.0f,   1.0f, -1.0f,                                            // Top Right Of The Quad (Back)

                -1.0f,  1.0f, -1.0f,                                            // Top Left Of The Quad (Top)
                -1.0f,  1.0f, 1.0f,                                             // Bottom Left Of The Quad (Top)
                1.0f,   1.0f, -1.0f,                                            // Top Right Of The Quad (Top)
                1.0f,   1.0f, 1.0f,                                             // Bottom Right Of The Quad (Top)

                -1.0f, -1.0f, -1.0f,                                            // Top Left Of The Quad (Bottom)
                1.0f,  -1.0f, -1.0f,                                            // Top Right Of The Quad (Bottom)
                -1.0f, -1.0f, 1.0f,                                             // Bottom Left Of The Quad (Bottom)
                1.0f,  -1.0f, 1.0f,                                             // Bottom Right Of The Quad (Bottom)

                1.0f,  -1.0f, -1.0f,                                            // Bottom Right Of The Quad (Right)
                1.0f,   1.0f, -1.0f,                                            // Top Right Of The Quad (Right)
                1.0f,  -1.0f, 1.0f,                                             // Bottom Left Of The Quad (Right)
                1.0f,   1.0f, 1.0f,                                             // Top Left Of The Quad (Right)

                -1.0f, -1.0f, -1.0f,                                            // Bottom Right Of The Quad (Left)
                -1.0f, -1.0f, 1.0f,                                             // Bottom Left Of The Quad (Left)
                -1.0f,  1.0f, -1.0f,                                            // Top Right Of The Quad (Left)
                -1.0f,  1.0f, 1.0f,                                             // Top Left Of The Quad (Left)
            };

            float [] cubeTexs =
            {
                0.0f, 0.0f,                                                                             // Top Left Of The Quad (Front)
                0.0f, 1.0f,                                                                             // Bottom Left Of The Quad (Front)
                1.0f, 0.0f,                                                                             // Top Right Of The Quad (Front)
                1.0f, 1.0f,                                                                             // Bottom Right Of The Quad (Front)

                1.0f, 1.0f,                                                                             // Bottom Left Of The Quad (Back)
                1.0f, 0.0f,                                                                             // Top Left Of The Quad (Back)
                0.0f, 1.0f,                                                                             // Bottom Right Of The Quad (Back)
                0.0f, 0.0f,                                                                             // Top Right Of The Quad (Back)

                0.0f, 0.0f,                                                                             // Top Left Of The Quad (Top)
                0.0f, 1.0f,                                                                             // Bottom Left Of The Quad (Top)
                1.0f, 0.0f,                                                                             // Top Right Of The Quad (Top)
                1.0f, 1.0f,                                                                             // Bottom Right Of The Quad (Top)

                1.0f, 0.0f,                                                                             // Top Left Of The Quad (Bottom)
                0.0f, 0.0f,                                                                             // Top Right Of The Quad (Bottom)
                1.0f, 1.0f,                                                                             // Bottom Left Of The Quad (Bottom)
                0.0f, 1.0f,                                                                             // Bottom Right Of The Quad (Bottom)

                1.0f, 1.0f,                                                                             // Bottom Right Of The Quad (Right)
                1.0f, 0.0f,                                                                             // Top Right Of The Quad (Right)
                0.0f, 1.0f,                                                                             // Bottom Left Of The Quad (Right)
                0.0f, 0.0f,                                                                             // Top Left Of The Quad (Right)

                0.0f, 1.0f,                                                                             // Bottom Right Of The Quad (Left)
                1.0f, 1.0f,                                                                             // Bottom Left Of The Quad (Left)
                0.0f, 0.0f,                                                                             // Top Right Of The Quad (Left)
                1.0f, 0.0f,                                                                             // Top Left Of The Quad (Left)
            };

            float [] cubeNormals =
            {
                0.0f,   0.0f, 1.0f,                                                             // Top Left Of The Quad (Front)
                0.0f,   0.0f, 1.0f,                                                             // Bottom Left Of The Quad (Front)
                0.0f,   0.0f, 1.0f,                                                             // Top Right Of The Quad (Front)
                0.0f,   0.0f, 1.0f,                                                             // Bottom Right Of The Quad (Front)

                0.0f,   0.0f, -1.0f,                                                            // Bottom Left Of The Quad (Back)
                0.0f,   0.0f, -1.0f,                                                            // Top Left Of The Quad (Back)
                0.0f,   0.0f, -1.0f,                                                            // Bottom Right Of The Quad (Back)
                0.0f,   0.0f, -1.0f,                                                            // Top Right Of The Quad (Back)

                0.0f,   1.0f, 0.0f,                                                             // Top Left Of The Quad (Top)
                0.0f,   1.0f, 0.0f,                                                             // Bottom Left Of The Quad (Top)
                0.0f,   1.0f, 0.0f,                                                             // Top Right Of The Quad (Top)
                0.0f,   1.0f, 0.0f,                                                             // Bottom Right Of The Quad (Top)

                0.0f,  -1.0f, 0.0f,                                                             // Top Left Of The Quad (Bottom)
                0.0f,  -1.0f, 0.0f,                                                             // Top Right Of The Quad (Bottom)
                0.0f,  -1.0f, 0.0f,                                                             // Bottom Left Of The Quad (Bottom)
                0.0f,  -1.0f, 0.0f,                                                             // Bottom Right Of The Quad (Bottom)

                1.0f,   0.0f, 0.0f,                                                             // Bottom Right Of The Quad (Right)
                1.0f,   0.0f, 0.0f,                                                             // Top Right Of The Quad (Right)
                1.0f,   0.0f, 0.0f,                                                             // Bottom Left Of The Quad (Right)
                1.0f,   0.0f, 0.0f,                                                             // Top Left Of The Quad (Right)

                -1.0f,  0.0f, 0.0f,                                                             // Bottom Right Of The Quad (Left)
                -1.0f,  0.0f, 0.0f,                                                             // Bottom Left Of The Quad (Left)
                -1.0f,  0.0f, 0.0f,                                                             // Top Right Of The Quad (Left)
                -1.0f,  0.0f, 0.0f,                                                             // Top Left Of The Quad (Left)
            };

            GL1.VertexPointer(3, All1.Float, 0, cubeVerticies);
            GL1.EnableClientState(All1.VertexArray);

            GL1.TexCoordPointer(2, All1.Float, 0, cubeTexs);
            GL1.EnableClientState(All1.TextureCoordArray);

            GL1.NormalPointer(All1.Float, 0, cubeNormals);
            GL1.EnableClientState(All1.NormalArray);

            GL1.DrawArrays(All1.TriangleStrip, 0, 4);

            GL1.DrawArrays(All1.TriangleStrip, 4, 4);

            GL1.DrawArrays(All1.TriangleStrip, 8, 4);

            GL1.DrawArrays(All1.TriangleStrip, 12, 4);

            GL1.DrawArrays(All1.TriangleStrip, 16, 4);

            GL1.DrawArrays(All1.TriangleStrip, 20, 4);

            SwapBuffers();

            xrot += xspeed;                             // Add xspeed To xrot
            yrot += yspeed;                             // Add yspeed To yrot

            handleButtons();
        }
Example #8
0
        public void DrawBatch11(SpriteSortMode sortMode)
        {
            // nothing to do
            if (_batchItemList.Count == 0)
            {
                return;
            }

            // sort the batch items
            switch (sortMode)
            {
            case SpriteSortMode.Texture:
                _batchItemList.Sort(CompareTexture);
                break;

            case SpriteSortMode.FrontToBack:
                _batchItemList.Sort(CompareDepth);
                break;

            case SpriteSortMode.BackToFront:
                _batchItemList.Sort(CompareReverseDepth);
                break;
            }

            // make sure an old draw isn't still going on.
            // cross fingers, commenting this out!!
            //GL.Flush();
            int size = VertexPosition2ColorTexture.GetSize();

            GL11.VertexPointer(2, All11.Float, size, _vertexHandle.AddrOfPinnedObject());
            GL11.ColorPointer(4, All11.UnsignedByte, size, (IntPtr)((uint)_vertexHandle.AddrOfPinnedObject() + (uint)(sizeof(float) * 2)));
            GL11.TexCoordPointer(2, All11.Float, size, (IntPtr)((uint)_vertexHandle.AddrOfPinnedObject() + (uint)(sizeof(float) * 2 + sizeof(uint))));

            // setup the vertexArray array
            int startIndex = 0;
            int index      = 0;
            int texID      = -1;

            // make sure the vertexArray has enough space
            if (_batchItemList.Count * 4 > _vertexArray.Length)
            {
                ExpandVertexArray(_batchItemList.Count);
            }

            foreach (SpriteBatchItem item in _batchItemList)
            {
                // if the texture changed, we need to flush and bind the new texture
                if (item.TextureID != texID)
                {
                    FlushVertexArray11(startIndex, index);
                    startIndex = index;
                    texID      = item.TextureID;
                    GL11.BindTexture(All11.Texture2D, texID);
                }
                // store the SpriteBatchItem data in our vertexArray
                _vertexArray[index++] = item.vertexTL;
                _vertexArray[index++] = item.vertexTR;
                _vertexArray[index++] = item.vertexBL;
                _vertexArray[index++] = item.vertexBR;

                _freeBatchItemQueue.Enqueue(item);
            }
            // flush the remaining vertexArray data
            FlushVertexArray11(startIndex, index);

            _batchItemList.Clear();
        }
Example #9
0
        static void arglDispImageStateful(ARGL_CONTEXT_SETTINGS contextSettings)
        {
            int texEnvModeSave;
            int i;

            if (contextSettings == null)
            {
                return;
            }
            if (contextSettings.textureObjectsHaveBeenSetup)
            {
                return;
            }
            if (contextSettings.textureGeometryHasBeenSetup)
            {
                return;
            }
            if (contextSettings.textureDataReady)
            {
                return;
            }

            GL1.ActiveTexture(All1.Texture0);

            GL1.MatrixMode(All1.Texture);
            GL1.LoadIdentity();
            GL1.MatrixMode(All1.Modelview);

            GL1.BindTexture(All1.Texture2D, contextSettings.texture);
            GL1.GetTexEnv(All1.TextureEnv, All1.TextureEnvMode, out texEnvModeSave); // Save GL texture environment mode.
            if (texEnvModeSave != (int)All1.Replace)
            {
                GL1.TexEnv(All1.TextureEnv, All1.TextureEnvMode, (int)All1.Replace);
            }
            GL1.Enable(All1.Texture2D);

            GL1.ClientActiveTexture(All1.Texture0);
            GL1.BindBuffer(All1.ArrayBuffer, contextSettings.t2bo);
            GL1.TexCoordPointer(2, All1.Float, 0, IntPtr.Zero);
            GL1.EnableClientState(All1.TextureCoordArray);

            GL1.BindBuffer(All1.ArrayBuffer, contextSettings.v2bo);
            GL1.VertexPointer(2, All1.Float, 0, IntPtr.Zero);
            GL1.EnableClientState(All1.VertexArray);
            GL1.DisableClientState(All1.NormalArray);

            if (contextSettings.disableDistortionCompensation)
            {
                GL1.DrawArrays(All1.TriangleStrip, 0, 4);
            }
            else
            {
                for (i = 0; i < 20; i++)
                {
                    GL1.DrawArrays(All1.TriangleStrip, i * 42, 42);
                }
            }

            GL1.BindBuffer(All1.ArrayBuffer, 0);
            GL1.DisableClientState(All1.VertexArray);
            GL1.DisableClientState(All1.TextureCoordArray);

            GL1.Disable(All1.Texture2D);
            if (texEnvModeSave != (int)All1.Replace)
            {
                GL1.TexEnv(All1.TextureEnv, All1.TextureEnvMode, texEnvModeSave);                                      // Restore GL texture environment mode.
            }
        }
Example #10
0
 private void setTexture(Texture2D texture)
 {
     GL11.BindTexture(All11.Texture2D, texture.Image.Name);
 }
Example #11
0
        public void DrawBatchGL11(SpriteSortMode sortMode, SamplerState samplerState)
        {
            // nothing to do
            if (_batchItemList.Count == 0)
            {
                return;
            }

            // sort the batch items
            switch (sortMode)
            {
            case SpriteSortMode.Texture:
                throw new NotSupportedException();

            //_batchItemList.Sort( CompareTexture );
            case SpriteSortMode.FrontToBack:
                throw new NotSupportedException();

            //_batchItemList.Sort ( CompareDepth );
            case SpriteSortMode.BackToFront:
                throw new NotSupportedException();
                //_batchItemList.Sort ( CompareReverseDepth );
            }

            // make sure an old draw isn't still going on.
            // cross fingers, commenting this out!!
            //GL.Flush();

            // make sure the vertexArray has enough space
            if (_batchItemList.Count * 6 > _index.Length)
            {
                ExpandIndexArray(_batchItemList.Count);
            }

            LinkVertexArray();

            int size = sizeof(float) * 4 + sizeof(uint);

            GL11.VertexPointer(2, ALL11.Float, size, _vertexHandle.AddrOfPinnedObject());
            GL11.ColorPointer(4, ALL11.UnsignedByte, size, (IntPtr)((uint)_vertexHandle.AddrOfPinnedObject() + (uint)(sizeof(float) * 2)));
            GL11.TexCoordPointer(2, ALL11.Float, size, (IntPtr)((uint)_vertexHandle.AddrOfPinnedObject() + (uint)(sizeof(float) * 2 + sizeof(uint))));

            // setup the vertexArray array
            int startIndex = 0;
            int index      = 0;
            int texID      = -1;


            foreach (SpriteBatchItem item in _batchItemList)
            {
                // if the texture changed, we need to flush and bind the new texture
                if (item.TextureID != texID)
                {
                    FlushVertexArrayGL11(startIndex, index);
                    startIndex = index;
                    texID      = item.TextureID;
                    GL11.BindTexture(ALL11.Texture2D, texID);

                    samplerState.Activate();
                }
                // store the SpriteBatchItem data in our vertexArray
                index += 4;
            }
            // flush the remaining vertexArray data
            FlushVertexArrayGL11(startIndex, index);

            _batchItemList.Clear();
            _vertexArray.Clear();
        }
        public void InitWithData(IntPtr data, SurfaceFormat pixelFormat, int width, int height, Size size, ALL11 filter, ALL11 wrap)
        {
            if (GraphicsDevice.OpenGLESVersion == OpenTK.Graphics.GLContextVersion.Gles2_0)
            {
                if (!MathHelper.IsPowerOfTwo(width) || !MathHelper.IsPowerOfTwo(height))
                {
                    filter = ALL11.Linear;
                    wrap   = ALL11.ClampToEdge;
                }
                GL20.GenTextures(1, ref _name);
                GL20.BindTexture(ALL20.Texture2D, _name);
                GL20.TexParameter(ALL20.Texture2D, ALL20.TextureMinFilter, (int)filter);
                GL20.TexParameter(ALL20.Texture2D, ALL20.TextureMagFilter, (int)filter);
                GL20.TexParameter(ALL20.Texture2D, ALL20.TextureWrapS, (int)wrap);
                GL20.TexParameter(ALL20.Texture2D, ALL20.TextureWrapT, (int)wrap);

                switch (pixelFormat)
                {
                case SurfaceFormat.Color /*kTexture2DPixelFormat_RGBA8888*/:
                case SurfaceFormat.Dxt1:
                case SurfaceFormat.Dxt3:
                    //sz = 4;
                    GL20.TexImage2D(ALL20.Texture2D, 0, (int)ALL20.Rgba, (int)width, (int)height, 0, ALL20.Rgba, ALL20.UnsignedByte, data);
                    break;

                case SurfaceFormat.Bgra4444 /*kTexture2DPixelFormat_RGBA4444*/:
                    //sz = 2;
                    GL20.TexImage2D(ALL20.Texture2D, 0, (int)ALL20.Rgba, (int)width, (int)height, 0, ALL20.Rgba, ALL20.UnsignedShort4444, data);
                    break;

                case SurfaceFormat.Bgra5551 /*kTexture2DPixelFormat_RGB5A1*/:
                    //sz = 2;
                    GL20.TexImage2D(ALL20.Texture2D, 0, (int)ALL20.Rgba, (int)width, (int)height, 0, ALL20.Rgba, ALL20.UnsignedShort5551, data);
                    break;

                case SurfaceFormat.Alpha8 /*kTexture2DPixelFormat_A8*/:
                    //sz = 1;
                    GL20.TexImage2D(ALL20.Texture2D, 0, (int)ALL20.Alpha, (int)width, (int)height, 0, ALL20.Alpha, ALL20.UnsignedByte, data);
                    break;

                default:
                    throw new NotSupportedException("Texture format");
                }
            }
            else
            {
                GL11.GenTextures(1, ref _name);
                GL11.BindTexture(ALL11.Texture2D, _name);
                GL11.TexParameter(ALL11.Texture2D, ALL11.TextureMinFilter, (int)filter);
                GL11.TexParameter(ALL11.Texture2D, ALL11.TextureMagFilter, (int)filter);
                GL11.TexParameter(ALL11.Texture2D, ALL11.TextureWrapS, (int)ALL11.ClampToEdge);
                GL11.TexParameter(ALL11.Texture2D, ALL11.TextureWrapT, (int)ALL11.ClampToEdge);

                switch (pixelFormat)
                {
                case SurfaceFormat.Color /*kTexture2DPixelFormat_RGBA8888*/:
                case SurfaceFormat.Dxt1:
                case SurfaceFormat.Dxt3:
                    GL11.TexImage2D(ALL11.Texture2D, 0, (int)ALL11.Rgba, (int)width, (int)height, 0, ALL11.Rgba, ALL11.UnsignedByte, data);
                    break;

                case SurfaceFormat.Bgra4444 /*kTexture2DPixelFormat_RGBA4444*/:
                    GL11.TexImage2D(ALL11.Texture2D, 0, (int)ALL11.Rgba, (int)width, (int)height, 0, ALL11.Rgba, ALL11.UnsignedShort4444, data);
                    break;

                case SurfaceFormat.Bgra5551 /*kTexture2DPixelFormat_RGB5A1*/:
                    GL11.TexImage2D(ALL11.Texture2D, 0, (int)ALL11.Rgba, (int)width, (int)height, 0, ALL11.Rgba, ALL11.UnsignedShort5551, data);
                    break;

                case SurfaceFormat.Alpha8 /*kTexture2DPixelFormat_A8*/:
                    GL11.TexImage2D(ALL11.Texture2D, 0, (int)ALL11.Alpha, (int)width, (int)height, 0, ALL11.Alpha, ALL11.UnsignedByte, data);
                    break;

                default:
                    throw new NotSupportedException("Texture format");
                }
            }

            _size   = size;
            _width  = width;
            _height = height;
            _format = pixelFormat;
            _maxS   = size.Width / (float)_width;
            _maxT   = size.Height / (float)_height;
        }
        public void InitWithBitmap(Bitmap imageSource, ALL11 filter, ALL11 wrap)
        {
            //TODO:  Android.Opengl.GLUtils.GetInternalFormat()
            try
            {
                _format = SurfaceFormat.Color;
                if (imageSource.HasAlpha)
                {
                    _format = SurfaceFormat.Color;
                }

                if (GraphicsDevice.OpenGLESVersion == OpenTK.Graphics.GLContextVersion.Gles2_0)
                {
                    _width  = imageSource.Width;
                    _height = imageSource.Height;

                    // There are rules for npot textures that we must abide by (wrap = ClampToEdge and filter = Nearest or Linear)
                    if (!MathHelper.IsPowerOfTwo(_width) || !MathHelper.IsPowerOfTwo(_height))
                    {
                        filter = ALL11.Linear;
                        wrap   = ALL11.ClampToEdge;
                    }
                }
                else
                {
                    //scale up bitmap to be power of 2 dimensions but dont exceed 1024x1024.
                    //Note: may not have to do this with OpenGL 2+
                    _width  = (int)Math.Pow(2, Math.Min(10, Math.Ceiling(Math.Log10(imageSource.Width) / Math.Log10(2))));
                    _height = (int)Math.Pow(2, Math.Min(10, Math.Ceiling(Math.Log10(imageSource.Height) / Math.Log10(2))));
                }

                _size.Width  = _width;
                _size.Height = _height;

                if (GraphicsDevice.OpenGLESVersion ==
                    OpenTK.Graphics.GLContextVersion.Gles2_0)
                {
                    GL20.GenTextures(1, ref _name);
                }
                else
                {
                    GL11.GenTextures(1, ref _name);
                }

                if (_name == 0)
                {
                    _originalBitmap = imageSource;
                    _originalFilter = filter;
                    _originalWrap   = wrap;
                    PrimaryThreadLoader.AddToList(this);
                    _textureCreated = false;
                }
                else
                {
                    _originalBitmap = null;
                    _textureCreated = true;

                    using (
                        Bitmap imagePadded = Bitmap.CreateBitmap(_width, _height,
                                                                 Bitmap.Config.Argb8888)
                        )
                    {
                        using (Canvas can = new Canvas(imagePadded))
                        {
                            can.DrawARGB(0, 0, 0, 0);

                            if (AndroidCompatibility.ScaleImageToPowerOf2)
                            {
                                can.DrawBitmap(imageSource, new Rect(0, 0, imageSource.Width, imageSource.Height), new Rect(0, 0, _width, _height), null);          //Scale to texture
                            }
                            else
                            {
                                can.DrawBitmap(imageSource, 0, 0, null);
                            }

                            if (GraphicsDevice.OpenGLESVersion ==
                                OpenTK.Graphics.GLContextVersion.Gles2_0)
                            {
                                GL20.BindTexture(ALL20.Texture2D, _name);
                                GL20.TexParameter(ALL20.Texture2D, ALL20.TextureMinFilter,
                                                  (int)filter);
                                GL20.TexParameter(ALL20.Texture2D, ALL20.TextureMagFilter,
                                                  (int)filter);
                                GL20.TexParameter(ALL20.Texture2D, ALL20.TextureWrapS, (int)wrap);
                                GL20.TexParameter(ALL20.Texture2D, ALL20.TextureWrapT, (int)wrap);
                                Android.Opengl.GLUtils.TexImage2D((int)ALL20.Texture2D, 0,
                                                                  imagePadded, 0);

                                // error checking
                                //int errAndroidGL = Android.Opengl.GLES20.GlGetError();
                                //ALL20 errGenericGL = GL20.GetError();
                                //if (errAndroidGL != Android.Opengl.GLES20.GlNoError || errGenericGL != ALL20.NoError)
                                //    Console.WriteLine(string.Format("OpenGL ES 2.0:\n\tAndroid error: {0,10:X}\n\tGeneric error: {1, 10:X}", errAndroidGL, errGenericGL));
                            }
                            else
                            {
                                GL11.BindTexture(ALL11.Texture2D, _name);
                                GL11.TexParameter(ALL11.Texture2D, ALL11.TextureMinFilter,
                                                  (int)filter);
                                GL11.TexParameter(ALL11.Texture2D, ALL11.TextureMagFilter,
                                                  (int)filter);
                                GL11.TexParameter(ALL11.Texture2D, ALL11.TextureWrapS, (int)wrap);
                                GL11.TexParameter(ALL11.Texture2D, ALL11.TextureWrapT, (int)wrap);
                                GL11.TexParameter(ALL11.Texture2D, ALL11.GenerateMipmap, 1);
                                Android.Opengl.GLUtils.TexImage2D((int)ALL11.Texture2D, 0,
                                                                  imagePadded, 0);
                            }
                        }
                    }
                }
            }
            finally
            {
                if (_originalBitmap != imageSource)
                {
                    // free bitmap
                    imageSource.Dispose();
                }
            }

            _maxS = _size.Width / (float)_width;
            _maxT = _size.Height / (float)_height;
        }