コード例 #1
0
        private void Load()
        {
            System.IO.Stream fs = null;
            try
            {
                IGL10 gl        = _glGraphics.GL10;
                int[] textureId = new int[1];
                gl.GlGenTextures(1, textureId, 0);
                _textureId = textureId [0];
                fs         = _fileIO.ReadAsset(_fileName);
                Bitmap b = BitmapFactory.DecodeStream(fs);

                gl.GlBindTexture(GL10.GlTexture2d, _textureId);

                SetFilters(GL10.GlNearest, GL10.GlNearest);

                Android.Opengl.GLUtils.TexImage2D(GL10.GlTexture2d, 0, b, 0);

                b.Recycle();
            }
            catch
            {
                throw new Java.Lang.RuntimeException("Couldn't load texture from asset");
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
            }
        }
コード例 #2
0
            /// <summary>
            /// 加载位图生成纹理
            /// </summary>
            /// <param name="gl"></param>
            private void loadTexture(IGL10 gl)
            {
                Bitmap bitmap = null;

                try
                {
                    // 加载位图
                    bitmap = BitmapFactory.DecodeResource(ma.Resources, Resource.Drawable.sand);
                    int[] textures = new int[1];
                    // 指定生成N个纹理(第一个参数指定生成一个纹理)
                    // textures数组将负责存储所有纹理的代号
                    gl.GlGenTextures(1, textures, 0);
                    // 获取textures纹理数组中的第一个纹理
                    texture = textures[0];
                    // 通知OpenGL将texture纹理绑定到GL10.GL_TEXTURE_2D目标中
                    gl.GlBindTexture(GL10.GlTexture2d, texture);
                    // 设置纹理被缩小(距离视点很远时被缩小)时的滤波方式
                    gl.GlTexParameterf(GL10.GlTexture2d, GL10.GlTextureMinFilter, GL10.GlNearest);
                    // 设置纹理被放大(距离视点很近时被方法)时的滤波方式
                    gl.GlTexParameterf(GL10.GlTexture2d, GL10.GlTextureMagFilter, GL10.GlLinear);
                    // 设置在横向、纵向上都是平铺纹理
                    gl.GlTexParameterf(GL10.GlTexture2d, GL10.GlTextureWrapS, GL10.GlRepeat);
                    gl.GlTexParameterf(GL10.GlTexture2d, GL10.GlTextureWrapT, GL10.GlRepeat);
                    // 加载位图生成纹理
                    GLUtils.TexImage2D(GL10.GlTexture2d, 0, bitmap, 0);
                }
                finally
                {
                    // 生成纹理之后,回收位图
                    if (bitmap != null)
                    {
                        bitmap.Recycle();
                    }
                }
            }
コード例 #3
0
        int LoadTexture(string assetName)
        {
            try
            {
                Bitmap b   = BitmapFactory.DecodeStream(game.FileIO.ReadAsset(assetName));
                IGL10  gl  = _glGraphics.GL10;
                int[]  tId = new int[1];
                gl.GlGenTextures(1, tId, 0);
                int textureId = tId[0];
                gl.GlBindTexture(GL10.GlTexture2d, textureId);

                gl.GlTexParameterf(GL10.GlTexture2d, GL10.GlTextureMinFilter, GL10.GlNearest);
                gl.GlTexParameterf(GL10.GlTexture2d, GL10.GlTextureMagFilter, GL10.GlNearest);

                Android.Opengl.GLUtils.TexImage2D(GL10.GlTexture2d, 0, b, 0);

                b.Recycle();
                return(textureId);
            }
            catch
            {
                throw new Java.Lang.RuntimeException("Couldn't load asset");
            }
        }
コード例 #4
0
        public void OnDrawFrame(IGL10 gl)
        {
            // First allocate texture if there is not one yet.
            if (DRAW_TEXTURE && mTextureIds == null)
            {
                // Generate texture.
                mTextureIds = new int[2];
                gl.GlGenTextures(2, mTextureIds, 0);
                foreach (int textureId in mTextureIds)
                {
                    // Set texture attributes.
                    gl.GlBindTexture(GL10.GlTexture2d, textureId);
                    gl.GlTexParameterf(GL10.GlTexture2d,
                                       GL10.GlTextureMinFilter, GL10.GlNearest);
                    gl.GlTexParameterf(GL10.GlTexture2d,
                                       GL10.GlTextureMagFilter, GL10.GlNearest);
                    gl.GlTexParameterf(GL10.GlTexture2d, GL10.GlTextureWrapS,
                                       GL10.GlClampToEdge);
                    gl.GlTexParameterf(GL10.GlTexture2d, GL10.GlTextureWrapT,
                                       GL10.GlClampToEdge);
                }
            }

            if (DRAW_TEXTURE && mTexturePage.TexturesChanged)
            {
                gl.GlBindTexture(GL10.GlTexture2d, mTextureIds[0]);
                Bitmap texture = mTexturePage.GetTexture(mTextureRectFront,
                                                         CurlPage.SIDE_FRONT);
                GLUtils.TexImage2D(GL10.GlTexture2d, 0, texture, 0);
                texture.Recycle();

                mTextureBack = mTexturePage.HasBackTexture;
                if (mTextureBack)
                {
                    gl.GlBindTexture(GL10.GlTexture2d, mTextureIds[1]);
                    texture = mTexturePage.GetTexture(mTextureRectBack,
                                                      CurlPage.SIDE_BACK);
                    GLUtils.TexImage2D(GL10.GlTexture2d, 0, texture, 0);
                    texture.Recycle();
                }
                else
                {
                    mTextureRectBack.Set(mTextureRectFront);
                }

                mTexturePage.Recycle();
                Reset();
            }

            // Some 'global' settings.
            gl.GlEnableClientState(GL10.GlVertexArray);

            // TODO: Drop shadow drawing is done temporarily here to hide some
            // problems with its calculation.
            if (DRAW_SHADOW)
            {
                gl.GlDisable(GL10.GlTexture2d);
                gl.GlEnable(GL10.GlBlend);
                gl.GlBlendFunc(GL10.GlSrcAlpha, GL10.GlOneMinusSrcAlpha);
                gl.GlEnableClientState(GL10.GlColorArray);
                gl.GlColorPointer(4, GL10.GlFloat, 0, mBufShadowColors);
                gl.GlVertexPointer(3, GL10.GlFloat, 0, mBufShadowVertices);
                gl.GlDrawArrays(GL10.GlTriangleStrip, 0, mDropShadowCount);
                gl.GlDisableClientState(GL10.GlColorArray);
                gl.GlDisable(GL10.GlBlend);
            }

            if (DRAW_TEXTURE)
            {
                gl.GlEnableClientState(GL10.GlTextureCoordArray);
                gl.GlTexCoordPointer(2, GL10.GlFloat, 0, mBufTexCoords);
            }
            gl.GlVertexPointer(3, GL10.GlFloat, 0, mBufVertices);
            // Enable color array.
            gl.GlEnableClientState(GL10.GlColorArray);
            gl.GlColorPointer(4, GL10.GlFloat, 0, mBufColors);

            // Draw front facing blank vertices.
            gl.GlDisable(GL10.GlTexture2d);
            gl.GlDrawArrays(GL10.GlTriangleStrip, 0, mVerticesCountFront);

            // Draw front facing texture.
            if (DRAW_TEXTURE)
            {
                gl.GlEnable(GL10.GlBlend);
                gl.GlEnable(GL10.GlTexture2d);

                if (!mFlipTexture || !mTextureBack)
                {
                    gl.GlBindTexture(GL10.GlTexture2d, mTextureIds[0]);
                }
                else
                {
                    gl.GlBindTexture(GL10.GlTexture2d, mTextureIds[1]);
                }

                gl.GlBlendFunc(GL10.GlSrcAlpha, GL10.GlOneMinusSrcAlpha);
                gl.GlDrawArrays(GL10.GlTriangleStrip, 0, mVerticesCountFront);

                gl.GlDisable(GL10.GlBlend);
                gl.GlDisable(GL10.GlTexture2d);
            }

            int backStartIdx = Math.Max(0, mVerticesCountFront - 2);
            int backCount    = mVerticesCountFront + mVerticesCountBack - backStartIdx;

            // Draw back facing blank vertices.
            gl.GlDrawArrays(GL10.GlTriangleStrip, backStartIdx, backCount);

            // Draw back facing texture.
            if (DRAW_TEXTURE)
            {
                gl.GlEnable(GL10.GlBlend);
                gl.GlEnable(GL10.GlTexture2d);

                if (mFlipTexture || !mTextureBack)
                {
                    gl.GlBindTexture(GL10.GlTexture2d, mTextureIds[0]);
                }
                else
                {
                    gl.GlBindTexture(GL10.GlTexture2d, mTextureIds[1]);
                }

                gl.GlBlendFunc(GL10.GlSrcAlpha, GL10.GlOneMinusSrcAlpha);
                gl.GlDrawArrays(GL10.GlTriangleStrip, backStartIdx, backCount);

                gl.GlDisable(GL10.GlBlend);
                gl.GlDisable(GL10.GlTexture2d);
            }

            // Disable textures and color array.
            gl.GlDisableClientState(GL10.GlTextureCoordArray);
            gl.GlDisableClientState(GL10.GlColorArray);

            if (DRAW_POLYGON_OUTLINES)
            {
                gl.GlEnable(GL10.GlBlend);
                gl.GlBlendFunc(GL10.GlSrcAlpha, GL10.GlOneMinusSrcAlpha);
                gl.GlLineWidth(1.0f);
                gl.GlColor4f(0.5f, 0.5f, 1.0f, 1.0f);
                gl.GlVertexPointer(3, GL10.GlFloat, 0, mBufVertices);
                gl.GlDrawArrays(GL10.GlLineStrip, 0, mVerticesCountFront);
                gl.GlDisable(GL10.GlBlend);
            }

            if (DRAW_CURL_POSITION)
            {
                gl.GlEnable(GL10.GlBlend);
                gl.GlBlendFunc(GL10.GlSrcAlpha, GL10.GlOneMinusSrcAlpha);
                gl.GlLineWidth(1.0f);
                gl.GlColor4f(1.0f, 0.5f, 0.5f, 1.0f);
                gl.GlVertexPointer(2, GL10.GlFloat, 0, mBufCurlPositionLines);
                gl.GlDrawArrays(GL10.GlLines, 0, mCurlPositionLinesCount * 2);
                gl.GlDisable(GL10.GlBlend);
            }

            if (DRAW_SHADOW)
            {
                gl.GlEnable(GL10.GlBlend);
                gl.GlBlendFunc(GL10.GlSrcAlpha, GL10.GlOneMinusSrcAlpha);
                gl.GlEnableClientState(GL10.GlColorArray);
                gl.GlColorPointer(4, GL10.GlFloat, 0, mBufShadowColors);
                gl.GlVertexPointer(3, GL10.GlFloat, 0, mBufShadowVertices);
                gl.GlDrawArrays(GL10.GlTriangleStrip, mDropShadowCount,
                                mSelfShadowCount);
                gl.GlDisableClientState(GL10.GlColorArray);
                gl.GlDisable(GL10.GlBlend);
            }

            gl.GlDisableClientState(GL10.GlVertexArray);
        }