Having multiples of the same mesh (3D)

So i’m loading an .OBJ mesh (using the loader that comes with java) each time i need it but i’m sure there’s a more efficient way of doing things, especially since i’ll be needing a lot of the same mesh.

What’s the more efficient way of doing this? Btw, i’m using Java3D. And to put into context, i’m building a 3D strategy game, the mesh in question being a “forest”. So the only thing that would need to be different is the X,Y and Z position, (and maybe the texture and the animation, but im not there yet).

I thought of using the same solution i found with my textures, ie load them once at the first creation of the object as a static variable and re-using them over and over, but it doesn’t seem to work with 3D mesh, it gives me errors when i try to attach it to the scene node. I don’t have the code right now but if it can help, i’ll post something tomorrow.

Thanks!

Load it once, store it in memory, then draw the same model multiple times at different positions.

Not sure how Java3D works, but I’m pretty sure it’s got a transformation matrix just like OpenGL. So, to draw the trees elsewhere, just translate the context around each time you draw.

Pseudocode:


for (int i = 0; i < trees.size(); i++)
{
    glLoadIdentity(); //Put the Matrix back to 0
    glTranslatef(trees.get(i).x, trees.get(i).y, trees.get(i).z);
    trees.get(i).getOBJ().render();
}

In Java3D the geometry is loaded as some form of a node in the scene graph. A node can only have one parent, which is why you can never add the instance again without causing exceptions. You need to extract the actually geometry information from the node and pass that geometry to the new nodes. Something like that, it’s been far too long since I’ve used Java3D and I don’t actually like it very much.

You can use a Link + SharedGroup. The SharedGroup contains the .OBJ mesh that you loaded once. Then you can have multiple Links pointing to the same SharedGroup.

Or you can use cloneTree to shallow copies of the loaded mesh. This is what I do. Link + SharedGroups makes a lot of things like picking more complicated.

How do you use cloneTree exactly?

this is what i get: javax.media.j3d.RestrictedAccessException: Node: Cannot clone a live or compiled scenegraph

Does it change anything if i need to clone it at runtime?

Are you trying to clone it after you’ve started rendering it? You should try cloning a copy right after you’ve loaded the .OBJ file.

I keep a template model that I don’t add to the scenegraph. When I need an instance I use cloneTree on the template and add the clone to the scenegraph.

Ok, so this is simple:


private void load3DAssets(){
		forestTemplateBG = new BranchGroup();
		String filename = "props\\forest.obj";
		ObjectFile f = new ObjectFile(ObjectFile.RESIZE);
	    forestMesh = null;
		try {
			forestMesh = f.load(filename);
		    } catch errors...
		    
	}

But then, i’m not sure how to cloneTree it ?


private void addTree(float posx, float posy, float posz){					
						
		TransformGroup tg = new TransformGroup();
		
		tg.addChild(forestTemplateBG.cloneTree());                   <-------????
		tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		Transform3D tr = new Transform3D();    
		tr.rotX( Math.PI / 2.0);	

		Vector3f translation = new Vector3f(posx, posy, posz + 0.1f);
		tr.setTranslation(translation);
		Vector3d scale = new Vector3d(0.5d, 0.5d, 0.5d);
		tr.setScale(scale);
		
		tg.setTransform(tr);
			    
	    forestBG.addChild(tg);
	    forestBG.setPickable(false);
	    forest.add( forestBG );
	    
	}

“forest” at the end is an ArrayList of BranchGroup that i use to add to the scene with:

sceneBG.addChild( forest.get( forest.size()-1 ) );

each time i’m adding a forest to the game. (Working on the level editor right now.)

It might be clumsy but, but i’m still learning :wink:

The cloning code looks correct. However I don’t see you adding frestMesh to forestTemplateBG.

omg… Thanks a lot! It’s working fine now ;))