纹理就是给物体表面添加图片,色彩之类,可以认为是上色。
基本步骤:
@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); }
在surfacecreated中启用纹理设置,创建纹理,生成纹理,设置过滤属性
@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); // //向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); // 绘制正方体6个面,这时每个面都会画好纹理 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; }
上面这时是给矩形6个面设置同样的纹理图片。如果要为不同面设置不同纹理,这个时候就需要用glDrawElements这个方法了,这个方法是按照自己指定的索引去绘图,这样就可以通过这个方法,来绘制6个面,使用不同纹理。下面我们来看看为矩形6个面设置不同纹理的实现。
核心方法:
1、纹理初始化,为6个面取6张图片来作为6个纹理
// Texture纹理初始化 public void GLTextureUtil(GL10 gl) { //为纹理数组初始化长度为6,对应6个面的纹理 IntBuffer textureBuffer = IntBuffer.allocate(6); gl.glGenTextures(6, textureBuffer); texture = textureBuffer.array(); //绑定第一个纹理,使用图片为mBitmap1,对应到texture第一个 gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[0]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, GLImage.mBitmap1, 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); // gl.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_WRAP_S,GL10.GL_REPEAT); // gl.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_WRAP_T,GL10.GL_REPEAT); //绑定第二个纹理,使用图片为mBitmap2,对应到texture第二个 gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[1]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, GLImage.mBitmap2, 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); // gl.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_WRAP_S,GL10.GL_REPEAT); // gl.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_WRAP_T,GL10.GL_REPEAT); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[2]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, GLImage.mBitmap3, 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); // gl.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_WRAP_S,GL10.GL_REPEAT); // gl.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_WRAP_T,GL10.GL_REPEAT); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[3]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, GLImage.mBitmap4, 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); // gl.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_WRAP_S,GL10.GL_REPEAT); // gl.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_WRAP_T,GL10.GL_REPEAT); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[4]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, GLImage.mBitmap5, 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); // gl.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_WRAP_S,GL10.GL_REPEAT); // gl.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_WRAP_T,GL10.GL_REPEAT); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[5]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, GLImage.mBitmap6, 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); // gl.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_WRAP_S,GL10.GL_REPEAT); // gl.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_WRAP_T,GL10.GL_REPEAT); GLImage.mBitmap1.recycle(); GLImage.mBitmap2.recycle(); GLImage.mBitmap3.recycle(); GLImage.mBitmap4.recycle(); GLImage.mBitmap5.recycle(); GLImage.mBitmap6.recycle(); }
2、在onDrawFrame中绘制6个面,使用对应的纹理
// 绘制四边形和Texture元素绑定 public void BindElementsWithTexture(GL10 gl) { //绘制第一个面,使用第一个纹理,对应面的顶点索引为indices1 gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[0]); gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 4, GL10.GL_UNSIGNED_BYTE, indices1); //绘制第二个面,使用第二个纹理,对应面的顶点索引为indices2 gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[1]); gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 8, GL10.GL_UNSIGNED_BYTE, indices2); //绘制第三个面,使用第三个纹理,对应面的顶点索引为indices3 gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[2]); gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 12, GL10.GL_UNSIGNED_BYTE, indices3); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[3]); gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 16, GL10.GL_UNSIGNED_BYTE, indices4); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[4]); gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 20, GL10.GL_UNSIGNED_BYTE, indices5); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[5]); gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 24, GL10.GL_UNSIGNED_BYTE, indices6); gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); gl.glDisableClientState(GL10.GL_NORMAL_ARRAY); }
3、对应的索引如下,就是上面的indices1、indices2.。。。。indices6
ByteBuffer indices1 = ByteBuffer.wrap(new byte[] { 0,1,3,2, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, }); ByteBuffer indices2 = ByteBuffer.wrap(new byte[] { 0,0,0,0, 4,5,7,6, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, }); ByteBuffer indices3 = ByteBuffer.wrap(new byte[] { 0,0,0,0, 0,0,0,0, 8,9,11,10, 0,0,0,0, 0,0,0,0, 0,0,0,0, }); ByteBuffer indices4 = ByteBuffer.wrap(new byte[] { 0,0,0,0, 0,0,0,0, 0,0,0,0, 12,13,15,14, 0,0,0,0, 0,0,0,0, }); ByteBuffer indices5 = ByteBuffer.wrap(new byte[] { 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 16,17,19,18, 0,0,0,0, }); ByteBuffer indices6 = ByteBuffer.wrap(new byte[] { 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 20,21,23,22, });
这样就绘制出来6个面对应不同纹理的矩形了
demo下载地址:链接:http://pan.baidu.com/s/1c2oXQes 密码:7jz2