Rajawali Tutorial 3: Materials
Posted by Dennis on Dec 5, 2011 in 3D, Rajawali • 2 commentsThis small tutorial is going to focus on the syntax for creating materials only.
If you want to go over the basics then please read this first:
- Rajawali Tutorial 1: Basic Setup & a Sphere
- Rajawali Tutorial 2: Creating a live wallpaper and importing a model
Alright. Let’s create some materials
Simple Material
A basic material is just a color or a texture without any lighting enabled. This renders a green cube:
SimpleMaterial simple = new SimpleMaterial();
mCube.setMaterial(simple);
mCube.setColor(0xff009900);
This renders a textured cube:
SimpleMaterial simple = new SimpleMaterial();
mCube.setMaterial(simple);
Bitmap texture = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.mytexture);
mCube.addTexture(mTextureManager.addTexture(texture));
Gouraud Material
This material can be used in combination with a light. For more info check out this Wikipedia article.
mLight = new DirectionalLight();
mLight.setPosition(0, 5, -3);
GouraudMaterial gouraud = new GouraudMaterial();
gouraud.setSpecularColor(0xffffff00); // yellow
mCube.setMaterial(gouraud);
mCube.setLight(mLight);
Phong Material
Use this material to get nice specular highlights. See this article on Wikipedia.
PhongMaterial phong = new PhongMaterial();
phong.setSpecularColor(0xffffffff); // white
phong.setAmbientcolor(0xffffff00); // yellow
phong.setShininess(0.5f);
mCube.setMaterial(phong);
mCube.setLight(mLight);
Environment map material
This is a reflective material. It requires 6 textures. Check this Wikipedia article for more information.
Bitmap[] textures = new Bitmap[6];
String ns = "com.mynamespace.myapp:drawable/";
String dns = "com.mynamespace.myapp";
String prefix = "";
textures[0] = BitmapFactory.decodeResource(mContext.getResources(), mContext.getResources().getIdentifier(ns + prefix + "_posx", null, dns));
textures[1] = BitmapFactory.decodeResource(mContext.getResources(), mContext.getResources().getIdentifier(ns + prefix + "_negx", null, dns));
textures[2] = BitmapFactory.decodeResource(mContext.getResources(), mContext.getResources().getIdentifier(ns + prefix + "_posy", null, dns));
textures[3] = BitmapFactory.decodeResource(mContext.getResources(), mContext.getResources().getIdentifier(ns + prefix + "_negy", null, dns));
textures[4] = BitmapFactory.decodeResource(mContext.getResources(), mContext.getResources().getIdentifier(ns + prefix + "_posz", null, dns));
textures[5] = BitmapFactory.decodeResource(mContext.getResources(), mContext.getResources().getIdentifier(ns + prefix + "_negz", null, dns));
TextureInfo tInfo = mTextureManager.addCubemapTextures(textures);
CubeMapMaterial mat = new CubeMapMaterial();
mat.addTexture(tInfo);
mCube.setMaterial(mat);
DirectionalLight light = new DirectionalLight();
light.setPosition(3, 5, -3);
mCube.setLight(light);
More materials will be added soon. If you have a specific request, just ping me an email
Continue to Rajawali Tutorial 4: Optimisation

Hi, I’m playing with your engine and I’ve noticed a bug (at least it looks like a bug to me..):
If I use the same texture twice, the second one is displayed wrong, colors are different etc.
@Kokonutt: thanks, I’ll investigate.