Rajawali Tutorial 1: Basic Setup & a Sphere

Posted by Dennis on Aug 24, 2011 in 3D, Android, Rajawali12 comments

So here’s the first very basic Rajawali tutorial. Any questions, let me know :-)

Create a new project in Eclipse (Right-click in Package Explorer and choose New > Project … Android Project). Use these values in the configuration screen:

  • project name: RajawaliTutorial1
  • the “Build Target” should be at least “2.2″
  • application name: RajawaliTutorial1
  • package name: rajawali.tutorials
  • create activity: RajawaliTutorial1Activity

Press “Finish”

Hit the download button on https://github.com/MasDennis/Rajawali and extract the contents of the “src” folder into the project’s “src” folder.

Open the RajawaliTutorial1Activity class and change this line:

extends Activity

into

extends RajawaliActivity

Right click on the rajawali.tutorials package in the package explorer and choose “New > Class”. Name the class RajawaliTutorial1Renderer and choose RajawaliRenderer as the Superclass. Hit “Finish”.

Add the constructor and set the frame rate:

public RajawaliTutorial1Renderer(Context context) {
	super(context);
	setFrameRate(30);
}

Override the onSurfaceCreated() method. This is where we’ll set up our 3D scene.

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
	super.onSurfaceCreated(gl, config);
}

Add a new class variable called mLight and add the following code to the onSurfaceCreated() method:

mLight = new DirectionalLight(0.1f, 0.2f, -1.0f); // set the direction
mLight.setColor(0, 0, 1.0f);
mLight.setPosition(.5f, 0, -2);

Create a new folder under /res/ called drawable-nodpi and download and save this image.

Next we are going to create a simple sphere and a texture.
First add a class variable called mSphere< of type Sphere and add this code to the onSurfaceCreated() method:

Bitmap bg = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.selera_sari);
mSphere = new Sphere(1, 12, 12);
mSphere.setLight(mLight);
mSphere.addTexture(mTextureManager.addTexture(bg));
addChild(mSphere);

Also add a camera and start rendering:

mCamera.setZ(-4.2f);
startRendering();

The last thing to do is create an instance of this renderer in our main activity. Open the RajawaliTutorial1Activity class and add the mRenderer class variable:

private RajawaliTutorial1Renderer mRenderer;

And finally, change the onCreate() method to:

@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	mRenderer = new RajawaliTutorial1Renderer(this);
	mRenderer.setSurfaceView(mSurfaceView);
	super.setRenderer(mRenderer);
}

Build and launch and behold the sphere!

Let’s make this a bit less boring and add some animation.
Go back to the RajawaliTutorial1Renderer and override the onDrawFrame() method and add y-axis rotation:

@Override
public void onDrawFrame(GL10 glUnused) {
	super.onDrawFrame(glUnused);
	mSphere.setRotY(mSphere.getRotY() + 1);
}

That’s it. Very basic tutorial here.
If you’re lazy you can download the Eclipse project here.

Go to tutorial 2, “Rajawali Tutorial 2: Creating a live wallpaper and importing a model“.



Tags: , , , ,


12 comments

» Comments RSS Feed
  1. hey, building your project in eclipse works fine, but when i try to run it crashes in the simulator with a throw that opengl es 20 is not available. i used a 2.3.3 vm so this shouldn’t be a problem. i set a breakpoint and turns out that at this point:
    if(info.reqGlEsVersion < 0×20000)
    the reqGlEsVersion is 0 but the gles20 test in the phone emulation work.
    you got any idea what i did wrong.
    i just imported and ran your code, also tried different apis (2.3.3 / 3.0) with no luck.
    thanks

  2. Hey, good question. I’ve actually never tried it in the emulator myself. I always test on handsets and tablets. I’ll have a look into it.

  3. your project not running my emu later i’m using android 2.2 ….

  4. its showing force quit

  5. Yeah, this is a known issue. I always use my phone to run the apps. The emulator used to be really slow.
    I’ll take a look into this. For now just use a handset if possible.
    Cheers
    Dennis

  6. other question. small bugfixes i made (found a null pointer exception that can be checked beforehand) should i send you a diff via email?

  7. Hi,
    Thanks for that. Yes, please send me an email.
    Dennis

  8. Unfortunately, OpenGL ES2.0 is not supported by android emulator (yet). The only option is to test it on a real device.
    (http://developer.android.com/resources/tutorials/opengl/opengl-es20.html)

    I have another issue, with texturing. When I run my app on my phone (Motorola Defy+, Android 2.3, OMAP 3630) textures work without any problem, but when I run it on any tablet with Tegra 2 processor (Lenovo Thinkpad Tablet, Asus Pad Transformer), terturing doesn’t work. Everything else works fine.

    Do you have any idea what is the problem?

    Thanks very much

  9. Hi, I am getting a compile error: import rajawali.math.Number3D;

    I can’t find this anywhere in your sample code. Is that class missing? Thanks!

  10. Hi, i d like to thank you for this excellent engine.. great job!
    i have an issue whit lights. they dont seem to work , no matter what..

    on surface created , i add this:

    mLight = new DirectionalLight(1.0f, 1.0f, 1.0f);
    mLight.setColor(0, 0, 1.0f);
    mLight.setPosition(0.0f, 3.0f, 2);

    and this is my object i want to lightup

    ObjParser parser1 = new ObjParser(mContext.getResources(), mTextureManager, R.raw.plane1);
    parser1.parse();
    plane1 = parser1.getParsedObject().getChildByName(“Plane”);
    addChild(plane1);
    plane1.setTransparent(true);
    plane1.setLight(mLight);
    plane1.addTexture(mTextureManager.addTexture(texture1));
    plane1.setScale(1.0f);

    any suggestions???
    Thanks.. and sorry for my English,,

  11. I am getting the same error on ‘import rajawali.math.Number3D;’ as Will when I download the package directly from github. It looks like the sample code for the tutorial does not use the Number3D class so it works. Is there a separate download for rajawali.math.Number3D?

    Thanks!

  12. @Jeff @Will: this class has been added to the Github repository.

Leave a comment