android OpenGLES开发 第五课 纹理映射
2011-08-16 18:54
387 查看
这里我们将加入纹理来代替颜色设置不同表面的内容,和上一课不同的地方是在Polygon类中加入了一些纹理的设置,下面将一一描述,代码如下:
可以到网上找一些256x256的图片代替文中的图片资源。
public class Polygon { // 保存纹理的FloatBuffer private 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; // 初始化保存纹理的FloatBuffer textureBuffer = 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 directory for (int i = 0; i < 8; i++) { Bitmap bitmap = loadBitmap(context, resourcesIds[i]); // Generate one texture pointer... // ...and bind it to our array gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[i]); // Create Nearest Filtered Texture gl.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 bitmap GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0); // Clean up bitmap.recycle(); } } // 加载bitmap public Bitmap loadBitmap(Context context, int resourceid) { InputStream is = context.getResources().openRawResource(resourceid); try { // BitmapFactory is an Android graphics utility for images return BitmapFactory.decodeStream(is); } finally { // Always clear and close try { is.close(); is = null; } catch (IOException e) { } } } }
可以到网上找一些256x256的图片代替文中的图片资源。
相关文章推荐
- 【Android开发学习14】Android OpenGL ES 纹理映射之glDrawElements
- OpenglES2.0 for Android:再谈纹理映射
- 【Android开发学习15】Android OpenGL ES 纹理映射之glDrawArrays
- 利用android_ndk开发OpenGLES如何利用上层Bitmap生成纹理
- android开发LiveWallpaper流程(利用OpenGLES开发)
- 【Visual C++】游戏开发笔记四十一 浅墨DirectX教程之九 为三维世界添彩:纹理映射技术(一)
- android开发中资源文件和资源ID是如何映射的
- Android+NDK+OpenGLES开发环境配置
- Android开发 第五课 Android的几种布局方式
- 【Visual C++】游戏开发笔记四十一 浅墨DirectX教程之九 为三维世界添彩:纹理映射技术(一)
- 有关android开发的一个网站,查android键盘映射时发现的
- Android 中使用OpenGL ES进行2D开发(纹理Texture使用)
- 【Visual C++】游戏开发笔记三十 DirectX11 2D纹理映射知识全攻略
- 基于Android opengles的魔方开发总结
- Android 开发第五弹:简易时钟(闹钟)
- android 使用OPENGL ES实现地月模型(3D)-纹理映射基础
- android opengl es--纹理映射,光照
- 【Visual C++】游戏开发笔记三十 DirectX11 2D纹理映射知识全攻略
- Android开发笔记(一百五十六)通过渲染纹理展示地球仪
- Android平台开发-Android keypad map-Android按键识别及映射过程