Rajawali Tutorial 4: Optimisation
Posted by Dennis on Jan 16, 2012 in 3D, Android, Rajawali • 6 commentsThe onSurfaceCreated() method in the Renderer is called every time the rendering starts or whenever the context is lost. This typically happens when the orientation changes or when the device wakes up after going to sleep.
When the context is lost, all the textures are deleted from memory. This is not something Rajawali inflicts upon you. This is the way OpenGL ES on Android works. So you always have to re-create your textures in the onSurfaceCreated() method.
Object instantiation however is something that needs to be done only once. So in onSurfaceCreated you could use the following code (make sure you set mClearChildren to false):
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
mClearChildren = false;
if(sceneInitialized == false) {
mLight= new DirectionalLight();
mLight.setPosition(3, 5, -3);
mSphere = = new Sphere(1, 24, 24);
mSphere.addLight(mLight);
addChild(mSphere);
}
SimpleMaterial simple = new SimpleMaterial();
mSphere.setMaterial(simple);
Bitmap texture = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.mytexture);
mSphere.addTexture(mTextureManager.addTexture(texture));
}
This speeds things up significantly when the context has to be re-created, especially when you use large models.
There’s another optimisation you can use when large models are involved. It’s called serialization. This prevents you from having to load a .obj file and parse it every time the application starts up. It basically writes all the relevant data (vertices, indices, normal, texture coordinates and colors) to a binary file on disk.
So the .obj file has to be loaded only once during development. Once the file has loaded you can save it to the sdcard.
In order to do this you first have to set the permission in AndroidManifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Then the .obj model can be loaded in and turned into a serialized file:
mSphere.serializeToSDCard("my_serialized_model.ser");
When this is done the code and the .obj file can be discarded. The serialized file is written to the sd card and should be copied into to your resources folder (res/raw). The serialized model is now ready to be loaded into your application:
try {
ObjectInputStream ois;
ois = new ObjectInputStream(mContext.getResources().openRawResource(R.raw.my_serialized_model));
mMyModel = new BaseObject3D((SerializedObject3D)ois.readObject());
addChild(mMyModel);
ois.close();
} catch (Exception e) {
e.printStackTrace();
}
WIN!
Continue to Rajawali Tutorial 5: Skybox.

the framework is great! but seems that the material does not work well on galaxy nexus android 4.0…
>>the material does not work well on galaxy nexus android 4.0
the situation is that on android 2.3.x the texture can be display with no problem,
for the exactly the same project, with no file path changed, installed on android 4.0,
the mesh, camera etc works well, but the texture become black color
Thank you for the engine. Quick note, it keeps throwing a Null Pointer Exception every time onDrawFrame() is called when I create BaseObject3Ds from a SerializedObject3D but doesn’t when I simply parse in the resource. Which leads to my question, how do I make copies of the BaseObject3D so that I don’t have to parse the obj every single time?
Dennis, thank you for the engine and the tutorial. For years I’ve been wanting to get into OpenGL and finally I decided to plunge in. Exploring your engine has been a great tutorial by itself!
I hope I can integrate OpenGL in my app soon…
@pmduque I’m glad it was helpful. Let me know if you have any questions!
[...] Rajawali Tutorial 4: Optimisation [...]