Getting started with Rajawali VR library
Join 6000+ Students
Upgrade your programming skills to include Virtual Reality. A premium step-by-step training course to get you building real world Virtual Reality apps and website components. You’ll have access to 40+ WebVR & Unity lessons along with their source code.
Start Learning Now
First step to getting started with Rajawali VR library is to extend your main Activity to VrActivity. VrActivity internally extends the google’s CardboardActivity providing a surface to render the 3D scene.
1 2 3 4 5 6 7 8 9 10 11 12 |
import org.rajawali3d.vr.VRActivity; public class MainActivity extends VRActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } |
There might be a warning if onCreate access is protected, if so change it public
Setting up a sample scene with Rajawali
First, we need to extend the Rajawali Renderer class. Let’s create a new class where our MainActivity is:
Name the class say Renderer and select the superclass as VrRenderer
Implement methods and create a constructor matching super.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import org.rajawali3d.vr.renderer.VRRenderer; public class Renderer extends VRRenderer { public Renderer(Context context) { super(context); } @Override protected void initScene() { } @Override public void onOffsetsChanged(float xOffset, float yOffset, float xOffsetStep, float yOffsetStep, int xPixelOffset, int yPixelOffset) { } @Override public void onTouchEvent(MotionEvent event) { } } |
initScene is where you initialize the scene. Lets render a simple cube with a blue colored material:
1 2 3 4 5 6 7 8 9 10 11 12 |
@Override protected void initScene() { Material material = new Material(); material.setColor(Color.BLUE); Cube cube = new Cube(2); cube.setMaterial(material); cube.setPosition(0, 0, -6); cube.rotate(Vector3.Axis.Y, 45); getCurrentScene().addChild(cube); } |
Setting up the Activity
Now we need to set this renderer to our activity. Just create a new Renderer object and call the setRenderer method
1 2 3 4 5 6 7 8 9 |
@Override public void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); super.onCreate(savedInstanceState); setRenderer(new Renderer(this)); } |
You may also lock the orientation to landscape for the activity in the AndroidManifest.xml
1 2 3 |
android:screenOrientation="landscape" |
The output will look like this:
Setting up the Manifest
To ensure the app can be launched from the cardboard app and the traditional launcher, add the following intent filter for the acitvity in the AndroidManifest.xml:
1 2 3 4 5 6 7 8 |
<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> <category android:name="com.google.intent.category.CARDBOARD" /> </intent-filter> |
Your final manifest entry should look something like this:
1 2 3 4 5 6 7 8 9 10 11 12 |
<activity android:name=".MainActivity" android:screenOrientation="landscape"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> <category android:name="com.google.intent.category.CARDBOARD" /> </intent-filter> </activity> |