Cube with face normals

Now, let's add a light to the scene and render the cube with it. To do this, we also need to define normal vectors for each face of the cube, which are used in the shader calculations.

If you derive Cube from the one in Chapter 3, Cardboard Box, you may already have this code:

    public static final float[] CUBE_NORMALS_FACES = new float[] {
            // Front face
            0.0f, 0.0f, 1.0f,
            // Right face    
            1.0f, 0.0f, 0.0f,
            // Back face
            0.0f, 0.0f, -1.0f,
            // Left face
            -1.0f, 0.0f, 0.0f,
            // Top face
            0.0f, 1.0f, 0.0f,
            // Bottom face
            0.0f, -1.0f, 0.0f,
    };

Now, add a buffers for the normals, like we have for colors and vertices, and allocate them:

    
public static FloatBuffer normalBuffer;
    ...

    public static void allocateBuffers(){
        ...
        
normalBuffer = ...

Get Cardboard VR Projects for Android now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.