opengl es纹理贴图效果实例
生活随笔
收集整理的這篇文章主要介紹了
opengl es纹理贴图效果实例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、先準備好一張用來貼圖的照片
二、紋理效果代碼:
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);三、實例代碼
1、Activity類代碼
import android.app.Activity; import android.opengl.GLSurfaceView; import android.os.Bundle;public class TieTuGLActivity extends Activity {MyRender myRender;GLSurfaceView glSurfaceView;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);initBitmap.init(this.getResources());glSurfaceView = new GLSurfaceView(this);myRender = new MyRender(this);glSurfaceView.setRenderer(myRender);setContentView(glSurfaceView);} }2、初始化bitmap類 import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory;public class initBitmap {public static Bitmap bitmap;public static void init(Resources res){bitmap = BitmapFactory.decodeResource(res, R.drawable.beach) ;} }
3、渲染類 import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.IntBuffer;import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10;import android.content.Context; import android.graphics.Bitmap; import android.opengl.GLSurfaceView.Renderer; import android.opengl.GLUtils;public class MyRender implements 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};public MyRender(Context context) {this.context = context;// 初始化textureids = new int[1];// 實例化bitmapbitmap = initBitmap.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);}@Overridepublic 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.0fgl.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.4f;zrot += 0.6f;}@Overridepublic void onSurfaceChanged(GL10 gl, int width, int height) {// 視角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();}@Overridepublic 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);} }
四、運行效果圖:
總結
以上是生活随笔為你收集整理的opengl es纹理贴图效果实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JAVA基础:Hibernate外键关联
- 下一篇: php ADODB使用方法