Hello, cube!
A flat triangle floating in 3D space may be amazing, but it's nothing compared to what we're going to do next: a 3D cube!
The cube model data
The triangle, with just three vertices, was declared in the MainActivity
class to keep the example simple. Now, we will introduce more complex geometry. We'll put it in a class named Cube
.
Okay, it's just a cube that is composed of eight distinct vertices, forming six faces, right?
Well, GPUs prefer to render triangles rather than quads, so subdivide each face into two triangles; that's 12 triangles in total. To define each triangle separately, that's a total of 36 vertices, with proper winding directions, defining our model, as shown in CUBE_COORDS
. Why not just define eight vertices and reuse them? We'll show you how to do this later.
Note
Remember that we always need to be careful of the winding order of the vertices (counter-clockwise) so that the visible side of each triangle is facing outward.
In Android Studio, in the Android project hierarchy...