Creating the ModelObject class

To begin with, we will define a ModelObject class that extends RenderObject. It will load model data from OBJ files and set up buffers needed by its material (and OpenGL ES shaders to be rendered in the VR scene).

Right-click on the app/java/com.cardboardvr.modelviewer/ folder, go to New | Java Class, and name it ModelObject. Define it so that it extends RenderObject, as follows:

public class ModelObject extends RenderObject {
}

Just like we've done in the previous chapters, when introducing new kinds of RenderObjects, we'll have one or more constructors that can instantiate a Material and set up buffers. For ModelObject, we'll pass in a file resource handle, parse the file (refer to the next topic), and create a solid color material (initially, without lighting), as follows:

    public ModelObject(int objFile) {
        super();
        InputStream inputStream = RenderBox.instance.mainActivity.getResources().openRawResource(objFile);
        if (inputStream == null)
            return; // error
        parseObj(inputStream);
        createMaterial();
    }

Now add the material as follows. First, declare variables for the buffers (as we have done for other RenderObjects in the previous projects). These can be private, but our convention is to keep them public if we want to define new materials outside:

    public static FloatBuffer vertexBuffer;
    public static FloatBuffer colorBuffer;
    public static FloatBuffer normalBuffer;
    public static ShortBuffer indexBuffer;
    public int numIndices;

Here's the createMaterial method (which is called from the constructor):

    public ModelObject createMaterial(){
        SolidColorLightingMaterial scm = new SolidColorLightingMaterial(new float[]{0.5f, 0.5f, 0.5f, 1});
        scm.setBuffers(vertexBuffer, normalBuffer, indexBuffer, numIndices);
        material = scm;
        return this;
    }

Next, we implement the parseObj method.

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

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