FREELessons: 8Length: 41 minutes

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.3 Shapes

In this lesson, we'll change the code for the cube shapes to illustrate how OpenGL defines surfaces. We'll change the floor to a ceiling and take one side off the cube to give it the appearance of an empty box. Finally, I'll illustrate how OpenGL models each square surface of the cube as two triangles.

2.3 Shapes

Hello. Welcome back to the Tuts+ VR for Google Cardboard on Android course. My name's Sue Smith. So far we have explored the sample project and made a small change to the color code. This time we will look in more detail at the shape code, and we'll make a couple of changes to better understand how it works. We saw earlier that the world layout data class defines the coordinates, colors, and normals, for the cube and floor. Let's say that instead of a floor we wanted the thing to have a ceiling. In the main activity class there is a variable called floor depth. Let's find out where it's used. In the on surface created method, the app uses this variable to determine where the floor shape should be drawn. And it uses a negative number so that the floor is below the user. Let's remove the negative sign and make it a ceiling. Now we may run the app We see the shape above rather than below. Let's look at the cube shape now. Each section in the world layout data class for coordinates colors and normals data defines the cube and six sections one for each side. Lets make it look more like an empty box by removing each section for the top side of the cube. And we need one more change before we carry on. In the main activity, the code is expecting six sides. In the draw cube method, when the draw erase method is called, change the 36 to 30 since that's how many values we have now for the cube's data. Now when we run the app, we can see inside the box. When we look at the data for each side of the cube, we can see that there are actually six sets of values for each square. This is because open GL uses triangles combined to make other shapes. So we can understand each side as two triangles. Let's demonstrate this by making a change to the color code for when the cube is found. Last time we changed it from yellow to white. Let's change one of triangles on each surface back to yellow,. Now when we run the app we can see that each square is made up of two triangles. Finally let's look at how the app uses scaling on the cube. In the main activity, when the user finds the cube the app hides it and moves it to another location. It uses the hide object method. The method chooses a random location and distance for the new cube position. It uses that distance to calculate how to scale the cube to make it appear closer or further away. And this illustrates how the shapes in a 3D scene are defined relative to the user position. Try some more experimenting with the 3D shapes in the app. Next time, we'll look at the interactive elements in the sample.

Back to the top