android opengl es 正方体纹理效果

(1)Activity类

 

package sim.feel;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.os.Bundle;

public class My3d2 extends Activity {
    /** Called when the activity is first created. */
    private GLSurfaceView surfaceView;
    private Renderer renderer;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 载入图片
        BitGL.init(this.getResources());
        surfaceView = new GLSurfaceView(this);
        renderer = new MyRenderer(this);
        surfaceView.setRenderer(renderer);
        setContentView(surfaceView);
    }
}

class BitGL {
    public static Bitmap bitmap;

    public static void init(Resources resources) {
        bitmap = BitmapFactory.decodeResource(resources, R.drawable.img);
    }
}

 

(2)Renderer类

 

package sim.feel;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Bitmap;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLUtils;

public class MyRenderer implements Renderer {

    public Context context;
    private int one = 0x10000;
    private Bitmap bitmap;
    private int[] textureids;
    private IntBuffer vertexBuffer; 
    private IntBuffer texBuffer; 
    // 旋转方向
    private float xrot, yrot, zrot;
    // 正方体顶点
    private int[] vertices = { 
            one, one, -one,   
            -one, one, -one, 
            one, one, one, 
            
            -one, one, one, 
            one, -one,one,
            -one, -one, one,
            
            one, -one, -one, 
            -one, -one, -one, 
            one, one,one,
            
            -one, one, one,
            one, -one, one,
            -one, -one, one,
            
            one, -one,-one,
            -one, -one, -one,
            one, one, -one,
            
            -one, one, -one,
            -one, one,one,
            -one, one, -one,
            
            -one, -one, one, 
            -one, -one, -one,
            one, one,-one,
            
            one, one, one,
            one, -one, -one,
            one, -one, one 
        };

    //纹理点
    private int[] texCoords = {   
            0, one,                 
            one, one, 
            0, 0, 
            one, 0 
    };
 

    public MyRenderer(Context context) {
        this.context = context;
        // 初始化
        textureids = new int[1];
 
        // 实例化bitmap
        bitmap = BitGL.bitmap;

        ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
        vbb.order(ByteOrder.nativeOrder());
        vertexBuffer = vbb.asIntBuffer(); 
        vertexBuffer.put(vertices); 
        vertexBuffer.position(0); 

        ByteBuffer tbb = ByteBuffer.allocateDirect(texCoords.length * 4 * 6);
        tbb.order(ByteOrder.nativeOrder());
        texBuffer = tbb.asIntBuffer();
        //为每一个面贴上纹理
        for (int i = 0; i < 6; i++) {
            texBuffer.put(texCoords);
        }
        texBuffer.position(0);
 
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        // 清除深度和颜色缓存
        gl.glClear(GL10.GL_DEPTH_BUFFER_BIT | GL10.GL_COLOR_BUFFER_BIT);
        gl.glLoadIdentity();
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

        gl.glVertexPointer(3, GL10.GL_FIXED, 0, vertexBuffer);
        gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, texBuffer); // Define
        
        //向z轴里移入6.0f
        gl.glTranslatef(0.0f, 0.0f, -6.0f);
        // 设置3个方向的旋转
        gl.glRotatef(xrot, one, 0.0f, 0.0f);
        gl.glRotatef(yrot, 0.0f, one, 0.0f);
        gl.glRotatef(zrot, 0.0f, 0.0f, one);

        // 绘制正方体
        for (int i = 0; i < 6; i++) {
            gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, i * 4, 4);
        }
 

        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

        // 设置旋转角度
        xrot += 0.5f;
        yrot += 0.6f;
        zrot += 0.3f;
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        // 视角
        gl.glViewport(0, 0, width, height);
        float ratio = (float) width / height;
        // 观察模式
        gl.glMatrixMode(GL10.GL_PROJECTION);
        // 重置观察布局
        gl.glLoadIdentity();
        gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {

        // 告诉系统对透视进行修正
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
        // 黑色背景
        gl.glClearColor(0, 0, 0, 0);
        // 启用阴影平滑
        gl.glShadeModel(GL10.GL_SMOOTH);

        // 清除深度缓存
        gl.glClearDepthf(one);
        // 启用深度测试
        gl.glEnable(GL10.GL_DEPTH_TEST);
        // 所做深度测试的类型
        gl.glDepthFunc(GL10.GL_LEQUAL);

    
        
        gl.glEnable(GL10.GL_TEXTURE_2D);
        // 创建纹理
        gl.glGenTextures(1, textureids, 0);
        // 绑定要使用的纹理
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textureids[0]);
        // 生成纹理
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 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);
 
    }
}

 

效果图如下:

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值