Rajawali Tutorial 3: Materials

Posted by Dennis on Dec 5, 2011 in 3D, Rajawali2 comments

This 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:

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



Tags: , , , ,


2 comments

» Comments RSS Feed
  1. 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.

  2. @Kokonutt: thanks, I’ll investigate.

Leave a comment