Skip to content

Example code how to create a 3D model via code

bitstars edited this page Apr 25, 2013 · 1 revision

An example object with multiple meshes combined via the composite pattern to one big mesh group:

@Override
public void addObjectsTo(GL1Renderer renderer, World world,
		final GLFactory objectFactory) {

	Location l = new Location("");
	l.setLatitude(123);
	l.setLongitude(123);
	Obj o = new GeoObj(l);

	MeshComponent completeMeshWithBothCubes = new Shape();
	// The composite pattern can be used to combine as many meshes together
	// as one big mesh as needed:

	MeshComponent cube = objectFactory.newCube();
	// move the 1. cube into a direction in the local object space:
	Vec localPositionOfCubeInCompleteMesh = new Vec(0, 1, 0);
	cube.setPosition(localPositionOfCubeInCompleteMesh);
	completeMeshWithBothCubes.addChild(cube);

	MeshComponent cube2 = objectFactory.newCube();
	// move the 2. cube into another direction in the local object space:
	Vec localPositionOfCube2InCompleteMesh = new Vec(10, 10, 0);
	cube2.setPosition(localPositionOfCube2InCompleteMesh);
	// animations for the submeshes of the group are possible as well:
	// cube2.addAnimation(anAnimationWhichIsOnlyAppliedOnCube2);
	completeMeshWithBothCubes.addChild(cube2);

	// its possible to add any other mesh group or textured mesh etc:
	// completeMeshWithBothCubes.addChild(anyOtherMeshGroup);

	// both cubes have no own color attribute so its possible to set the
	// color of their parent mesh to blue and both children will be rendered
	// blue too:
	completeMeshWithBothCubes.setColor(Color.blue());

	o.setComp(completeMeshWithBothCubes);