今天要学习的是纹理,关于这一部分内容,我在我的图形学的相关文章中有纹理坐标,纹理映射的讲解,可以去看下。下面是网上找的代码
texture.java
package my.texture;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
public class texture extends Activity {
/** Called when the activity is first created. */
GLSurfaceView mSurfaceView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BitGL.init(this.getResources());
mSurfaceView=new GLSurfaceView(this);
mSurfaceView.setRenderer(new Renderer(this));
setContentView(mSurfaceView);
}
}
class BitGL {
public static Bitmap bitmap;
public static void init(Resources resources) {
bitmap = BitmapFactory.decodeResource(resources, R.drawable.nine);
}
}
Renderer.java
package my.texture;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import android.content.Context;
import android.graphics.Bitmap;
import android.opengl.*;
import javax.microedition.khronos.egl.*;
import javax.microedition.khronos.opengles.*;
public class Renderer implements GLSurfaceView.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
one,0,
0,0,
one,one,
0,one
};
public Renderer(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) {
// TODO Auto-generated method stub
// 清除深度和颜色缓存
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) {
// TODO Auto-generated method stub
// 视角
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) {
// TODO Auto-generated method stub
// 告诉系统对透视进行修正
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
// 灰色背景
gl.glClearColor(0.5f,0.5f, 0.5f,0.5f);
// 启用阴影平滑
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);
}
}
效果图:

代码中的方法可以在我的gl10方法解析中找到相应解释。
这个图片呢,大家自己添加到资源中就可以了,命名为nine的话就不用改代码了。
其实,注释很清楚了,但是这里还是稍微讲解一下。
首先,我们要创建我们的纹理对象,这个对象并不是我们要用的纹理图像,它只是纹理图像的名字,或者说是纹理图像的索引。glGenTextures方法起到这个作用,索引被存放到第二个参数textureids的整数数组中。
然后,使用glBindTexture方法绑定纹理对象,就是说应用该纹理作为下面被处理的对象。
第三步,创建纹理,GLUtils.texImage2D方法在我前面的文档中并没有解释,这里稍作解释。
public static void texImage2D(int target, int level, Bitmapbitmap, int border)
Since: API Level 1
A version of texImage2D thatdetermines the internalFormat and type automatically.
这段是其官方解释。这里解释下参数
target指目标目标纹理,设为GL10.GL_TEXTURE_2D即可
level指纹理等级,一般都设为0
bitmap指你想要映射的图像,即你之前添加的资源。
border指边界值,一般为0.
第四步,设定过滤器。定义了opengl现实图像的效果,如纹理放大时的马赛克消除等。
下面再讲下纹理映射的过程。
在我们定义了当前纹理后,我们为它设置了一下参数与属性,这以后我们要将该纹理贴到相应的对象,以上面的代码为例,我们要画一个立方体,每个面都要贴上纹理。这个纹理图实际上在我们绘制立方体的每一个面的同时就会把纹理贴上。
这里纹理贴上后是有方向的,我们这里贴的是9这个数字,如果把方向弄反了,贴上去的就是6了,而方向是由纹理坐标决定的,具体讲解请参考我图形学的相关文章中的纹理坐标,纹理映射的讲解。