Skip to main content
Samsung Developer Program

Writing a Basic GearVRf Application

Steps to create a fundamental GearVRf app

To become familiar with the basics of using the GearVRf API, write a fundamental application. 
The GearVRf sample application simplesample, used as an example in this section, presents a 3D view of a GearVRf banner in a VR world with a white background.

GearVRf runs full-screen and in a single Android View of its own, so you do not need any themes, menus, or layout files. But GearVRf apps can use Android resources (such as strings, ints, drawables, assets, and res/raw files).

 

Step 1 - Implement your activity by extending GVRActivity

To implement your activity, your application needs to extend GVRActivity to meet the application needs. Call GVRActivity setScript to initialize your application by providing a device XML file and a GVRScript object (SampleViewManager in the example below) to create your own scene graph.

Modify the wizard-generated Activity so that it extends GVRActivity (instead of extending android.app.Activity) and change its onCreate() method so that it calls setMain() instead of setContentView().

To extend GVRActivity for a sample application and a supported device (Samsung Galaxy Note 4):

package org.gearvrf.simplesample;
import android.os.Bundle;
import org.gearvrf.GVRActivity;
public class GalleryActivity extends GVRActivity {

   @Override
   protected void onCreate(Bundle icicle) {
       super.onCreate(icicle);
       setMain(new SampleViewManager(), "gvr_note4.xml");

   }
}

 

Step 2 - Implement your VR world by extending GVRMain

The main task of your application is to extend the script class to meet the needs of your application. Create a new class that extends GVRMain and implements the onInit() and onStep() callbacks.

Your application needs to implement the onInit function:

  • Set the background color.
  • Load the mesh and texture to be used for scene objects.
    NOTE: In simplesample, default loading of mesh data is used.
  • Create each scene object and set its material and position.
  • Add scene objects to the scene graph.

To extend GVRMain:

package org.gearvrf.simplesample;

import org.gearvrf.GVRContext;
import org.gearvrf.GVRScene;
import org.gearvrf.GVRSceneObject;
import org.gearvrf.GVRMain;
import org.gearvrf.GVRTexture;
import android.graphics.Color;

public class SampleViewManager extends GVRMain {

   private GVRContext mGVRContext;
   @Override
   public void onInit(GVRContext gvrContext) {
       *// save context for possible use in onStep(), even though that is empty*
       *// in this sample*
       mGVRContext = gvrContext;

       *// set background color*
       GVRScene scene = gvrContext.getMainScene();
       scene.getMainCameraRig().getLeftCamera()
               .setBackgroundColor(Color.WHITE);
       scene.getMainCameraRig().getRightCamera()
               .setBackgroundColor(Color.WHITE);

       *// load texture*
       GVRTexture texture = gvrContext.loadTexture("GearVR.jpg");
       *// create a a scene object (this constructor creates a rectangular scene*
       *// object that uses the standard 'unlit' shader)*
       GVRSceneObject sceneObject = new GVRSceneObject(gvrContext, 4.0f, 2.0f, texture);

       *// set the scene object position*
       sceneObject.getTransform().setPosition(0.0f, 0.0f, -3.0f);

       *// add the scene object to the scene graph*
       scene.addSceneObject(sceneObject);
   }

Step 3 - Implement the onStep method for each frame

This is where your application needs to adjust the SceneObject for each frame. If you need to update shader uniforms, you should do that in this function. If your application needs to adjust scene properties for each frame or a particular frame, the onStep method is right place to change them. For OpenGL ES applications, shader uniform update happens for each frame. If you have uniform updates, you need to perform the updates in onStep.

NOTE: The onStep method could be empty (as in simplesample, which as a static scene, and the perspective only changes based on user-initiated HMD motion).


To change the scene for the next frame:

   @Override
   public void onStep() {

   }

}
  • Was this article helpful?