android OpenGLES开发 第五课 纹理映射
来源:互联网 发布:谷歌输入法 mac 编辑:程序博客网 时间:2024/06/05 01:06
这里我们将加入纹理来代替颜色设置不同表面的内容,和上一课不同的地方是在Polygon类中加入了一些纹理的设置,下面将一一描述,代码如下:
public class Polygon {// 保存纹理的FloatBufferprivate FloatBuffer[] textureBuffer;// 用来加载纹理的数组private int[] textures = new int[8];// 保存纹理顶点坐标的数组private float[][] texture = {new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.5f, 1.0f, 0.5f, 0.0f,1.0f, 1.0f, 1.0f,},new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.5f, 1.0f, 0.5f, 0.0f,1.0f, 1.0f, 1.0f,}, new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,}, new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,}, new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,}, new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,}, new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,}, new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,} };public Polygon() {ByteBuffer bb;// 初始化保存纹理的FloatBuffertextureBuffer = new FloatBuffer[8];for (int i = 0; i < 8; i++) {bb = ByteBuffer.allocateDirect(texture[i].length * 4);bb.order(ByteOrder.nativeOrder());textureBuffer[i] = bb.asFloatBuffer();textureBuffer[i].put(texture[i]);textureBuffer[i].position(0);}}public void draw(GL10 gl) {gl.glFrontFace(GL10.GL_CW);for (int i = 0; i < 8; i++) {// Generate one texture pointer...// ...and bind it to our array// 生成一个纹理引用,并把它和当前的数组绑定gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[i]);// Point to our buffers// 设置纹理坐标gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer[i]);// Enable the texture state// 加入纹理坐标的权限gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);// Disable the client state before leaving// 取消纹理坐标的权限gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);}}// 加载和绑定纹理的方法public void loadTexture(GL10 gl, Context context) {// define the resourcesId int[] resourcesIds = { R.drawable.gou, R.drawable.hou, R.drawable.laohu, R.drawable.laoshu, R.drawable.tuzi, R.drawable.xiaolong, R.drawable.xiaoniu, R.drawable.zhu };gl.glGenTextures(8, textures, 0);// Get the texture from the Android resource directoryfor (int i = 0; i < 8; i++) {Bitmap bitmap = loadBitmap(context, resourcesIds[i]);// Generate one texture pointer...// ...and bind it to our arraygl.glBindTexture(GL10.GL_TEXTURE_2D, textures[i]);// Create Nearest Filtered Texturegl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_NEAREST);gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,GL10.GL_LINEAR);// Use the Android GLUtils to specify a two-dimensional texture// image from our bitmapGLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);// Clean upbitmap.recycle();}}// 加载bitmappublic Bitmap loadBitmap(Context context, int resourceid) {InputStream is = context.getResources().openRawResource(resourceid);try {// BitmapFactory is an Android graphics utility for imagesreturn BitmapFactory.decodeStream(is);} finally {// Always clear and closetry {is.close();is = null;} catch (IOException e) {}}}}
可以到网上找一些256x256的图片代替文中的图片资源。
- android OpenGLES开发 第五课 纹理映射
- opengles(五) 纹理映射
- android opengles 纹理
- OpenGLES 1.1教程(四)-纹理映射
- OpenGLES 1.1教程(四)-纹理映射
- Android OpenglES 绘制多个纹理
- 利用android_ndk开发OpenGLES如何利用上层Bitmap生成纹理
- opengles 纹理对象&纹理单元
- 第六课纹理映射:
- OpenGLES---加载纹理
- OpenGLES---纹理的API
- opengles之mipmap纹理
- OpenGLES获取纹理流程图
- android 游戏 纹理映射 未完成
- android OpenGLES开发第一课 绘制简单的Polygon
- android OpenGLES开发第二课 为Polygon添加颜色
- android OpenGLES开发第四课 绘制一个六棱柱
- 从零开始学OpenGLES开发——第五章
- android OpenGLES开发第二课 为Polygon添加颜色
- android OpenGLES开发第三课 绘制一个可以翻转的Polygon
- android OpenGLES开发第四课 绘制一个六棱柱
- 赚钱啦(单源最短路径(含有负权)Bellman_Forld()算法)
- ACMSTEP 2.3.1 A+B Problem II //大数加法
- android OpenGLES开发 第五课 纹理映射
- the first time use the header of zlib.h
- iphone国际化(转)
- 探索C++的秘密之详解extern "C"
- poj Cow Exhibition 01背包的灵活运用
- 程序员面试宝典笔记3--循环递归概率
- hdu 3943 K-th Nya Number
- ACMSTEP 2.3.5 How Many Trees? //高精度卡特兰数
- 项目管理之敏捷与瀑布