How to Make a Lego Block?

I'm trying to create a Lego block in jMonkey, but I'm having trouble figuring out how to go about doing this. I'm pretty new to jMonkey (and 3D graphics in general), so I'd greatly appreciate some help. :slight_smile:



I want to draw a three-cylinder Lego block, but I'm also hoping to be able to customize it to other sizes.



My initial idea was to extend the Trimesh class and specify the appropriate vertex, normal, texture, and index data, but thanks to my lack of understanding and inexperience with this, I'm running into some problems.



The Lego block would essentially be a box with the bottom exposed, showing the bottom three cylinders. I could probably figure out how to draw a box without a bottom face (mostly by just examining the Box class), but how would I incorporate the vertices for the three cylinders? The Lego block's vertex data would consist of the points of the box part of the Lego as well as radial samples and axis samples for the cylinder part of the Lego. How can I combine this vertex data together, as well as create the proper placement for each cylinder of the Lego block?



If I understand correctly, index data specifies (in threes) the indices of what vertices create triangles. I understand vertex, normal, and index data, but how does texture data work? I looked at the Box class, and I wasn't able to figure out why each of the box's 6 planes has (1,0), (0,0), (0,1), and (1,1) for its texture data. Can anyone explain this to me please or point me to a tutorial of some sort that explains this?



My second idea would be to create a custom Lego class that simply utilizes the Box and Cylinder classes. However, that would need a few customizations, as I'd have to get rid of the bottom plane of the Box, and each Cylinder would need a closed top and open bottom. Would this be the simpler way to go, or should I not consider it because of performance issues or anything like that? Any tips as to how to go about this approach, such as whether or not to use the SharedNode class to render the cylinders?



I'm not sure which approach to use, and I'm very new at this, so any help would be greatly appreciated. If I could be pointed to a tutorial on making custom objects in jMonkey, that'd be great, too.

I strongly recommend modelling the brick in something like blender, 3D Studio Max or Maya, exporting it in some format jme can read (collada, obj, md5, …) and just loading the model into the application.

An alternative, if you want to support parametric blocks, is to generate the vertex and index data into arrays in memory, and then create the TriMesh object with the vertex, normal and index data arrays as FloatBuffer/IntBuffer arguments. See the constructor for TriMesh(). You can generate triangles for the walls, cylinders, etc, all using math. For example, you could write a class something like:


public class BlockData {
  TriMesh theTriMesh;
  public BlockData(int width, int height) {
    float[] vertices = new float[   number of vertices for the given size * 3    ];
    float[] normals = new float[   number of vertices for the given size * 3    ];
    int[] indices = new int[   number of triangles for the given size * 3    ];
    generateData(width, height, vertices, normals, indices);
    theTriMesh = new TriMesh(vertices, normals, null, null, indices);
  }
  public TriMesh getTriMesh() {
    return theTriMesh;
  }
}



The trick is, of course, to write the "generateData" function and by extension the function that calculates the needed amount of data. If you don't yet know how meshes are put together of vertex data and indices that generate triangles, then I suggest you go about finding a basic 3D mesh tutorial first. Then you can google for something like "parametric geometry generation."

If that's too much work for you, then model each size brick in a modeler like Blender, MilkShape, DeleD, 3D Studio Max or Maya, and import it as a model (.obj, .ase or similar format). The draw-back is that you can't make bricks of arbitrary size; you have to model each.