Android之GLSurfaceView立方体图片纹理测试代码

public class MainActivity extends Activity {

	private GLSurfaceView mGLView; 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	
		mGLView = new DemoGLSurfaceView(this);
        setContentView(mGLView); 
	}
}
class DemoGLSurfaceView extends GLSurfaceView{  
	DomeGLRender mRenderer;  
    public DemoGLSurfaceView(Context context) {  
        super(context);  
        mRenderer = new DomeGLRender(context.getResources());  
        this.setRenderer(mRenderer);  
    }  
      
    public boolean onTouchEvent(final MotionEvent event){  
        this.queueEvent(new Runnable() {   
            @Override  
            public void run() {  
            	if(mRenderer.mSwitch){
            		mRenderer.mSwitch = false;
            	}else{
            		mRenderer.mSwitch = true;
            	}
            }  
        });  
        return true;  
    }   
} 
class DomeGLRender implements Renderer  {  
    boolean mSwitch = true;  
    float xrot, yrot;  
    float xspeed, yspeed;  
    float z = -5.0f;  
 
    public static Bitmap mBitmap; 
    
    public DomeGLRender(Resources resources){
    	mBitmap = BitmapFactory.decodeResource(resources, R.drawable.img);  
    }
      
    //光线参数  
    FloatBuffer lightAmbient = FloatBuffer.wrap(new float[]{0.5f,0.5f,0.5f,1.0f});   
    FloatBuffer lightDiffuse = FloatBuffer.wrap(new float[]{1.0f,1.0f,1.0f,1.0f});   
    FloatBuffer lightPosition = FloatBuffer.wrap(new float[]{0.0f,0.0f,2.0f,1.0f});   
    
    FloatBuffer normalsBuf;
    FloatBuffer verticesBuf;
    FloatBuffer texCoordsBuf;
    ByteBuffer indicesBuf;
      
    int filter = 1;  
    int[] texture;    
    float[] vertices = {  
            -1f,-1f,1f,  
            1f,-1f,1f,  
            1f,1f,1f,  
            -1f,1f,1f,  
              
            -1f,-1f,-1f,  
            -1f,1f,-1f,  
            1f,1f,-1f,  
            1f,-1f,-1f,  
              
            -1f,1f,-1f,  
            -1f,1f,1f,  
            1f,1f,1f,  
            1f,1f,-1f,  
              
            -1f,-1f,-1f,  
            1f,-1f,-1f,  
            1f,-1f,1f,  
            -1f,-1f,1f,  
              
            1f,-1f,-1f,  
            1f,1f,-1f,  
            1f,1f,1f,  
            1f,-1f,1f,  
              
            -1f,-1f,-1f,  
            -1f,-1f,1f,  
            -1f,1f,1f,  
            -1f,1f,-1f,  
              
    };  
      
    float[] normals = {  
            0,0,1f,  
            0,0,1f,  
            0,0,1f,  
            0,0,1f,  
              
            0,0,1f,  
            0,0,1f,  
            0,0,1f,  
            0,0,1f,  
              
            0,1f,0,  
            0,1f,0,  
            0,1f,0,  
            0,1f,0,  
              
            0,-1f,0,  
            0,-1f,0,  
            0,-1f,0,  
            0,-1f,0,  
              
            1f,0,0,  
            1f,0,0,  
            1f,0,0,  
            1f,0,0,  
              
            -1f,0,0,  
            -1f,0,0,  
            -1f,0,0,  
            -1f,0,0,  
    };  
      
    float[] texCoords = {  
        1f,0,0,0,0,1f,1f,1f,      
        0,0,0,1f,1f,1f,1f,0,  
        1f,1f,1f,0,0,0,0,1f,  
        0,1f,1f,1f,1f,0,0,0,  
        0,0,0,1f,1f,1f,1f,0,  
        1f,0,0,0,0,1f,1f,1f,  
    };  
      
    byte[] indices = {  
            0,1,3,2,  
            4,5,7,6,  
            8,9,11,10,  
            12,13,15,14,  
            16,17,19,18,  
            20,21,23,22,  
    };  
      
    @Override  
    public void onDrawFrame(GL10 gl)  {  
        // 清除屏幕和深度缓存  
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);  
          
        gl.glMatrixMode(GL10.GL_MODELVIEW);  
        // 重置当前的模型观察矩阵  
        gl.glLoadIdentity();  
        //使能光照 
        gl.glEnable(GL10.GL_LIGHTING);  
          
          
        gl.glTranslatef(0.0f, 0.0f, z);  
          
        //设置旋转  
        gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);  
        gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);  
  
        //设置纹理  
        gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[filter]);  
          
        gl.glNormalPointer(GL10.GL_FLOAT, 0, normalsBuf);  
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, verticesBuf);  
        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texCoordsBuf);  
  
        gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);  
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);  
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);  
          
        //绘制四边形  
        gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 24,  GL10.GL_UNSIGNED_BYTE, indicesBuf);  
          
        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);  
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);  
        gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);  
        //修改旋转角度  
        xrot+=0.3f;   
        yrot+=0.2f;  
 
        //混合开关  
        if (mSwitch){  
            gl.glEnable(GL10.GL_BLEND);         // 打开混合  
            gl.glDisable(GL10.GL_DEPTH_TEST);   // 关闭深度测试  
        }  
        else{  
            gl.glDisable(GL10.GL_BLEND);        // 关闭混合  
            gl.glEnable(GL10.GL_DEPTH_TEST);    // 打开深度测试  
        }  
    }  
  
    @Override  
    public void onSurfaceChanged(GL10 gl, int width, int height)  {  
        float ratio = (float) width / height;  
        //设置OpenGL场景的大小  
        gl.glViewport(0, 0, 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.glDisable(GL10.GL_DITHER);  
  
        // 告诉系统对透视进行修正  
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);  
        // 黑色背景  
        gl.glClearColor(0, 0, 0, 0);  
          
        gl.glEnable(GL10.GL_CULL_FACE);  
        // 启用阴影平滑  
        gl.glShadeModel(GL10.GL_SMOOTH);  
        // 启用深度测试  
        gl.glEnable(GL10.GL_DEPTH_TEST);    
        //设置光线,,1.0f为全光线,a=50%  
        gl.glColor4f(1.0f,1.0f,1.0f,0.5f);    
        // 基于源象素alpha通道值的半透明混合函数  
        gl.glBlendFunc(GL10.GL_SRC_ALPHA,GL10.GL_ONE);        
  
        //纹理相关  
        IntBuffer textureBuffer = IntBuffer.allocate(3);  
        gl.glGenTextures(3, textureBuffer);  
        texture = textureBuffer.array();  
         
        gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[0]);  
        gl.glTexParameterx(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MAG_FILTER,GL10.GL_NEAREST);   
        gl.glTexParameterx(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_NEAREST);   
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, mBitmap, 0);  
          
        gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[1]);  
        gl.glTexParameterx(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MAG_FILTER,GL10.GL_LINEAR);  
        gl.glTexParameterx(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_LINEAR);  
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, mBitmap, 0);  
          
        gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[2]);  
        gl.glTexParameterx(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MAG_FILTER,GL10.GL_LINEAR);   
        gl.glTexParameterx(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_LINEAR);  
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, mBitmap, 0);  
          
        //深度测试相关  
        gl.glClearDepthf(1.0f);  
        gl.glDepthFunc(GL10.GL_LEQUAL);  
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);  
        gl.glEnable(GL10.GL_TEXTURE_2D);  
          
        //设置环境光  
        gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_AMBIENT, lightAmbient);  
        //设置漫射光  
        gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_DIFFUSE, lightDiffuse);  
        //设置光源位置  
        gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_POSITION, lightPosition);  
        //开启一号光源  
        gl.glEnable(GL10.GL_LIGHT1);   
        //开启混合  
        gl.glEnable(GL10.GL_BLEND);  
        
        ByteBuffer nbb = ByteBuffer.allocateDirect(normals.length*4);  
        nbb.order(ByteOrder.nativeOrder());  
        normalsBuf = nbb.asFloatBuffer();  
        normalsBuf.put(normals);  
        normalsBuf.position(0);
        
        ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);  
        vbb.order(ByteOrder.nativeOrder());  
        verticesBuf = vbb.asFloatBuffer();  
        verticesBuf.put(vertices);  
        verticesBuf.position(0);
        
        ByteBuffer tbb = ByteBuffer.allocateDirect(texCoords.length*4);  
        tbb.order(ByteOrder.nativeOrder());  
        texCoordsBuf = tbb.asFloatBuffer();  
        texCoordsBuf.put(texCoords);  
        texCoordsBuf.position(0);
        
        indicesBuf = ByteBuffer.allocateDirect(indices.length);  
        indicesBuf.put(indices);  
        indicesBuf.position(0);
        
        mSwitch = false;  
    }  
} 

要在android.opengl.glsurfaceview上显示立方体,需要进行以下步骤: 1. 首先,在项目的布局文件中添加一个GLSurfaceView的元素,用于显示OpenGL的内容: ``` <android.opengl.GLSurfaceView android:id="@+id/glsurfaceview" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 2. 在Activity或Fragment中,获取GLSurfaceView对象并设置Renderer: ``` GLSurfaceView glSurfaceView = findViewById(R.id.glsurfaceview); glSurfaceView.setEGLContextClientVersion(2); // 设置OpenGL ES版本 Renderer renderer = new CubeRenderer(); // 创建自定义的Renderer glSurfaceView.setRenderer(renderer); ``` 3. 创建一个自定义的Renderer类,用于绘制立方体: ``` public class CubeRenderer implements Renderer { private Cube cube; // 自定义的立方体类 @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { // 设置OpenGL的环境参数 GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); GLES20.glEnable(GLES20.GL_DEPTH_TEST); // 创建立方体对象 cube = new Cube(); } @Override public void onSurfaceChanged(GL10 gl, int width, int height) { // 设置OpenGL视口 GLES20.glViewport(0, 0, width, height); } @Override public void onDrawFrame(GL10 gl) { // 清除屏幕 GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT); // 绘制立方体 cube.draw(); } } ``` 4. 创建一个Cube类,用于绘制立方体的顶点、纹理、颜色等信息: ``` public class Cube { // 定义顶点、纹理、颜色等数据 public void draw() { // 使用OpenGL方法绘制立方体 } } ``` 以上是在android.opengl.glsurfaceview上显示立方体的基本步骤,具体的绘制立方体代码需要根据具体需求进行实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值