当前位置:操作系统 > 安卓/Android >>

[Android开发学习14]Android OpenGL ES 纹理映射之glDrawElements

目标: 为四方体的每个面贴上一张图片,并自动旋转。


一、基础知识:
要实现每个面上贴一张图片,首先需要创建一个纹理,并使用图片来生成一个纹理。
==========================================================================


1.初始化:


 IntBuffer intBuffer = IntBuffer.allocate(2);
 // 1.允许2D贴图,纹理
 gl.glEnable(GL10.GL_TEXTURE_2D);
 // 2.创建纹理
 gl.glGenTextures(2, intBuffer);
 texture = intBuffer.get(0);
 // 3.设置要使用的纹理
 gl.glBindTexture(GL10.GL_TEXTURE_2D, texture);
 // 4.生成纹理
 GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, GLImage.close_Bitmap, 0);
 // 5.线形滤波
 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);


==========================================================================


2.使用:


 // 1.清除屏幕和深度缓存
 gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

 // 2.重置当前的模型观察矩阵
 gl.glLoadIdentity();
 // 3.移动到指定的位置
 gl.glTranslatef(0.0f, 0.0f, -5.0f);

 // 4.设置3个方向的旋转
 gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);
 gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);
 gl.glRotatef(zrot, 0.0f, 0.0f, 1.0f);

 texture = intBuffer.get(1);;
 //  5.绑定纹理
 gl.glBindTexture(GL10.GL_TEXTURE_2D, texture);
 
 // 6.开启顶点和纹理功能
 gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
 gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
 
 // 7.纹理和四边形对应的顶点
 gl.glVertexPointer(3, GL10.GL_FIXED, 0, vertices);
 gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, texCoords);

 // 8.绘制0
 GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, GLImage.iBitmap, 0);
 gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 4,  GL10.GL_UNSIGNED_BYTE, indices_0);

 // 9.关闭顶点和纹理功能
 gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
 gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
==========================================================================

 

 

二、实现:

1. 界面编辑:
【res\layout\main.xml】

[html] 
<SPAN style="BACKGROUND-COLOR: #999999"><SPAN style="BACKGROUND-COLOR: #333333"><?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<TextView   
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:text="@string/hello" 
    /> 
 
<Button 
    android:id="@+id/button1" 
    android:layout_width="145dp" 
    android:layout_height="wrap_content" 
    android:text="演示开始" /> 
 
</LinearLayout> 
</SPAN></SPAN> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />

<Button
    android:id="@+id/button1"
    android:layout_width="145dp"
    android:layout_height="wrap_content"
    android:text="演示开始" />

</LinearLayout>

2.代码编辑:
【\src\com\yarin\android\Examples\Activity01.java】

[java] 
package com.yarin.android.Examples_12_05; 
 
import java.io.IOException; 
import java.io.InputStream; 
 
import javax.microedition.khronos.opengles.GL10; 
 
import android.app.Activity; 
import android.content.res.Resources; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.opengl.GLSurfaceView; 
import android.opengl.GLUtils; 
import android.opengl.GLSurfaceView.Renderer; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
 
public class Activity01 extends Activity 

    Renderer render = new GLRender(); 
    GLSurfaceView glView; 
    Button start;           // 演示开始  
 
     
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState); 
        GLImage.load(this.getResources()); 
        glView = new GLSurfaceView(this); 
         
         
        glView.setRenderer(render); 
        setContentView(R.layout.main); 
        start=(Button)findViewById(R.id.button1);   // "演示开始"按钮初始化  
        start.setOnClickListener(new View.OnClickListener() { 
             
            @Override 
            public void onClick(View v) { 
                // TODO Auto-generated method stub  
                setContentView(glView); 
            } 
        }); 
        //setContentView(glView);  
    } 

 
 
class GLImage 

    public static Bitmap iBitmap; 
    public static Bitm

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,