Android OpenGLES – Load Customized Mesh File

本文介绍如何从外部文件加载纹理立方体到Android OpenGL ES应用程序中,包括创建和加载自定义网格文件、处理字节序问题以及在Eclipse中设置资源加载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

screen_shot1-181x300

This program originally come from an Android OpenGLES sample. It display a cube with texture. What I did is I pull the hard code cube mesh data out of the code, and save as an external file “cube.mesh”, then let the android try to load this mesh file.

 

Build the customized Mesh

To create the external mesh file that we could use it in the android program, a new project named “MeshCreator” was created. What this project just save the cube mesh as a binary file. And the mesh file format we design here is very simple.

 

Add the Mesh data as Android Resource

After you create the binary mesh file, you should add it to the android program resource. To do this, you should create a new folder under the android ‘res‘ folder, and move this mesh file into that new folder. And then, you switch to the Eclipse, select your project, right click, choose ‘Refresh‘. This will add the external file into your android resource list. You could check you ‘gen‘ folder R.java file, you could notice that a new item will created for your new added files.

 

Load the mesh data in Java

To read my customize mesh file from android program, some code wrote like this:

Resources resources = OpenGLES_ReadMeshActivity.GetInstance().getResources();
InputStream iStream = resources.openRawResource( R.raw.cube );
DataInputStream dis = new DataInputStream(iStream);int v_num = dis.readInt();
byte[] bytes = new byte[v_num * 4];
dis.read(bytes, 0, v_num *4);

A InputStream could be got from Resources.openRawSource(). I did some research on the web, it seems there are only some directories under the ‘res’ folder could be used. So maybe you need to do some test to check whether android support more folder under the ‘res’ and how depth about those folders.

 

Java use BigEdian Vs OpenGL-ES use LittleEdian

Java program use BigEdian, but the OpenGLES need some data in LittleEdian. If you did not do the conversion, you will get some running time exception: must use a native order direct buffer. There are already some solutions on the web to address this issue. To get the short cut way, I will cut their code to here:

public static IntBuffer GetNativeBuffer(int[] array)
{  
    // !!! Be careful, we use ‘allocateDirect’ here instead of ‘allocate’
    ByteBuffer buffer = ByteBuffer.allocateDirect(array.length * 4);  
    buffer.order(ByteOrder.nativeOrder());  
    IntBuffer intBuffer = buffer.asIntBuffer();  
    intBuffer.put(array);  
    intBuffer.position(0);  
    return intBuffer;  
}      public static FloatBuffer GetNativeBuffer(float[] array)
{  
    // !!! Be careful, we use ‘allocateDirect’ here instead of ‘allocate’
    ByteBuffer buffer = ByteBuffer.allocateDirect(array.length * 4);  
    buffer.order(ByteOrder.nativeOrder());  
    FloatBuffer floatBuffer = buffer.asFloatBuffer();  
    floatBuffer.put(array);  
    floatBuffer.position(0);  
    return floatBuffer;  
}

 

Something More

One trick thing to write android program under the Eclipse is that you need to toggle on “Build Automatically” (under the “Project” menu item). This will make the R.java file generated automatically and avoid some projects compiling errors. In one word, you MUST to toggle on it.

 

To Confirm

I tried to launch this sample on Android Device. But the result was the texture mapping missing. And Some one told me that, I should compress the texture into PVRTC format because of Android CPU architecture.

 

The full source code could be downloaded from here. A document about how to set up the Android develop environment also included. The process to download the Android SDK will be a bit long, please be patient wait for it (Or only download the specified Android version). And the process of launch the Android emulator also a bit long.

转载于:https://www.cnblogs.com/open-coder/archive/2012/08/24/2653561.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值