OpenGL给正方体的各个面加上纹理(贴图)
注意事项:
*图片如果放在drawable-mdpi目录下,则在模拟器上可以正常显示,但是在真机上不能显示图片,本处新建一个drawable-nodpi目录,将图片资源放在此目录下
1.代码
import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.opengl.GLSurfaceView; import android.os.Bundle; public class OpenGLActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Bitmap bm1 = BitmapFactory.decodeResource(getResources(), R.drawable.a1); Bitmap bm2 = BitmapFactory.decodeResource(getResources(), R.drawable.a2); Bitmap bm3 = BitmapFactory.decodeResource(getResources(), R.drawable.a3); Bitmap bm4 = BitmapFactory.decodeResource(getResources(), R.drawable.a4); Bitmap bm5 = BitmapFactory.decodeResource(getResources(), R.drawable.a5); Bitmap bm6 = BitmapFactory.decodeResource(getResources(), R.drawable.a6); GLSurfaceView glView = new GLSurfaceView(this); glView.setRenderer(new GLTextureRender(bm1, bm2, bm3, bm4, bm5, bm6)); setContentView(glView); } }
package com.lanhuidong.opengl; import java.nio.ByteBuffer; import java.nio.IntBuffer; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import com.lanhuidong.opengl.util.BufferToNativeOrder; import android.graphics.Bitmap; import android.opengl.GLSurfaceView.Renderer; import android.opengl.GLUtils; public class GLTextureRender implements Renderer { float xrot, yrot, zrot; int texture[]; int one = 0x10000; int[] vertices = new int[]{ -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, }; int[] texCoords = new int[]{ one,one,one,0,0,0,0,one, 0,0,0,one,one,one,one,0, one,one,one,0,0,0,0,one, 0,one,one,one,one,0,0,0, 0,0,0,one,one,one,one,0, one,0,0,0,0,one,one,one, }; 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, }); private Bitmap bm1; private Bitmap bm2; private Bitmap bm3; private Bitmap bm4; private Bitmap bm5; private Bitmap bm6; public GLTextureRender(Bitmap bm1, Bitmap bm2,Bitmap bm3, Bitmap bm4,Bitmap bm5, Bitmap bm6){ this.bm1 = bm1; this.bm2 = bm2; this.bm3 = bm3; this.bm4 = bm4; this.bm5 = bm5; this.bm6 = bm6; } @Override public void onDrawFrame(GL10 gl) { gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); gl.glLoadIdentity(); gl.glTranslatef(0.0f, 0.0f, -5.0f); 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); gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); gl.glVertexPointer(3, GL10.GL_FIXED, 0, BufferToNativeOrder.getNativeOrderIntBuffer(vertices)); gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, BufferToNativeOrder.getNativeOrderIntBuffer(texCoords)); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[0]); gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 24, GL10.GL_UNSIGNED_BYTE, indices1); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[1]); gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 24, GL10.GL_UNSIGNED_BYTE, indices2); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[2]); gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 24, GL10.GL_UNSIGNED_BYTE, indices3); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[3]); gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 24, GL10.GL_UNSIGNED_BYTE, indices4); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[4]); gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 24, 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); xrot+=1f; yrot+=1.2f; zrot+=0.6f; } @Override public void onSurfaceChanged(GL10 gl, int width, int height) { float ratio = (float) width / height; 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.glClearColor(1, 1, 1, 1); gl.glEnable(GL10.GL_CULL_FACE); gl.glShadeModel(GL10.GL_SMOOTH); gl.glEnable(GL10.GL_DEPTH_TEST); 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); IntBuffer intBuffer = IntBuffer.allocate(6); gl.glGenTextures(6, intBuffer); texture = intBuffer.array(); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[0]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm1, 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); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[1]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm2, 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); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[2]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm3, 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); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[3]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm4, 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); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[4]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm5, 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); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[5]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm6, 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); } }
2.效果