读书人

Android OpenGL ES画金字塔并为每一边

发布时间: 2012-06-29 15:48:46 作者: rapoo

Android OpenGL ES画金字塔并为每一面贴不同的纹理图
睡觉之前再记录一点,也算是自己学习的小总结吧,前段时间总能看见有人问怎么为一个物体的每一面贴不同的纹理图,我这有个为金字塔形状四面贴不同纹理图的例子,拿出来整理分享一下,有需要的同志们可以看一看。
不废话了,上代码,
第一步: Main.java
在入口Activity中定义一个内部类,用于获取Bitmap用的

class BitGL {public static Bitmap bitmap;public static void init(Resources resources) {bitmap = BitmapFactory.decodeResource(resources, R.drawable.walla);}}


第二步:在MySurfaceView.java中的onSurfaceCreated()方法中初始化金字塔类,并赋相应的值
py = new Pyramid(gl,15.0f, 2.6f, 5.0f, 0, mContext); // 初始化金字塔
绘制金字塔的时候在onDrawFrame()方法中调用py.drawSlef(gl); 画金字塔


第三步:然后就是画金字塔并贴图了,注意,这是一个单独的类
public class Pyramid {Context mContext = null;private int one = 0x10000;public float mAngleX;public float mAngleY;private IntBuffer mVertexBuffer;private FloatBuffer mTexBuffer;FloatBuffer lightDiffuse = FloatBuffer.wrap(new float[] { 0.5f, 0.5f, 0.5f,1.0f });FloatBuffer specularParams = FloatBuffer.wrap(new float[] { 0.5f, 1.0f,0.5f, 1.0f });FloatBuffer lightPosition = FloatBuffer.wrap(new float[] { 0.3f, 0.0f,2.0f, 1.0f });int vertices[] = { 0, one, 0, -one, -one, one, one, -one, one,0, one, 0, one, -one, one, one, -one, -one,0, one, 0, one, -one, -one, -one, -one, -one,0, one, 0, -one, -one, -one, -one, -one, one };    //纹理点    private int[] texCoords = {               0, one,one,             one,0, 0,             one, 0    }; float x, y, z;boolean isY = true;private Bitmap bitmap;private int[] textureids=null;private IntBuffer texBuffer;private Bitmap[] bit =new Bitmap[4];public Pyramid(GL10 gl,float x, float y, float z, float rot,Context context) {        this.mContext = context;this.x = x;this.y = y;this.z = z;ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);vbb.order(ByteOrder.nativeOrder());mVertexBuffer = vbb.asIntBuffer();mVertexBuffer.put(vertices);mVertexBuffer.position(0); // 初始化        textureids = new int[4];         bit[0]=BitmapFactory.decodeResource(mContext.getResources(), R.drawable.walla);        bit[1]=BitmapFactory.decodeResource(mContext.getResources(), R.drawable.wallb);        bit[2]=BitmapFactory.decodeResource(mContext.getResources(), R.drawable.walld);        bit[3]=BitmapFactory.decodeResource(mContext.getResources(), R.drawable.wallf);                // 实例化bitmap        bitmap = BitGL.bitmap;        ByteBuffer tbbs = ByteBuffer.allocateDirect(texCoords.length * 3 * 4);        tbbs.order(ByteOrder.nativeOrder());        texBuffer = tbbs.asIntBuffer();        //为每一个面贴上纹理        for (int i = 0; i < 3; i++) {            texBuffer.put(texCoords);        }        texBuffer.position(0);         /*********贴图开始********///打开纹理        gl.glEnable(GL10.GL_TEXTURE_2D);        IntBuffer ib = IntBuffer.allocate(4);        gl.glGenTextures(4, ib);        textureids = ib.array();                //gl.glGenTextures(4, textureids, 0); //4个纹理个数,用于四面        // 绑定要使用的纹理        gl.glBindTexture(GL10.GL_TEXTURE_2D, textureids[0]);        // 生成纹理        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bit[0], 0);        // 线性滤波        gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,                GL10.GL_LINEAR);        gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,                GL10.GL_LINEAR);         gl.glBindTexture(GL10.GL_TEXTURE_2D, textureids[1]);        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bit[1], 0);        gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,                GL10.GL_LINEAR);        gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,                GL10.GL_LINEAR);                 gl.glBindTexture(GL10.GL_TEXTURE_2D, textureids[2]);        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bit[2], 0);        gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,                GL10.GL_LINEAR);        gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,                GL10.GL_LINEAR);                 gl.glBindTexture(GL10.GL_TEXTURE_2D, textureids[3]);        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bit[3], 0);        gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,                GL10.GL_LINEAR);        gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,                GL10.GL_LINEAR);         /*********贴图结束********/}public void drawSlef(GL10 gl) {gl.glPushMatrix();gl.glTranslatef(x, y, z); // 绘制对象的位置//gl.glRotatef(mAngleX, 0, 1, 0); // 对象的X旋转角度//gl.glRotatef(mAngleY, 1, 0, 0); // 对象的Y旋转角度gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);// 使用纹理数组gl.glVertexPointer(3, gl.GL_FIXED, 0, mVertexBuffer);        gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, texBuffer); // Definefor (int i = 0; i < 4; i++) {        // 创建纹理        gl.glBindTexture(GL10.GL_TEXTURE_2D, textureids[i]);gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, i * 3, 3);}gl.glDisable(GL10.GL_TEXTURE_2D);// 关闭纹理gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);gl.glLoadIdentity();mAngleX++;if (isY) {mAngleY = mAngleY + 0.01f;if (mAngleY > 1.8) {isY = false;}} else {mAngleY = mAngleY - 0.01f;if (mAngleY < -2.5) {isY = true;}}gl.glPopMatrix();}


下面是Pyramid.java类的下载,有些多余的调试代码,可以删掉

读书人网 >Android

热点推荐