bandarra.me

OpenGL SuperBible in Java: The GLBatchFactory

Different 3D Shapes

The GLBatchFactory is a helper class that helps the developer to create models. Besides providing a tool to create more complex model, where the developer may chose to add each vertex of the triangle separately, it provides 5 static methods that create some useful shapes.

The first shape is the cube, created with the makeCube method. It takes 3 float parameters, the cube width, height and depth. The cube is created with the point 0.0.0 on its center;

The second shape is the sphere. It also takes 3 float parameters, but with different meaning. The first is da radius of the sphere. Now, the sphere is constructed of triangles organized in slices and stacks. The second and third parameters determine the number of slices and stacks of the sphere.. The more slices and stacks, the better the sphere will look. But slower to build and render it will be. The sphere is constructed around 0.0.0 too.

The third shape is the cylinder (or cone). It takes 5 parametres. The radius of the top, the radius of the bottom, the length, the number of slices and number of stacks.

The fourth shape is the disk. A shape that resembles a CD. It takes four parameters, the inner radius (size of the middle of the disk), the outer radius, number of stacks and slices.

The fifth shape is the Torus, a shape that looks like a donut. It takes 4 parameters, the inner radius, the outer radius, number of stacks and number of slices.

On the OpenGL Superbible C++ code, this code is inside the GLBatch class. I decided to put it on another class, just to make the code more clear.

Again, the example is available at the Example5.java, on http://code.google.com/p/opengl-superbible-java/

If you want to check out the older tutorials, go to http://www.codemansion.com/p/opengl-superbible-in-java-using-lwjgl.html