Chapter 3. Cardboard Box

Remember when you were a kid and happy to just play in a cardboard box? This project might even be more fun than that! Our first Cardboard project will be a simple scene with a box (a geometric cube), a triangle, and a bit of user interaction. Let's call it "CardboardBox." Get it?

Specifically, we're going to create a new project, build a simple app that just draws a triangle, then enhance the app to draw a shaded 3D cube, and illustrate some user interactions by highlighting the cube when you look at it.

In this chapter, you will be:

  • Creating a new Cardboard project
  • Adding a triangle object to the scene, including geometry, simple shaders, and render buffers
  • Using a 3D camera, perspective, and head rotation
  • Using model transformations
  • Making and drawing a cube object
  • Adding a light source and shading
  • Spinning the cube
  • Adding a floor
  • Highlighting the object that the user is looking at

The project in this chapter is derived from an example application provided by the Google Cardboard team called Treasure Hunt. Originally, we considered instructing you to simply download Treasure Hunt, and we'd walk you through the code explaining how it works. Instead, we decided to build a similar project from scratch, explaining as we go along. This also mitigates the possibility that Google changes or even replaces that project after this book is published.

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

The Android SDK version is important to your finished app, but your desktop environment can also be set up in a number of ways. We mentioned earlier that we used Android Studio 2.1 to build the projects in this book. We also used the Java SDK Version 8 (1.8). It will be important for you to have this version installed (you can have many versions installed side by side) in order to import the projects. As with any development environment, any changes made to Java or Android Studio may "break" the import process in the future, but the actual source code should compile and run for many years to come.

Creating a new project

If you'd like more details and explanation about these steps, refer to the Creating a new Cardboard project section in Chapter 2, The Skeleton Cardboard Project, and follow along there:

  1. With Android Studio opened, create a new project. Let's name it CardboardBox and target Android 4.4 KitKat (API 19) with an Empty Activity.
  2. Add the Cardboard SDK common.aar and core.aar library files to your project as new modules, using File | New | New Module....
  3. Set the library modules as dependencies to the project app, using File | Project Structure.
  4. Edit the AndroidManifest.xml file as explained in Chapter 2, The Skeleton Cardboard Project, being careful to preserve the package name for this project.
  5. Edit the build.gradle file as explained in Chapter 2, The Skeleton Cardboard Project, to compile against SDK 22.
  6. Edit the activity_main.xml layout file as explained in Chapter 2, The Skeleton Cardboard Project.
  7. Edit the MainActivity Java class so that it extends CardboardActivity and implements CardboardView.StereoRenderer. Modify the class declaration line as follows:
    public class MainActivity extends CardboardActivity implements CardboardView.StereoRenderer {
  8. Add the stub method overrides for the interface (using intellisense implement methods or pressing Ctrl + I).
  9. At the top of the MainActivity class, add the following comments as placeholders for variables that we will be creating in this project:
    CardboardView.StereoRenderer {
       private static final String TAG = "MainActivity";
    
       // Scene variables
       // Model variables
       // Viewing variables
       // Rendering variables
  10. Lastly, edit onCreate() by adding the CardboadView instance as follows:
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            CardboardView cardboardView = (CardboardView) findViewById(R.id.cardboard_view);
            cardboardView.setRenderer(this);
            setCardboardView(cardboardView);  
        }
..................Content has been hidden....................

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