Chapter 6. Solar System

When I was 8 years old, for a science project at school, I made a Solar System from wires, styrofoam balls, and paint. Today, 8-year olds all around the world will be able to make virtual Solar Systems in VR, especially if they read this chapter! This project creates a Cardboard VR app that simulates our Solar System. Well, maybe not with total scientific accuracy, but good enough for a kid's project and better than styrofoam balls.

In this chapter, you will create a new Solar System project with the RenderBox library by performing the following steps:

  • Setting up the new project
  • Creating a Sphere component and a solid color material
  • Adding an Earth texture material with lighting
  • Arranging the Solar System geometry
  • Animating the heavenly bodies
  • Interactively changing camera locations
  • Updating the RenderBox library with our new code

As we put these together, we will create planets and moons from a sphere. Much of the code, however, will be in the various materials and shaders for rendering these bodies.

Note

The source code for this project can be found on the Packt Publishing website, and on GitHub at https://github.com/cardbookvr/solarsystem (with each topic as a separate commit).

Setting up a new project

To build this project, we will use our RenderBox library created in Chapter 5, RenderBox Engine. You can use yours, or grab a copy from the downloadable files provided with this book or our GitHub repository (use the commit tagged after-ch5https://github.com/cardbookvr/renderboxlib/releases/tag/after-ch5). For a more detailed description of how to import the RenderBox library, refer to the final Using RenderBox in future projects section of Chapter 5, RenderBox Engine. Perform the following steps to create a new project:

  1. With Android Studio opened, create a new project. Let's name it SolarSystem and target Android 4.4 KitKat (API 19) with an Empty Activity.
  2. Create new modules for each of renderbox, common and core packages, using File | New Module | Import .JAR/.AAR Package.
  3. Set the modules as dependencies for the app, using File | Project Structure.
  4. Edit the build.gradle file as explained in Chapter 2, The Skeleton Cardboard Project, to compile against SDK 22.
  5. Update /res/layout/activity_main.xml and AndroidManifest.xml, as explained in the previous chapters.
  6. Edit MainActivity as class MainActivity extends CardboardActivity implements IRenderBox, and implement the interface method stubs (Ctrl + I).

We can go ahead and define the onCreate method in MainActivity. The class now has the following code:

public class MainActivity extends CardboardActivity implements IRenderBox {
    private static final String TAG = "SolarSystem";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        CardboardView cardboardView = (CardboardView) findViewById(R.id.cardboard_view);
        cardboardView.setRenderer(new RenderBox(this, this));
        setCardboardView(cardboardView);
    }
    @Override
    public void setup() {

    }
    @Override
    public void preDraw() {
    }
    @Override
    public void postDraw() {
    }
}

While we build this project, we will be creating new classes that could be good extensions to RenderBox lib. We'll make them regular classes in this project at first. Then, at the end of the chapter, we'll help you move them into the RenderBox lib project and rebuild the library:

  1. Right-click on the solarsystem folder (com.cardbookvr.solarsystem), select New | Package, and name it RenderBoxExt.
  2. Within RenderBoxExt, create package subfolders named components and materials.

There's no real technical need to make it a separate package, but this helps organize our files, as the ones in RenderBoxExt will be moved into our reusable library at the end of this chapter.

You can add a cube to the scene, temporarily, to help ensure that everything is set up properly. Add it to the setup method as follows:

    public void setup() {
        new Transform()
            .setLocalPosition(0,0,-7)
            .setLocalRotation(45,60,0)
            .addComponent(new Cube(true));
    }

If you remember, a cube is a component that's added to a transform. The cube defines its geometry (for example, vertices). The transform defines its position, rotation, and scale in 3D space.

You should be able to click on Run 'app' with no compile errors, and see the cube and Cardboard split screen view on your Android device.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset