最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 正文

用Android OpenGL画Bitmap的方法

来源:动视网 责编:小OO 时间:2025-09-23 23:46:54
文档

用Android OpenGL画Bitmap的方法

用AndroidOpenGL画Bitmap的方法HaiyongLei用OpenGLAPI画BITMAP,BITMAP的W、H须为2的倍数,比如32、、512。注意代码里的Vertices和Texture的坐标顺序。importandroid.support.v7.app.AppCompatActivity;importandroid.os.Bundle;importandroid.graphics.Bitmap;importandroid.graphics.BitmapFactory;im
推荐度:
导读用AndroidOpenGL画Bitmap的方法HaiyongLei用OpenGLAPI画BITMAP,BITMAP的W、H须为2的倍数,比如32、、512。注意代码里的Vertices和Texture的坐标顺序。importandroid.support.v7.app.AppCompatActivity;importandroid.os.Bundle;importandroid.graphics.Bitmap;importandroid.graphics.BitmapFactory;im
用Android OpenGL画Bitmap的方法

Haiyong Lei

用OpenGL API画BITMAP,BITMAP的W、H须为2的倍数,比如32、、512。

注意代码里的Vertices和Texture的坐标顺序。

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.opengl.GLSurfaceView;

import android.opengl.GLSurfaceView.Renderer;

import android.opengl.GLUtils;

import javax.microedition.khronos.egl.EGLConfig;

import javax.microedition.khronos.opengles.GL10;

import java.nio.ByteBuffer;

import java.nio.ByteOrder;

import java.nio.FloatBuffer;

public class MainActivity extends AppCompatActivity {

    Bitmap bitmap = null;

    class BitmapRender implements Renderer {

        /* the border of bitmap

             V3 ------ V4

             |         |

             |         |

             |         |

             V1 ------ V2

         */

        float borderVerticesCoord[] = {

                // x, y, z

                -1.0f, -1.0f, 0.0f,     // V1 - left bottom

                 1.0f, -1.0f, 0.0f,     // V2 - right bottom

                -1.0f,  1.0f, 0.0f,     // V3 - left top

                 1.0f,  1.0f, 0.0f,     // V4 - right top

        };

        float bitmapTextureCoord[] = {

                // x, y

                0.0f, 1.0f, // V3 - left top

                1.0f, 1.0f, // V4 - right top

                0.0f, 0.0f, // V1 - left bottom

                1.0f, 0.0f, // V2 - right bottom

        };

        FloatBuffer borderVerticesBuffer = null;

        FloatBuffer bitmapTextureBuffer = null;

        int bitmapTextureHandle = -1;

        private void loadBitmapTexture(GL10 gl) {

            int[] textures = new int[1];

            gl.glGenTextures(1, textures, 0);

            bitmapTextureHandle = textures[0];

            gl.glBindTexture(GL10.GL_TEXTURE_2D, bitmapTextureHandle);

            gl.glTexParameterf(GL10.GL_TEXTURE_2D,

                    GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

            gl.glTexParameterf(GL10.GL_TEXTURE_2D,

                    GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);

            //gl.glTexParameterf(GL10.GL_TEXTURE_2D,

            //        GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);

            //gl.glTexParameterf(GL10.GL_TEXTURE_2D,

            //        GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);

            GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

        }

        private void deleteBitmapTexture(GL10 gl) {

            int[] textures = new int[1];

            textures[0] = bitmapTextureHandle;

            gl.glDeleteTextures(1, textures, 0);

        }

        private void buildBitmapTextureBuffer() {

            ByteBuffer tb = ByteBuffer.allocateDirect(bitmapTextureCoord.length * 4);

            tb.order(ByteOrder.nativeOrder());

            bitmapTextureBuffer = tb.asFloatBuffer();

            bitmapTextureBuffer.put(bitmapTextureCoord);

            bitmapTextureBuffer.position(0);

        }

        private void buildBorderVertexBuffer() {

            ByteBuffer vb = ByteBuffer.allocateDirect(borderVerticesCoord.length * 4);

            vb.order(ByteOrder.nativeOrder());

            borderVerticesBuffer = vb.asFloatBuffer();

            borderVerticesBuffer.put(borderVerticesCoord);

            borderVerticesBuffer.position(0);

        }

        private void drawBitmap(GL10 gl) {

            gl.glBindTexture(GL10.GL_TEXTURE_2D, bitmapTextureHandle);

            gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

            gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

            if (borderVerticesBuffer == null)

                buildBorderVertexBuffer();

            if (bitmapTextureBuffer == null)

                buildBitmapTextureBuffer();

            gl.glVertexPointer(3, GL10.GL_FLOAT, 0, borderVerticesBuffer);

            gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, bitmapTextureBuffer);

            gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, borderVerticesCoord.length / 3);

            gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

            gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

        }

        @Override

        public void onSurfaceCreated(GL10 gl, EGLConfig config) {

            gl.glEnable(GL10.GL_TEXTURE_2D);

        }

        @Override

        public void onSurfaceChanged(GL10 gl, int width, int height) {

            gl.glClearColor(0, 0, 0, 1);

            gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

            gl.glViewport(0, 0, width, height);

        }

        @Override

        public void onDrawFrame(GL10 gl) {

            if (bitmapTextureHandle == -1)

                loadBitmapTexture(gl);

            drawBitmap(gl);

        }

    }

    GLSurfaceView glSurfaceView = null;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.lotus);

        glSurfaceView = new GLSurfaceView(this);

        glSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0); // need by emulator

        glSurfaceView.setRenderer(new BitmapRender());

        setContentView(glSurfaceView);

    }

    @Override

    public void onPause() {

        glSurfaceView.onPause();

        super.onPause();

    }

    @Override

    public void onResume() {

        super.onResume();

        glSurfaceView.onResume();

    }

}

文档

用Android OpenGL画Bitmap的方法

用AndroidOpenGL画Bitmap的方法HaiyongLei用OpenGLAPI画BITMAP,BITMAP的W、H须为2的倍数,比如32、、512。注意代码里的Vertices和Texture的坐标顺序。importandroid.support.v7.app.AppCompatActivity;importandroid.os.Bundle;importandroid.graphics.Bitmap;importandroid.graphics.BitmapFactory;im
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top